KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
19
20 import com.buchuki.ensmer.*;
21 import com.buchuki.ensmer.input.command.*;
22 import com.buchuki.ensmer.input.event.*;
23
24 /**
25  * Class to respond to events pertaining to manipulating the system (rather
26  * than object data). This class operates on three modes. Navigaton mode
27  * forwards events to the navigator. Selection mode forwards events to the
28  * Selector. Manipulation mode forwards events to the Manipulator. Shift key
29  * switches to Selector mode, Alt key switches to Manipulator mode. This
30  * class tries to be implemented in a non-input-specific way, but it does not
31  * fully succeed. There are input-specific assumptions made that assume the
32  * default command input events are not changed. For example, navigation is
33  * assumed to need mouse translation and the cursor is hidden. This would not
34  * make sense if the navigation events are changed to keypresses. Since it is
35  * not possible to change the event map at this time, these assumptions will
36  * not be a problem. The problem should be noted, however, for future
37  * adjustments.
38  *
39  * @author Dusty Phillips [dusty@buchuki.com]
40  */

41 public class DefaultSystemInputManager implements InputProcessor {
42
43     /**
44      * Construct the DefaultSystemInputManager. Sets up the available commands.
45      */

46     public DefaultSystemInputManager() {
47         CommandMap map = InputManager.getCommandMap();
48
49         //placed here instead of in the selector to reset to navigation mode
50
Command objectMode =
51             new Command() {
52                 public boolean execute(EnsmerInputEvent event) {
53                     if (mode == InputMode.SELECT) {
54                         navigateMode(); //reset to navigate mode for return to object mode
55
MouseEvent mouse = (MouseEvent) event.getInputEvent();
56                         Long JavaDoc 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                     /*stop the accelerator even if the mouse release occured
142                     after the user went into select mode. IE: user presses
143                     left mouse button to start accelerating, then shift key
144                     to enter select mode, then releases left mouse button, it
145                     should still turn off the acceleration. However, when in
146                     select mode, the event should not be consumed, as it almost
147                     certainly is needed by the rest of the event as well.
148                     This will almost certainly cause bugs when the event can
149                     be changed from mouse press*/

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 JavaDoc storageID = spec.getAreaID(spec.STORAGE);
179                     if (storageID != null && storageID.equals(current.getAreaID())) {
180                         //if in storageArea, remove permanently
181
ens.getSelectionManager().removeSelectedObjects();
182                     }
183                     else {
184                         Area storage = man.getArea(storageID);
185                         for (Object JavaDoc obj : EnsmerManager.instance().getSelectionManager().getSelectedObjects()) {
186                             Long JavaDoc id = (Long JavaDoc) 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         //Default mode is navigate mode, so hide the cursor
224
EnsmerManager.instance().getInterfaceManager().setCursorVisible(false);
225     }
226
227     /**
228      * Process a particular input event forwarded from the InputManager. The
229      * event is forwarded to the Navigator, Selector, or Manipulator, unless it
230      * is one of the commands to switch between these modes.
231      *
232      * @param event the EnsmerInputEvent to be processed.
233      */

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     /**
256      * Switch to navigate mode
257      */

258     private void navigateMode() {
259         mode = InputMode.NAVIGATE;
260         EnsmerManager.instance().getInterfaceManager().setCursorVisible(false);
261         navigator.resetProcessor();
262     }
263
264     /**
265      * Switch to select mode
266      */

267     private void selectMode() {
268         mode = InputMode.SELECT;
269         EnsmerManager.instance().getInterfaceManager().setCursorVisible(true);
270     }
271
272     /**
273      * Switch to manipulate mode
274      */

275     private void manipulateMode() {
276         mode = InputMode.MANIPULATE;
277         manipulator.resetProcessor();
278     }
279
280     /**
281      * Switch to rotate mode
282      */

283     private void rotateMode() {
284         mode = InputMode.ROTATE;
285         rotator.resetProcessor();
286     }
287
288     /**
289      * The Accelerator used to manage acceleration in both navigation and
290      * manipulation mode
291      */

292     private Accelerator accelerator = new Accelerator();
293
294     /**
295      * The TranslateMouseEvent object that encapsulates movements sent to the
296      * Navigator.
297      */

298     private TranslateMouseMoveEvent navigator = new TranslateMouseMoveEvent(
299         new Navigator(accelerator));
300
301     /**
302      * The TranslateMouseEvent object that encapsulates movements sent to the
303      * Manipulator.
304      */

305     private TranslateMouseMoveEvent manipulator = new TranslateMouseMoveEvent(
306         new Manipulator(accelerator));
307
308     /**
309      * The TranslateMouseEvent object that encapsulates movements sent to the
310      * Rotator.
311      */

312     private TranslateMouseMoveEvent rotator = new TranslateMouseMoveEvent(
313         new Rotator(accelerator));
314
315     /**
316      * The Selector object that encapsulates commands issued in selection mode
317      */

318     private Selector selector = new Selector();
319
320     /**
321      * The current mode
322      */

323     private InputMode mode = InputMode.NAVIGATE;
324
325     /**
326      * Description of Enumeration
327      *
328      * @author Dusty Phillips [dusty@buchuki.com]
329      */

330     private static enum InputMode { NAVIGATE, SELECT, MANIPULATE, ROTATE }
331 }
332
333
Popular Tags