| 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 | #ifndef OPTION_BUTTON_H |
| 32 | #define OPTION_BUTTON_H |
| 33 | |
| 34 | #include "scene/gui/button.h" |
| 35 | #include "scene/gui/popup_menu.h" |
| 36 | |
| 37 | class OptionButton : public Button { |
| 38 | GDCLASS(OptionButton, Button); |
| 39 | |
| 40 | bool disable_shortcuts = false; |
| 41 | PopupMenu * = 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 | |
| 48 | struct ThemeCache { |
| 49 | Ref<StyleBox> normal; |
| 50 | |
| 51 | Color font_color; |
| 52 | Color font_focus_color; |
| 53 | Color font_pressed_color; |
| 54 | Color font_hover_color; |
| 55 | Color font_hover_pressed_color; |
| 56 | Color font_disabled_color; |
| 57 | |
| 58 | int h_separation = 0; |
| 59 | |
| 60 | Ref<Texture2D> arrow_icon; |
| 61 | int arrow_margin = 0; |
| 62 | int modulate_arrow = 0; |
| 63 | } theme_cache; |
| 64 | |
| 65 | void _focused(int p_which); |
| 66 | void _selected(int p_which); |
| 67 | void _select(int p_which, bool p_emit = false); |
| 68 | void _select_int(int p_which); |
| 69 | void _refresh_size_cache(); |
| 70 | |
| 71 | virtual void pressed() override; |
| 72 | |
| 73 | protected: |
| 74 | Size2 get_minimum_size() const override; |
| 75 | virtual void _queue_update_size_cache() override; |
| 76 | |
| 77 | void _notification(int p_what); |
| 78 | bool _set(const StringName &p_name, const Variant &p_value); |
| 79 | bool _get(const StringName &p_name, Variant &r_ret) const; |
| 80 | void _get_property_list(List<PropertyInfo> *p_list) const; |
| 81 | void _validate_property(PropertyInfo &p_property) const; |
| 82 | static void _bind_methods(); |
| 83 | |
| 84 | virtual void shortcut_input(const Ref<InputEvent> &p_event) override; |
| 85 | |
| 86 | public: |
| 87 | // ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes, |
| 88 | // this value should be updated to reflect the new size. |
| 89 | static const int ITEM_PROPERTY_SIZE = 5; |
| 90 | |
| 91 | void add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1); |
| 92 | void add_item(const String &p_label, int p_id = -1); |
| 93 | |
| 94 | void set_item_text(int p_idx, const String &p_text); |
| 95 | void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon); |
| 96 | void set_item_id(int p_idx, int p_id); |
| 97 | void set_item_metadata(int p_idx, const Variant &p_metadata); |
| 98 | void set_item_disabled(int p_idx, bool p_disabled); |
| 99 | void set_item_tooltip(int p_idx, const String &p_tooltip); |
| 100 | |
| 101 | String get_item_text(int p_idx) const; |
| 102 | Ref<Texture2D> get_item_icon(int p_idx) const; |
| 103 | int get_item_id(int p_idx) const; |
| 104 | int get_item_index(int p_id) const; |
| 105 | Variant get_item_metadata(int p_idx) const; |
| 106 | bool is_item_disabled(int p_idx) const; |
| 107 | bool is_item_separator(int p_idx) const; |
| 108 | String get_item_tooltip(int p_idx) const; |
| 109 | |
| 110 | bool has_selectable_items() const; |
| 111 | int get_selectable_item(bool p_from_last = false) const; |
| 112 | |
| 113 | void set_item_count(int p_count); |
| 114 | int get_item_count() const; |
| 115 | void set_fit_to_longest_item(bool p_fit); |
| 116 | bool is_fit_to_longest_item() const; |
| 117 | |
| 118 | void set_allow_reselect(bool p_allow); |
| 119 | bool get_allow_reselect() const; |
| 120 | |
| 121 | void add_separator(const String &p_text = "" ); |
| 122 | |
| 123 | void clear(); |
| 124 | |
| 125 | void select(int p_idx); |
| 126 | int get_selected() const; |
| 127 | int get_selected_id() const; |
| 128 | Variant get_selected_metadata() const; |
| 129 | |
| 130 | void remove_item(int p_idx); |
| 131 | |
| 132 | PopupMenu *() const; |
| 133 | void (); |
| 134 | |
| 135 | void set_disable_shortcuts(bool p_disabled); |
| 136 | |
| 137 | OptionButton(const String &p_text = String()); |
| 138 | ~OptionButton(); |
| 139 | }; |
| 140 | |
| 141 | #endif // OPTION_BUTTON_H |
| 142 | |