KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > Navigator


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer.input;
17
18 import java.awt.event.KeyEvent JavaDoc;
19 import java.awt.event.MouseEvent JavaDoc;
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 /**
30  * Class to process navigation mode events. The events in this module move
31  * the user's view about the scene via calls to the UserManager class
32  *
33  * @author Dusty Phillips [dusty@buchuki.com]
34  */

35 public class Navigator implements InputProcessor, Transformable3D {
36
37     /**
38      * Construct the Navigator. Sets up the available commands.
39      *
40      * @param accelerator the Accelerator to manage the acceleration of the
41      * user
42      */

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         //built-in commands
55
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 JavaDoc obj : EnsmerManager.instance().getSelectionManager().getSelectedObjects()) {
124                         Long JavaDoc id = (Long JavaDoc) 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 JavaDoc[] obj = sel.getSelectedObjects();
156                     if (obj == null || man.getCurrentArea().isReadOnly()) {
157                         return false;
158                     }
159                     Long JavaDoc id = (Long JavaDoc) 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     /**
194      * Process an EnsmerInputEvent that occurs. The navigator looks up the
195      * possible actions in the CommandMap and acts on them.
196      *
197      * @param event the event that needs to be processed by the navigator
198      */

199     public void processInput(EnsmerInputEvent event) {
200         CommandMap map = InputManager.getCommandMap();
201         map.interpretEvent(this, event);
202     }
203
204     /**
205      * Move the user by the relative amount given
206      *
207      * @param matrix the relative matrix to be applied to the user movement
208      */

209     public void moveTransform(Matrix4f matrix) {
210         EnsmerManager.instance().getUserManager().setUserPosition(matrix);
211     }
212 }
213
214
Popular Tags