option_button.h 5.3 KB

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