summaryrefslogtreecommitdiff
path: root/scene/gui/tree.h
blob: 4c3d03c91a2738d79e53ed6a7179157be9f35726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*************************************************************************/
/*  tree.h                                                               */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef TREE_H
#define TREE_H

#include "scene/gui/control.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/scroll_bar.h"
#include "scene/gui/slider.h"
#include "scene/resources/text_line.h"

class Tree;

class TreeItem : public Object {
	GDCLASS(TreeItem, Object);

public:
	enum TreeCellMode {
		CELL_MODE_STRING, ///< just a string
		CELL_MODE_CHECK, ///< string + check
		CELL_MODE_RANGE, ///< Contains a range
		CELL_MODE_ICON, ///< Contains an icon, not editable
		CELL_MODE_CUSTOM, ///< Contains a custom value, show a string, and an edit button
	};

	enum TextAlign {
		ALIGN_LEFT,
		ALIGN_CENTER,
		ALIGN_RIGHT
	};

private:
	friend class Tree;

	struct Cell {
		TreeCellMode mode;

		Ref<Texture2D> icon;
		Rect2i icon_region;
		String text;
		String suffix;
		Ref<TextLine> text_buf;
		Dictionary opentype_features;
		String language;
		Control::StructuredTextParser st_parser = Control::STRUCTURED_TEXT_DEFAULT;
		Array st_args;
		Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
		bool dirty;
		double min, max, step, val;
		int icon_max_w;
		bool expr;
		bool checked;
		bool editable;
		bool selected;
		bool selectable;
		bool custom_color;
		Color color;
		bool custom_bg_color;
		bool custom_bg_outline;
		Color bg_color;
		bool custom_button;
		bool expand_right;
		Color icon_color;

		TextAlign text_align;

		Variant meta;
		String tooltip;

		ObjectID custom_draw_obj;
		StringName custom_draw_callback;

		struct Button {
			int id;
			bool disabled;
			Ref<Texture2D> texture;
			Color color;
			String tooltip;
			Button() {
				id = 0;
				disabled = false;
				color = Color(1, 1, 1, 1);
				tooltip = "";
			}
		};

		Vector<Button> buttons;

		Cell() {
			text_buf.instance();
			dirty = true;
			custom_draw_obj = ObjectID();
			custom_button = false;
			mode = TreeItem::CELL_MODE_STRING;
			min = 0;
			max = 100;
			step = 1;
			val = 0;
			checked = false;
			editable = false;
			selected = false;
			selectable = true;
			custom_color = false;
			custom_bg_color = false;
			expr = false;
			icon_max_w = 0;
			text_align = ALIGN_LEFT;
			expand_right = false;
			icon_color = Color(1, 1, 1);
		}

		Size2 get_icon_size() const;
		void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2(), const Color &p_color = Color()) const;
	};

	Vector<Cell> cells;

	bool collapsed; // won't show children
	bool disable_folding;
	int custom_min_height;

	TreeItem *parent; // parent item
	TreeItem *next; // next in list
	TreeItem *children; //child items
	Tree *tree; //tree (for reference)

	TreeItem(Tree *p_tree);

	void _changed_notify(int p_cell);
	void _changed_notify();
	void _cell_selected(int p_cell);
	void _cell_deselected(int p_cell);

protected:
	static void _bind_methods();
	//bind helpers
	Dictionary _get_range_config(int p_column) {
		Dictionary d;
		double min = 0.0, max = 0.0, step = 0.0;
		get_range_config(p_column, min, max, step);
		d["min"] = min;
		d["max"] = max;
		d["step"] = step;
		d["expr"] = false;

		return d;
	}
	void _remove_child(Object *p_child) {
		remove_child(Object::cast_to<TreeItem>(p_child));
	}

	Variant _call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);

