Skip to content

Commit eb56fde

Browse files
committed
Allow configuring events that trigger Button:OnClick
1 parent 859b489 commit eb56fde

File tree

3 files changed

+222
-16
lines changed

3 files changed

+222
-16
lines changed

include/lxgui/gui_button.hpp

Lines changed: 108 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
*/
@@ -345,6 +354,99 @@ class button : public frame {
345354
*/
346355
const vector2f& get_pushed_text_offset() const;
347356

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

@@ -373,6 +475,8 @@ class button : public frame {
373475
utils::observer_ptr<font_string> current_font_string_ = nullptr;
374476

375477
vector2f pushed_text_offset_ = vector2f::zero;
478+
479+
std::set<std::string> reg_click_list_;
376480
};
377481

378482
} // namespace lxgui::gui

src/gui_button.cpp

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ button::button(utils::control_block& block, manager& mgr, const frame_core_attri
1616
initialize_(*this, attr);
1717

1818
enable_mouse();
19+
enable_button_clicks(input::mouse_button::left, input::mouse_button_event::up);
1920
}
2021

2122
std::string button::serialize(const std::string& tab) const {
@@ -44,15 +45,35 @@ void button::fire_script(const std::string& script_name, const event_data& data)
4445
release();
4546
}
4647

47-
if (script_name == "OnMouseDown") {
48-
push();
49-
fire_script("OnClick");
50-
if (!checker.is_alive())
51-
return;
48+
if (script_name == "OnMouseDown" || script_name == "OnMouseUp" ||
49+
script_name == "OnDoubleClick") {
50+
51+
const input::mouse_button_event event_id =
52+
script_name == "OnMouseDown" ? input::mouse_button_event::down
53+
: script_name == "OnMouseUp" ? input::mouse_button_event::up
54+
: input::mouse_button_event::double_click;
55+
56+
const auto event_name = data.get<std::string>(1) +
57+
std::string{input::get_mouse_button_event_codename(event_id)};
58+
59+
if (is_button_clicks_enabled(event_name)) {
60+
if (event_id == input::mouse_button_event::down)
61+
push();
62+
else if (event_id == input::mouse_button_event::up)
63+
release();
64+
65+
event_data new_data;
66+
new_data.add(data.get(0)); // Mouse button ID
67+
new_data.add(static_cast<std::underlying_type_t<input::mouse_button_event>>(
68+
event_id)); // Mouse button event
69+
new_data.add(event_name); // Mouse button event name
70+
new_data.add(data.get(2)); // Mouse pos X
71+
new_data.add(data.get(3)); // Mouse pos Y
72+
fire_script("OnClick", new_data);
73+
if (!checker.is_alive())
74+
return;
75+
}
5276
}
53-
54-
if (script_name == "OnMouseUp")
55-
release();
5677
}
5778
}
5879

@@ -436,6 +457,44 @@ const vector2f& button::get_pushed_text_offset() const {
436457
return pushed_text_offset_;
437458
}
438459

460+
void button::enable_button_clicks(const std::string& mouse_state) {
461+
reg_click_list_.insert(mouse_state);
462+
}
463+
464+
void button::enable_button_clicks(
465+
input::mouse_button button_id, input::mouse_button_event button_event) {
466+
reg_click_list_.insert(
467+
std::string{input::get_mouse_button_codename(button_id)} +
468+
std::string{input::get_mouse_button_event_codename(button_event)});
469+
}
470+
471+
void button::disable_button_clicks(const std::string& mouse_state) {
472+
reg_click_list_.erase(mouse_state);
473+
}
474+
475+
void button::disable_button_clicks(
476+
input::mouse_button button_id, input::mouse_button_event button_event) {
477+
reg_click_list_.erase(
478+
std::string{input::get_mouse_button_codename(button_id)} +
479+
std::string{input::get_mouse_button_event_codename(button_event)});
480+
}
481+
482+
void button::disable_button_clicks() {
483+
reg_click_list_.clear();
484+
}
485+
486+
bool button::is_button_clicks_enabled(const std::string& mouse_event) const {
487+
return reg_click_list_.find(mouse_event) != reg_click_list_.end();
488+
}
489+
490+
bool button::is_button_clicks_enabled(
491+
input::mouse_button button_id, input::mouse_button_event button_event) const {
492+
return reg_click_list_.find(
493+
std::string{input::get_mouse_button_codename(button_id)} +
494+
std::string{input::get_mouse_button_event_codename(button_event)}) !=
495+
reg_click_list_.end();
496+
}
497+
439498
const std::vector<std::string>& button::get_type_list_() const {
440499
return get_type_list_impl_<button>();
441500
}

