ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Events.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Logging.h>
11 #include <json/json.h>
12 
13 #include <cstdint>
14 #include <sstream>
15 #include <string>
16 
17 namespace cloudViewer {
18 namespace visualization {
19 namespace gui {
20 
21 std::string MouseEvent::ToString() const {
22  std::stringstream ss;
23  ss << "MouseEvent{";
24  ss << "type: ";
25  if (type == Type::MOVE) {
26  ss << "MOVE";
27  } else if (type == Type::BUTTON_DOWN) {
28  ss << "BUTTON_DOWN";
29  } else if (type == Type::DRAG) {
30  ss << "DRAG";
31  } else if (type == Type::BUTTON_UP) {
32  ss << "BUTTON_UP";
33  } else if (type == Type::WHEEL) {
34  ss << "WHEEL";
35  } else {
36  ss << "ERROR";
37  }
38  ss << ", x: " << x;
39  ss << ", y: " << y;
40  ss << ", modifiers: " << modifiers;
41  if (type == Type::MOVE || type == Type::DRAG) {
42  ss << ", move.buttons : " << move.buttons;
43  } else if (type == Type::BUTTON_DOWN || type == Type::BUTTON_UP) {
44  ss << ", button.button: ";
45  if (button.button == MouseButton::NONE) {
46  ss << "NONE";
47  } else if (button.button == MouseButton::LEFT) {
48  ss << "LEFT";
49  } else if (button.button == MouseButton::MIDDLE) {
50  ss << "MIDDLE";
51  } else if (button.button == MouseButton::RIGHT) {
52  ss << "RIGHT";
53  } else if (button.button == MouseButton::BUTTON4) {
54  ss << "BUTTON4";
55  } else if (button.button == MouseButton::BUTTON5) {
56  ss << "BUTTON5";
57  } else {
58  ss << "ERROR";
59  }
60  ss << ", button.count: " << button.count;
61  } else if (type == Type::WHEEL) {
62  ss << ", wheel.dx: " << wheel.dx;
63  ss << ", wheel.dy: " << wheel.dy;
64  ss << ", wheel.isTrackpad: " << wheel.isTrackpad;
65  }
66  ss << "}";
67  return ss.str();
68 }
69 
70 bool MouseEvent::FromJson(const Json::Value& value) {
71  if (!value.isObject()) {
72  utility::LogWarning("MouseEvent::FromJson failed: Not an object.");
73  return false;
74  }
75 
76  std::string class_name = value.get("class_name", "").asString();
77  if (class_name != "MouseEvent") {
79  "MouseEvent::FromJson failed: Incorrect class name {}.",
80  class_name);
81  return false;
82  }
83 
84  std::string type_name = value.get("type", "").asString();
85  if (type_name == "MOVE") {
86  this->type = MouseEvent::Type::MOVE;
87  } else if (type_name == "BUTTON_DOWN") {
88  this->type = MouseEvent::Type::BUTTON_DOWN;
89  } else if (type_name == "BUTTON_UP") {
90  this->type = MouseEvent::Type::BUTTON_UP;
91  } else if (type_name == "DRAG") {
92  this->type = MouseEvent::Type::DRAG;
93  } else if (type_name == "WHEEL") {
94  this->type = MouseEvent::Type::WHEEL;
95  } else {
97  "MouseEvent::FromJson failed: Incorrect type name {}.",
98  type_name);
99  return false;
100  }
101  this->x = value.get("x", 0).asInt();
102  this->y = value.get("y", 0).asInt();
103  this->modifiers = value.get("modifiers", 0).asInt();
104 
105  if (this->type == Type::MOVE || this->type == Type::DRAG) {
106  this->move.buttons = value["move"].get("buttons", 0).asInt();
107  } else if (this->type == Type::BUTTON_DOWN ||
108  this->type == Type::BUTTON_UP) {
109  std::string button_name = value["button"].get("button", "").asString();
110  if (button_name == "NONE") {
111  this->button.button = MouseButton::NONE;
112  } else if (button_name == "LEFT") {
113  this->button.button = MouseButton::LEFT;
114  } else if (button_name == "MIDDLE") {
115  this->button.button = MouseButton::MIDDLE;
116  } else if (button_name == "RIGHT") {
117  this->button.button = MouseButton::RIGHT;
118  } else if (button_name == "BUTTON4") {
119  this->button.button = MouseButton::BUTTON4;
120  } else if (button_name == "BUTTON5") {
121  this->button.button = MouseButton::BUTTON5;
122  } else {
124  "MouseEvent::FromJson failed: Incorrect button name {}.",
125  button_name);
126  return false;
127  }
128  this->button.count = value["button"].get("count", 1).asInt();
129  } else if (this->type == Type::WHEEL) {
130  this->wheel.dx = value["wheel"].get("dx", 0.f).asFloat();
131  this->wheel.dy = value["wheel"].get("dy", 0.f).asFloat();
132  this->wheel.isTrackpad =
133  value["wheel"].get("isTrackpad", false).asBool();
134  }
135 
136  return true;
137 }
138 
140  const int x,
141  const int y,
142  const int modifiers,
143  const int buttons) {
144  MouseEvent me;
145  me.type = type;
146  me.x = x;
147  me.y = y;
148  me.modifiers = modifiers;
149  me.move.buttons = buttons;
150  return me;
151 }
152 
154  const int x,
155  const int y,
156  const int modifiers,
157  const MouseButton button,
158  const int count) {
159  MouseEvent me;
160  me.type = type;
161  me.x = x;
162  me.y = y;
163  me.modifiers = modifiers;
164  me.button.button = button;
165  me.button.count = count;
166  return me;
167 }
168 
170  const int x,
171  const int y,
172  const int modifiers,
173  const float dx,
174  const float dy,
175  const bool isTrackpad) {
176  MouseEvent me;
177  me.type = type;
178  me.x = x;
179  me.y = y;
180  me.modifiers = modifiers;
181  me.wheel.dx = dx;
182  me.wheel.dy = dy;
183  me.wheel.isTrackpad = isTrackpad;
184  return me;
185 }
186 
187 } // namespace gui
188 } // namespace visualization
189 } // namespace cloudViewer
int count
char type
#define LogWarning(...)
Definition: Logging.h:72
Generic file read and write utility for python interface.
struct cloudViewer::visualization::gui::MouseEvent::@17::@21 wheel
bool FromJson(const Json::Value &value)
Definition: Events.cpp:70
struct cloudViewer::visualization::gui::MouseEvent::@17::@19 move
static MouseEvent MakeWheelEvent(const Type type, const int x, const int y, const int modifiers, const float dx, const float dy, const bool isTrackpad)
Definition: Events.cpp:169
static MouseEvent MakeButtonEvent(const Type type, const int x, const int y, const int modifiers, const MouseButton button, const int count)
Definition: Events.cpp:153
static MouseEvent MakeMoveEvent(const Type type, const int x, const int y, const int modifiers, const int buttons)
Definition: Events.cpp:139