item_list.h 9.9 KB

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