SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting GUI values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 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 #ifndef NO_TRACI
34 
35 #include <fx.h>
42 #include <guisim/GUINet.h>
43 #include <guisim/GUIVehicle.h>
44 #include "TraCIServerAPI_GUI.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 bool
56  tcpip::Storage& outputStorage) {
57  // variable & id
58  int variable = inputStorage.readUnsignedByte();
59  std::string id = inputStorage.readString();
60  // check variable
61  if (variable != ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
62  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
63  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable specified", outputStorage);
64  }
65  // begin response building
66  tcpip::Storage tempMsg;
67  // response-code, variableID, objectID
69  tempMsg.writeUnsignedByte(variable);
70  tempMsg.writeString(id);
71  // process request
72  if (variable == ID_LIST) {
73  std::vector<std::string> ids = getMainWindow()->getViewIDs();
75  tempMsg.writeStringList(ids);
76  } else {
78  if (v == 0) {
79  return server.writeErrorStatusCmd(CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
80  }
81  switch (variable) {
82  case VAR_VIEW_ZOOM:
84  tempMsg.writeDouble(v->getChanger().getZoom());
85  break;
86  case VAR_VIEW_OFFSET:
88  tempMsg.writeDouble(v->getChanger().getXPos());
89  tempMsg.writeDouble(v->getChanger().getYPos());
90  break;
91  case VAR_VIEW_SCHEMA: {
92  FXComboBox& c = v->getColoringSchemesCombo();
94  tempMsg.writeString((std::string)c.getItem(c.getCurrentItem()).text());
95  break;
96  }
97  case VAR_VIEW_BOUNDARY: {
100  tempMsg.writeDouble(b.xmin());
101  tempMsg.writeDouble(b.ymin());
102  tempMsg.writeDouble(b.xmax());
103  tempMsg.writeDouble(b.ymax());
104  break;
105  }
106  default:
107  break;
108  }
109  }
110  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, "", outputStorage);
111  server.writeResponseWithLength(outputStorage, tempMsg);
112  return true;
113 }
114 
115 
116 bool
118  tcpip::Storage& outputStorage) {
119  std::string warning = ""; // additional description for response
120  // variable
121  int variable = inputStorage.readUnsignedByte();
122  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
123  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
124  ) {
125  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable specified", outputStorage);
126  }
127  // id
128  std::string id = inputStorage.readString();
130  if (v == 0) {
131  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
132  }
133  // process
134  switch (variable) {
135  case VAR_VIEW_ZOOM: {
136  Position off, p;
137  double zoom = 1;
138  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
139  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
140  }
141  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), zoom);
142  v->setViewport(off, p);
143  }
144  break;
145  case VAR_VIEW_OFFSET: {
146  Position off, p;
147  if (!server.readTypeCheckingPosition2D(inputStorage, off)) {
148  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
149  }
150  off.set(off.x(), off.y(), v->getChanger().getZoom());
151  v->setViewport(off, p);
152  }
153  break;
154  case VAR_VIEW_SCHEMA: {
155  std::string schema;
156  if (!server.readTypeCheckingString(inputStorage, schema)) {
157  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
158  }
159  if (!v->setColorScheme(schema)) {
160  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
161  }
162  }
163  break;
164  case VAR_VIEW_BOUNDARY: {
165  Boundary b;
166  if (!server.readTypeCheckingBoundary(inputStorage, b)) {
167  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
168  }
169  v->centerTo(b);
170  break;
171  }
172  case VAR_SCREENSHOT: {
173  std::string filename;
174  if (!server.readTypeCheckingString(inputStorage, filename)) {
175  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Making a snapshot requires a file name.", outputStorage);
176  }
177  std::string error = v->makeSnapshot(filename);
178  if (error != "") {
179  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, error, outputStorage);
180  }
181  }
182  break;
183  case VAR_TRACK_VEHICLE: {
184  std::string id;
185  if (!server.readTypeCheckingString(inputStorage, id)) {
186  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
187  }
188  if (id == "") {
189  v->stopTrack();
190  } else {
192  if (veh == 0) {
193  return server.writeErrorStatusCmd(CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
194  }
195  if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIVehicle::VO_TRACKED)) {
196  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
197  static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIVehicle::VO_TRACKED);
198  }
199  }
200  }
201  default:
202  break;
203  }
204  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
205  return true;
206 }
207 
208 
211  FXWindow* w = FXApp::instance()->getRootWindow()->getFirst();
212  while (w != 0 && dynamic_cast<GUIMainWindow*>(w) == 0) {
213  w = w->getNext();
214  }
215  if (w == 0) {
216  // main window not found
217  return 0;
218  }
219  return dynamic_cast<GUIMainWindow*>(w);
220 }
221 
222 
224 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
225  GUIMainWindow* mw = static_cast<GUIMainWindow*>(getMainWindow());
226  if (mw == 0) {
227  return 0;
228  }
229  GUIGlChildWindow* c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
230  if (c == 0) {
231  return 0;
232  }
233  return c->getView();
234 }
235 
236 
237 #endif
238 
239 
240 /****************************************************************************/
241 
GUISUMOAbstractView * getView() const
virtual void startTrack(int)
virtual void setViewport(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
virtual void centerTo(GUIGlID id, bool applyZoom, SUMOReal zoomDist=20)
centers to the chosen artifact
#define POSITION_2D
virtual SUMOReal getZoom() const =0
Returns the zoom factor computed stored in this changer.
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
#define RTYPE_OK
track vehicle
Definition: GUIVehicle.h:240
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
static GUIMainWindow * getMainWindow()
Returns the main window.
virtual void writeUnsignedByte(int)
#define CMD_SET_GUI_VARIABLE
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
#define TYPE_STRING
virtual int readUnsignedByte()
FXMDIChild * getViewByID(const std::string &id) const
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
GUIPerspectiveChanger & getChanger() const
#define VAR_SCREENSHOT
#define VAR_VIEW_BOUNDARY
#define VAR_TRACK_VEHICLE
Boundary getVisibleBoundary() const
Representation of a vehicle.
Definition: SUMOVehicle.h:64
virtual SUMOReal getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:269
#define TYPE_BOUNDINGBOX
#define VAR_VIEW_SCHEMA
virtual void writeStringList(const std::vector< std::string > &s)
FXComboBox & getColoringSchemesCombo()
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
virtual std::string readString()
#define CMD_GET_GUI_VARIABLE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
std::string makeSnapshot(const std::string &destFile)
Takes a snapshots and writes it into the given file.
#define RESPONSE_GET_GUI_VARIABLE
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
#define VAR_VIEW_ZOOM
virtual void writeString(const std::string &s)
#define TYPE_DOUBLE
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
virtual bool setColorScheme(const std::string &)
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
virtual SUMOReal getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
std::vector< std::string > getViewIDs() const
#define ID_LIST
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
#define VAR_VIEW_OFFSET
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:68