-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.h
264 lines (195 loc) · 9.02 KB
/
Client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#pragma once
#include "Category.h"
#include "InteractiveMarker.h"
#include "Plot.h"
namespace mc_rtc::imgui
{
namespace form
{
struct ObjectWidget;
struct OneOfWidget;
} // namespace form
struct Client : public mc_control::ControllerClient
{
/** Default constructor
*
* Contrary to its parent class default constructor, this constructs a connected client to a reasonable default
* address
*/
Client();
/** Creates a new interactive marker */
virtual InteractiveMarkerPtr make_marker(const sva::PTransformd & pose = sva::PTransformd::Identity(),
ControlAxis mask = ControlAxis::NONE) = 0;
/** Update the client data from the latest server message */
void update();
/** Draw ImGui elements */
void draw2D(ImVec2 windowSize);
/** Draw 3D elements */
void draw3D();
/** Remove all elements */
void clear();
inline const mc_rtc::Configuration & data() const noexcept { return data_; }
inline void set_bold_font(ImFont * font) { bold_font_ = font; }
void enable_bold_font();
void disable_bold_font();
protected:
std::vector<char> buffer_ = std::vector<char>(65535);
std::chrono::system_clock::time_point t_last_ = std::chrono::system_clock::now();
/** No message for unsupported types */
void default_impl(const std::string &, const ElementId &) final {}
void started() override;
void category(const std::vector<std::string> &, const std::string &) override;
void label(const ElementId & id, const std::string & txt) override;
void array_label(const ElementId & id,
const std::vector<std::string> & labels,
const Eigen::VectorXd & data) override;
void button(const ElementId & id) override;
void checkbox(const ElementId & id, bool state) override;
void string_input(const ElementId & id, const std::string & data) override;
void integer_input(const ElementId & id, int data) override;
void number_input(const ElementId & id, double data) override;
void number_slider(const ElementId & id, double data, double min, double max) override;
void array_input(const ElementId & id,
const std::vector<std::string> & labels,
const Eigen::VectorXd & data) override;
void combo_input(const ElementId & id, const std::vector<std::string> & values, const std::string & data) override;
void data_combo_input(const ElementId & id, const std::vector<std::string> & ref, const std::string & data) override;
void table_start(const ElementId & id, const std::vector<std::string> & header) override;
void table_row(const ElementId & id, const std::vector<std::string> & data) override;
void table_end(const ElementId & id) override;
void schema(const ElementId & id, const std::string & schema) override;
void form(const ElementId & id) override;
void form_checkbox(const ElementId & formId,
const std::string & name,
bool required,
bool default_,
bool user_default) override;
void form_integer_input(const ElementId & formId,
const std::string & name,
bool required,
int default_,
bool user_default) override;
void form_number_input(const ElementId & formId,
const std::string & name,
bool required,
double default_,
bool user_default) override;
void form_string_input(const ElementId & formId,
const std::string & name,
bool required,
const std::string & default_,
bool user_default) override;
void form_array_input(const ElementId & formId,
const std::string & name,
bool required,
const std::vector<std::string> & labels,
const Eigen::VectorXd & default_,
bool fixed_size,
bool user_default) override;
void form_point3d_input(const ElementId & formId,
const std::string & name,
bool requried,
const Eigen::Vector3d & default_,
bool user_default,
bool interactive) override;
void form_rotation_input(const ElementId & formId,
const std::string & name,
bool requried,
const sva::PTransformd & default_,
bool user_default,
bool interactive) override;
void form_transform_input(const ElementId & formId,
const std::string & name,
bool requried,
const sva::PTransformd & default_,
bool user_default,
bool interactive) override;
void form_combo_input(const ElementId & formId,
const std::string & name,
bool required,
const std::vector<std::string> & values,
bool send_index,
int user_default) override;
void form_data_combo_input(const ElementId & formId,
const std::string & name,
bool required,
const std::vector<std::string> & ref,
bool send_index) override;
void start_form_object_input(const std::string & name, bool required) override;
void end_form_object_input() override;
void start_form_generic_array_input(const std::string & name,
bool required,
std::optional<std::vector<mc_rtc::Configuration>> data) override;
void end_form_generic_array_input() override;
void start_form_one_of_input(const std::string & name,
bool required,
const std::optional<std::pair<size_t, mc_rtc::Configuration>> & data) override;
void end_form_one_of_input() override;
void start_plot(uint64_t id, const std::string & title) override;
void plot_setup_xaxis(uint64_t id, const std::string & legend, const mc_rtc::gui::plot::Range & range) override;
void plot_setup_yaxis_left(uint64_t id, const std::string & legend, const mc_rtc::gui::plot::Range & range) override;
void plot_setup_yaxis_right(uint64_t id, const std::string & legend, const mc_rtc::gui::plot::Range & range) override;
void plot_point(uint64_t id,
uint64_t did,
const std::string & legend,
double x,
double y,
mc_rtc::gui::Color color,
mc_rtc::gui::plot::Style style,
mc_rtc::gui::plot::Side side) override;
void plot_polygon(uint64_t id,
uint64_t did,
const std::string & legend,
const mc_rtc::gui::plot::PolygonDescription & polygon,
mc_rtc::gui::plot::Side side) override;
void plot_polygons(uint64_t id,
uint64_t did,
const std::string & legend,
const std::vector<mc_rtc::gui::plot::PolygonDescription> & polygons,
mc_rtc::gui::plot::Side side) override;
void end_plot(uint64_t id) override;
void stopped() override;
Category root_;
/** Returns a category (creates it if it does not exist */
Category & getCategory(const std::vector<std::string> & category);
/** Currently active form */
form::ObjectWidget * active_form_ = nullptr;
/** Throw if active_form is not set */
inline void require_active_form()
{
if(!active_form_) { mc_rtc::log::error_and_throw("No active form at this point"); }
}
/** Currently active plots */
std::unordered_map<uint64_t, std::shared_ptr<Plot>> active_plots_;
/** Currently inactive plots */
std::vector<std::shared_ptr<Plot>> inactive_plots_;
/** Bold font, default font if unset */
ImFont * bold_font_ = nullptr;
/** Get a widget with the right type and id */
template<typename T, typename... Args>
T & widget(const ElementId & id, Args &&... args)
{
auto & category = getCategory(id.category);
auto it =
std::find_if(category.widgets.begin(), category.widgets.end(), [&](auto & w) { return w->id.name == id.name; });
if(it == category.widgets.end())
{
auto & w = category.widgets.emplace_back(std::make_unique<T>(*this, id, std::forward<Args>(args)...));
w->seen = true;
return *dynamic_cast<T *>(w.get());
}
else
{
auto w_ptr = dynamic_cast<T *>(it->get());
if(w_ptr)
{
w_ptr->seen = true;
return *w_ptr;
}
/** Different type, remove and add the widget again */
category.widgets.erase(it);
return widget<T>(id, std::forward<Args>(args)...);
}
}
};
} // namespace mc_rtc::imgui