public:
	/* cell mode */
	void set_cell_mode(int p_column, TreeCellMode p_mode);
	TreeCellMode get_cell_mode(int p_column) const;

	/* check mode */
	void set_checked(int p_column, bool p_checked);
	bool is_checked(int p_column) const;

	void set_text(int p_column, String p_text);
	String get_text(int p_column) const;

	void set_text_direction(int p_column, Control::TextDirection p_text_direction);
	Control::TextDirection get_text_direction(int p_column) const;

	void set_opentype_feature(int p_column, const String &p_name, int p_value);
	int get_opentype_feature(int p_column, const String &p_name) const;
	void clear_opentype_features(int p_column);

	void set_structured_text_bidi_override(int p_column, Control::StructuredTextParser p_parser);
	Control::StructuredTextParser get_structured_text_bidi_override(int p_column) const;

	void set_structured_text_bidi_override_options(int p_column, Array p_args);
	Array get_structured_text_bidi_override_options(int p_column) const;

	void set_language(int p_column, const String &p_language);
	String get_language(int p_column) const;

	void set_suffix(int p_column, String p_suffix);
	String get_suffix(int p_column) const;

	void set_icon(int p_column, const Ref<Texture2D> &p_icon);
	Ref<Texture2D> get_icon(int p_column) const;

	void set_icon_region(int p_column, const Rect2 &p_icon_region);
	Rect2 get_icon_region(int p_column) const;

	void set_icon_modulate(int p_column, const Color &p_modulate);
	Color get_icon_modulate(int p_column) const;

	void set_icon_max_width(int p_column, int p_max);
	int get_icon_max_width(int p_column) const;

	void add_button(int p_column, const Ref<Texture2D> &p_button, int p_id = -1, bool p_disabled = false, const String &p_tooltip = "");
	int get_button_count(int p_column) const;
	String get_button_tooltip(int p_column, int p_idx) const;
	Ref<Texture2D> get_button(int p_column, int p_idx) const;
	int get_button_id(int p_column, int p_idx) const;
	void erase_button(int p_column, int p_idx);
	int get_button_by_id(int p_column, int p_id) const;
	void set_button(int p_column, int p_idx, const Ref<Texture2D> &p_button);
	void set_button_color(int p_column, int p_idx, const Color &p_color);
	void set_button_disabled(int p_column, int p_idx, bool p_disabled);
	bool is_button_disabled(int p_column, int p_idx) const;

	/* range works for mode number or mode combo */

	void set_range(int p_column, double p_value);
	double get_range(int p_column) const;

	void set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp = false);
	void get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const;
	bool is_range_exponential(int p_column) const;

	void set_metadata(int p_column, const Variant &p_meta);
	Variant get_metadata(int p_column) const;

	void set_custom_draw(int p_column, Object *p_object, const StringName &p_callback);

	void set_collapsed(bool p_collapsed);
	bool is_collapsed();

	void set_custom_minimum_height(int p_height);
	int get_custom_minimum_height() const;

	TreeItem *get_prev();
	TreeItem *get_next();
	TreeItem *get_parent();
	TreeItem *get_children();

	TreeItem *get_prev_visible(bool p_wrap = false);
	TreeItem *get_next_visible(bool p_wrap = false);

	void remove_child(TreeItem *p_item);

	void set_selectable(int p_column, bool p_selectable);
	bool is_selectable(int p_column) const;

	bool is_selected(int p_column);
	void select(int p_column);
	void deselect(int p_column);
	void set_as_cursor(int p_column);

	void set_editable(int p_column, bool p_editable);
	bool is_editable(int p_column);

	void set_custom_color(int p_column, const Color &p_color);
	Color get_custom_color(int p_column) const;
	void clear_custom_color(int p_column);

	void set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline = false);
	void clear_custom_bg_color(int p_column);
	Color get_custom_bg_color(int p_column) const;

	void set_custom_as_button(int p_column, bool p_button);
	bool is_custom_set_as_button(int p_column) const;

	void set_tooltip(int p_column, const String &p_tooltip);
	String get_tooltip(int p_column) const;

	void clear_children();

	void set_text_align(int p_column, TextAlign p_align);
	TextAlign get_text_align(int p_column) const;

	void set_expand_right(int p_column, bool p_enable);
	bool get_expand_right(int p_column) const;

	void move_to_top();
	void move_to_bottom();

	void set_disable_folding(bool p_disable);
	bool is_folding_disabled() const;

	void call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);

	~TreeItem();
};

VARIANT_ENUM_CAST(TreeItem::TreeCellMode);
VARIANT_ENUM_CAST(TreeItem::TextAlign);

class VBoxContainer;

class Tree : public Control {
	GDCLASS(Tree, Control);

public:
	enum SelectMode {
		SELECT_SINGLE,
		SELECT_ROW,
		SELECT_MULTI
	};

	enum DropModeFlags {
		DROP_MODE_DISABLED = 0,
		DROP_MODE_ON_ITEM = 1,
		DROP_MODE_INBETWEEN = 2
	};

private:
	friend class TreeItem;

	TreeItem *root;
	TreeItem *popup_edited_item;
	TreeItem *selected_item;
	TreeItem *edited_item;

	TreeItem *drop_mode_over;
	int drop_mode_section;

	TreeItem *single_select_defer;
	int single_select_defer_column;

	int pressed_button;
	bool pressing_for_editor;
	String pressing_for_editor_text;
	Vector2 pressing_pos;
	Rect2 pressing_item_rect;

