0.2.0-dev Download ZIP
166 lines ·6.6 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* option_button.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 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() {} // Stub for PropertyListHelper (_set() doesn't use it). |
| 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 | // ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes, |
| 97 | // this value should be updated to reflect the new size. |
| 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 | void set_search_bar_enabled(bool p_enabled); |
| 122 | bool is_search_bar_enabled() const; |
| 123 | |
| 124 | void set_search_bar_min_item_count(int p_count); |
| 125 | int get_search_bar_min_item_count() const; |
| 126 | |
| 127 | void set_search_bar_fuzzy_search_enabled(bool p_enabled); |
| 128 | bool is_search_bar_fuzzy_search_enabled() const; |
| 129 | |
| 130 | void set_search_bar_fuzzy_search_max_misses(int p_max_misses); |
| 131 | int get_search_bar_fuzzy_search_max_misses() const; |
| 132 | |
| 133 | bool has_selectable_items() const; |
| 134 | int get_selectable_item(bool p_from_last = false) const; |
| 135 | |
| 136 | void set_item_count(int p_count); |
| 137 | int get_item_count() const; |
| 138 | void set_fit_to_longest_item(bool p_fit); |
| 139 | bool is_fit_to_longest_item() const; |
| 140 | |
| 141 | void set_allow_reselect(bool p_allow); |
| 142 | bool get_allow_reselect() const; |
| 143 | |
| 144 | void add_separator(const String &p_text = ""); |
| 145 | |
| 146 | void clear(); |
| 147 | |
| 148 | void select(int p_idx); |
| 149 | int get_selected() const; |
| 150 | int get_selected_id() const; |
| 151 | Variant get_selected_metadata() const; |
| 152 | |
| 153 | void remove_item(int p_idx); |
| 154 | |
| 155 | PopupMenu *get_popup() const; |
| 156 | void show_popup(); |
| 157 | |
| 158 | void set_disable_shortcuts(bool p_disabled); |
| 159 | |
| 160 | #ifdef TOOLS_ENABLED |
| 161 | PackedStringArray get_configuration_warnings() const override; |
| 162 | #endif |
| 163 | |
| 164 | OptionButton(const String &p_text = String()); |
| 165 | ~OptionButton(); |
| 166 | }; |