| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/gui/button.h" |
| 34 |
#include "scene/gui/popup_menu.h" |
| 35 |
#include "scene/property_list_helper.h" |
| 36 |
|
| 37 |
class OptionButton : public Button { |
| 38 |
GDCLASS(OptionButton, Button); |
| 39 |
|
| 40 |
bool disable_shortcuts = false; |
| 41 |
PopupMenu *popup = nullptr; |
| 42 |
int current = -1; |
| 43 |
bool fit_to_longest_item = true; |
| 44 |
Vector2 _cached_size; |
| 45 |
bool cache_refresh_pending = false; |
| 46 |
bool allow_reselect = false; |
| 47 |
bool initialized = false; |
| 48 |
int queued_current = -1; |
| 49 |
|
| 50 |
struct ThemeCache { |
| 51 |
Ref<StyleBox> normal; |
| 52 |
|
| 53 |
Color font_color; |
| 54 |
Color font_focus_color; |
| 55 |
Color font_pressed_color; |
| 56 |
Color font_hover_color; |
| 57 |
Color font_hover_pressed_color; |
| 58 |
Color font_disabled_color; |
| 59 |
|
| 60 |
int h_separation = 0; |
| 61 |
|
| 62 |
Ref<Texture2D> arrow_icon; |
| 63 |
int arrow_margin = 0; |
| 64 |
int modulate_arrow = 0; |
| 65 |
} theme_cache; |
| 66 |
|
| 67 |
static inline PropertyListHelper base_property_helper; |
| 68 |
PropertyListHelper property_helper; |
| 69 |
|
| 70 |
void _focused(int p_which); |
| 71 |
void _selected(int p_which); |
| 72 |
void _select(int p_which, bool p_emit = false); |
| 73 |
void _select_int(int p_which); |
| 74 |
void _refresh_size_cache(); |
| 75 |
void _dummy_setter() {} |
| 76 |
|
| 77 |
virtual void pressed() override; |
| 78 |
|
| 79 |
protected: |
| 80 |
Size2 get_minimum_size() const override; |
| 81 |
virtual void _queue_update_size_cache() override; |
| 82 |
virtual String _get_translated_text(const String &p_text) const override; |
| 83 |
|
| 84 |
void _notification(int p_what); |
| 85 |
bool _set(const StringName &p_name, const Variant &p_value); |
| 86 |
bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); } |
| 87 |
void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); } |
| 88 |
bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); } |
| 89 |
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); } |
| 90 |
void _validate_property(PropertyInfo &p_property) const; |
| 91 |
static void _bind_methods(); |
| 92 |
|
| 93 |
virtual void shortcut_input(const Ref<InputEvent> &p_event) override; |
| 94 |
|
| 95 |
public: |
| 96 |
|
| 97 |
|
| 98 |
static const int ITEM_PROPERTY_SIZE = 5; |
| 99 |
|
| 100 |
void add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1); |
| 101 |
void add_item(const String &p_label, int p_id = -1); |
| 102 |
|
| 103 |
void set_item_text(int p_idx, const String &p_text); |
| 104 |
void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon); |
| 105 |
void set_item_id(int p_idx, int p_id); |
| 106 |
void set_item_metadata(int p_idx, const Variant &p_metadata); |
| 107 |
void set_item_disabled(int p_idx, bool p_disabled); |
| 108 |
void set_item_tooltip(int p_idx, const String &p_tooltip); |
| 109 |
void set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode); |
| 110 |
|
| 111 |
String get_item_text(int p_idx) const; |
| 112 |
Ref<Texture2D> get_item_icon(int p_idx) const; |
| 113 |
int get_item_id(int p_idx) const; |
| 114 |
int get_item_index(int p_id) const; |
| 115 |
Variant get_item_metadata(int p_idx) const; |
| 116 |
bool is_item_disabled(int p_idx) const; |
| 117 |
bool is_item_separator(int p_idx) const; |
| 118 |
String get_item_tooltip(int p_idx) const; |
| 119 |
AutoTranslateMode get_item_auto_translate_mode(int p_idx) const; |
| 120 |
|
| 121 |
bool has_selectable_items() const; |
| 122 |
int get_selectable_item(bool p_from_last = false) const; |
| 123 |
|
| 124 |
void set_item_count(int p_count); |
| 125 |
int get_item_count() const; |
| 126 |
void set_fit_to_longest_item(bool p_fit); |
| 127 |
bool is_fit_to_longest_item() const; |
| 128 |
|
| 129 |
void set_allow_reselect(bool p_allow); |
| 130 |
bool get_allow_reselect() const; |
| 131 |
|
| 132 |
void add_separator(const String &p_text = ""); |
| 133 |
|
| 134 |
void clear(); |
| 135 |
|
| 136 |
void select(int p_idx); |
| 137 |
int get_selected() const; |
| 138 |
int get_selected_id() const; |
| 139 |
Variant get_selected_metadata() const; |
| 140 |
|
| 141 |
void remove_item(int p_idx); |
| 142 |
|
| 143 |
PopupMenu *get_popup() const; |
| 144 |
void show_popup(); |
| 145 |
|
| 146 |
void set_disable_shortcuts(bool p_disabled); |
| 147 |
|
| 148 |
#ifdef TOOLS_ENABLED |
| 149 |
PackedStringArray get_configuration_warnings() const override; |
| 150 |
#endif |
| 151 |
|
| 152 |
OptionButton(const String &p_text = String()); |
| 153 |
~OptionButton(); |
| 154 |
}; |