001 package com.croftsoft.apps.infant; 002 003 import java.awt.event.*; 004 import java.util.*; 005 006 import net.java.games.input.Component; 007 import net.java.games.input.Controller; 008 import net.java.games.input.ControllerEnvironment; 009 010 import com.croftsoft.core.gui.controller.NilController; 011 import com.croftsoft.core.lang.NullArgumentException; 012 import com.croftsoft.core.util.log.Log; 013 014 /*********************************************************************** 015 * Modifies model based on user input. 016 * 017 * @version 018 * $Id: InfantController.java,v 1.28 2007/12/01 00:51:07 croft Exp $ 019 * @since 020 * 2005-03-16 021 * @author 022 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 023 ***********************************************************************/ 024 025 public final class InfantController 026 extends NilController 027 //////////////////////////////////////////////////////////////////////// 028 //////////////////////////////////////////////////////////////////////// 029 { 030 031 private final InfantConfig infantConfig; 032 033 private final Queue<InfantMessage> requestQueue; 034 035 // 036 037 private Controller pacifierController; 038 039 private boolean 040 displayRequested, 041 experimentEndRequested, 042 pacifierPreviouslyPressed, 043 pacifierReleased, 044 resetRequested, 045 saveFilenameRequested; 046 047 private int controllerIndex; 048 049 //////////////////////////////////////////////////////////////////////// 050 //////////////////////////////////////////////////////////////////////// 051 052 public InfantController ( 053 final InfantConfig infantConfig, 054 final Queue<InfantMessage> requestQueue ) 055 //////////////////////////////////////////////////////////////////////// 056 { 057 NullArgumentException.checkArgs ( 058 this.infantConfig = infantConfig, 059 this.requestQueue = requestQueue ); 060 061 selectController ( ); 062 } 063 064 //////////////////////////////////////////////////////////////////////// 065 //////////////////////////////////////////////////////////////////////// 066 067 @Override 068 public void actionPerformed ( final ActionEvent actionEvent ) 069 //////////////////////////////////////////////////////////////////////// 070 { 071 final String actionCommand = actionEvent.getActionCommand ( ); 072 073 if ( actionCommand.equals ( 074 InfantMessage.Type.STIMULUS_WINDOW_OPEN.name ( ) ) ) 075 { 076 this.displayRequested = true; 077 } 078 else if ( actionCommand.equals ( 079 InfantMessage.Type.EXPERIMENT_BEGIN.name ( ) ) ) 080 { 081 this.resetRequested = true; 082 } 083 else if ( actionCommand.equals ( 084 InfantMessage.Type.EXPERIMENT_END.name ( ) ) ) 085 { 086 experimentEndRequested = true; 087 } 088 else if ( actionCommand.equals ( 089 InfantMessage.Type.LOAD_SETUP.name ( ) ) ) 090 { 091 requestQueue.offer ( 092 new InfantMessage ( InfantMessage.Type.LOAD_SETUP ) ); 093 } 094 else if ( actionCommand.equals ( 095 InfantMessage.Type.REQUEST_SAVE_FILENAME.name ( ) ) ) 096 { 097 saveFilenameRequested = true; 098 } 099 else 100 { 101 this.infantConfig.getLog ( ).record ( 102 "InfantController: unknown command: " + actionCommand ); 103 } 104 } 105 106 @Override 107 public void keyReleased ( final KeyEvent keyEvent ) 108 //////////////////////////////////////////////////////////////////////// 109 { 110 final int keyCode = keyEvent.getKeyCode ( ); 111 112 if ( keyCode == KeyEvent.VK_SPACE ) 113 { 114 pacifierReleased = true; 115 } 116 else 117 { 118 experimentEndRequested = true; 119 } 120 } 121 122 //////////////////////////////////////////////////////////////////////// 123 //////////////////////////////////////////////////////////////////////// 124 125 @Override 126 public void update ( ) 127 //////////////////////////////////////////////////////////////////////// 128 { 129 final boolean pacifierCurrentlyPressed = pollPacifier ( ); 130 131 if ( pacifierPreviouslyPressed 132 && !pacifierCurrentlyPressed ) 133 { 134 pacifierReleased = true; 135 } 136 137 pacifierPreviouslyPressed = pacifierCurrentlyPressed; 138 139 if ( displayRequested ) 140 { 141 displayRequested = !requestQueue.offer ( 142 new InfantMessage ( InfantMessage.Type.STIMULUS_WINDOW_OPEN ) ); 143 } 144 145 if ( pacifierReleased ) 146 { 147 pacifierReleased = !requestQueue.offer ( 148 new InfantMessage ( InfantMessage.Type.PACIFIER ) ); 149 } 150 151 if ( resetRequested ) 152 { 153 resetRequested = !requestQueue.offer ( 154 new InfantMessage ( InfantMessage.Type.EXPERIMENT_BEGIN ) ); 155 } 156 157 if ( experimentEndRequested ) 158 { 159 experimentEndRequested = !requestQueue.offer ( 160 new InfantMessage ( InfantMessage.Type.EXPERIMENT_END ) ); 161 } 162 163 if ( saveFilenameRequested ) 164 { 165 saveFilenameRequested = !requestQueue.offer ( 166 new InfantMessage ( 167 InfantMessage.Type.REQUEST_SAVE_FILENAME ) ); 168 } 169 } 170 171 //////////////////////////////////////////////////////////////////////// 172 // private methods 173 //////////////////////////////////////////////////////////////////////// 174 175 private void selectController ( ) 176 //////////////////////////////////////////////////////////////////////// 177 { 178 controllerIndex = infantConfig.getControllerIndex ( ); 179 180 final Log log = infantConfig.getLog ( ); 181 182 Controller pacifierController = null; 183 184 try 185 { 186 final ControllerEnvironment controllerEnvironment 187 = ControllerEnvironment.getDefaultEnvironment ( ); 188 189 final Controller [ ] controllers 190 = controllerEnvironment.getControllers ( ); 191 192 for ( int i = 1; i <= controllers.length; i++ ) 193 { 194 final Controller controller = controllers [ i - 1 ]; 195 196 if ( i == controllerIndex ) 197 { 198 log.record ( i + " / " + controller.getType ( ) + " / " 199 + controller.getName ( ) ); 200 201 if ( Controller.Type.STICK.equals ( controller.getType ( ) ) ) 202 { 203 pacifierController = controller; 204 } 205 else 206 { 207 log.record ( 208 "Controller is not of type " + Controller.Type.STICK ); 209 } 210 } 211 } 212 } 213 catch ( Exception ex ) 214 { 215 ex.printStackTrace ( ); 216 } 217 218 if ( pacifierController != null ) 219 { 220 log.record ( "Using controller " + controllerIndex + " / " 221 + pacifierController.getType ( ) + " / " 222 + pacifierController.getName ( ) ); 223 } 224 else 225 { 226 log.record ( "Controller not configured (use spacebar to test)" ); 227 } 228 229 this.pacifierController = pacifierController; 230 } 231 232 /***********&*********************************************************** 233 * Polls the pacifier. 234 * 235 * @return 236 * 237 * True if the pacifier is currently pressed. 238 ***********************************************************************/ 239 private boolean pollPacifier ( ) 240 //////////////////////////////////////////////////////////////////////// 241 { 242 if ( controllerIndex != infantConfig.getControllerIndex ( ) ) 243 { 244 selectController ( ); 245 } 246 247 if ( pacifierController == null ) 248 { 249 return false; 250 } 251 252 final Component [ ] components 253 = pacifierController.getComponents ( ); 254 255 if ( components == null ) 256 { 257 return false; 258 } 259 260 final Float [ ] floats = new Float [ components.length ]; 261 262 String format = ""; 263 264 for ( int i = 0; i < components.length; i++ ) 265 { 266 format += "%" + ( i + 1 ) + "$+1.3f "; 267 } 268 269 format += "%n"; 270 271 if ( !pacifierController.poll ( ) ) 272 { 273 return false; 274 } 275 276 for ( int i = 0; i < components.length; i++ ) 277 { 278 Component component = components [ i ]; 279 280 floats [ i ] = new Float ( component.getPollData ( ) ); 281 } 282 283 // System.out.printf ( format, ( Object [ ] ) floats ); 284 285 if ( ( floats.length > 2 ) 286 && ( floats [ 2 ].floatValue ( ) < 0.5 ) ) 287 { 288 return true; 289 } 290 291 return false; 292 } 293 294 //////////////////////////////////////////////////////////////////////// 295 //////////////////////////////////////////////////////////////////////// 296 }