1 16 package com.buchuki.ensmer.input; 17 18 import java.awt.event.KeyEvent ; 19 import java.awt.event.MouseEvent ; 20 import javax.vecmath.*; 21 22 import com.buchuki.ensmer.*; 23 import com.buchuki.ensmer.builtin.Teleport; 24 import com.buchuki.ensmer.input.command.*; 25 import com.buchuki.ensmer.input.command.navigation.*; 26 import com.buchuki.ensmer.input.event.*; 27 28 29 35 public class Navigator implements InputProcessor, Transformable3D { 36 37 43 public Navigator(Accelerator accelerator) { 44 final CommandMap map = InputManager.getCommandMap(); 45 final Command moveForwardBack = 46 new MoveCommand(this, accelerator, -0.01f, MoveCommand.Axis.Z); 47 final RotateCommand rotateForwardBack = 48 new RotateCommand(this, accelerator, 0.001f, RotateCommand.Axis.X); 49 final Command rotateLeftRight = 50 new RotateCommand(this, accelerator, 0.001f, RotateCommand.Axis.Y); 51 Command moveUpDown = 52 new MoveCommand(this, accelerator, -0.01f, MoveCommand.Axis.Y); 53 54 Command rotateModeCommand = 56 new Command() { 57 public boolean execute(EnsmerInputEvent e) { 58 map.removeCommand(Navigator.this, new VerticalMouseMoveEvent(-1)); 59 map.removeCommand(Navigator.this, new VerticalMouseMoveEvent(1)); 60 map.removeCommand(Navigator.this, new HorizontalMouseMoveEvent(-1)); 61 map.removeCommand(Navigator.this, new HorizontalMouseMoveEvent(1)); 62 map.addCommand(Navigator.this, rotateForwardBack, new VerticalMouseMoveEvent(-1)); 63 map.addCommand(Navigator.this, rotateForwardBack, new VerticalMouseMoveEvent(1)); 64 return true; 65 } 66 }; 67 Command normalModeCommand = 68 new Command() { 69 public boolean execute(EnsmerInputEvent e) { 70 rotateForwardBack.undoRotation(); 71 map.removeCommand(Navigator.this, new VerticalMouseMoveEvent(-1)); 72 map.removeCommand(Navigator.this, new VerticalMouseMoveEvent(1)); 73 map.addCommand(Navigator.this, moveForwardBack, new VerticalMouseMoveEvent(-1)); 74 map.addCommand(Navigator.this, moveForwardBack, new VerticalMouseMoveEvent(1)); 75 map.addCommand(Navigator.this, rotateLeftRight, new HorizontalMouseMoveEvent(-1)); 76 map.addCommand(Navigator.this, rotateLeftRight, new HorizontalMouseMoveEvent(1)); 77 return true; 78 } 79 }; 80 Command addObjectCommand = 81 new Command() { 82 public boolean execute(EnsmerInputEvent e) { 83 AreaManager man = EnsmerManager.instance().getAreaManager(); 84 if (man.getCurrentArea().isReadOnly()) { 85 return false; 86 } 87 SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager(); 88 spec.buildAddObjectArea(); 89 man.setCurrentArea(spec.getAreaID(spec.ADD_OBJECT)); 90 return true; 91 } 92 }; 93 Command previousAreaCommand = 94 new Command() { 95 public boolean execute(EnsmerInputEvent e) { 96 AreaManager man = EnsmerManager.instance().getAreaManager(); 97 if (man.getHistoricalAreaID(1) != null) { 98 man.previousArea(); 99 return true; 100 } 101 return false; 102 } 103 }; 104 Command nextAreaCommand = 105 new Command() { 106 public boolean execute(EnsmerInputEvent e) { 107 AreaManager man = EnsmerManager.instance().getAreaManager(); 108 if (man.getHistoricalAreaID(-1) != null) { 109 man.nextArea(); 110 return true; 111 } 112 return false; 113 } 114 }; 115 Command addSelectedToInventoryCommand = 116 new Command() { 117 public boolean execute(EnsmerInputEvent event) { 118 AreaManager man = EnsmerManager.instance().getAreaManager(); 119 SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager(); 120 Area inventory = man.getArea(spec.getAreaID(spec.INVENTORY)); 121 Area current = man.getCurrentArea(); 122 123 for (Object obj : EnsmerManager.instance().getSelectionManager().getSelectedObjects()) { 124 Long id = (Long ) obj; 125 current.removeObject(id); 126 inventory.addObject(id); 127 inventory.setObjectPosition(id, SceneGraphUtils.randomPositionInFrontOfUser(inventory)); 128 } 129 return true; 130 } 131 }; 132 Command lookInInventoryCommand = 133 new Command() { 134 public boolean execute(EnsmerInputEvent event) { 135 AreaManager man = EnsmerManager.instance().getAreaManager(); 136 SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager(); 137 man.setCurrentArea(spec.getAreaID(spec.INVENTORY)); 138 return true; 139 } 140 }; 141 Command lookInStorageCommand = 142 new Command() { 143 public boolean execute(EnsmerInputEvent event) { 144 AreaManager man = EnsmerManager.instance().getAreaManager(); 145 SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager(); 146 man.setCurrentArea(spec.getAreaID(spec.STORAGE)); 147 return true; 148 } 149 }; 150 Command createTeleportCommand = 151 new Command() { 152 public boolean execute(EnsmerInputEvent event) { 153 SelectionManager sel = EnsmerManager.instance().getSelectionManager(); 154 AreaManager man = EnsmerManager.instance().getAreaManager(); 155 Object [] obj = sel.getSelectedObjects(); 156 if (obj == null || man.getCurrentArea().isReadOnly()) { 157 return false; 158 } 159 Long id = (Long ) obj[0]; 160 Teleport tel = new Teleport(); 161 tel.setTeleportObject(id); 162 Matrix4f loc = SceneGraphUtils.positionInFrontOfUser(man.getCurrentArea()); 163 man.getCurrentArea().newObject(tel, loc); 164 sel.clearSelection(); 165 sel.toggleSelection(tel.getId()); 166 return true; 167 } 168 }; 169 Command showConfiguratorCommand = 170 new Command() { 171 public boolean execute(EnsmerInputEvent event) { 172 return EnsmerManager.instance().getConfigManager().showConfigurator(); 173 } 174 }; 175 176 map.addCommand(this, moveForwardBack, new VerticalMouseMoveEvent(-1)); 177 map.addCommand(this, moveForwardBack, new VerticalMouseMoveEvent(1)); 178 map.addCommand(this, rotateLeftRight, new HorizontalMouseMoveEvent(-1)); 179 map.addCommand(this, rotateLeftRight, new HorizontalMouseMoveEvent(1)); 180 map.addCommand(this, moveUpDown, new MouseWheelEvent()); 181 map.addCommand(this, rotateModeCommand, new MousePressEvent(MouseEvent.BUTTON2)); 182 map.addCommand(this, normalModeCommand, new MouseReleaseEvent(MouseEvent.BUTTON2)); 183 map.addCommand(this, addObjectCommand, new KeyPressEvent(KeyEvent.VK_A)); 184 map.addCommand(this, previousAreaCommand, new KeyPressEvent(KeyEvent.VK_LEFT)); 185 map.addCommand(this, nextAreaCommand, new KeyPressEvent(KeyEvent.VK_RIGHT)); 186 map.addCommand(this, addSelectedToInventoryCommand, new KeyPressEvent(KeyEvent.VK_P)); 187 map.addCommand(this, lookInInventoryCommand, new KeyPressEvent(KeyEvent.VK_I)); 188 map.addCommand(this, lookInStorageCommand, new KeyPressEvent(KeyEvent.VK_S)); 189 map.addCommand(this, createTeleportCommand, new KeyPressEvent(KeyEvent.VK_T)); 190 map.addCommand(this, showConfiguratorCommand, new KeyPressEvent(KeyEvent.VK_C)); 191 } 192 193 199 public void processInput(EnsmerInputEvent event) { 200 CommandMap map = InputManager.getCommandMap(); 201 map.interpretEvent(this, event); 202 } 203 204 209 public void moveTransform(Matrix4f matrix) { 210 EnsmerManager.instance().getUserManager().setUserPosition(matrix); 211 } 212 } 213 214 | Popular Tags |