SUMO - Simulation of Urban MObility
GUIDanielPerspectiveChanger.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A class that allows to steer the visual output in dependence to
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <fxkeys.h>
34 #include <utils/geom/Boundary.h>
35 #include <utils/geom/Position.h>
37 #include "GUIPerspectiveChanger.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49  GUISUMOAbstractView& callBack, const Boundary& viewPort) :
50  GUIPerspectiveChanger(callBack, viewPort),
51  myOrigWidth(viewPort.getWidth()),
52  myOrigHeight(viewPort.getHeight()),
53  myRotation(0),
54  myMouseButtonState(MOUSEBTN_NONE),
55  myMoveOnClick(false),
56  myZoomBase(viewPort.getCenter()),
57  myDragDelay(0) {
58 }
59 
60 
62 
63 
64 void
65 GUIDanielPerspectiveChanger::move(int xdiff, int ydiff) {
66  myViewPort.moveby(myCallback.p2m(xdiff), -myCallback.p2m(ydiff));
67  myCallback.update();
68 }
69 
70 
71 void
73  if (myCallback.getApp()->reg().readIntEntry("gui", "zoomAtCenter", 1)) {
75  }
76  if (factor > 0) {
78  myZoomBase.x() - (myZoomBase.x() - myViewPort.xmin()) / factor,
79  myZoomBase.y() - (myZoomBase.y() - myViewPort.ymin()) / factor,
80  myZoomBase.x() - (myZoomBase.x() - myViewPort.xmax()) / factor,
81  myZoomBase.y() - (myZoomBase.y() - myViewPort.ymax()) / factor);
82  myCallback.update();
83  }
84 }
85 
86 
87 void
89  /*
90  if (myCallback.allowRotation()) {
91  myRotation += (SUMOReal) diff / (SUMOReal) 10.0;
92  myCallback.update();
93  }
94  */
95 }
96 
97 
100  return myRotation;
101 }
102 
103 
104 SUMOReal
106  return myViewPort.getCenter().x();
107 }
108 
109 
110 SUMOReal
112  return myViewPort.getCenter().y();
113 }
114 
115 
116 SUMOReal
118  return myOrigWidth / myViewPort.getWidth() * 100;
119 }
120 
121 
122 SUMOReal
124  return myViewPort.getWidth();
125 }
126 
127 
128 SUMOReal
130  return myOrigWidth / (zoom / 100);
131 }
132 
133 
134 SUMOReal
136  return (myOrigWidth / zPos) * 100;
137 }
138 
139 
140 void
142  bool applyZoom) {
143  if (applyZoom) {
144  myViewPort = Boundary();
145  myViewPort.add(pos);
146  myViewPort.grow(radius);
147  } else {
148  myViewPort.moveby(pos.x() - getXPos(), pos.y() - getYPos());
149  }
150 }
151 
152 
153 void
156  FXEvent* e = (FXEvent*) data;
157  myMouseXPosition = e->win_x;
158  myMouseYPosition = e->win_y;
159  myMoveOnClick = false;
160  myMouseDownTime = FXThread::time();
161 }
162 
163 
164 bool
167  FXEvent* e = (FXEvent*) data;
168  myMouseXPosition = e->win_x;
169  myMouseYPosition = e->win_y;
170  return myMoveOnClick;
171 }
172 
173 
174 void
177  FXEvent* e = (FXEvent*) data;
178  myMouseXPosition = e->win_x;
179  myMouseYPosition = e->win_y;
180  myMoveOnClick = false;
181  myMouseDownTime = FXThread::time();
183 }
184 
185 
186 bool
189  if (data != 0) {
190  FXEvent* e = (FXEvent*) data;
191  myMouseXPosition = e->win_x;
192  myMouseYPosition = e->win_y;
193  }
194  return myMoveOnClick;
195 }
196 
197 
198 void
200  FXEvent* e = (FXEvent*) data;
201  SUMOReal diff = 0.1;
202  if ((e->state & CONTROLMASK) != 0) {
203  diff /= 2;
204  } else if ((e->state & SHIFTMASK) != 0) {
205  diff *= 2;
206  }
207  if (e->code < 0) {
208  diff = -diff;
209  }
211  zoom(1.0 + diff);
213 }
214 
215 
216 void
218  FXEvent* e = (FXEvent*) data;
219  myCallback.setWindowCursorPosition(e->win_x, e->win_y);
220  const int xdiff = myMouseXPosition - e->win_x;
221  const int ydiff = myMouseYPosition - e->win_y;
222  const bool moved = xdiff != 0 || ydiff != 0;
223  const bool pastDelay = !gSchemeStorage.getDefault().gaming && FXThread::time() > (myMouseDownTime + myDragDelay);
224  switch (myMouseButtonState) {
225  case MOUSEBTN_LEFT:
226  if (pastDelay) {
227  move(xdiff, ydiff);
228  if (moved) {
229  myMoveOnClick = true;
230  }
231  }
232  break;
233  case MOUSEBTN_RIGHT:
234  if (pastDelay) {
235  zoom(1 + 10.0 * ydiff / myCallback.getWidth());
236  rotate(xdiff);
237  if (moved) {
238  myMoveOnClick = true;
239  }
240  }
241  break;
242  default:
243  if (moved) {
245  }
246  break;
247  }
248  myMouseXPosition = e->win_x;
249  myMouseYPosition = e->win_y;
250 }
251 
252 
253 void
255  SUMOReal xPos, SUMOReal yPos) {
256  const SUMOReal zoomFactor = zoom / 50; // /100 to normalize, *2 because growth is added on both sides
257  myViewPort = Boundary();
258  myViewPort.add(Position(xPos, yPos));
259  myViewPort.growHeight(myOrigHeight / zoomFactor);
260  myViewPort.growWidth(myOrigWidth / zoomFactor);
261  myCallback.update();
262 }
263 
264 
265 void
267  setViewport(zPos2Zoom(zPos), xPos, yPos);
268 }
269 
270 
271 void
274  myViewPort.xmin() - myCallback.p2m(change),
275  myViewPort.ymin(),
276  myViewPort.xmax(),
277  myViewPort.ymax());
278 }
279 
280 
281 long
283  FXEvent* e = (FXEvent*) data;
284  SUMOReal zoomDiff = 0.1;
285  SUMOReal moveX = 0;
286  SUMOReal moveY = 0;
287  SUMOReal moveFactor = 1;
288  bool pageVertical = true;
289  bool ctrl = false;
290  if (e->state & CONTROLMASK) {
291  ctrl = true;
292  zoomDiff /= 2;
293  moveFactor /= 10;
294  } else if (e->state & SHIFTMASK) {
295  pageVertical = false;
296  zoomDiff *= 2;
297  }
298  switch (e->code) {
299  case FX::KEY_Left:
300  moveX = -1;
301  moveFactor /= 10;
302  break;
303  case FX::KEY_Right:
304  moveX = 1;
305  moveFactor /= 10;
306  break;
307  case FX::KEY_Up:
308  moveY = -1;
309  moveFactor /= 10;
310  break;
311  case FX::KEY_Down:
312  moveY = 1;
313  moveFactor /= 10;
314  break;
315  case FX::KEY_Page_Up:
316  if (pageVertical) {
317  moveY = -1;
318  } else {
319  moveX = -1;
320  }
321  break;
322  case FX::KEY_Page_Down:
323  if (pageVertical) {
324  moveY = 1;
325  } else {
326  moveX = 1;
327  }
328  break;
329  case FX::KEY_plus:
330  case FX::KEY_KP_Add:
332  zoom(1.0 + zoomDiff);
334  return 1;
335  case FX::KEY_minus:
336  case FX::KEY_KP_Subtract:
337  zoomDiff = -zoomDiff;
339  zoom(1.0 + zoomDiff);
341  return 1;
342  case FX::KEY_Home:
343  case FX::KEY_KP_Home:
345  myCallback.update();
346  return 1;
347  case FX::KEY_v:
348  // from an architecture standpoint this isn't the best place to put
349  // this. But its simple
350  if (ctrl) {
352  return 1;
353  }
354  default:
355  return 0;
356  }
357  myViewPort.moveby(moveX * moveFactor * myViewPort.getWidth(),
358  -moveY * moveFactor * myViewPort.getHeight());
359  myCallback.update();
360  return 1;
361 }
362 
363 
364 /****************************************************************************/
GUICompleteSchemeStorage gSchemeStorage
void growWidth(SUMOReal by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:241
bool myMoveOnClick
Information whether the user has moved the cursor while pressing a mouse button.
virtual void recenterView()
recenters the view
virtual SUMOReal zPos2Zoom(SUMOReal zPos) const
Returns the zoom level that is achieved at a given camera height.
void onRightBtnPress(void *data)
called when user press right button
SUMOReal getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:172
bool onLeftBtnRelease(void *data)
called when user releases left button
long onKeyPress(void *data)
called when user press a key
bool gaming
whether the application is in gaming mode or not
virtual SUMOReal getXPos() const
Returns the x-offset of the field to show stored in this changer.
Position myZoomBase
the network location on which to zoom using right click+drag
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
virtual SUMOReal getRotation() const
Returns the rotation of the canvas stored in this changer.
void rotate(int diff)
Performs the rotation of the view.
virtual SUMOReal getYPos() const
Returns the y-offset of the field to show stored in this changer.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
SUMOReal getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:166
SUMOReal myOrigWidth
the original viewport dimensions in m which serve as the reference point for 100% zoom ...
void onLeftBtnPress(void *data)
mouse functions
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
void onMouseWheel(void *data)
called when user changes mouse wheel
bool onRightBtnRelease(void *data)
called when user releases right button
GUIDanielPerspectiveChanger(GUISUMOAbstractView &callBack, const Boundary &viewPort)
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
void centerTo(const Position &pos, SUMOReal radius, bool applyZoom=true)
Centers the view to the given position, setting it to a size that covers the radius.
int myMouseButtonState
the current mouse state
SUMOReal p2m(SUMOReal pixel) const
pixels-to-meters conversion method
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
void onMouseMove(void *data)
called when user moves mouse
GUISUMOAbstractView & myCallback
The parent window (canvas to scale)
void zoom(SUMOReal factor)
Performs the zooming of the view.
SUMOReal myRotation
the current rotation
void setViewport(SUMOReal zoom, SUMOReal xPos, SUMOReal yPos)
Sets the viewport.
void setViewportFrom(SUMOReal xPos, SUMOReal yPos, SUMOReal zPos)
Alternative method for setting the viewport.
GUIVisualizationSettings & getDefault()
Returns the default scheme.
Boundary & grow(SUMOReal by)
extends the boundary by the given amount
Definition: Boundary.cpp:232
Boundary myViewPort
the intended viewport
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
void add(SUMOReal x, SUMOReal y, SUMOReal z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:90
void setWindowCursorPosition(FXint x, FXint y)
Returns the information whether rotation is allowd.
virtual SUMOReal getZoom() const
Returns the zoom factor computed stored in this changer.
Position getCenter() const
Returns the center of the boundary.
Definition: Boundary.cpp:124
virtual SUMOReal zoom2ZPos(SUMOReal zoom) const
Returns the camera height at which the given zoom level is reached.
#define SUMOReal
Definition: config.h:213
Position getPositionInformation() const
Returns the cursor&#39;s x/y position within the network.
void updateToolTip()
A method that updates the tooltip.
void growHeight(SUMOReal by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:248
FXint myMouseXPosition
the current mouse position
void showViewschemeEditor()
show viewsscheme editor
virtual SUMOReal getZPos() const
Returns the camera height corresponding to the current zoom factor.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void moveby(SUMOReal x, SUMOReal y, SUMOReal z=0)
Moves the boundary by the given amount.
Definition: Boundary.cpp:281