1 16 package com.buchuki.ensmer.input; 17 18 import java.awt.event.*; 19 20 import com.buchuki.ensmer.*; 21 import com.buchuki.ensmer.input.command.*; 22 import com.buchuki.ensmer.input.event.*; 23 24 41 public class DefaultSystemInputManager implements InputProcessor { 42 43 46 public DefaultSystemInputManager() { 47 CommandMap map = InputManager.getCommandMap(); 48 49 Command objectMode = 51 new Command() { 52 public boolean execute(EnsmerInputEvent event) { 53 if (mode == InputMode.SELECT) { 54 navigateMode(); MouseEvent mouse = (MouseEvent) event.getInputEvent(); 56 Long id = EnsmerManager.instance().getInterfaceManager().pickObject(mouse.getX(), mouse.getY()); 57 if (id != null) { 58 EnsmerManager.instance().getUserManager().getInputManager().objectMode(id); 59 return true; 60 } 61 } 62 return false; 63 } 64 }; 65 66 Command enterSelect = 67 new Command() { 68 public boolean execute(EnsmerInputEvent event) { 69 if (mode == InputMode.NAVIGATE) { 70 selectMode(); 71 return true; 72 } 73 return false; 74 } 75 }; 76 Command enterManipulate = 77 new Command() { 78 public boolean execute(EnsmerInputEvent event) { 79 if (mode == InputMode.NAVIGATE) { 80 manipulateMode(); 81 return true; 82 } 83 return false; 84 } 85 }; 86 Command enterRotate = 87 new Command() { 88 public boolean execute(EnsmerInputEvent event) { 89 if (mode == InputMode.NAVIGATE) { 90 rotateMode(); 91 return true; 92 } 93 return false; 94 } 95 }; 96 Command leaveSelect = 97 new Command() { 98 public boolean execute(EnsmerInputEvent event) { 99 if (mode == InputMode.SELECT) { 100 navigateMode(); 101 return true; 102 } 103 return false; 104 } 105 }; 106 Command leaveManipulate = 107 new Command() { 108 public boolean execute(EnsmerInputEvent event) { 109 if (mode == InputMode.MANIPULATE) { 110 navigateMode(); 111 return true; 112 } 113 return false; 114 } 115 }; 116 Command leaveRotate = 117 new Command() { 118 public boolean execute(EnsmerInputEvent event) { 119 if (mode == InputMode.ROTATE) { 120 navigateMode(); 121 return true; 122 } 123 return false; 124 } 125 }; 126 Command startAccelerating = 127 new Command() { 128 public boolean execute(EnsmerInputEvent event) { 129 if (mode == InputMode.NAVIGATE || 130 mode == InputMode.MANIPULATE) { 131 accelerator.startAcceleration(true); 132 return true; 133 } 134 return false; 135 } 136 }; 137 Command stopAccelerating = 138 new Command() { 139 public boolean execute(EnsmerInputEvent event) { 140 accelerator.stopAcceleration(); 141 150 return mode == InputMode.SELECT ? false : true; 151 } 152 }; 153 Command startDecelerating = 154 new Command() { 155 public boolean execute(EnsmerInputEvent event) { 156 if (mode == InputMode.NAVIGATE || 157 mode == InputMode.MANIPULATE) { 158 accelerator.startAcceleration(false); 159 return true; 160 } 161 return false; 162 } 163 }; 164 Command escapeSelection = 165 new Command() { 166 public boolean execute(EnsmerInputEvent event) { 167 EnsmerManager.instance().getSelectionManager().clearSelection(); 168 return true; 169 } 170 }; 171 Command removeSelectedObjects = 172 new Command() { 173 public boolean execute(EnsmerInputEvent event) { 174 EnsmerManager ens = EnsmerManager.instance(); 175 SpecialAreaManager spec = ens.getSpecialAreaManager(); 176 AreaManager man = ens.getAreaManager(); 177 Area current = man.getCurrentArea(); 178 Long storageID = spec.getAreaID(spec.STORAGE); 179 if (storageID != null && storageID.equals(current.getAreaID())) { 180 ens.getSelectionManager().removeSelectedObjects(); 182 } 183 else { 184 Area storage = man.getArea(storageID); 185 for (Object obj : EnsmerManager.instance().getSelectionManager().getSelectedObjects()) { 186 Long id = (Long ) obj; 187 current.removeObject(id); 188 storage.addObject(id); 189 storage.setObjectPosition(id, SceneGraphUtils.randomPositionInFrontOfUser(storage)); 190 } 191 } 192 return true; 193 } 194 }; 195 196 map.addCommand(this, objectMode, new MousePressEvent( 197 MouseEvent.BUTTON2)); 198 map.addCommand(this, enterSelect, new KeyPressEvent( 199 KeyEvent.VK_SHIFT)); 200 map.addCommand(this, enterManipulate, new KeyPressEvent( 201 KeyEvent.VK_ALT)); 202 map.addCommand(this, enterRotate, new KeyPressEvent( 203 KeyEvent.VK_CONTROL)); 204 map.addCommand(this, leaveSelect, new KeyReleaseEvent( 205 KeyEvent.VK_SHIFT)); 206 map.addCommand(this, leaveManipulate, new KeyReleaseEvent( 207 KeyEvent.VK_ALT)); 208 map.addCommand(this, leaveRotate, new KeyReleaseEvent( 209 KeyEvent.VK_CONTROL)); 210 map.addCommand(this, startAccelerating, new MousePressEvent( 211 MouseEvent.BUTTON1)); 212 map.addCommand(this, startDecelerating, new MousePressEvent( 213 MouseEvent.BUTTON3)); 214 map.addCommand(this, stopAccelerating, new MouseReleaseEvent( 215 MouseEvent.BUTTON1)); 216 map.addCommand(this, stopAccelerating, new MouseReleaseEvent( 217 MouseEvent.BUTTON3)); 218 map.addCommand(this, escapeSelection, new KeyPressEvent( 219 KeyEvent.VK_ESCAPE)); 220 map.addCommand(this, removeSelectedObjects, new KeyPressEvent( 221 KeyEvent.VK_BACK_SPACE)); 222 223 EnsmerManager.instance().getInterfaceManager().setCursorVisible(false); 225 } 226 227 234 public void processInput(EnsmerInputEvent event) { 235 CommandMap map = InputManager.getCommandMap(); 236 AreaManager man = EnsmerManager.instance().getAreaManager(); 237 if (!map.interpretEvent(this, event)) { 238 if (mode == InputMode.SELECT) { 239 selector.processInput(event); 240 } 241 else if (mode == InputMode.MANIPULATE) { 242 manipulator.processInput(event); 243 } 244 else if (mode == InputMode.ROTATE) { 245 rotator.processInput(event); 246 } 247 else { 248 if (!map.interpretEvent(man.getCurrentArea(), event)) { 249 navigator.processInput(event); 250 } 251 } 252 } 253 } 254 255 258 private void navigateMode() { 259 mode = InputMode.NAVIGATE; 260 EnsmerManager.instance().getInterfaceManager().setCursorVisible(false); 261 navigator.resetProcessor(); 262 } 263 264 267 private void selectMode() { 268 mode = InputMode.SELECT; 269 EnsmerManager.instance().getInterfaceManager().setCursorVisible(true); 270 } 271 272 275 private void manipulateMode() { 276 mode = InputMode.MANIPULATE; 277 manipulator.resetProcessor(); 278 } 279 280 283 private void rotateMode() { 284 mode = InputMode.ROTATE; 285 rotator.resetProcessor(); 286 } 287 288 292 private Accelerator accelerator = new Accelerator(); 293 294 298 private TranslateMouseMoveEvent navigator = new TranslateMouseMoveEvent( 299 new Navigator(accelerator)); 300 301 305 private TranslateMouseMoveEvent manipulator = new TranslateMouseMoveEvent( 306 new Manipulator(accelerator)); 307 308 312 private TranslateMouseMoveEvent rotator = new TranslateMouseMoveEvent( 313 new Rotator(accelerator)); 314 315 318 private Selector selector = new Selector(); 319 320 323 private InputMode mode = InputMode.NAVIGATE; 324 325 330 private static enum InputMode { NAVIGATE, SELECT, MANIPULATE, ROTATE } 331 } 332 333 | Popular Tags |