src/gui_button_glues.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@
2020
* case, the "normal" font is rendered with a slight offset that you
2121
* are free to define.
2222
*
23-
* Note that a @{Button} has @{Frame:enable_mouse} set to `true` by
23+
* The button "click" event will be triggered by all registered combinations
24+
* of mouse button events configured with @{Button:enable_button_clicks}.
25+
*
26+
* Note that a @{Button} has @{Frame:enable_mouse} set to `true` and
27+
* @{Button:enable_button_clicks} set to `true` for `"LeftMouseUp"` by
2428
* default.
2529
*
2630
* __Events.__ Hard-coded events available to all @{Button}s, in
2731
* addition to those from @{Frame}:
2832
*
2933
* - `OnClick`: Triggered when the button is clicked, either when
30-
* @{Button:click} is called, or just when a mouse button is pressed
31-
* when the cursor is over the button.
32-
* - `OnDoubleClick`: Triggered when the button is double-clicked.
34+
* @{Button:click} is called, or just when a mouse button is released or
35+
* pressed when the cursor is over the button (depending on whether clicks
36+
* are enabled for the mouse button and mouse button state, see
37+
* @{Button:enable_button_clicks}). This event provides five arguments to
38+
* the registered callback: a number identifying the mouse button, a number
39+
* identifying the mouse button event, a string containing the human-readable
40+
* name of this button event (e.g., `"LeftButtonUp"`, `"RightButtonDown"`, ...),
41+
* and the mouse X and Y position.
3342
* - `OnEnable`: Triggered by @{Button:enable}.
3443
* - `OnDisable`: Triggered by @{Button:disable}.
3544
*
@@ -293,6 +302,40 @@ void button::register_on_lua(sol::state& lua) {
293302
/** @function unlock_highlight
294303
*/
295304
type.set_function("unlock_highlight", member_function<&button::unlock_highlight>());
305+
306+
/** @function disable_button_clicks
307+
*/
308+
type.set_function(
309+
"disable_button_clicks", [](button& self, sol::optional<std::string> mouse_event_name) {
310+
if (mouse_event_name.has_value()) {
311+
self.disable_button_clicks(mouse_event_name.value());
312+
} else {
313+
self.disable_button_clicks();
314+
}
315+
});
316+
317+
/** @function enable_button_clicks
318+
*/
319+
type.set_function(
320+
"enable_button_clicks",
321+
member_function< // select the right overload for Lua
322+
static_cast<void (button::*)(const std::string&)>(&button::enable_button_clicks)>());
323+
324+
/** @function is_button_clicks_enabled
325+
*/
326+
type.set_function(
327+
"is_button_clicks_enabled",
328+
member_function< // select the right overload for Lua
329+
static_cast<bool (button::*)(const std::string&) const>(
330+
&button::is_button_clicks_enabled)>());
331+
332+
/** @function set_button_clicks_enabled
333+
*/
334+
type.set_function(
335+
"set_button_clicks_enabled",
336+
member_function< // select the right overload for Lua
337+
static_cast<void (button::*)(const std::string&, bool)>(
338+
&button::set_button_clicks_enabled)>());
296339
}
297340

298341
} // namespace lxgui::gui

0 commit comments

Comments
 (0)