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-2017 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  // catch empty ghost events after scroll (seem to occur only on Ubuntu)
202  if (e->code == 0) {
203  return;
204  }
205  // zoom scale relative delta and its inverse; is optimized (all literals)
206  const SUMOReal zScale_rDelta_norm = 0.1;
207  const SUMOReal zScale_rDelta_inv = -zScale_rDelta_norm / (1. + zScale_rDelta_norm);
208  SUMOReal zScale_rDelta = zScale_rDelta_norm ;
209  if (e->code < 0) {
210  // for inverse zooming direction
211  zScale_rDelta = zScale_rDelta_inv;
212  }
213  // keyboard modifier: slow, fast mouse-zoom
214  if ((e->state & CONTROLMASK) != 0) {
215  zScale_rDelta /= 4;
216  } else if ((e->state & SHIFTMASK) != 0) {
217  zScale_rDelta *= 4;
218  }
220  zoom(1.0 + zScale_rDelta);
222 }
223 
224 
225 void
227  FXEvent* e = (FXEvent*) data;
228  myCallback.setWindowCursorPosition(e->win_x, e->win_y);
229  const int xdiff = myMouseXPosition - e->win_x;
230  const int ydiff = myMouseYPosition - e->win_y;
231  const bool moved = xdiff != 0 || ydiff != 0;
232  const bool pastDelay = !gSchemeStorage.getDefault().gaming && FXThread::time() > (myMouseDownTime + myDragDelay);
233  switch (myMouseButtonState) {
234  case MOUSEBTN_LEFT:
235  if (pastDelay) {
236  move(xdiff, ydiff);
237  if (moved) {
238  myMoveOnClick = true;
239  }
240  }
241  break;
242  case MOUSEBTN_RIGHT:
243  if (pastDelay) {
244  zoom(1 + 10.0 * ydiff / myCallback.getWidth());
245  rotate(xdiff);
246  if (moved) {
247  myMoveOnClick = true;
248  }
249  }
250  break;
251  default:
252  if (moved) {
254  }
255  break;
256  }
257  myMouseXPosition = e->win_x;
258  myMouseYPosition = e->win_y;
259 }
260 
261 
262 void
264  SUMOReal xPos, SUMOReal yPos) {
265  const SUMOReal zoomFactor = zoom / 50; // /100 to normalize, *2 because growth is added on both sides
266  myViewPort = Boundary();
267  myViewPort.add(Position(xPos, yPos));
268  myViewPort.growHeight(myOrigHeight / zoomFactor);
269  myViewPort.growWidth(myOrigWidth / zoomFactor);
270  myCallback.update();
271 }
272 
273 
274 void
276  setViewport(zPos2Zoom(zPos), xPos, yPos);
277 }
278 
279 
280 void
283  myViewPort.xmin() - myCallback.p2m(change),
284  myViewPort.ymin(),
285  myViewPort.xmax(),
286  myViewPort.ymax());
287 }
288 
289 
290 long
292  // ignore key events in gaming mode
294  return 0;
295  }
296  FXEvent* e = (FXEvent*) data;
297  SUMOReal zoomDiff = 0.1;
298  SUMOReal moveX = 0;
299  SUMOReal moveY = 0;
300  SUMOReal moveFactor = 1;
301  bool pageVertical = true;
302  bool ctrl = false;
303  if (e->state & CONTROLMASK) {
304  ctrl = true;
305  zoomDiff /= 2;
306  moveFactor /= 10;
307  } else if (e->state & SHIFTMASK) {
308  pageVertical = false;
309  zoomDiff *= 2;
310  }
311  switch (e->code) {
312  case FX::KEY_Left:
313  moveX = -1;
314  moveFactor /= 10;
315  break;
316  case FX::KEY_Right:
317  moveX = 1;
318  moveFactor /= 10;
319  break;
320  case FX::KEY_Up:
321  moveY = -1;
322  moveFactor /= 10;
323  break;
324  case FX::KEY_Down:
325  moveY = 1;
326  moveFactor /= 10;
327  break;
328  case FX::KEY_Page_Up:
329  if (pageVertical) {
330  moveY = -1;
331  } else {
332  moveX = -1;
333  }
334  break;
335  case FX::KEY_Page_Down:
336  if (pageVertical) {
337  moveY = 1;
338  } else {
339  moveX = 1;
340  }
341  break;
342  case FX::KEY_plus:
343  case FX::KEY_KP_Add:
345  zoom(1.0 + zoomDiff);
347  return 1;
348  case FX::KEY_minus:
349  case FX::KEY_KP_Subtract:
350  zoomDiff = -zoomDiff;
352  zoom(1.0 + zoomDiff);
354  return 1;
355  case FX::KEY_Home:
356  case FX::KEY_KP_Home:
358  myCallback.update();
359  return 1;
360  case FX::KEY_v:
361  // from an architecture standpoint this isn't the best place to put
362  // this. But its simple
363  if (ctrl) {
365  return 1;
366  }
367  default:
368  return 0;
369  }
370  myViewPort.moveby(moveX * moveFactor * myViewPort.getWidth(),
371  -moveY * moveFactor * myViewPort.getHeight());
372  myCallback.update();
373  return 1;
374 }
375 
376 
377 /****************************************************************************/
SUMOReal getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:172
GUICompleteSchemeStorage gSchemeStorage
void growWidth(SUMOReal by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:247
SUMOReal getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:166
bool myMoveOnClick
Information whether the user has moved the cursor while pressing a mouse button.
Position getCenter() const
Returns the center of the boundary.
Definition: Boundary.cpp:124
virtual void recenterView()
recenters the view
void onRightBtnPress(void *data)
called when user press right button
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
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
Position myZoomBase
the network location on which to zoom using right click+drag
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
SUMOReal p2m(SUMOReal pixel) const
pixels-to-meters conversion method
virtual SUMOReal getRotation() const
Returns the rotation of the canvas stored in this changer.
void rotate(int diff)
Performs the rotation of the view.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal myOrigWidth
the original viewport dimensions in m which serve as the reference point for 100% zoom ...
void onLeftBtnPress(void *data)
mouse functions
virtual SUMOReal getXPos() const
Returns the x-offset of the field to show stored in this changer.
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
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
void onMouseMove(void *data)
called when user moves mouse
virtual SUMOReal getZPos() const
Returns the camera height corresponding to the current zoom factor.
virtual SUMOReal zoom2ZPos(SUMOReal zoom) const
Returns the camera height at which the given zoom level is reached.
virtual SUMOReal zPos2Zoom(SUMOReal zPos) const
Returns the zoom level that is achieved at a given camera height.
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:238
Boundary myViewPort
the intended viewport
void add(SUMOReal x, SUMOReal y, SUMOReal z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:90
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void setWindowCursorPosition(FXint x, FXint y)
Returns the information whether rotation is allowd.
virtual SUMOReal getYPos() const
Returns the y-offset of the field to show stored in this changer.
#define SUMOReal
Definition: config.h:208
void updateToolTip()
A method that updates the tooltip.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
void growHeight(SUMOReal by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:254
FXint myMouseXPosition
the current mouse position
void showViewschemeEditor()
show viewsscheme editor
Position getPositionInformation() const
Returns the cursor&#39;s x/y position within the network.
virtual SUMOReal getZoom() const
Returns the zoom factor computed stored in this changer.
void moveby(SUMOReal x, SUMOReal y, SUMOReal z=0)
Moves the boundary by the given amount.
Definition: Boundary.cpp:287