item_list.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* item_list.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 ITEMLIST_H
  31. #define ITEMLIST_H
  32. #include "scene/gui/control.h"
  33. #include "scene/gui/scroll_bar.h"
  34. class ItemList : public Control {
  35. GDCLASS(ItemList, Control);
  36. public:
  37. enum IconMode {
  38. ICON_MODE_TOP,
  39. ICON_MODE_LEFT
  40. };
  41. enum SelectMode {
  42. SELECT_SINGLE,
  43. SELECT_MULTI
  44. };
  45. private:
  46. struct Item {
  47. Ref<Texture> icon;
  48. Rect2i icon_region;
  49. Ref<Texture> tag_icon;
  50. String text;
  51. bool selectable;
  52. bool selected;
  53. bool disabled;
  54. bool tooltip_enabled;
  55. Variant metadata;
  56. String tooltip;
  57. Color custom_fg;
  58. Color custom_bg;
  59. Rect2 rect_cache;
  60. Rect2 min_rect_cache;
  61. Size2 get_icon_size() const;
  62. bool operator<(const Item &p_another) const { return text < p_another.text; }
  63. };
  64. int current;
  65. bool shape_changed;
  66. bool ensure_selected_visible;
  67. bool same_column_width;
  68. bool auto_height;
  69. float auto_height_value;
  70. Vector<Item> items;
  71. Vector<int> separators;
  72. SelectMode select_mode;
  73. IconMode icon_mode;
  74. VScrollBar *scroll_bar;
  75. uint64_t search_time_msec;
  76. String search_string;
  77. int current_columns;
  78. int fixed_column_width;
  79. int max_text_lines;
  80. int max_columns;
  81. Size2 fixed_icon_size;
  82. Size2 max_item_size_cache;
  83. int defer_select_single;
  84. bool allow_rmb_select;
  85. real_t icon_scale;
  86. Array _get_items() const;
  87. void _set_items(const Array &p_items);
  88. void _scroll_changed(double);
  89. void _gui_input(const Ref<InputEvent> &p_event);
  90. protected:
  91. void _notification(int p_what);
  92. static void _bind_methods();
  93. public:
  94. void add_item(const String &p_item, const Ref<Texture> &p_texture = Ref<Texture>(), bool p_selectable = true);
  95. void add_icon_item(const Ref<Texture> &p_item, bool p_selectable = true);
  96. void set_item_text(int p_idx, const String &p_text);
  97. String get_item_text(int p_idx) const;
  98. void set_item_icon(int p_idx, const Ref<Texture> &p_icon);
  99. Ref<Texture> get_item_icon(int p_idx) const;
  100. void set_item_icon_region(int p_idx, const Rect2 &p_region);
  101. Rect2 get_item_icon_region(int p_idx) const;
  102. void set_item_selectable(int p_idx, bool p_selectable);
  103. bool is_item_selectable(int p_idx) const;
  104. void set_item_disabled(int p_idx, bool p_disabled);
  105. bool is_item_disabled(int p_idx) const;
  106. void set_item_metadata(int p_idx, const Variant &p_metadata);
  107. Variant get_item_metadata(int p_idx) const;
  108. void set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon);
  109. Ref<Texture> get_item_tag_icon(int p_idx) const;
  110. void set_item_tooltip_enabled(int p_idx, const bool p_enabled);
  111. bool is_item_tooltip_enabled(int p_idx) const;
  112. void set_item_tooltip(int p_idx, const String &p_tooltip);
  113. String get_item_tooltip(int p_idx) const;
  114. void set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color);
  115. Color get_item_custom_bg_color(int p_idx) const;
  116. void set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color);
  117. Color get_item_custom_fg_color(int p_idx) const;
  118. void select(int p_idx, bool p_single = true);
  119. void unselect(int p_idx);
  120. bool is_selected(int p_idx) const;
  121. Vector<int> get_selected_items();
  122. void set_current(int p_current);
  123. int get_current() const;
  124. void move_item(int p_item, int p_to_pos);
  125. int get_item_count() const;
  126. void remove_item(int p_idx);
  127. void clear();
  128. void set_fixed_column_width(int p_size);
  129. int get_fixed_column_width() const;
  130. void set_same_column_width(bool p_enable);
  131. bool is_same_column_width() const;
  132. void set_max_text_lines(int p_lines);
  133. int get_max_text_lines() const;
  134. void set_max_columns(int p_amount);
  135. int get_max_columns() const;
  136. void set_select_mode(SelectMode p_mode);
  137. SelectMode get_select_mode() const;
  138. void set_icon_mode(IconMode p_mode);
  139. IconMode get_icon_mode() const;
  140. void set_fixed_icon_size(const Size2 &p_size);
  141. Size2 get_fixed_icon_size() const;
  142. void set_allow_rmb_select(bool p_allow);
  143. bool get_allow_rmb_select() const;
  144. void ensure_current_is_visible();
  145. void sort_items_by_text();
  146. int find_metadata(const Variant &p_metadata) const;
  147. virtual String get_tooltip(const Point2 &p_pos) const;
  148. int get_item_at_position(const Point2 &p_pos, bool p_exact = false) const;
  149. bool is_pos_at_end_of_items(const Point2 &p_pos) const;
  150. void set_icon_scale(real_t p_scale);
  151. real_t get_icon_scale() const;
  152. void set_auto_height(bool p_enable);
  153. bool has_auto_height() const;
  154. Size2 get_minimum_size() const;
  155. VScrollBar *get_v_scroll() { return scroll_bar; }
  156. ItemList();
  157. ~ItemList();
  158. };
  159. VARIANT_ENUM_CAST(ItemList::SelectMode);
  160. VARIANT_ENUM_CAST(ItemList::IconMode);
  161. #endif // ITEMLIST_H