Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/gui/TraCIServerAPI_GUI.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file TraCIServerAPI_GUI.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @author Mirko Barthauer
19
/// @date 07.05.2009
20
///
21
// APIs for getting/setting GUI values via TraCI
22
/****************************************************************************/
23
#include <config.h>
24
25
#include <libsumo/GUI.h>
26
#include <libsumo/StorageHelper.h>
27
#include <libsumo/TraCIConstants.h>
28
#include "GUISUMOViewParent.h"
29
#include "TraCIServerAPI_GUI.h"
30
31
32
// ===========================================================================
33
// method definitions
34
// ===========================================================================
35
bool
36
TraCIServerAPI_GUI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
37
tcpip::Storage& outputStorage) {
38
const int variable = inputStorage.readUnsignedByte();
39
const std::string id = inputStorage.readString();
40
server.initWrapper(libsumo::RESPONSE_GET_GUI_VARIABLE, variable, id);
41
try {
42
if (!libsumo::GUI::handleVariable(id, variable, &server, &inputStorage)) {
43
switch (variable) {
44
case libsumo::VAR_SELECT: {
45
std::string objType;
46
if (!server.readTypeCheckingString(inputStorage, objType)) {
47
return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
48
}
49
StoHelp::writeTypedInt(server.getWrapperStorage(), libsumo::GUI::isSelected(id, objType) ? 1 : 0);
50
break;
51
}
52
default:
53
return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
54
}
55
}
56
} catch (libsumo::TraCIException& e) {
57
return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, e.what(), outputStorage);
58
}
59
server.writeStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
60
server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
61
return true;
62
}
63
64
65
bool
66
TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
67
tcpip::Storage& outputStorage) {
68
std::string warning = ""; // additional description for response
69
const int variable = inputStorage.readUnsignedByte();
70
if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
71
&& variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
72
&& variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
73
&& variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
74
&& variable != libsumo::ADD && variable != libsumo::REMOVE
75
) {
76
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
77
}
78
const std::string id = inputStorage.readString();
79
try {
80
switch (variable) {
81
case libsumo::VAR_VIEW_ZOOM: {
82
double zoom = 0.;
83
if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
84
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
85
}
86
libsumo::GUI::setZoom(id, zoom);
87
break;
88
}
89
case libsumo::VAR_VIEW_OFFSET: {
90
libsumo::TraCIPosition tp;
91
if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
92
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
93
}
94
libsumo::GUI::setOffset(id, tp.x, tp.y);
95
break;
96
}
97
case libsumo::VAR_SELECT: {
98
std::string objType;
99
if (!server.readTypeCheckingString(inputStorage, objType)) {
100
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
101
}
102
libsumo::GUI::toggleSelection(id, objType);
103
break;
104
}
105
case libsumo::VAR_VIEW_SCHEMA: {
106
std::string schema;
107
if (!server.readTypeCheckingString(inputStorage, schema)) {
108
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
109
}
110
libsumo::GUI::setSchema(id, schema);
111
break;
112
}
113
case libsumo::VAR_VIEW_BOUNDARY: {
114
PositionVector p;
115
if (!server.readTypeCheckingPolygon(inputStorage, p)) {
116
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
117
}
118
libsumo::GUI::setBoundary(id, p[0].x(), p[0].y(), p[1].x(), p[1].y());
119
break;
120
}
121
case libsumo::VAR_ANGLE: {
122
double rot;
123
if (!server.readTypeCheckingDouble(inputStorage, rot)) {
124
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The rotation must be given as a double.", outputStorage);
125
}
126
libsumo::GUI::setAngle(id, rot);
127
break;
128
}
129
case libsumo::VAR_SCREENSHOT: {
130
if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
131
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
132
}
133
int parameterCount = inputStorage.readInt();
134
if (parameterCount != 3) {
135
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
136
}
137
std::string filename;
138
if (!server.readTypeCheckingString(inputStorage, filename)) {
139
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
140
}
141
const int width = StoHelp::readTypedInt(inputStorage, "The second variable must be the width given as int.");
142
const int height = StoHelp::readTypedInt(inputStorage, "The third variable must be the height given as int.");
143
// take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
144
libsumo::GUI::screenshot(id, filename, width, height);
145
break;
146
}
147
case libsumo::VAR_TRACK_VEHICLE: {
148
std::string objID;
149
if (!server.readTypeCheckingString(inputStorage, objID)) {
150
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string ID.", outputStorage);
151
}
152
libsumo::GUI::trackVehicle(id, objID);
153
break;
154
}
155
case libsumo::ADD: {
156
if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
157
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires a compound object.", outputStorage);
158
}
159
int parameterCount = inputStorage.readInt();
160
if (parameterCount != 2) {
161
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires two values as parameter.", outputStorage);
162
}
163
std::string scheme;
164
if (!server.readTypeCheckingString(inputStorage, scheme)) {
165
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a scheme name.", outputStorage);
166
}
167
const int viewType = StoHelp::readTypedInt(inputStorage, "The second variable must be the view type given as int.");
168
libsumo::GUI::addView(id, scheme,
169
viewType == 1 ? GUISUMOViewParent::VIEW_3D_OSG : GUISUMOViewParent::VIEW_2D_OPENGL);
170
break;
171
}
172
case libsumo::REMOVE: {
173
libsumo::GUI::removeView(id);
174
break;
175
}
176
default:
177
break;
178
}
179
} catch (libsumo::TraCIException& e) {
180
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
181
}
182
server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
183
return true;
184
}
185
186
187
/****************************************************************************/
188
189