item_list.h 8.9 KB

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