KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > event > TranslateMouseMoveEvent


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.event;
17
18 import java.awt.*;
19 import java.awt.event.*;
20 import com.buchuki.ensmer.*;
21 import com.buchuki.ensmer.input.*;
22
23 /**
24  * This is a special utility class that creates special LateralMove events
25  * from mouse input. It implements InputProcessor and listens for events
26  * coming into it. All events that are not MouseMoveEvents are forwarded
27  * directly to an associated processor. However, MouseMoveEvents are
28  * translated into LateralMouseMoveEvents and VerticalMouseMoveEvents. The
29  * mouse is repositioned at the centre of an associated component each time
30  * an event is interpreted and motion is perceived as relative to that point.
31  * The general idea is to convert the asynchronous mouse input into movements
32  * that are only horizontal or vertical in nature. However, this is not the
33  * same as constraining the mouse to horizontal or vertical movements, as it
34  * is kept near the centre of the window.
35  *
36  * @author Dusty Phillips [dusty@buchuki.com]
37  */

38 public class TranslateMouseMoveEvent implements InputProcessor {
39
40     /**
41      * Construct a TranslateMouseMoveEvent object with reference to the
42      * InputProcessor that events should be forwarded to. All events are
43      * forwarded intact except MouseMoveEvents, which become
44      * LateralMouseMoveEvents and VerticalMouseMoveEvents.
45      *
46      * @param processor the InputProcessor to forward events to
47      */

48     public TranslateMouseMoveEvent(InputProcessor processor) {
49         this.processor = processor;
50         try {
51             mousePositioner = new Robot();
52         } catch (AWTException e) {
53             e.printStackTrace();
54             System.err.println("No way to reposition the pointer. Navigation" +
55                 "will not work");
56         }
57     }
58
59     /**
60      * Process input and forward to the associated processor.
61      *
62      * @param event the EnsmerInputEvent to be processed If it is a
63      * MouseMoveEvent, it is translated.
64      */

65     public void processInput(EnsmerInputEvent event) {
66         if (event instanceof MouseMoveEvent) {
67             Point newPoint = ((MouseEvent) event.getInputEvent()).getPoint();
68             if (lastInputRobotic) {
69                 lastInputRobotic = false;
70                 lastPoint = getCanvasCenter();
71                 return;
72             }
73             if (lastPoint != null) {
74                 int dx = lastPoint.x - newPoint.x;
75                 int dy = lastPoint.y - newPoint.y;
76                 int adx = Math.abs(dx);
77                 int ady = Math.abs(dy);
78                 if (adx < 10 && ady < 10) {
79                     return;
80                 }
81                 if (adx > ady) { //movement in x
82
processor.processInput(new HorizontalMouseMoveEvent(dx));
83                 }
84                 else { //movement in y.
85
processor.processInput(new VerticalMouseMoveEvent(dy));
86                 }
87                 centerMouse();
88             }
89             lastPoint = newPoint;
90         }
91         else {
92             processor.processInput(event);
93         }
94     }
95
96     /**
97      * Reset the TranslateMouseMoveEvent object to have a lastpoint of 0. This
98      * is used to prevent an odd movement when the behavior is suddenly enabled.
99      */

100     public void resetProcessor() {
101         lastPoint = null;
102     }
103
104     /**
105      * Center the mouse on the canvas. This is used to prevent the cursor from
106      * going out of the screen.
107      */

108     private void centerMouse() {
109         Component component
110              = EnsmerManager.instance().getInterfaceManager().getCanvas();
111         Point componentLoc = component.getLocationOnScreen();
112         Point centre = getCanvasCenter();
113         lastInputRobotic = true;
114         mousePositioner.mouseMove(
115             componentLoc.x + centre.x, componentLoc.y + centre.y);
116     }
117
118     /**
119      * Get the centre point of the canvas
120      *
121      * @return a Point representing the centre of the InterfaceManager's
122      * canvas
123      */

124     private Point getCanvasCenter() {
125         Component component
126              = EnsmerManager.instance().getInterfaceManager().getCanvas();
127
128         return new Point(
129             component.getSize().width / 2,
130             component.getSize().height / 2);
131     }
132
133     /**
134      * The InputProcessor that converted movement events should be forwarded to
135      */

136     private InputProcessor processor;
137
138     /**
139      * The robot that is used by the centerMouse method.
140      */

141     private Robot mousePositioner;
142
143     /**
144      * The last recorded position of the mouse.
145      */

146     private Point lastPoint;
147
148     /**
149      * If the last input was automatic and should be ignored
150      */

151     private boolean lastInputRobotic = false;
152 }
153
154
Popular Tags