mirror of
https://github.com/Redot-Engine/redot-engine.git
synced 2025-12-06 23:31:53 -05:00
Merge pull request #80203 from WhalesState/Dev2
Add shortcut handling to `OptionButton`
This commit is contained in:
@@ -148,6 +148,13 @@
|
|||||||
Passing [code]-1[/code] as the index deselects any currently selected item.
|
Passing [code]-1[/code] as the index deselects any currently selected item.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
<method name="set_disable_shortcuts">
|
||||||
|
<return type="void" />
|
||||||
|
<param index="0" name="disabled" type="bool" />
|
||||||
|
<description>
|
||||||
|
If [code]true[/code], shortcuts are disabled and cannot be used to trigger the button.
|
||||||
|
</description>
|
||||||
|
</method>
|
||||||
<method name="set_item_disabled">
|
<method name="set_item_disabled">
|
||||||
<return type="void" />
|
<return type="void" />
|
||||||
<param index="0" name="idx" type="int" />
|
<param index="0" name="idx" type="int" />
|
||||||
|
|||||||
@@ -30,10 +30,26 @@
|
|||||||
|
|
||||||
#include "option_button.h"
|
#include "option_button.h"
|
||||||
|
|
||||||
|
#include "core/os/keyboard.h"
|
||||||
#include "core/string/print_string.h"
|
#include "core/string/print_string.h"
|
||||||
|
|
||||||
static const int NONE_SELECTED = -1;
|
static const int NONE_SELECTED = -1;
|
||||||
|
|
||||||
|
void OptionButton::shortcut_input(const Ref<InputEvent> &p_event) {
|
||||||
|
ERR_FAIL_COND(p_event.is_null());
|
||||||
|
|
||||||
|
if (disable_shortcuts) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p_event->is_pressed() && !p_event->is_echo() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
|
||||||
|
accept_event();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Button::shortcut_input(p_event);
|
||||||
|
}
|
||||||
|
|
||||||
Size2 OptionButton::get_minimum_size() const {
|
Size2 OptionButton::get_minimum_size() const {
|
||||||
Size2 minsize;
|
Size2 minsize;
|
||||||
if (fit_to_longest_item) {
|
if (fit_to_longest_item) {
|
||||||
@@ -574,6 +590,7 @@ void OptionButton::_bind_methods() {
|
|||||||
ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
|
ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
|
||||||
ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect);
|
ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect);
|
||||||
ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect);
|
ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &OptionButton::set_disable_shortcuts);
|
||||||
|
|
||||||
// "selected" property must come after "item_count", otherwise GH-10213 occurs.
|
// "selected" property must come after "item_count", otherwise GH-10213 occurs.
|
||||||
ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
|
ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
|
||||||
@@ -584,9 +601,14 @@ void OptionButton::_bind_methods() {
|
|||||||
ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
|
ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OptionButton::set_disable_shortcuts(bool p_disabled) {
|
||||||
|
disable_shortcuts = p_disabled;
|
||||||
|
}
|
||||||
|
|
||||||
OptionButton::OptionButton(const String &p_text) :
|
OptionButton::OptionButton(const String &p_text) :
|
||||||
Button(p_text) {
|
Button(p_text) {
|
||||||
set_toggle_mode(true);
|
set_toggle_mode(true);
|
||||||
|
set_process_shortcut_input(true);
|
||||||
set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
|
set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
|
||||||
set_action_mode(ACTION_MODE_BUTTON_PRESS);
|
set_action_mode(ACTION_MODE_BUTTON_PRESS);
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
class OptionButton : public Button {
|
class OptionButton : public Button {
|
||||||
GDCLASS(OptionButton, Button);
|
GDCLASS(OptionButton, Button);
|
||||||
|
|
||||||
|
bool disable_shortcuts = false;
|
||||||
PopupMenu *popup = nullptr;
|
PopupMenu *popup = nullptr;
|
||||||
int current = -1;
|
int current = -1;
|
||||||
bool fit_to_longest_item = true;
|
bool fit_to_longest_item = true;
|
||||||
@@ -79,6 +80,7 @@ protected:
|
|||||||
void _get_property_list(List<PropertyInfo> *p_list) const;
|
void _get_property_list(List<PropertyInfo> *p_list) const;
|
||||||
void _validate_property(PropertyInfo &p_property) const;
|
void _validate_property(PropertyInfo &p_property) const;
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes,
|
// ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes,
|
||||||
@@ -129,6 +131,8 @@ public:
|
|||||||
PopupMenu *get_popup() const;
|
PopupMenu *get_popup() const;
|
||||||
void show_popup();
|
void show_popup();
|
||||||
|
|
||||||
|
void set_disable_shortcuts(bool p_disabled);
|
||||||
|
|
||||||
OptionButton(const String &p_text = String());
|
OptionButton(const String &p_text = String());
|
||||||
~OptionButton();
|
~OptionButton();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user