Skip to content

Commit cbb2853

Browse files
committed
Use input struct for events and added was_dragged to on_mouse_released
1 parent 29bd10c commit cbb2853

File tree

14 files changed

+254
-175
lines changed

14 files changed

+254
-175
lines changed

examples/common/examples_common.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ void examples_setup_gui(gui::manager& manager) {
195195
// Usage: manager.get_input_dispatcher().get_source().
196196

197197
input::world_dispatcher& world_input_dispatcher = manager.get_world_input_dispatcher();
198-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_code) {
198+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
199199
// Process keyboard inputs for the game...
200-
switch (key_code) {
200+
switch (args.key) {
201201
case input::key::k_p: gui::out << manager.print_ui() << std::endl; break;
202202
case input::key::k_c: manager.get_root().toggle_caching(); break;
203203
case input::key::k_r: manager.reload_ui(); break;
@@ -213,8 +213,7 @@ void examples_setup_gui(gui::manager& manager) {
213213
}
214214
});
215215

216-
world_input_dispatcher.on_mouse_pressed.connect(
217-
[&](input::mouse_button button_code, const gui::vector2f& mouse_pos) {
218-
// Process mouse inputs for the game...
219-
});
216+
world_input_dispatcher.on_mouse_released.connect([&](const input::mouse_released_data& args) {
217+
// Process mouse inputs for the game...
218+
});
220219
}

examples/opengl-sdl-emscripten/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ int main(int argc, char* argv[]) {
174174
// Register a callback on Escape to terminate the program.
175175
// Doing it this way, we only react to keyboard input that is not captured by the GUI.
176176
input::world_dispatcher& world_input_dispatcher = manager->get_world_input_dispatcher();
177-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_id) {
178-
if (key_id == input::key::k_escape)
177+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
178+
if (args.key == input::key::k_escape)
179179
emscripten_cancel_main_loop();
180180
});
181181

examples/opengl-sdl/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ int main(int argc, char* argv[]) {
117117

118118
// Register a callback on Escape to terminate the program.
119119
// Doing it this way, we only react to keyboard input that is not captured by the GUI.
120-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_id) {
121-
if (key_id == input::key::k_escape)
120+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
121+
if (args.key == input::key::k_escape)
122122
running = false;
123123
});
124124

examples/opengl-sfml/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ int main(int argc, char* argv[]) {
7070

7171
// Register a callback on Escape to terminate the program.
7272
// Doing it this way, we only react to keyboard input that is not captured by the GUI.
73-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_id) {
74-
if (key_id == input::key::k_escape)
73+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
74+
if (args.key == input::key::k_escape)
7575
running = false;
7676
});
7777

examples/sdl-emscripten/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ int main(int argc, char* argv[]) {
149149
// Register a callback on Escape to terminate the program.
150150
// Doing it this way, we only react to keyboard input that is not captured by the GUI.
151151
input::world_dispatcher& world_input_dispatcher = manager->get_world_input_dispatcher();
152-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_id) {
153-
if (key_id == input::key::k_escape)
152+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
153+
if (args.key == input::key::k_escape)
154154
emscripten_cancel_main_loop();
155155
});
156156

examples/sdl/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ int main(int argc, char* argv[]) {
8888

8989
// Register a callback on Escape to terminate the program.
9090
// Doing it this way, we only react to keyboard input that is not captured by the GUI.
91-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_id) {
92-
if (key_id == input::key::k_escape)
91+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
92+
if (args.key == input::key::k_escape)
9393
running = false;
9494
});
9595