	float range_drag_base;
	bool range_drag_enabled;
	Vector2 range_drag_capture_pos;

	bool propagate_mouse_activated;

	//TreeItem *cursor_item;
	//int cursor_column;

	Rect2 custom_popup_rect;
	int edited_col;
	int selected_col;
	int popup_edited_item_col;
	bool hide_root;
	SelectMode select_mode;

	int blocked;

	int drop_mode_flags;

	struct ColumnInfo {
		int min_width;
		bool expand;
		String title;
		Ref<TextLine> text_buf;
		Dictionary opentype_features;
		String language;
		Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
		ColumnInfo() {
			text_buf.instance();
			min_width = 1;
			expand = true;
		}
	};

	bool show_column_titles;

	VBoxContainer *popup_editor_vb;

	Popup *popup_editor;
	LineEdit *text_editor;
	HSlider *value_editor;
	bool updating_value_editor;
	uint64_t focus_in_id;
	PopupMenu *popup_menu;

	Vector<ColumnInfo> columns;

	Timer *range_click_timer;
	TreeItem *range_item_last;
	bool range_up_last;
	void _range_click_timeout();

	int compute_item_height(TreeItem *p_item) const;
	int get_item_height(TreeItem *p_item) const;
	void _update_all();
	void update_column(int p_col);
	void update_item_cell(TreeItem *p_item, int p_col);
	void update_item_cache(TreeItem *p_item);
	//void draw_item_text(String p_text,const Ref<Texture2D>& p_icon,int p_icon_max_w,bool p_tool,Rect2i p_rect,const Color& p_color);
	void draw_item_rect(TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color);
	int draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item);
	void select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev = nullptr, bool *r_in_range = nullptr, bool p_force_deselect = false);
	int propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool p_doubleclick, TreeItem *p_item, int p_button, const Ref<InputEventWithModifiers> &p_mod);
	void _text_editor_enter(String p_text);
	void _text_editor_modal_close();
	void value_editor_changed(double p_value);

	void popup_select(int p_option);

	void _gui_input(Ref<InputEvent> p_event);
	void _notification(int p_what);

	Size2 get_minimum_size() const override;

	void item_edited(int p_column, TreeItem *p_item, bool p_lmb = true);
	void item_changed(int p_column, TreeItem *p_item);
	void item_selected(int p_column, TreeItem *p_item);
	void item_deselected(int p_column, TreeItem *p_item);

	void propagate_set_columns(TreeItem *p_item);

	struct Cache {
		Ref<Font> font;
		Ref<Font> tb_font;
		int font_size;
		int tb_font_size;
		Ref<StyleBox> bg;
		Ref<StyleBox> selected;
		Ref<StyleBox> selected_focus;
		Ref<StyleBox> cursor;
		Ref<StyleBox> cursor_unfocus;
		Ref<StyleBox> button_pressed;
		Ref<StyleBox> title_button;
		Ref<StyleBox> title_button_hover;
		Ref<StyleBox> title_button_pressed;
		Ref<StyleBox> custom_button;
		Ref<StyleBox> custom_button_hover;
		Ref<StyleBox> custom_button_pressed;

		Color title_button_color;

		Ref<Texture2D> checked;
		Ref<Texture2D> unchecked;
		Ref<Texture2D> arrow_collapsed;
		Ref<Texture2D> arrow;
		Ref<Texture2D> select_arrow;
		Ref<Texture2D> updown;

		Color font_color;
		Color font_color_selected;
		Color guide_color;
		Color drop_position_color;
		Color relationship_line_color;
		Color custom_button_font_highlight;

		int hseparation;
		int vseparation;
		int item_margin;
		int button_margin;
		Point2 offset;
		int draw_relationship_lines;
		int draw_guides;
		int scroll_border;
		int scroll_speed;

		enum ClickType {
			CLICK_NONE,
			CLICK_TITLE,
			CLICK_BUTTON,

		};

		ClickType click_type;
		ClickType hover_type;
		int click_index;
		int click_id;
		TreeItem *click_item;
		int click_column;
		int hover_index;
		Point2 click_pos;

		TreeItem *hover_item;
		int hover_cell;

		Point2i text_editor_position;

	} cache;

	int _get_title_button_height() const;

	void _scroll_moved(float p_value);
	HScrollBar *h_scroll;
	VScrollBar *v_scroll;

	Size2 get_internal_min_size() const;
	void update_cache();
	void update_scrollbars();

	Rect2 search_item_rect(TreeItem *p_from, TreeItem *p_item);
	//Rect2 get_item_rect(TreeItem *p_item);
	uint64_t last_keypress;
	String incr_search;
	bool cursor_can_exit_tree;
	void _do_incr_search(const String &p_add);

	TreeItem *_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards = false);

	TreeItem *_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const;

	/*	float drag_speed;
	float drag_accum;

	float last_drag_accum;
	float last_drag_time;
	float time_since_motion;*/

	float drag_speed;
	float drag_from;
	float drag_accum;
	Vector2 last_speed;
	bool drag_touching;
	bool drag_touching_deaccel;
	bool click_handled;
	bool allow_rmb_select;
	bool scrolling;

	bool allow_reselect;

	bool force_edit_checkbox_only_on_checkbox;

	bool hide_folding;

	int _count_selected_items(TreeItem *p_from) const;
	void _go_left();
	void _go_right();
	void _go_down();
	void _go_up();

