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 
31 #ifndef ITEMLIST_H
32 #define ITEMLIST_H
33 
34 #include "scene/gui/control.h"
35 #include "scene/gui/scroll_bar.h"
36 
37 class ItemList : public Control {
38 
39 	GDCLASS(ItemList, Control);
40 
41 public:
42 	enum IconMode {
43 		ICON_MODE_TOP,
44 		ICON_MODE_LEFT
45 	};
46 
47 	enum SelectMode {
48 		SELECT_SINGLE,
49 		SELECT_MULTI
50 	};
51 
52 private:
53 	struct Item {
54 
55 		Ref<Texture> icon;
56 		bool icon_transposed;
57 		Rect2i icon_region;
58 		Color icon_modulate;
59 		Ref<Texture> tag_icon;
60 		String text;
61 		bool selectable;
62 		bool selected;
63 		bool disabled;
64 		bool tooltip_enabled;
65 		Variant metadata;
66 		String tooltip;
67 		Color custom_fg;
68 		Color custom_bg;
69 
70 		Rect2 rect_cache;
71 		Rect2 min_rect_cache;
72 
73 		Size2 get_icon_size() const;
74 
75 		bool operator<(const Item &p_another) const { return text < p_another.text; }
76 	};
77 
78 	int current;
79 
80 	bool shape_changed;
81 
82 	bool ensure_selected_visible;
83 	bool same_column_width;
84 
85 	bool auto_height;
86 	float auto_height_value;
87 
88 	Vector<Item> items;
89 	Vector<int> separators;
90 
91 	SelectMode select_mode;
92 	IconMode icon_mode;
93 	VScrollBar *scroll_bar;
94 
95 	uint64_t search_time_msec;
96 	String search_string;
97 
98 	int current_columns;
99 	int fixed_column_width;
100 	int max_text_lines;
101 	int max_columns;
102 
103 	Size2 fixed_icon_size;
104 
105 	Size2 max_item_size_cache;
106 
107 	int defer_select_single;
108 
109 	bool allow_rmb_select;
110 
111 	bool allow_reselect;
112 
113 	real_t icon_scale;
114 
115 	bool do_autoscroll_to_bottom;
116 
117 	Array _get_items() const;
118 	void _set_items(const Array &p_items);
119 
120 	void _scroll_changed(double);
121 	void _gui_input(const Ref<InputEvent> &p_event);
122 
123 protected:
124 	void _notification(int p_what);
125 	static void _bind_methods();
126 
127 public:
128 	void add_item(const String &p_item, const Ref<Texture> &p_texture = Ref<Texture>(), bool p_selectable = true);
129 	void add_icon_item(const Ref<Texture> &p_item, bool p_selectable = true);
130 
131 	void set_item_text(int p_idx, const String &p_text);
132 	String get_item_text(int p_idx) const;
133 
134 	void set_item_icon(int p_idx, const Ref<Texture> &p_icon);
135 	Ref<Texture> get_item_icon(int p_idx) const;
136 
137 	void set_item_icon_transposed(int p_idx, const bool transposed);
138 	bool is_item_icon_transposed(int p_idx) const;
139 
140 	void set_item_icon_region(int p_idx, const Rect2 &p_region);
141 	Rect2 get_item_icon_region(int p_idx) const;
142 
143 	void set_item_icon_modulate(int p_idx, const Color &p_modulate);
144 	Color get_item_icon_modulate(int p_idx) const;
145 
146 	void set_item_selectable(int p_idx, bool p_selectable);
147 	bool is_item_selectable(int p_idx) const;
148 
149 	void set_item_disabled(int p_idx, bool p_disabled);
150 	bool is_item_disabled(int p_idx) const;
151 
152 	void set_item_metadata(int p_idx, const Variant &p_metadata);
153 	Variant get_item_metadata(int p_idx) const;
154 
155 	void set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon);
156 	Ref<Texture> get_item_tag_icon(int p_idx) const;
157 
158 	void set_item_tooltip_enabled(int p_idx, const bool p_enabled);
159 	bool is_item_tooltip_enabled(int p_idx) const;
160 
161 	void set_item_tooltip(int p_idx, const String &p_tooltip);
162 	String get_item_tooltip(int p_idx) const;
163 
164 	void set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color);
165 	Color get_item_custom_bg_color(int p_idx) const;
166 
167 	void set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color);
168 	Color get_item_custom_fg_color(int p_idx) const;
169 
170 	void select(int p_idx, bool p_single = true);
171 	void unselect(int p_idx);
172 	void unselect_all();
173 	bool is_selected(int p_idx) const;
174 	Vector<int> get_selected_items();
175 	bool is_anything_selected();
176 
177 	void set_current(int p_current);
178 	int get_current() const;
179 
180 	void move_item(int p_from_idx, int p_to_idx);
181 
182 	int get_item_count() const;
183 	void remove_item(int p_idx);
184 
185 	void clear();
186 
187 	void set_fixed_column_width(int p_size);
188 	int get_fixed_column_width() const;
189 
190 	void set_same_column_width(bool p_enable);
191 	bool is_same_column_width() const;
192 
193 	void set_max_text_lines(int p_lines);
194 	int get_max_text_lines() const;
195 
196 	void set_max_columns(int p_amount);
197 	int get_max_columns() const;
198 
199 	void set_select_mode(SelectMode p_mode);
200 	SelectMode get_select_mode() const;
201 
202 	void set_icon_mode(IconMode p_mode);
203 	IconMode get_icon_mode() const;
204 
205 	void set_fixed_icon_size(const Size2 &p_size);
206 	Size2 get_fixed_icon_size() const;
207 
208 	void set_allow_rmb_select(bool p_allow);
209 	bool get_allow_rmb_select() const;
210 
211 	void set_allow_reselect(bool p_allow);
212 	bool get_allow_reselect() const;
213 
214 	void ensure_current_is_visible();
215 
216 	void sort_items_by_text();
217 	int find_metadata(const Variant &p_metadata) const;
218 
219 	virtual String get_tooltip(const Point2 &p_pos) const;
220 	int get_item_at_position(const Point2 &p_pos, bool p_exact = false) const;
221 	bool is_pos_at_end_of_items(const Point2 &p_pos) const;
222 
223 	void set_icon_scale(real_t p_scale);
224 	real_t get_icon_scale() const;
225 
226 	void set_auto_height(bool p_enable);
227 	bool has_auto_height() const;
228 
229 	Size2 get_minimum_size() const;
230 
231 	void set_autoscroll_to_bottom(const bool p_enable);
232 
get_v_scroll()233 	VScrollBar *get_v_scroll() { return scroll_bar; }
234 
235 	ItemList();
236 	~ItemList();
237 };
238 
239 VARIANT_ENUM_CAST(ItemList::SelectMode);
240 VARIANT_ENUM_CAST(ItemList::IconMode);
241 
242 #endif // ITEMLIST_H
243