View Javadoc

1   /*
2    * @(#) EditorInputListener.java Mar 2, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
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 2003 <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/gpl.html">General Public
34   * License (GPL) </a>, no warranty <br>
35   * 
36   * @version 1.0 <br>
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
58       *            the application
59       * @param panel
60       *            the panel
61       */
62      public EditorInputListener(final DSOLApplicationInterface application,
63              final AnimationPanel panel)
64      {
65          super(application, panel);
66          this.panel = (Editor2DPanel) panel;
67      }
68  
69      /***
70       * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
71       */
72      public void mouseClicked(final MouseEvent event)
73      {
74          this.panel.requestFocus();
75      }
76  
77      /***
78       * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
79       */
80      public void mouseDragged(final MouseEvent event)
81      {
82          super.mouseDragged(event);
83  
84          Point2D newCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
85                  event.getPoint(), this.panel.getExtent(), this.panel.getSize());
86          Point2D centerCoordinate = Renderable2DInterface.Util
87                  .getWorldCoordinates(mouseClicked, this.panel.getExtent(),
88                          this.panel.getSize());
89          Point2D oldCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
90                  this.dragCoord, this.panel.getExtent(), this.panel.getSize());
91  
92          if ((this.mouseButton == MouseEvent.BUTTON1)
93                  && (this.panel.getSelectedMode() == Editor2DPanel.MODE_EDIT)
94                  && (this.panel.getSelectedPoint() == null)
95                  && (this.clickedWithinSelected))
96          {
97              if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_MOVE)
98              {
99                  // Move the entire shape
100                 EditorUtilities.moveEditable(this.panel
101                         .getSelectedEditableRenderable(), newCoordinate,
102                         oldCoordinate);
103             } else if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_ROTATE)
104             {
105                 // Rotate the entire shape
106                 EditorUtilities.rotateEditable(this.panel
107                         .getSelectedEditableRenderable(), newCoordinate,
108                         centerCoordinate, oldCoordinate);
109             }
110         }
111 
112         this.dragCoord = (Point2D) event.getPoint().clone();
113     }
114 
115     /***
116      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
117      */
118     public void mousePressed(final MouseEvent event)
119     {
120         super.mousePressed(event);
121 
122         this.mouseButton = event.getButton();
123         this.dragCoord = event.getPoint();
124 
125         // What to do in which mode
126         if (this.panel.getSelectedMode() == Editor2DPanel.MODE_EDIT)
127         {
128             CartesianPoint selectedPoint = this
129                     .determineSelectedVertice(this.mouseClicked);
130             this.panel.setSelectedPoint(selectedPoint);
131             if (this.panel.getSelectedPoint() == null)
132             {
133                 this.clickedWithinSelected = this.isInside(this.mouseClicked);
134             }
135         }
136     }
137 
138     /***
139      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
140      */
141     public void mouseReleased(final MouseEvent event)
142     {
143         super.mouseReleased(event);
144 
145         switch (this.mouseButton) 
146         {
147         case MouseEvent.BUTTON1:
148             this.performLeftMouseReleasedAction(event);
149             break;
150         case MouseEvent.BUTTON3:
151             this.performRightMouseReleasedAction(event);
152             break;
153         default:
154         }
155         this.panel.repaint();
156     }
157 
158     /***
159      * perform one of the actions of the editor
160      * 
161      * @param event
162      *            mouse event
163      */
164     protected void performLeftMouseReleasedAction(final MouseEvent event)
165     {
166         Point2D point = event.getPoint();
167         Point2D newCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
168                 point, this.panel.getExtent(), this.panel.getSize());
169         Point2D oldCoordinate = Renderable2DInterface.Util.getWorldCoordinates(
170                 this.dragCoord, this.panel.getExtent(), this.panel.getSize());
171         Point2D centerCoordinate = Renderable2DInterface.Util
172                 .getWorldCoordinates(mouseClicked, this.panel.getExtent(),
173                         this.panel.getSize());
174         switch (this.panel.getSelectedMode()) 
175         {
176         case Editor2DPanel.MODE_SELECT:
177             EditorUtilities.selectEditable(newCoordinate, this.panel);
178             break;
179         case Editor2DPanel.MODE_NEW:
180             EditorUtilities.instantiateNewEditable(newCoordinate,
181                     this.application, this.panel);
182             break;
183         case Editor2DPanel.MODE_EDIT:
184             EditableRenderable2DInterface selected = this.panel
185                     .getSelectedEditableRenderable();
186             if (selected != null)
187             {
188                 if (this.panel.getSelectedPoint() != null)
189                 {
190                     // Move selected point
191                     EditorUtilities.moveSelectedPoint(selected, newCoordinate,
192                             this.panel);
193                 } else if (this.clickedWithinSelected)
194                 {
195                     if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_MOVE)
196                     {
197                         // Move the entire shape
198                         EditorUtilities.moveEditable(selected, newCoordinate,
199                                 oldCoordinate);
200                     }
201                     if (this.panel.getSelectedEditMode() == Editor2DPanel.EDIT_MODE_ROTATE)
202                     {
203                         // Rotate the entire shape
204                         EditorUtilities.rotateEditable(selected, newCoordinate,
205                                 centerCoordinate, oldCoordinate);
206                     }
207                 }
208             }
209             break;
210         default:
211         }
212     }
213 
214     /***
215      * what to do if the right mouse button was released
216      * 
217      * @param event
218      *            MouseEvent
219      */
220     protected void performRightMouseReleasedAction(final MouseEvent event)
221     {
222         Point2D world = Renderable2DInterface.Util.getWorldCoordinates(event
223                 .getPoint(), this.panel.getExtent(), this.panel.getSize());
224         if (this.panel.getSelectedMode() == Editor2DPanel.MODE_SELECT)
225         {
226             // Determine which object was selected, if any, and
227             // show the introspection window for it.
228             Renderable2DInterface selected = EditorUtilities.selectEditable(
229                     world, this.panel);
230             if (selected != null)
231             {
232                 new IntroSpectionDialog(selected.getSource(), selected
233                         .getSource().toString());
234             }
235         } else if (this.panel.getSelectedMode() == Editor2DPanel.MODE_EDIT)
236         {
237             if (event.isPopupTrigger())
238             {
239                 this.popup(event);
240                 return;
241             }
242         }
243     }
244 
245     /***
246      * determine if a vertice was selected
247      * 
248      * @param mousePosition
249      *            the position of the mouse cursor
250      * @return the selected vertice
251      */
252     private CartesianPoint determineSelectedVertice(final Point2D mousePosition)
253     {
254         CartesianPoint selectedPoint = null;
255         this.panel.setSelectedPoint(null);
256         EditableRenderable2DInterface selected = this.panel
257                 .getSelectedEditableRenderable();
258         if (selected == null)
259         {
260             return null;
261         }
262         Editable editable = (Editable) selected.getSource();
263         CartesianPoint[] vertices = editable.getVertices();
264         CartesianPoint[] positions = new CartesianPoint[vertices.length];
265         for (int i = 0; i < vertices.length; i++)
266         {
267             positions[i] = EditorUtilities.convertToGlobalCoordinates(
268                     vertices[i], editable.getLocation());
269             Point2D p1 = Renderable2DInterface.Util.getScreenCoordinates(
270                     new Point2D.Double(positions[i].x, positions[i].y),
271                     this.panel.getExtent(), this.panel.getSize());
272             if (Math.abs(mousePosition.getX() - (int) p1.getX()) <= Editor2DPanel.PLACEHOLDERSIZE)
273             {
274                 if (Math.abs(mousePosition.getY() - (int) p1.getY()) <= Editor2DPanel.PLACEHOLDERSIZE)
275                 {
276                     selectedPoint = vertices[i];
277                 }
278             }
279         }
280         return selectedPoint;
281     }
282 
283     /***
284      * determine if the mouse pointer is inside a renderable
285      * 
286      * @param mousePosition
287      *            the position of the mouse cursor
288      * @return true if inside
289      */
290     private boolean isInside(final Point2D mousePosition)
291     {
292         EditableRenderable2DInterface selected = this.panel
293                 .getSelectedEditableRenderable();
294         if (selected == null)
295         {
296             return false;
297         }
298         Point2D worldCoord = Renderable2DInterface.Util
299                 .getWorldCoordinates(this.mouseClicked, this.panel.getExtent(),
300                         this.panel.getSize());
301         if (((Renderable2DInterface) selected).contains(worldCoord, this.panel
302                 .getExtent(), this.panel.getSize()))
303         {
304             return true;
305         }
306         return false;
307 
308     }
309 }