Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/gui/TraCIServerAPI_GUI.cpp
193674 views
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
/****************************************************************************/
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
StoHelp::writeTypedInt(server.getWrapperStorage(), libsumo::GUI::isSelected(id, StoHelp::readTypedString(inputStorage, "The type of the object must be given as a string.")) ? 1 : 0);
46
break;
47
}
48
default:
49
return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
50
}
51
}
52
} catch (libsumo::TraCIException& e) {
53
return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, e.what(), outputStorage);
54
}
55
server.writeStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
56
server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
57
return true;
58
}
59
60
61
bool
62
TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
63
tcpip::Storage& outputStorage) {
64
std::string warning = ""; // additional description for response
65
const int variable = inputStorage.readUnsignedByte();
66
if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
67
&& variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
68
&& variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
69
&& variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
70
&& variable != libsumo::ADD && variable != libsumo::REMOVE
71
) {
72
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
73
}
74
const std::string id = inputStorage.readString();
75
try {
76
switch (variable) {
77
case libsumo::VAR_VIEW_ZOOM: {
78
libsumo::GUI::setZoom(id, StoHelp::readTypedDouble(inputStorage, "The zoom must be given as a double."));
79
break;
80
}
81
case libsumo::VAR_VIEW_OFFSET: {
82
const libsumo::TraCIPosition tp = StoHelp::readTypedPosition2D(inputStorage, "The view port must be given as a position.");
83
libsumo::GUI::setOffset(id, tp.x, tp.y);
84
break;
85
}
86
case libsumo::VAR_SELECT: {
87
libsumo::GUI::toggleSelection(id, StoHelp::readTypedString(inputStorage, "The type of the object must be given as a string."));
88
break;
89
}
90
case libsumo::VAR_VIEW_SCHEMA: {
91
libsumo::GUI::setSchema(id, StoHelp::readTypedString(inputStorage, "The scheme must be specified by a string."));
92
break;
93
}
94
case libsumo::VAR_VIEW_BOUNDARY: {
95
const libsumo::TraCIPositionVector tp = StoHelp::readTypedPolygon(inputStorage, "The boundary must be specified by a bounding box.");
96
libsumo::GUI::setBoundary(id, tp.value[0].x, tp.value[0].y, tp.value[1].x, tp.value[1].y);
97
break;
98
}
99
case libsumo::VAR_ANGLE: {
100
libsumo::GUI::setAngle(id, StoHelp::readTypedDouble(inputStorage, "The rotation must be given as a double."));
101
break;
102
}
103
case libsumo::VAR_SCREENSHOT: {
104
StoHelp::readCompound(inputStorage, 3, "Screenshot requires three values as parameter.");
105
const std::string filename = StoHelp::readTypedString(inputStorage, "The first variable must be a file name.");
106
const int width = StoHelp::readTypedInt(inputStorage, "The second variable must be the width given as int.");
107
const int height = StoHelp::readTypedInt(inputStorage, "The third variable must be the height given as int.");
108
// take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
109
libsumo::GUI::screenshot(id, filename, width, height);
110
break;
111
}
112
case libsumo::VAR_TRACK_VEHICLE: {
113
libsumo::GUI::trackVehicle(id, StoHelp::readTypedString(inputStorage, "Tracking requires a string ID."));
114
break;
115
}
116
case libsumo::ADD: {
117
StoHelp::readCompound(inputStorage, 2, "Adding a view requires two values as parameter.");
118
const std::string scheme = StoHelp::readTypedString(inputStorage, "The first variable must be a scheme name.");
119
const int viewType = StoHelp::readTypedInt(inputStorage, "The second variable must be the view type given as int.");
120
libsumo::GUI::addView(id, scheme,
121
viewType == 1 ? GUISUMOViewParent::VIEW_3D_OSG : GUISUMOViewParent::VIEW_2D_OPENGL);
122
break;
123
}
124
case libsumo::REMOVE: {
125
libsumo::GUI::removeView(id);
126
break;
127
}
128
default:
129
break;
130
}
131
} catch (libsumo::TraCIException& e) {
132
return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
133
}
134
server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
135
return true;
136
}
137
138
139
/****************************************************************************/
140
141