Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GUIDanielPerspectiveChanger.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// A class that allows to steer the visual output in dependence to
21/****************************************************************************/
22#include <config.h>
23
24#include <fxkeys.h>
25#include <utils/geom/Boundary.h>
26#include <utils/geom/Position.h>
29#include "GUIMainWindow.h"
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
38 GUISUMOAbstractView& callBack, const Boundary& viewPort) :
39 GUIPerspectiveChanger(callBack, viewPort),
40 myOrigWidth(viewPort.getWidth()),
41 myOrigHeight(viewPort.getHeight()),
42 myRotation(0),
43 myMouseButtonState(MOUSEBTN_NONE),
44 myMoveOnClick(false),
45 myZoomBase(viewPort.getCenter()),
46 myDragDelay(0) {
47}
48
49
51
52
53void
54GUIDanielPerspectiveChanger::move(int xdiff, int ydiff) {
56 myCallback.update();
57}
58
59
60void
62 if (myCallback.getApp()->reg().readIntEntry("gui", "zoomAtCenter", 0)) {
64 }
65 if (factor > 0) {
67 myZoomBase.x() - (myZoomBase.x() - myViewPort.xmin()) / factor,
68 myZoomBase.y() - (myZoomBase.y() - myViewPort.ymin()) / factor,
69 myZoomBase.x() - (myZoomBase.x() - myViewPort.xmax()) / factor,
70 myZoomBase.y() - (myZoomBase.y() - myViewPort.ymax()) / factor);
71 myCallback.update();
72 }
73}
74
75
76void
78 /*
79 if (myCallback.allowRotation()) {
80 myRotation += (double) diff / (double) 10.0;
81 myCallback.update();
82 }
83 */
84}
85
86
87double
91
92
93double
97
98
99double
103
104
105double
109
110
111double
115
116
117double
119 return myOrigWidth / (zoom / 100);
120}
121
122
123double
125 return (myOrigWidth / zPos) * 100;
126}
127
128
129void
131 bool applyZoom) {
132 if (applyZoom) {
134 myViewPort.add(pos);
135 myViewPort.grow(radius);
136 } else {
137 myViewPort.moveby(pos.x() - getXPos(), pos.y() - getYPos());
138 }
139}
140
141
142void
145 FXEvent* e = (FXEvent*) data;
146 myMouseXPosition = e->win_x;
147 myMouseYPosition = e->win_y;
148 myMoveOnClick = false;
149 myMouseDownTime = FXThread::time();
150}
151
152
153bool
155 myMouseButtonState &= ~MOUSEBTN_LEFT;
156 FXEvent* e = (FXEvent*) data;
157 myMouseXPosition = e->win_x;
158 myMouseYPosition = e->win_y;
159 return myMoveOnClick;
160}
161
162
163void
166 FXEvent* e = (FXEvent*) data;
167 myMouseXPosition = e->win_x;
168 myMouseYPosition = e->win_y;
169 myMoveOnClick = false;
170 myMouseDownTime = FXThread::time();
172}
173
174
175bool
177 myMouseButtonState &= ~MOUSEBTN_MIDDLE;
178 FXEvent* e = (FXEvent*) data;
179 myMouseXPosition = e->win_x;
180 myMouseYPosition = e->win_y;
181 return myMoveOnClick;
182}
183
184
185void
188 FXEvent* e = (FXEvent*) data;
189 myMouseXPosition = e->win_x;
190 myMouseYPosition = e->win_y;
191 myMoveOnClick = false;
192 myMouseDownTime = FXThread::time();
194}
195
196
197bool
199 myMouseButtonState &= ~MOUSEBTN_RIGHT;
200 if (data != nullptr) {
201 FXEvent* e = (FXEvent*) data;
202 myMouseXPosition = e->win_x;
203 myMouseYPosition = e->win_y;
204 }
205 return myMoveOnClick;
206}
207
208
209void
211 FXEvent* e = (FXEvent*) data;
212 // catch empty ghost events after scroll (seem to occur only on Ubuntu)
213 if (e->code == 0) {
214 return;
215 }
216 // zoom scale relative delta and its inverse; is optimized (all literals)
217 const double zScale_rDelta_norm = 0.1;
218 const double zScale_rDelta_inv = -zScale_rDelta_norm / (1. + zScale_rDelta_norm);
219 double zScale_rDelta = zScale_rDelta_norm ;
220 if (e->code < 0) {
221 // for inverse zooming direction
222 zScale_rDelta = zScale_rDelta_inv;
223 }
224 // keyboard modifier: slow, fast mouse-zoom
225 if ((e->state & CONTROLMASK) != 0) {
226 zScale_rDelta /= 4;
227 } else if ((e->state & SHIFTMASK) != 0) {
228 zScale_rDelta *= 4;
229 }
231 zoom(1.0 + zScale_rDelta);
233}
234
235
236void
238 FXEvent* e = (FXEvent*) data;
239 myCallback.setWindowCursorPosition(e->win_x, e->win_y);
240 const int xdiff = myMouseXPosition - e->win_x;
241 const int ydiff = myMouseYPosition - e->win_y;
242 const bool moved = xdiff != 0 || ydiff != 0;
243 // no drag events in gaming mode
244 const bool pastDelay = !GUIMainWindow::getInstance()->isGaming() && FXThread::time() > (myMouseDownTime + myDragDelay);
245 switch (myMouseButtonState) {
246 case MOUSEBTN_LEFT:
247 case MOUSEBTN_MIDDLE:
248 if (pastDelay) {
249 if (myRotation != 0) {
250 Position diffRot = Position(xdiff, ydiff).rotateAround2D(
251 DEG2RAD(myRotation), Position(0, 0));
252 move((int)diffRot.x(), (int)diffRot.y());
253 } else {
254 move(xdiff, ydiff);
255 }
256 if (moved) {
257 myMoveOnClick = true;
258 }
259 }
260 break;
261 case MOUSEBTN_RIGHT:
262 if (pastDelay) {
263 zoom(1 + 10.0 * ydiff / myCallback.getWidth());
264 rotate(xdiff);
265 if (moved) {
266 myMoveOnClick = true;
267 }
268 }
269 break;
270 default:
271 if (moved) {
273 }
274 break;
275 }
276 myMouseXPosition = e->win_x;
277 myMouseYPosition = e->win_y;
278}
279
280
281void
283 double xPos, double yPos) {
284 const double zoomFactor = zoom / 50; // /100 to normalize, *2 because growth is added on both sides
286 myViewPort.add(Position(xPos, yPos));
287 myViewPort.growHeight(myOrigHeight / zoomFactor);
288 myViewPort.growWidth(myOrigWidth / zoomFactor);
289 myCallback.update();
290}
291
292
293void
294GUIDanielPerspectiveChanger::setViewportFrom(double xPos, double yPos, double zPos) {
295 setViewport(zPos2Zoom(zPos), xPos, yPos);
296}
297
298
299void
301 myRotation = rotation;
302}
303
304void
312
313
314long
316 // ignore key events in gaming mode
317 if (GUIMainWindow::getInstance()->isGaming()) {
318 return 0;
319 }
320 FXEvent* e = (FXEvent*) data;
321 double zoomDiff = 0.1;
322 double moveX = 0;
323 double moveY = 0;
324 double moveFactor = 1;
325 if (e->state & CONTROLMASK) {
326 zoomDiff /= 2;
327 moveFactor /= 10;
328 } else if (e->state & SHIFTMASK) {
329 zoomDiff *= 2;
330 } else if (e->state & ALTMASK) {
331 moveFactor *= 10;
332 }
333 switch (e->code) {
334 case FX::KEY_Left:
335 moveX = -1;
336 moveFactor /= 10;
337 break;
338 case FX::KEY_Right:
339 moveX = 1;
340 moveFactor /= 10;
341 break;
342 case FX::KEY_Up:
343 moveY = -1;
344 moveFactor /= 10;
345 break;
346 case FX::KEY_Down:
347 moveY = 1;
348 moveFactor /= 10;
349 break;
350 case FX::KEY_plus:
351 case FX::KEY_KP_Add:
353 zoom(1.0 + zoomDiff);
355 return 1;
356 case FX::KEY_minus:
357 case FX::KEY_KP_Subtract:
358 zoomDiff = -zoomDiff;
360 zoom(1.0 + zoomDiff);
362 return 1;
363 case FX::KEY_Home:
364 case FX::KEY_KP_Home:
366 myCallback.update();
367 return 1;
368 default:
369 return 0;
370 }
371 myViewPort.moveby(moveX * moveFactor * myViewPort.getWidth(),
372 -moveY * moveFactor * myViewPort.getHeight());
373 myCallback.update();
374 return 1;
375}
376
377
378/****************************************************************************/
#define DEG2RAD(x)
Definition GeomHelper.h:35
A class that stores a 2D geometrical boundary.
Definition Boundary.h:39
Position getCenter() const
Returns the center of the boundary.
Definition Boundary.cpp:109
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition Boundary.cpp:75
void moveby(double x, double y, double z=0)
Moves the boundary by the given amount.
Definition Boundary.cpp:433
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition Boundary.cpp:366
double ymin() const
Returns minimum y-coordinate.
Definition Boundary.cpp:127
double xmin() const
Returns minimum x-coordinate.
Definition Boundary.cpp:115
Boundary & grow(double by)
extends the boundary by the given amount
Definition Boundary.cpp:340
double getHeight() const
Returns the height of the boundary (y-axis)
Definition Boundary.cpp:157
double getWidth() const
Returns the width of the boudary (x-axis)
Definition Boundary.cpp:151
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition Boundary.cpp:359
double ymax() const
Returns maximum y-coordinate.
Definition Boundary.cpp:133
double xmax() const
Returns maximum x-coordinate.
Definition Boundary.cpp:121
bool myMoveOnClick
Information whether the user has moved the cursor while pressing a mouse button.
virtual double getRotation() const
Returns the rotation of the canvas stored in this changer.
void onRightBtnPress(void *data)
called when user press right button
int myMouseButtonState
the current mouse state
void onMiddleBtnPress(void *data)
called when user press middle button
virtual double getYPos() const
Returns the y-offset of the field to show stored in this changer.
virtual double getZoom() const
Returns the zoom factor computed stored in this changer.
void onMouseWheel(void *data)
called when user changes mouse wheel
virtual double zoom2ZPos(double zoom) const
Returns the camera height at which the given zoom level is reached.
void onMouseMove(void *data)
called when user moves mouse
double myOrigWidth
the original viewport dimensions in m which serve as the reference point for 100% zoom
virtual double getZPos() const
Returns the camera height corresponding to the current zoom factor.
virtual double getXPos() const
Returns the x-offset of the field to show stored in this changer.
bool onMiddleBtnRelease(void *data)
called when user releases middle button
void centerTo(const Position &pos, double radius, bool applyZoom=true)
Centers the view to the given position, setting it to a size that covers the radius.
void setRotation(double rotation)
Sets the rotation.
Position myZoomBase
the network location on which to zoom using right click+drag
double myRotation
the current rotation
void rotate(int diff)
Performs the rotation of the view.
bool onLeftBtnRelease(void *data)
called when user releases left button
bool onRightBtnRelease(void *data)
called when user releases right button
void onLeftBtnPress(void *data)
mouse functions
GUIDanielPerspectiveChanger(GUISUMOAbstractView &callBack, const Boundary &viewPort)
void zoom(double factor)
Performs the zooming of the view.
void setViewport(double zoom, double xPos, double yPos)
Sets the viewport.
long onKeyPress(void *data)
called when user press a key
virtual double zPos2Zoom(double zPos) const
Returns the zoom level that is achieved at a given camera height.
void setViewportFrom(double xPos, double yPos, double zPos)
Alternative method for setting the viewport.
bool isGaming() const
return whether the gui is in gaming mode
static GUIMainWindow * getInstance()
get instance
GUISUMOAbstractView & myCallback
The parent window (canvas to scale)
Boundary myViewPort
the intended viewport
FXint myMouseXPosition
the current mouse position
void updateToolTip()
A method that updates the tooltip.
virtual void recenterView()
recenters the view
double p2m(double pixel) const
pixels-to-meters conversion method
virtual Position getPositionInformation() const
Returns the cursor's x/y position within the network.
void setWindowCursorPosition(FXint x, FXint y)
Returns the gl-id of the object under the given coordinates.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double x() const
Returns the x-position.
Definition Position.h:52
Position rotateAround2D(double rad, const Position &origin)
rotate this position by rad around origin and return the result
Definition Position.cpp:43
double y() const
Returns the y-position.
Definition Position.h:57