examples/sfml/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ int main(int argc, char* argv[]) {
5454

5555
// Register a callback on Escape to terminate the program.
5656
// Doing it this way, we only react to keyboard input that is not captured by the GUI.
57-
world_input_dispatcher.on_key_pressed.connect([&](input::key key_id) {
58-
if (key_id == input::key::k_escape)
57+
world_input_dispatcher.on_key_pressed.connect([&](const input::key_pressed_data& args) {
58+
if (args.key == input::key::k_escape)
5959
running = false;
6060
});
6161

include/lxgui/gui_frame.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ using script_list_view = script_signal::slot_list_view;
191191
* last call to `OnMouseMove` (or since the last position before the mouse
192192
* entered this frame), and the mouse X and Y position.
193193
* - `OnMouseUp`: Similar to `OnMouseDown`, but triggered when the mouse button
194-
* is released.
194+
* is released. This event provides one extra argument compared to `OnMouseDown`:
195+
* a boolean flag indicating whether the mouse was released after a drag
196+
* operation (true) or not (false).
195197
* - `OnMouseWheel`: Triggered when the mouse wheel is moved and this frame is
196198
* the topmost mouse-wheel-enabled frame under the mouse pointer. This event
197199
* provides three arguments to the registered callback. The first is a number

include/lxgui/gui_root.hpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "lxgui/gui_key_binder.hpp"
88
#include "lxgui/gui_registry.hpp"
99
#include "lxgui/gui_vector2.hpp"
10+
#include "lxgui/input_signals.hpp"
1011
#include "lxgui/lxgui.hpp"
1112
#include "lxgui/utils_observer.hpp"
1213
#include "lxgui/utils_signal.hpp"
@@ -295,16 +296,17 @@ class root :
295296
set_hovered_frame_(utils::observer_ptr<frame> obj, const vector2f& mouse_pos = vector2f::zero);
296297

297298
void on_window_resized_(const vector2ui& dimensions);
298-
void on_mouse_moved_(const vector2f& movement, const vector2f& mouse_pos);
299-
void on_mouse_wheel_(float wheel_scroll, const vector2f& mouse_pos);
300-
void on_drag_start_(input::mouse_button button_id, const vector2f& mouse_pos);
301-
void on_drag_stop_(input::mouse_button button_id, const vector2f& mouse_pos);
302-
void on_text_entered_(std::uint32_t c);
303-
void on_key_state_changed_(input::key key, bool is_down, bool is_repeat);
304-
void on_mouse_button_state_changed_(
299+
bool on_mouse_moved_(const input::mouse_moved_data& args);
300+
bool on_mouse_wheel_(const input::mouse_wheel_data& args);
301+
bool on_drag_start_(const input::mouse_drag_start_data& args);
302+
bool on_drag_stop_(const input::mouse_drag_stop_data& args);
303+
bool on_text_entered_(const input::text_entered_data& args);
304+
bool on_key_state_changed_(input::key key, bool is_down, bool is_repeat);
305+
bool on_mouse_button_state_changed_(
305306
input::mouse_button button_id,
306307
bool is_down,
307308
bool is_double_click,
309+
bool was_dragged,
308310
const vector2f& mouse_pos);
309311

310312
manager& manager_;

include/lxgui/input_signals.hpp

+76-40
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,71 @@
77

88
namespace lxgui::input {
99

10+
/// Data for on_mouse_moved signal
11+
struct mouse_moved_data {
12+
gui::vector2f motion; /// Mouse motion that generated this event, in points
13+
gui::vector2f position; /// Mouse position, in points
14+
};
15+
16+
/// Data for on_mouse_wheel signal
17+
struct mouse_wheel_data {
18+
float motion; /// Mouse wheel motion that generated this event
19+
gui::vector2f position; /// Mouse position, in points
20+
};
21+
22+
/// Data for on_mouse_pressed signal
23+
struct mouse_pressed_data {
24+
input::mouse_button button; /// Mouse button that generated this event
25+
gui::vector2f position; /// Mouse position, in points
26+
};
27+
28+
/// Data for on_mouse_released signal
29+
struct mouse_released_data {
30+
input::mouse_button button; /// Mouse button that generated this event
31+
gui::vector2f position; /// Mouse position, in points
32+
bool was_dragged; /// Was mouse dragged before release?
33+
};
34+
35+
/// Data for on_mouse_double_clicked signal
36+
struct mouse_double_clicked_data {
37+
input::mouse_button button; /// Mouse button that generated this event
38+
gui::vector2f position; /// Mouse position, in points
39+
};
40+
41+
/// Data for on_mouse_drag_start signal
42+
struct mouse_drag_start_data {
43+
input::mouse_button button; /// Mouse button that generated this event (if more than one, only
44+
/// the first pressed)
45+
gui::vector2f position; /// Mouse position, in points
46+
};
47+
48+
/// Data for on_mouse_drag_stop signal
49+
struct mouse_drag_stop_data {
50+
input::mouse_button button; /// Mouse button that generated this event (if more than one, only
51+
/// the first pressed)
52+
gui::vector2f position; /// Mouse position, in points
53+
};
54+
55+
/// Data for on_key_pressed signal
56+
struct key_pressed_data {
57+
input::key key; /// Keyboard key that generated this event
58+
};
59+
60+
/// Data for on_key_pressed_repeat signal
61+
struct key_pressed_repeat_data {
62+
input::key key; /// Keyboard key that generated this event
63+
};
64+
65+
/// Data for on_key_released signal
66+
struct key_released_data {
67+
input::key key; /// Keyboard key that generated this event
68+
};
69+
70+
/// Data for on_text_entered signal
71+
struct text_entered_data {
72+
std::uint32_t character; /// Unicode UTF-32 code point of the typed character
73+
};
74+
1075
/// Stores signals for input events.
1176
class signals {
1277
public:
@@ -21,88 +86,59 @@ class signals {
2186

2287
/**
2388
* \brief Signal triggered when the mouse moves
24-
* \details Arguments:
25-
* - mouse motion that generated this event, in points
26-
* - mouse position, in points
2789
*/
28-
utils::signal<void(const gui::vector2f&, const gui::vector2f&)> on_mouse_moved;
90+
utils::signal<void(const mouse_moved_data&)> on_mouse_moved;
2991

3092
/**
3193
* \brief Signal triggered when the mouse wheel is moved
32-
* \details Arguments:
33-
* - mouse wheel motion that generated this event
34-
* - mouse position, in points
3594
*/
36-
utils::signal<void(float, const gui::vector2f&)> on_mouse_wheel;
95+
utils::signal<void(const mouse_wheel_data&)> on_mouse_wheel;
3796

3897
/**
3998
* \brief Signal triggered when a mouse button is pressed
40-
* \details Arguments:
41-
* - mouse button that generated this event
42-
* - mouse position, in points
4399
*/
44-
utils::signal<void(input::mouse_button, const gui::vector2f&)> on_mouse_pressed;
100+
utils::signal<void(const mouse_pressed_data&)> on_mouse_pressed;
45101

46102
/**
47103
* \brief Signal triggered when a mouse button is released
48-
* \details Arguments:
49-
* - mouse button that generated this event
50-
* - mouse position, in points
51104
*/
52-
utils::signal<void(input::mouse_button, const gui::vector2f&)> on_mouse_released;
105+
utils::signal<void(const mouse_released_data&)> on_mouse_released;
53106

54107
/**
55108
* \brief Signal triggered when a mouse button is double clicked
56-
* \details Arguments:
57-
* - mouse button that generated this event
58-
* - mouse position, in points
59109
*/
60-
utils::signal<void(input::mouse_button, const gui::vector2f&)> on_mouse_double_clicked;
110+
utils::signal<void(const mouse_double_clicked_data&)> on_mouse_double_clicked;
61111

62112
/**
63113
* \brief Signal triggered when the mouse starts a drag operation
64-
* \details Arguments:
65-
* - mouse button that is pressed (if more than one, only the first pressed)
66-
* - mouse position, in points
67114
*/
68-
utils::signal<void(input::mouse_button, const gui::vector2f&)> on_mouse_drag_start;
115+
utils::signal<void(const mouse_drag_start_data&)> on_mouse_drag_start;
69116

70117
/**
71118
* \brief Signal triggered when the mouse ends a drag operation
72-
* \details Arguments:
73-
* - mouse button that was pressed (if more than one, only the first pressed)
74-
* - mouse position, in points
75119
*/
76-
utils::signal<void(input::mouse_button, const gui::vector2f&)> on_mouse_drag_stop;
120+
utils::signal<void(const mouse_drag_stop_data&)> on_mouse_drag_stop;
77121

78122
/**
79123
* \brief Signal triggered when a keyboard key is pressed
80-
* \details Arguments:
81-
* - keyboard key that generated this event
82124
*/
83-
utils::signal<void(input::key)> on_key_pressed;
125+
utils::signal<void(const key_pressed_data&)> on_key_pressed;
84126

85127
/**
86128
* \brief Signal triggered when a keyboard key is long-pressed and repeats
87-
* \details Arguments:
88-
* - keyboard key that generated this event
89129
*/
90-
utils::signal<void(input::key)> on_key_pressed_repeat;
130+
utils::signal<void(const key_pressed_repeat_data&)> on_key_pressed_repeat;
91131

92132
/**
93133
* \brief Signal triggered when a keyboard key is released
94-
* \details Arguments:
95-
* - keyboard key that generated this event
96134
*/
97-
utils::signal<void(input::key)> on_key_released;
135+
utils::signal<void(const key_released_data&)> on_key_released;
98136

99137
/**
100138
* \brief Signal triggered when text is entered
101-
* \details Arguments:
102-
* - Unicode UTF-32 code point of the typed character
103139
* \note The event will trigger repeatedly if more than one character is generated.
104140
*/
105-
utils::signal<void(std::uint32_t)> on_text_entered;
141+
utils::signal<void(const text_entered_data&)> on_text_entered;
106142
};
107143

108144
} // namespace lxgui::input

src/gui_frame_glues.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@
150150
* last call to `OnMouseMove` (or since the last position before the mouse
151151
* entered this frame), and the mouse X and Y position.
152152
* - `OnMouseUp`: Similar to `OnMouseDown`, but triggered when the mouse button
153-
* is released.
153+
* is released. This event provides one extra argument compared to `OnMouseDown`:
154+
* a boolean flag indicating whether the mouse was released after a drag
155+
* operation (true) or not (false).
154156
* - `OnMouseWheel`: Triggered when the mouse wheel is moved and this frame is
155157
* the topmost mouse-wheel-enabled frame under the mouse pointer. This event
156158
* provides three arguments to the registered callback. The first is a number

0 commit comments

Comments
 (0)