SUMO - Simulation of Urban MObility

GUIDanielPerspectiveChanger.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A class that allows to steer the visual output in dependence to
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <utils/geom/Boundary.h>
00031 #include <utils/geom/Position.h>
00032 #include <utils/gui/settings/GUICompleteSchemeStorage.h>
00033 #include "GUIPerspectiveChanger.h"
00034 #include "GUIDanielPerspectiveChanger.h"
00035 
00036 #ifdef CHECK_MEMORY_LEAKS
00037 #include <foreign/nvwa/debug_new.h>
00038 #endif // CHECK_MEMORY_LEAKS
00039 
00040 
00041 // ===========================================================================
00042 // method definitions
00043 // ===========================================================================
00044 GUIDanielPerspectiveChanger::GUIDanielPerspectiveChanger(
00045     GUISUMOAbstractView& callBack, const Boundary& viewPort) :
00046     GUIPerspectiveChanger(callBack, viewPort),
00047     myOrigWidth(viewPort.getWidth()),
00048     myOrigHeight(viewPort.getHeight()),
00049     myRotation(0),
00050     myMouseButtonState(MOUSEBTN_NONE),
00051     myMoveOnClick(false),
00052     myDragDelay(0) {}
00053 
00054 
00055 GUIDanielPerspectiveChanger::~GUIDanielPerspectiveChanger() {}
00056 
00057 
00058 void
00059 GUIDanielPerspectiveChanger::move(int xdiff, int ydiff) {
00060     myViewPort.moveby(myCallback.p2m(xdiff), -myCallback.p2m(ydiff));
00061     myCallback.update();
00062 }
00063 
00064 
00065 void
00066 GUIDanielPerspectiveChanger::zoom(SUMOReal factor) {
00067     if (myCallback.getApp()->reg().readIntEntry("gui","zoomAtCenter", 1)) {
00068         myZoomBase = myViewPort.getCenter();
00069     }
00070     if (factor > 0) {
00071         myViewPort = Boundary(
00072                          myZoomBase.x() - (myZoomBase.x() - myViewPort.xmin()) / factor,
00073                          myZoomBase.y() - (myZoomBase.y() - myViewPort.ymin()) / factor,
00074                          myZoomBase.x() - (myZoomBase.x() - myViewPort.xmax()) / factor,
00075                          myZoomBase.y() - (myZoomBase.y() - myViewPort.ymax()) / factor);
00076         myCallback.update();
00077     }
00078 }
00079 
00080 
00081 void
00082 GUIDanielPerspectiveChanger::rotate(int diff) {
00083     if (false) {//myCallback.allowRotation()) {
00084         myRotation += (SUMOReal) diff / (SUMOReal) 10.0;
00085         myCallback.update();
00086     }
00087 }
00088 
00089 
00090 SUMOReal
00091 GUIDanielPerspectiveChanger::getRotation() const {
00092     return myRotation;
00093 }
00094 
00095 
00096 SUMOReal
00097 GUIDanielPerspectiveChanger::getXPos() const {
00098     return myViewPort.getCenter().x();
00099 }
00100 
00101 
00102 SUMOReal
00103 GUIDanielPerspectiveChanger::getYPos() const {
00104     return myViewPort.getCenter().y();
00105 }
00106 
00107 
00108 SUMOReal
00109 GUIDanielPerspectiveChanger::getZoom() const {
00110     return myOrigWidth / myViewPort.getWidth() * 100;
00111 }
00112 
00113 
00114 void
00115 GUIDanielPerspectiveChanger::centerTo(const Position& pos, SUMOReal radius,
00116                                       bool applyZoom) {
00117     if (applyZoom) {
00118         myViewPort = Boundary();
00119         myViewPort.add(pos);
00120         myViewPort.grow(radius);
00121     } else {
00122         myViewPort.moveby(pos.x() - getXPos(), pos.y() - getYPos());
00123     }
00124 }
00125 
00126 
00127 void
00128 GUIDanielPerspectiveChanger::onLeftBtnPress(void* data) {
00129     myMouseButtonState |= MOUSEBTN_LEFT;
00130     FXEvent* e = (FXEvent*) data;
00131     myMouseXPosition = e->win_x;
00132     myMouseYPosition = e->win_y;
00133     myMoveOnClick = false;
00134     myMouseDownTime = FXThread::time();
00135 }
00136 
00137 
00138 bool
00139 GUIDanielPerspectiveChanger::onLeftBtnRelease(void* data) {
00140     myMouseButtonState &= !MOUSEBTN_LEFT;
00141     FXEvent* e = (FXEvent*) data;
00142     myMouseXPosition = e->win_x;
00143     myMouseYPosition = e->win_y;
00144     return myMoveOnClick;
00145 }
00146 
00147 
00148 void
00149 GUIDanielPerspectiveChanger::onRightBtnPress(void* data) {
00150     myMouseButtonState |= MOUSEBTN_RIGHT;
00151     FXEvent* e = (FXEvent*) data;
00152     myMouseXPosition = e->win_x;
00153     myMouseYPosition = e->win_y;
00154     myMoveOnClick = false;
00155     myMouseDownTime = FXThread::time();
00156     myZoomBase = myCallback.getPositionInformation();
00157 }
00158 
00159 
00160 bool
00161 GUIDanielPerspectiveChanger::onRightBtnRelease(void* data) {
00162     myMouseButtonState &= !MOUSEBTN_RIGHT;
00163     if (data != 0) {
00164         FXEvent* e = (FXEvent*) data;
00165         myMouseXPosition = e->win_x;
00166         myMouseYPosition = e->win_y;
00167     }
00168     return myMoveOnClick;
00169 }
00170 
00171 
00172 void
00173 GUIDanielPerspectiveChanger::onMouseWheel(void* data) {
00174     FXEvent* e = (FXEvent*) data;
00175     SUMOReal diff = 0.1;
00176     if (e->state&CONTROLMASK) {
00177         diff /= 2;
00178     } else if (e->state&SHIFTMASK) {
00179         diff *= 2;
00180     }
00181     if (e->code < 0) {
00182         diff = -diff;
00183     }
00184     myZoomBase = myCallback.getPositionInformation();
00185     zoom(1.0 + diff);
00186     myCallback.updateToolTip();
00187 }
00188 
00189 
00190 void
00191 GUIDanielPerspectiveChanger::onMouseMove(void* data) {
00192     FXEvent* e = (FXEvent*) data;
00193     myCallback.setWindowCursorPosition(e->win_x, e->win_y);
00194     const int xdiff = myMouseXPosition - e->win_x;
00195     const int ydiff = myMouseYPosition - e->win_y;
00196     const bool moved = xdiff!=0 || ydiff!=0;
00197     const bool pastDelay = FXThread::time() > (myMouseDownTime + myDragDelay);
00198     switch (myMouseButtonState) {
00199     case MOUSEBTN_LEFT:
00200         if (pastDelay) {
00201             move(xdiff, ydiff);
00202             if (moved) {
00203                 myMoveOnClick = true;
00204             }
00205         }
00206         break;
00207     case MOUSEBTN_RIGHT:
00208         if (pastDelay) {
00209             zoom(1 + 10.0 * ydiff / myCallback.getWidth());
00210             rotate(xdiff);
00211             if (moved) {
00212                 myMoveOnClick = true;
00213             }
00214         }
00215         break;
00216     default:
00217         if (moved) {
00218             myCallback.updateToolTip();
00219         }
00220         break;
00221     }
00222     myMouseXPosition = e->win_x;
00223     myMouseYPosition = e->win_y;
00224 }
00225 
00226 
00227 void
00228 GUIDanielPerspectiveChanger::setViewport(SUMOReal zoom,
00229         SUMOReal xPos, SUMOReal yPos) {
00230     const SUMOReal zoomFactor = zoom / 50; // /100 to normalize, *2 because growth is added on both sides
00231     myViewPort = Boundary();
00232     myViewPort.add(Position(xPos, yPos));
00233     myViewPort.growHeight(myOrigHeight / zoomFactor);
00234     myViewPort.growWidth(myOrigWidth / zoomFactor);
00235     myCallback.update();
00236 }
00237 
00238 
00239 void
00240 GUIDanielPerspectiveChanger::changeCanvassLeft(int change) {
00241     myViewPort = Boundary(
00242                      myViewPort.xmin() - myCallback.p2m(change),
00243                      myViewPort.ymin(),
00244                      myViewPort.xmax(),
00245                      myViewPort.ymax());
00246 }
00247 
00248 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines