Skip to content

Commit b43b6a2

Browse files
authored
Merge pull request #128 from cschreib/button-click
Allow configuring which events trigger Button click
2 parents 2d78240 + 856dee5 commit b43b6a2

File tree

9 files changed

+444
-34
lines changed

9 files changed

+444
-34
lines changed

include/lxgui/gui_button.hpp

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,25 @@ class font_string;
2727
* case, the "normal" font is rendered with a slight offset that you
2828
* are free to define.
2929
*
30-
* Note that a button has frame::enable_mouse set to `true` by
30+
* The button "click" event will be triggered by all registered combinations
31+
* of mouse events configured with button::enable_button_clicks.
32+
*
33+
* Note that a button has frame::enable_mouse set to `true` and
34+
* button::enable_button_clicks set to `true` for `"LeftMouseUp"` by
3135
* default.
3236
*
3337
* __Events.__ Hard-coded events available to all buttons, in
3438
* addition to those from frame:
3539
*
3640
* - `OnClick`: Triggered when the button is clicked, either when
37-
* button::click is called, or just when a mouse button is pressed
38-
* when the cursor is over the button.
39-
* - `OnDoubleClick`: Triggered when the button is double-clicked.
41+
* button::click is called, or just when a mouse button is released or
42+
* pressed when the cursor is over the button (depending on whether clicks
43+
* are enabled for the mouse button and mouse button state, see
44+
* button::enable_button_clicks). This event provides five arguments to
45+
* the registered callback: a number identifying the mouse button, a number
46+
* identifying the mouse button event, a string containing the human-readable
47+
* name of this button event (e.g., `"LeftButtonUp"`, `"RightButtonDown"`, ...),
48+
* and the mouse X and Y position.
4049
* - `OnEnable`: Triggered by button::enable.
4150
* - `OnDisable`: Triggered by button::disable.
4251
*/
@@ -301,6 +310,21 @@ class button : public frame {
301310
*/
302311
virtual void release();
303312

313+
/**
314+
* \brief Handle a mouse click over this button.
315+
* \param mouse_event The mouse event with which to generate a click
316+
* \note This calls the OnClick() handler only if the event is registered for clicks.
317+
*/
318+
void click(const std::string& mouse_event);
319+
320+
/**
321+
* \brief Handle a mouse click over this button.
322+
* \param button_id The mouse button with which to generate a click
323+
* \param button_event The mouse button event with which to generate a click
324+
* \note This calls the OnClick() handler only if the event is registered for clicks.
325+
*/
326+
void click(input::mouse_button button_id, input::mouse_button_event button_event);
327+
304328
/**
305329
* \brief Highlights this button.
306330
* \note The button will be highlighted even if the
@@ -345,6 +369,99 @@ class button : public frame {
345369
*/
346370
const vector2f& get_pushed_text_offset() const;
347371

372+
/**
373+
* \brief Make this button generate OnClick events when a specific mouse event occurs over the button.
374+
* \param mouse_event The mouse event for which to enable or disable OnClick events
375+
* \param enable 'true' to enable, 'false' to disable
376+
* \see enable_button_clicks()
377+
* \see disable_button_clicks()
378+
*/
379+
void set_button_clicks_enabled(const std::string& mouse_event, bool enable) {
380+
if (enable) {
381+
enable_button_clicks(mouse_event);
382+
} else {
383+
disable_button_clicks(mouse_event);
384+
}
385+
}
386+
387+
/**
388+
* \brief Make this button generate OnClick events when a specific mouse event occurs over the button.
389+
* \param button_id The mouse button for which to enable or disable OnClick events
390+
* \param button_event The mouse button event for which to enable or disable OnClick events
391+
* \param enable 'true' to enable, 'false' to disable
392+
* \see enable_button_clicks()
393+
* \see disable_button_clicks()
394+
*/
395+
void set_button_clicks_enabled(
396+
input::mouse_button button_id, input::mouse_button_event button_event, bool enable) {
397+
if (enable) {
398+
enable_button_clicks(button_id, button_event);
399+
} else {
400+
disable_button_clicks(button_id, button_event);
401+
}
402+
}
403+
404+
/**
405+
* \brief Make this button generate OnClick events when a specific mouse event occurs over the button.
406+
* \param mouse_event The mouse event for which to enable OnClick events
407+
* \note The mouse event string must be of the form "<button>Up" or "<button>Down". For
408+
* example, "LeftButtonUp".
409+
* \see disable_button_clicks()
410+
*/
411+
void enable_button_clicks(const std::string& mouse_event);
412+
413+
/**
414+
* \brief Make this button generate OnClick events when a specific mouse event occurs over the button.
415+
* \param button_id The mouse button for which to enable OnClick events
416+
* \param button_event The mouse button event for which to enable OnClick events
417+
* \note See @ref enable_button_clicks(const std::string&) for more information.
418+
* \see disable_button_clicks()
419+
*/
420+
void
421+
enable_button_clicks(input::mouse_button button_id, input::mouse_button_event button_event);
422+
423+
/**
424+
* \brief Stop this button from generating OnClick events when a specific mouse event occurs over the button.
425+
* \param mouse_event The mouse event for which to stop generating OnClick events
426+
* \see enable_button_clicks()
427+
*/
428+
void disable_button_clicks(const std::string& mouse_event);
429+
430+
/**
431+
* \brief Stop this button from generating OnClick events when a specific mouse event occurs over the button.
432+
* \param button_id The mouse button for which to stop generating OnClick events
433+
* \param button_event The state of the button for which to stop generating OnClick events
434+
* \note See @ref disable_button_clicks(const std::string&) for more information.
435+
* \see enable_button_clicks()
436+
*/
437+
void
438+
disable_button_clicks(input::mouse_button button_id, input::mouse_button_event button_event);
439+
440+
/**
441+
* \brief Stop this button from generating OnClick events.
442+
* \see enable_button_clicks()
443+
* \see is_button_clicks_enabled()
444+
*/
445+
void disable_button_clicks();
446+
447+
/**
448+
* \brief Checks if this button can generate OnClick events when a specific mouse event occurs over the button.
449+
* \param mouse_event The mouse event to check
450+
* \return 'true' if this button can generate OnClick events from this mouse event
451+
* \see enable_button_clicks()
452+
*/
453+
bool is_button_clicks_enabled(const std::string& mouse_event) const;
454+
455+
/**
456+
* \brief Checks if this button can generate OnClick events when a specific mouse event occurs over the button.
457+
* \param button_id The mouse button for which to check
458+
* \param button_event The mouse button event for which to check
459+
* \return 'true' if this button can generate OnClick events from this mouse event
460+
* \see enable_button_clicks()
461+
*/
462+
bool is_button_clicks_enabled(
463+
input::mouse_button button_id, input::mouse_button_event button_event) const;
464+
348465
/// Registers this region class to the provided Lua state
349466
static void register_on_lua(sol::state& lua);
350467

@@ -354,6 +471,11 @@ class button : public frame {
354471
void parse_attributes_(const layout_node& node) override;
355472
void parse_all_nodes_before_children_(const layout_node& node) override;
356473

474+
bool is_button_clicks_enabled_(input::mouse_button button_id) const;
475+
476+
virtual void click_(
477+
input::mouse_button button_id, input::mouse_button_event button_event, float mx, float my);
478+
357479
const std::vector<std::string>& get_type_list_() const override;
358480

359481
state state_ = state::up;
@@ -373,6 +495,8 @@ class button : public frame {
373495
utils::observer_ptr<font_string> current_font_string_ = nullptr;
374496

375497
vector2f pushed_text_offset_ = vector2f::zero;
498+
499+
std::set<std::string> reg_click_list_;
376500
};
377501

378502
} // namespace lxgui::gui

include/lxgui/gui_frame.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ using script_list_view = script_signal::slot_list_view;
133133
*
134134
* - `OnChar`: Triggered whenever a character is typed and the frame has focus
135135
* (see @ref frame::set_focus).
136+
* - `OnDoubleClick`: Triggered when the frame is double-clicked.
136137
* - `OnDragStart`: Triggered when one of the mouse button registered for
137138
* dragging (see frame::enable_drag) has been pressed inside the
138139
* area of the screen occupied by the frame, and a mouse movement is first
@@ -200,9 +201,10 @@ using script_list_view = script_signal::slot_list_view;
200201
* last call to `OnMouseMove` (or since the last position before the mouse
201202
* entered this frame), and the mouse X and Y position.
202203
* - `OnMouseUp`: Similar to `OnMouseDown`, but triggered when the mouse button
203-
* is released. This event provides one extra argument compared to `OnMouseDown`:
204+
* is released. This event provides two extra argument compared to `OnMouseDown`:
204205
* a boolean flag indicating whether the mouse was released after a drag
205-
* operation (true) or not (false).
206+
* operation (true) or not (false), and another boolean flag indicating whether
207+
* the mouse button was initially pressed on this frame (true) or not (false).
206208
* - `OnMouseWheel`: Triggered when the mouse wheel is moved and this frame is
207209
* the topmost mouse-wheel-enabled frame under the mouse pointer. Will not
208210
* trigger if the frame is hidden. This event provides three arguments to the
@@ -557,7 +559,6 @@ class frame : public region {
557559

558560
/**
559561
* \brief Marks this frame as unable to receive keyboard input from any key.
560-
* \param key_name The key to capture
561562
* \see enable_key_capture()
562563
* \see is_key_capture_enabled()
563564
* \see set_keyboard_enabled()

include/lxgui/gui_root.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ class root :
4949
~root() override;
5050

5151
// Non-copiable, non-movable
52-
root(const root&) = delete;
53-
root(root&&) = delete;
52+
root(const root&) = delete;
53+
root(root&&) = delete;
5454
root& operator=(const root&) = delete;
55-
root& operator=(root&&) = delete;
55+
root& operator=(root&&) = delete;
5656

5757
/**
5858
* \brief Returns the width and height of this renderer's main render target (e.g., screen).
@@ -327,8 +327,9 @@ class root :
327327
std::vector<utils::scoped_connection> connections_;
328328

329329
// Mouse IO
330-
utils::observer_ptr<frame> hovered_frame_ = nullptr;
331-
utils::observer_ptr<frame> dragged_frame_ = nullptr;
330+
utils::observer_ptr<frame> hovered_frame_ = nullptr;
331+
utils::observer_ptr<frame> dragged_frame_ = nullptr;
332+
utils::observer_ptr<frame> start_click_frame_ = nullptr;
332333

333334
utils::observer_ptr<region> moved_object_ = nullptr;
334335
utils::observer_ptr<region> sized_object_ = nullptr;

include/lxgui/input_keys.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
#include "lxgui/lxgui.hpp"
55

66
#include <cstdint>
7+
#include <optional>
78
#include <string_view>
89

910
namespace lxgui::input {
1011

1112
enum class mouse_button : std::uint8_t { left = 0, right, middle };
1213

14+
enum class mouse_button_event : std::uint8_t { up = 0, down = 1, double_click = 2 };
15+
1316
enum class key : std::uint8_t {
1417
k_unassigned = 0x00,
1518
k_escape = 0x01,
@@ -162,6 +165,7 @@ enum class key : std::uint8_t {
162165
/**
163166
* \brief Returns a standard English name for the provided mouse button.
164167
* \param button_id The ID code of the mouse button
168+
* \return The corresponding code name in English
165169
* \note This will return a standard English button name, e.g., "LeftButton" for the left mouse
166170
* button. This can be used for string-based key identification in scripts, where key
167171
* integer codes would be less usable, or for displaying debug or error messages.
@@ -177,6 +181,45 @@ std::string_view get_mouse_button_codename(mouse_button button_id);
177181
*/
178182
std::string_view get_localizable_mouse_button_name(mouse_button button_id);
179183

184+
/**
185+
* \brief Returns a standard English name for the provided mouse button event.
186+
* \param button_event The ID code of the mouse button event
187+
* \return The corresponding code name in English
188+
* \note This will return a standard English button name, e.g., "Up" for the mouse up event.
189+
* This can be used for string-based key identification in scripts, where key
190+
* integer codes would be less usable, or for displaying debug or error messages.
191+
*/
192+
std::string_view get_mouse_button_event_codename(mouse_button_event button_event);
193+
194+
/**
195+
* \brief Returns the localizable name of the provided mouse button event.
196+
* \param button_event The ID code of the mouse button event
197+
* \return The localizable name of the provided mouse button event
198+
* \note This will return localizable button names, e.g., "{mouse_event_up}" for the mouse up event.
199+
* Use a @ref lxgui::gui::localizer to transform this into a user-friendly name.
200+
*/
201+
std::string_view get_localizable_mouse_button_event_name(mouse_button_event button_event);
202+
203+
/**
204+
* \brief Returns a standard Engilsh name for the provided mouse button and event.
205+
* \param button_id The ID code of the mouse button
206+
* \param button_event The ID code of the mouse button event
207+
* \return The corresponding code name in English
208+
* \note This will return a string with the format "<button>:<event>".
209+
* \see get_mouse_button_codename()
210+
* \see get_mouse_button_event_codename()
211+
*/
212+
std::string_view
213+
get_mouse_button_and_event_codename(mouse_button button_id, mouse_button_event button_event);
214+
215+
/**
216+
* \brief Returns a mouse button and a button event ID from a string formatted as "<button>:<event>".
217+
* \param event_name The name of the mouse button event, as "<button>:<event>"
218+
* \return The pair of decoded button and event ID, or std::nullopt if parsing failed.
219+
*/
220+
std::optional<std::pair<mouse_button, mouse_button_event>>
221+
get_mouse_button_and_event_from_codename(std::string_view button_and_event_name);
222+
180223
/**
181224
* \brief Returns a standard English name for the provided key.
182225
* \param key_id The key

0 commit comments

Comments
 (0)