item_list.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**************************************************************************/
  2. /* item_list.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 ITEM_LIST_H
  31. #define ITEM_LIST_H
  32. #include "scene/gui/control.h"
  33. #include "scene/gui/scroll_bar.h"
  34. #include "scene/resources/text_paragraph.h"
  35. class ItemList : public Control {
  36. GDCLASS(ItemList, Control);
  37. public:
  38. enum IconMode {
  39. ICON_MODE_TOP,
  40. ICON_MODE_LEFT
  41. };
  42. enum SelectMode {
  43. SELECT_SINGLE,
  44. SELECT_MULTI
  45. };
  46. private:
  47. struct Item {
  48. Ref<Texture2D> icon;
  49. bool icon_transposed = false;
  50. Rect2i icon_region;
  51. Color icon_modulate = Color(1, 1, 1, 1);
  52. Ref<Texture2D> tag_icon;
  53. String text;
  54. Ref<TextParagraph> text_buf;
  55. String language;
  56. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  57. bool selectable = true;
  58. bool selected = false;
  59. bool disabled = false;
  60. bool tooltip_enabled = true;
  61. Variant metadata;
  62. String tooltip;
  63. Color custom_fg;
  64. Color custom_bg = Color(0.0, 0.0, 0.0, 0.0);
  65. Rect2 rect_cache;
  66. Rect2 min_rect_cache;
  67. Size2 get_icon_size() const;
  68. bool operator<(const Item &p_another) const { return text < p_another.text; }
  69. Item() {
  70. text_buf.instantiate();
  71. }
  72. };
  73. int current = -1;
  74. bool shape_changed = true;
  75. bool ensure_selected_visible = false;
  76. bool same_column_width = false;
  77. bool auto_height = false;
  78. float auto_height_value = 0.0;
  79. Vector<Item> items;
  80. Vector<int> separators;
  81. SelectMode select_mode = SELECT_SINGLE;
  82. IconMode icon_mode = ICON_MODE_LEFT;
  83. VScrollBar *scroll_bar = nullptr;
  84. TextServer::OverrunBehavior text_overrun_behavior = TextServer::OVERRUN_TRIM_ELLIPSIS;
  85. uint64_t search_time_msec = 0;
  86. String search_string;
  87. int current_columns = 1;
  88. int fixed_column_width = 0;
  89. int max_text_lines = 1;
  90. int max_columns = 1;
  91. Size2 fixed_icon_size;
  92. Size2 max_item_size_cache;
  93. int defer_select_single = -1;
  94. bool allow_rmb_select = false;
  95. bool allow_reselect = false;
  96. real_t icon_scale = 1.0;
  97. bool do_autoscroll_to_bottom = false;
  98. struct ThemeCache {
  99. int h_separation = 0;
  100. int v_separation = 0;
  101. Ref<StyleBox> panel_style;
  102. Ref<StyleBox> focus_style;
  103. Ref<Font> font;
  104. int font_size = 0;
  105. Color font_color;
  106. Color font_selected_color;
  107. int font_outline_size = 0;
  108. Color font_outline_color;
  109. int line_separation = 0;
  110. int icon_margin = 0;
  111. Ref<StyleBox> selected_style;
  112. Ref<StyleBox> selected_focus_style;
  113. Ref<StyleBox> cursor_style;
  114. Ref<StyleBox> cursor_focus_style;
  115. Color guide_color;
  116. } theme_cache;
  117. void _scroll_changed(double);
  118. void _shape(int p_idx);
  119. protected:
  120. virtual void _update_theme_item_cache() override;
  121. void _notification(int p_what);
  122. bool _set(const StringName &p_name, const Variant &p_value);
  123. bool _get(const StringName &p_name, Variant &r_ret) const;
  124. void _get_property_list(List<PropertyInfo> *p_list) const;
  125. static void _bind_methods();
  126. public:
  127. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  128. int add_item(const String &p_item, const Ref<Texture2D> &p_texture = Ref<Texture2D>(), bool p_selectable = true);
  129. int add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable = true);
  130. void set_item_text(int p_idx, const String &p_text);
  131. String get_item_text(int p_idx) const;
  132. void set_item_text_direction(int p_idx, TextDirection p_text_direction);
  133. TextDirection get_item_text_direction(int p_idx) const;
  134. void set_item_language(int p_idx, const String &p_language);
  135. String get_item_language(int p_idx) const;
  136. void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon);
  137. Ref<Texture2D> get_item_icon(int p_idx) const;
  138. void set_item_icon_transposed(int p_idx, const bool transposed);
  139. bool is_item_icon_transposed(int p_idx) const;
  140. void set_item_icon_region(int p_idx, const Rect2 &p_region);
  141. Rect2 get_item_icon_region(int p_idx) const;
  142. void set_item_icon_modulate(int p_idx, const Color &p_modulate);
  143. Color get_item_icon_modulate(int p_idx) const;
  144. void set_item_selectable(int p_idx, bool p_selectable);
  145. bool is_item_selectable(int p_idx) const;
  146. void set_item_disabled(int p_idx, bool p_disabled);
  147. bool is_item_disabled(int p_idx) const;
  148. void set_item_metadata(int p_idx, const Variant &p_metadata);
  149. Variant get_item_metadata(int p_idx) const;
  150. void set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon);
  151. void set_item_tooltip_enabled(int p_idx, const bool p_enabled);
  152. bool is_item_tooltip_enabled(int p_idx) const;
  153. void set_item_tooltip(int p_idx, const String &p_tooltip);
  154. String get_item_tooltip(int p_idx) const;
  155. void set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color);
  156. Color get_item_custom_bg_color(int p_idx) const;
  157. void set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color);
  158. Color get_item_custom_fg_color(int p_idx) const;
  159. void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior);
  160. TextServer::OverrunBehavior get_text_overrun_behavior() const;
  161. void select(int p_idx, bool p_single = true);
  162. void deselect(int p_idx);
  163. void deselect_all();
  164. bool is_selected(int p_idx) const;
  165. Vector<int> get_selected_items();
  166. bool is_anything_selected();
  167. void set_current(int p_current);
  168. int get_current() const;
  169. void move_item(int p_from_idx, int p_to_idx);
  170. void set_item_count(int p_count);
  171. int get_item_count() const;
  172. void remove_item(int p_idx);
  173. void clear();
  174. void set_fixed_column_width(int p_size);
  175. int get_fixed_column_width() const;
  176. void set_same_column_width(bool p_enable);
  177. bool is_same_column_width() const;
  178. void set_max_text_lines(int p_lines);
  179. int get_max_text_lines() const;
  180. void set_max_columns(int p_amount);
  181. int get_max_columns() const;
  182. void set_select_mode(SelectMode p_mode);
  183. SelectMode get_select_mode() const;
  184. void set_icon_mode(IconMode p_mode);
  185. IconMode get_icon_mode() const;
  186. void set_fixed_icon_size(const Size2i &p_size);
  187. Size2i get_fixed_icon_size() const;
  188. void set_allow_rmb_select(bool p_allow);
  189. bool get_allow_rmb_select() const;
  190. void set_allow_reselect(bool p_allow);
  191. bool get_allow_reselect() const;
  192. void ensure_current_is_visible();
  193. void sort_items_by_text();
  194. int find_metadata(const Variant &p_metadata) const;
  195. virtual String get_tooltip(const Point2 &p_pos) const override;
  196. int get_item_at_position(const Point2 &p_pos, bool p_exact = false) const;
  197. bool is_pos_at_end_of_items(const Point2 &p_pos) const;
  198. void set_icon_scale(real_t p_scale);
  199. real_t get_icon_scale() const;
  200. void set_auto_height(bool p_enable);
  201. bool has_auto_height() const;
  202. Size2 get_minimum_size() const override;
  203. void set_autoscroll_to_bottom(const bool p_enable);
  204. VScrollBar *get_v_scroll_bar() { return scroll_bar; }
  205. ItemList();
  206. ~ItemList();
  207. };
  208. VARIANT_ENUM_CAST(ItemList::SelectMode);
  209. VARIANT_ENUM_CAST(ItemList::IconMode);
  210. #endif // ITEM_LIST_H