protected:
	static void _bind_methods();

	//bind helpers
	TreeItem *_create_item(Object *p_parent, int p_idx = -1) {
		return create_item(Object::cast_to<TreeItem>(p_parent), p_idx);
	}

	TreeItem *_get_next_selected(Object *p_item) {
		return get_next_selected(Object::cast_to<TreeItem>(p_item));
	}

	Rect2 _get_item_rect(Object *p_item, int p_column) const {
		return get_item_rect(Object::cast_to<TreeItem>(p_item), p_column);
	}

public:
	virtual String get_tooltip(const Point2 &p_pos) const override;

	TreeItem *get_item_at_position(const Point2 &p_pos) const;
	int get_column_at_position(const Point2 &p_pos) const;
	int get_drop_section_at_position(const Point2 &p_pos) const;
	int get_button_id_at_position(const Point2 &p_pos) const;

	void clear();

	TreeItem *create_item(TreeItem *p_parent = nullptr, int p_idx = -1);
	TreeItem *get_root();
	TreeItem *get_last_item();

	void set_column_min_width(int p_column, int p_min_width);
	void set_column_expand(int p_column, bool p_expand);
	int get_column_width(int p_column) const;

	void set_hide_root(bool p_enabled);
	bool is_root_hidden() const;
	TreeItem *get_next_selected(TreeItem *p_item);
	TreeItem *get_selected() const;
	int get_selected_column() const;
	int get_pressed_button() const;
	void set_select_mode(SelectMode p_mode);
	SelectMode get_select_mode() const;
	void deselect_all();
	bool is_anything_selected();

	void set_columns(int p_columns);
	int get_columns() const;

	void set_column_title(int p_column, const String &p_title);
	String get_column_title(int p_column) const;

	void set_column_title_direction(int p_column, Control::TextDirection p_text_direction);
	Control::TextDirection get_column_title_direction(int p_column) const;

	void set_column_title_opentype_feature(int p_column, const String &p_name, int p_value);
	int get_column_title_opentype_feature(int p_column, const String &p_name) const;
	void clear_column_title_opentype_features(int p_column);

	void set_column_title_language(int p_column, const String &p_language);
	String get_column_title_language(int p_column) const;

	void set_column_titles_visible(bool p_show);
	bool are_column_titles_visible() const;

	TreeItem *get_edited() const;
	int get_edited_column() const;

	void ensure_cursor_is_visible();

	Rect2 get_custom_popup_rect() const;

	int get_item_offset(TreeItem *p_item) const;
	Rect2 get_item_rect(TreeItem *p_item, int p_column = -1) const;
	bool edit_selected();

	// First item that starts with the text, from the current focused item down and wraps around.
	TreeItem *search_item_text(const String &p_find, int *r_col = nullptr, bool p_selectable = false);
	// First item that matches the whole text, from the first item down.
	TreeItem *get_item_with_text(const String &p_find) const;

	Point2 get_scroll() const;
	void scroll_to_item(TreeItem *p_item);

	void set_cursor_can_exit_tree(bool p_enable);
	bool can_cursor_exit_tree() const;

	VScrollBar *get_vscroll_bar() { return v_scroll; }

	void set_hide_folding(bool p_hide);
	bool is_folding_hidden() const;

	void set_drop_mode_flags(int p_flags);
	int get_drop_mode_flags() const;

	void set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable);
	bool get_edit_checkbox_cell_only_when_checkbox_is_pressed() const;

	void set_allow_rmb_select(bool p_allow);
	bool get_allow_rmb_select() const;

	void set_allow_reselect(bool p_allow);
	bool get_allow_reselect() const;

	Tree();
	~Tree();
};

VARIANT_ENUM_CAST(Tree::SelectMode);
VARIANT_ENUM_CAST(Tree::DropModeFlags);
#endif