1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.gui.editor2D.mouse;
11
12 import java.awt.event.MouseEvent;
13 import java.awt.geom.Point2D;
14
15 import nl.tudelft.simulation.dsol.animation.Editable;
16 import nl.tudelft.simulation.dsol.animation.D2.EditableRenderable2DInterface;
17 import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
18 import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
19 import nl.tudelft.simulation.dsol.gui.animation2D.AnimationPanel;
20 import nl.tudelft.simulation.dsol.gui.animation2D.mouse.InputListener;
21 import nl.tudelft.simulation.dsol.gui.editor2D.Editor2DPanel;
22 import nl.tudelft.simulation.dsol.gui.editor2D.actions.EditorUtilities;
23 import nl.tudelft.simulation.introspection.gui.IntroSpectionDialog;
24 import nl.tudelft.simulation.language.d3.CartesianPoint;
25
26 /***
27 * An input listener for the editor
28 * <p>
29 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
30 * University of Technology </a>, the Netherlands. <br>
31 * See for project information <a
32 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
33 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
34 * General Public License (LGPL) </a>, no warranty.
35 *
36 * @version $Revision$ $Date$
37 * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
38 * </a>
39 */
40 public class EditorInputListener extends InputListener
41 {
42 /*** the source panel */
43 protected Editor2DPanel panel = null;
44
45 /*** did we click within the selected object? */
46 protected boolean clickedWithinSelected = false;
47
48 /*** Last mouse button that was pressed */
49 protected int mouseButton = 0;
50
51 /*** Point where the mouse is when dragging */
52 protected Point2D dragCoord = null;
53
54 /***
55 * constructor
56 *
57 * @param application the application
58 * @param panel the panel
59 */
60 public EditorInputListener(final DSOLApplicationInterface application,
61 final AnimationPanel panel)
62 {
63 super(application, panel);
64 this.panel = (Editor2DPanel) panel;
65 }
66
67 /***
68 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
69 */
70 public void mouseClicked(final MouseEvent event)
71 {
72 this.panel.requestFocus();
73 }
74
75 /***
76 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
77 */
78 public void mouseDragged(final MouseEvent event)
79 {
80 super.mouseDragged(event);
81
82 Point2D newCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
83 event.getPoint(), this.panel.getExtent(), this.panel.getSize());
84 Point2D centerCoordinate = Renderable2DInterface.Util
85 .getWorldCoordinates(this.mouseClicked, this.panel.getExtent(),
86 this.panel.getSize());
87 Point2D oldCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
88 this.dragCoord, this.panel.getExtent(), this.panel.getSize());
89
90 if ((this.mouseButton == MouseEvent.BUTTON1)
91 && (this.panel.getSelectedMode() == Editor2DPanel.MODE_EDIT)
92 && (this.panel.getSelectedPoint() == null)
93 && (this.clickedWithinSelected))
94 {
95 if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_MOVE)
96 {
97
98 EditorUtilities.moveEditable(this.panel
99 .getSelectedEditableRenderable(), newCoordinate,
100 oldCoordinate);
101 } else if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_ROTATE)
102 {
103
104 EditorUtilities.rotateEditable(this.panel
105 .getSelectedEditableRenderable(), newCoordinate,
106 centerCoordinate, oldCoordinate);
107 }
108 }
109
110 this.dragCoord = (Point2D) event.getPoint().clone();
111 }
112
113 /***
114 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
115 */
116 public void mousePressed(final MouseEvent event)
117 {
118 super.mousePressed(event);
119
120 this.mouseButton = event.getButton();
121 this.dragCoord = event.getPoint();
122
123
124 if (this.panel.getSelectedMode() == Editor2DPanel.MODE_EDIT)
125 {
126 CartesianPoint selectedPoint = this
127 .determineSelectedVertice(this.mouseClicked);
128 this.panel.setSelectedPoint(selectedPoint);
129 if (this.panel.getSelectedPoint() == null)
130 {
131 this.clickedWithinSelected = this.isInside(this.mouseClicked);
132 }
133 }
134 }
135
136 /***
137 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
138 */
139 public void mouseReleased(final MouseEvent event)
140 {
141 super.mouseReleased(event);
142
143 switch (this.mouseButton)
144 {
145 case MouseEvent.BUTTON1 :
146 this.performLeftMouseReleasedAction(event);
147 break;
148 case MouseEvent.BUTTON3 :
149 this.performRightMouseReleasedAction(event);
150 break;
151 default :
152 }
153 this.panel.repaint();
154 }
155
156 /***
157 * perform one of the actions of the editor
158 *
159 * @param event mouse event
160 */
161 protected void performLeftMouseReleasedAction(final MouseEvent event)
162 {
163 Point2D point = event.getPoint();
164 Point2D newCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
165 point, this.panel.getExtent(), this.panel.getSize());
166 Point2D oldCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
167 this.dragCoord, this.panel.getExtent(), this.panel.getSize());
168 Point2D centerCoordinate = Renderable2DInterface.Util
169 .getWorldCoordinates(this.mouseClicked, this.panel.getExtent(),
170 this.panel.getSize());
171 switch (this.panel.getSelectedMode())
172 {
173 case Editor2DPanel.MODE_SELECT :
174 EditorUtilities.selectEditable(newCoordinate, this.panel);
175 break;
176 case Editor2DPanel.MODE_NEW :
177 EditorUtilities.instantiateNewEditable(newCoordinate,
178 this.application, this.panel);
179 break;
180 case Editor2DPanel.MODE_EDIT :
181 EditableRenderable2DInterface selected = this.panel
182 .getSelectedEditableRenderable();
183 if (selected != null)
184 {
185 if (this.panel.getSelectedPoint() != null)
186 {
187
188 EditorUtilities.moveSelectedPoint(selected,
189 newCoordinate, this.panel);
190 } else if (this.clickedWithinSelected)
191 {
192 if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_MOVE)
193 {
194
195 EditorUtilities.moveEditable(selected,
196 newCoordinate, oldCoordinate);
197 }
198 if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_ROTATE)
199 {
200
201 EditorUtilities.rotateEditable(selected,
202 newCoordinate, centerCoordinate,
203 oldCoordinate);
204 }
205 }
206 }
207 break;
208 default :
209 }
210 }
211
212 /***
213 * what to do if the right mouse button was released
214 *
215 * @param event MouseEvent
216 */
217 protected void performRightMouseReleasedAction(final MouseEvent event)
218 {
219 Point2D world = Renderable2DInterface.Util.getWorldCoordinates(event
220 .getPoint(), this.panel.getExtent(), this.panel.getSize());
221 if (this.panel.getSelectedMode() == Editor2DPanel.MODE_SELECT)
222 {
223
224
225 Renderable2DInterface selected = EditorUtilities.selectEditable(
226 world, this.panel);
227 if (selected != null)
228 {
229 new IntroSpectionDialog(selected.getSource(), selected
230 .getSource().toString());
231 }
232 } else if (this.panel.getSelectedMode() == Editor2DPanel.MODE_EDIT)
233 {
234 if (event.isPopupTrigger())
235 {
236 this.popup(event);
237 return;
238 }
239 }
240 }
241
242 /***
243 * determine if a vertice was selected
244 *
245 * @param mousePosition the position of the mouse cursor
246 * @return the selected vertice
247 */
248 private CartesianPoint determineSelectedVertice(final Point2D mousePosition)
249 {
250 CartesianPoint selectedPoint = null;
251 this.panel.setSelectedPoint(null);
252 EditableRenderable2DInterface selected = this.panel
253 .getSelectedEditableRenderable();
254 if (selected == null)
255 {
256 return null;
257 }
258 Editable editable = (Editable) selected.getSource();
259 CartesianPoint[] vertices = editable.getVertices();
260 CartesianPoint[] positions = new CartesianPoint[vertices.length];
261 for (int i = 0; i < vertices.length; i++)
262 {
263 positions[i] = EditorUtilities.convertToGlobalCoordinates(
264 vertices[i], editable.getLocation());
265 Point2D p1 = Renderable2DInterface.Util.getScreenCoordinates(
266 new Point2D.Double(positions[i].x, positions[i].y),
267 this.panel.getExtent(), this.panel.getSize());
268 if (Math.abs(mousePosition.getX() - (int) p1.getX()) <= Editor2DPanel.PLACEHOLDERSIZE)
269 {
270 if (Math.abs(mousePosition.getY() - (int) p1.getY()) <= Editor2DPanel.PLACEHOLDERSIZE)
271 {
272 selectedPoint = vertices[i];
273 }
274 }
275 }
276 return selectedPoint;
277 }
278
279 /***
280 * determine if the mouse pointer is inside a renderable
281 *
282 * @param mousePosition the position of the mouse cursor
283 * @return true if inside
284 */
285 private boolean isInside(final Point2D mousePosition)
286 {
287 EditableRenderable2DInterface selected = this.panel
288 .getSelectedEditableRenderable();
289 if (selected == null)
290 {
291 return false;
292 }
293 Point2D worldCoord = Renderable2DInterface.Util
294 .getWorldCoordinates(this.mouseClicked, this.panel.getExtent(),
295 this.panel.getSize());
296 if (((Renderable2DInterface) selected).contains(worldCoord, this.panel
297 .getExtent(), this.panel.getSize()))
298 {
299 return true;
300 }
301 return false;
302
303 }
304 }