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