diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2021-08-03 17:27:45 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2021-08-05 13:48:43 +0200 |
commit | de2c2be19bed9f78be627045fb671367b2b93122 (patch) | |
tree | cacdc888b9e0afa64d2b80b0d1faf8480064bd57 /scene/gui | |
parent | 1eff610e2bd71521db6d0f4fee25a641e1c7bdd6 (diff) |
Shortcut: Rename `shortcut` property to `event`
Having a property which has the same name as its class leads to confusing
situations (e.g. `BaseButton` has a `shortcut` property of type `Shortcut`
which has a `shortcut` property of type `InputEvent`).
Also renames `is_event` to `matches_event`, and `is_valid` to `has_valid_event`
to better reflect what the methods check.
Diffstat (limited to 'scene/gui')
-rw-r--r-- | scene/gui/base_button.cpp | 4 | ||||
-rw-r--r-- | scene/gui/popup_menu.cpp | 6 | ||||
-rw-r--r-- | scene/gui/shortcut.cpp | 33 | ||||
-rw-r--r-- | scene/gui/shortcut.h | 12 |
4 files changed, 25 insertions, 30 deletions
diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 75a4464a40..82f4a216b8 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -345,7 +345,7 @@ void BaseButton::_unhandled_key_input(Ref<InputEvent> p_event) { return; } - if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) { + if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) { on_action_event(p_event); accept_event(); } @@ -353,7 +353,7 @@ void BaseButton::_unhandled_key_input(Ref<InputEvent> p_event) { String BaseButton::get_tooltip(const Point2 &p_pos) const { String tooltip = Control::get_tooltip(p_pos); - if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->is_valid()) { + if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) { String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")"; if (tooltip != String() && shortcut->get_name().nocasecmp_to(tooltip) != 0) { text += "\n" + tooltip; diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 490548fce0..44f7200cd7 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -74,7 +74,7 @@ Size2 PopupMenu::_get_contents_minimum_size() const { size.width += items[i].text_buf->get_size().x; size.height += vseparation; - if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) { + if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) { int accel_w = hseparation * 2; accel_w += items[i].accel_text_buf->get_size().x; accel_max_w = MAX(accel_w, accel_max_w); @@ -635,7 +635,7 @@ void PopupMenu::_draw_items() { } // Accelerator / Shortcut - if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) { + if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) { if (rtl) { item_ofs.x = scroll_width + style->get_margin(SIDE_LEFT) + item_end_padding; } else { @@ -1301,7 +1301,7 @@ bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_fo continue; } - if (items[i].shortcut.is_valid() && items[i].shortcut->is_shortcut(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) { + if (items[i].shortcut.is_valid() && items[i].shortcut->matches_event(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) { activate_item(i); return true; } diff --git a/scene/gui/shortcut.cpp b/scene/gui/shortcut.cpp index 962c6dcc60..885a51e058 100644 --- a/scene/gui/shortcut.cpp +++ b/scene/gui/shortcut.cpp @@ -32,42 +32,39 @@ #include "core/os/keyboard.h" -void Shortcut::set_shortcut(const Ref<InputEvent> &p_shortcut) { - shortcut = p_shortcut; +void Shortcut::set_event(const Ref<InputEvent> &p_event) { + event = p_event; emit_changed(); } -Ref<InputEvent> Shortcut::get_shortcut() const { - return shortcut; +Ref<InputEvent> Shortcut::get_event() const { + return event; } -bool Shortcut::is_shortcut(const Ref<InputEvent> &p_event) const { - return shortcut.is_valid() && shortcut->is_match(p_event, true); +bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const { + return event.is_valid() && event->is_match(p_event, true); } String Shortcut::get_as_text() const { - if (shortcut.is_valid()) { - return shortcut->as_text(); + if (event.is_valid()) { + return event->as_text(); } else { return "None"; } } -bool Shortcut::is_valid() const { - return shortcut.is_valid(); +bool Shortcut::has_valid_event() const { + return event.is_valid(); } void Shortcut::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_shortcut", "event"), &Shortcut::set_shortcut); - ClassDB::bind_method(D_METHOD("get_shortcut"), &Shortcut::get_shortcut); + ClassDB::bind_method(D_METHOD("set_event", "event"), &Shortcut::set_event); + ClassDB::bind_method(D_METHOD("get_event"), &Shortcut::get_event); - ClassDB::bind_method(D_METHOD("is_valid"), &Shortcut::is_valid); + ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event); - ClassDB::bind_method(D_METHOD("is_shortcut", "event"), &Shortcut::is_shortcut); + ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event); ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), "set_shortcut", "get_shortcut"); -} - -Shortcut::Shortcut() { + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), "set_event", "get_event"); } diff --git a/scene/gui/shortcut.h b/scene/gui/shortcut.h index ea91f29b5d..249dd1971f 100644 --- a/scene/gui/shortcut.h +++ b/scene/gui/shortcut.h @@ -37,20 +37,18 @@ class Shortcut : public Resource { GDCLASS(Shortcut, Resource); - Ref<InputEvent> shortcut; + Ref<InputEvent> event; protected: static void _bind_methods(); public: - void set_shortcut(const Ref<InputEvent> &p_shortcut); - Ref<InputEvent> get_shortcut() const; - bool is_shortcut(const Ref<InputEvent> &p_event) const; - bool is_valid() const; + void set_event(const Ref<InputEvent> &p_shortcut); + Ref<InputEvent> get_event() const; + bool matches_event(const Ref<InputEvent> &p_event) const; + bool has_valid_event() const; String get_as_text() const; - - Shortcut(); }; #endif // SHORTCUT_H |