item_list.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*************************************************************************/
  2. /* item_list.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. OBJ_TYPE(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_bg;
  58. Rect2 rect_cache;
  59. Rect2 min_rect_cache;
  60. Size2 get_icon_size() const;
  61. bool operator<(const Item &p_another) const { return text < p_another.text; }
  62. };
  63. int current;
  64. bool shape_changed;
  65. bool ensure_selected_visible;
  66. bool same_column_width;
  67. Vector<Item> items;
  68. Vector<int> separators;
  69. SelectMode select_mode;
  70. IconMode icon_mode;
  71. VScrollBar *scroll_bar;
  72. uint64_t search_time_msec;
  73. String search_string;
  74. int current_columns;
  75. int fixed_column_width;
  76. int max_text_lines;
  77. int max_columns;
  78. Size2 fixed_icon_size;
  79. Size2 max_item_size_cache;
  80. int defer_select_single;
  81. bool allow_rmb_select;
  82. real_t icon_scale;
  83. void _scroll_changed(double);
  84. void _input_event(const InputEvent &p_event);
  85. protected:
  86. void _notification(int p_what);
  87. static void _bind_methods();
  88. public:
  89. void add_item(const String &p_item, const Ref<Texture> &p_texture = Ref<Texture>(), bool p_selectable = true);
  90. void add_icon_item(const Ref<Texture> &p_item, bool p_selectable = true);
  91. void set_item_text(int p_idx, const String &p_text);
  92. String get_item_text(int p_idx) const;
  93. void set_item_icon(int p_idx, const Ref<Texture> &p_icon);
  94. Ref<Texture> get_item_icon(int p_idx) const;
  95. void set_item_icon_region(int p_idx, const Rect2 &p_region);
  96. Rect2 get_item_icon_region(int p_idx) const;
  97. void set_item_selectable(int p_idx, bool p_selectable);
  98. bool is_item_selectable(int p_idx) const;
  99. void set_item_disabled(int p_idx, bool p_disabled);
  100. bool is_item_disabled(int p_idx) const;
  101. void set_item_metadata(int p_idx, const Variant &p_metadata);
  102. Variant get_item_metadata(int p_idx) const;
  103. void set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon);
  104. Ref<Texture> get_item_tag_icon(int p_idx) const;
  105. void set_item_tooltip_enabled(int p_idx, const bool p_enabled);
  106. bool is_item_tooltip_enabled(int p_idx) const;
  107. void set_item_tooltip(int p_idx, const String &p_tooltip);
  108. String get_item_tooltip(int p_idx) const;
  109. void set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color);
  110. Color get_item_custom_bg_color(int p_idx) const;
  111. void select(int p_idx, bool p_single = true);
  112. void unselect(int p_idx);
  113. bool is_selected(int p_idx) const;
  114. Vector<int> get_selected_items();
  115. void set_current(int p_current);
  116. int get_current() const;
  117. void move_item(int p_item, int p_to_pos);
  118. int get_item_count() const;
  119. void remove_item(int p_idx);
  120. void clear();
  121. void set_fixed_column_width(int p_size);
  122. int get_fixed_column_width() const;
  123. void set_same_column_width(bool p_enable);
  124. int is_same_column_width() const;
  125. void set_max_text_lines(int p_amount);
  126. int get_max_text_lines() const;
  127. void set_max_columns(int p_amount);
  128. int get_max_columns() const;
  129. void set_select_mode(SelectMode p_mode);
  130. SelectMode get_select_mode() const;
  131. void set_icon_mode(IconMode p_mode);
  132. IconMode get_icon_mode() const;
  133. void set_fixed_icon_size(const Size2 &p_size);
  134. Size2 get_fixed_icon_size() const;
  135. void set_allow_rmb_select(bool p_allow);
  136. bool get_allow_rmb_select() const;
  137. void ensure_current_is_visible();
  138. void sort_items_by_text();
  139. int find_metadata(const Variant &p_metadata) const;
  140. virtual String get_tooltip(const Point2 &p_pos) const;
  141. int get_item_at_pos(const Point2 &p_pos, bool p_exact = false) const;
  142. void set_icon_scale(real_t p_scale);
  143. real_t get_icon_scale() const;
  144. VScrollBar *get_v_scroll() { return scroll_bar; }
  145. ItemList();
  146. ~ItemList();
  147. };
  148. VARIANT_ENUM_CAST(ItemList::SelectMode);
  149. VARIANT_ENUM_CAST(ItemList::IconMode);
  150. #endif // ITEMLIST_H