tree.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /**************************************************************************/
  2. /* tree.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. #pragma once
  31. #include "scene/gui/control.h"
  32. #include "scene/resources/text_paragraph.h"
  33. class VBoxContainer;
  34. class HScrollBar;
  35. class HSlider;
  36. class LineEdit;
  37. class Popup;
  38. class PopupMenu;
  39. class TextEdit;
  40. class Tree;
  41. class VScrollBar;
  42. class TreeItem : public Object {
  43. GDCLASS(TreeItem, Object);
  44. public:
  45. enum TreeCellMode {
  46. CELL_MODE_STRING,
  47. CELL_MODE_CHECK,
  48. CELL_MODE_RANGE,
  49. CELL_MODE_ICON,
  50. CELL_MODE_CUSTOM,
  51. };
  52. private:
  53. friend class Tree;
  54. struct Cell {
  55. mutable RID accessibility_cell_element;
  56. TreeCellMode mode = TreeItem::CELL_MODE_STRING;
  57. Ref<Texture2D> icon;
  58. Ref<Texture2D> icon_overlay;
  59. Rect2i icon_region;
  60. String text;
  61. String xl_text;
  62. Node::AutoTranslateMode auto_translate_mode = Node::AUTO_TRANSLATE_MODE_INHERIT;
  63. String description;
  64. bool edit_multiline = false;
  65. String suffix;
  66. Ref<TextParagraph> text_buf;
  67. String language;
  68. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  69. Array st_args;
  70. Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
  71. TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
  72. bool dirty = true;
  73. double min = 0.0;
  74. double max = 100.0;
  75. double step = 1.0;
  76. double val = 0.0;
  77. int icon_max_w = 0;
  78. bool expr = false;
  79. bool checked = false;
  80. bool indeterminate = false;
  81. bool editable = false;
  82. bool selected = false;
  83. bool selectable = true;
  84. bool custom_color = false;
  85. Color color;
  86. bool custom_bg_color = false;
  87. bool custom_bg_outline = false;
  88. Color bg_color;
  89. bool custom_button = false;
  90. bool expand_right = false;
  91. Color icon_color = Color(1, 1, 1);
  92. Size2i cached_minimum_size;
  93. bool cached_minimum_size_dirty = true;
  94. HorizontalAlignment text_alignment = HORIZONTAL_ALIGNMENT_LEFT;
  95. Variant meta;
  96. String tooltip;
  97. Callable custom_draw_callback;
  98. struct Button {
  99. mutable RID accessibility_button_element;
  100. int id = 0;
  101. bool disabled = false;
  102. Ref<Texture2D> texture;
  103. Color color = Color(1, 1, 1, 1);
  104. String tooltip;
  105. String description;
  106. };
  107. Vector<Button> buttons;
  108. Ref<Font> custom_font;
  109. int custom_font_size = -1;
  110. Cell() {
  111. text_buf.instantiate();
  112. text_buf->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  113. }
  114. Size2 get_icon_size() const;
  115. void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2(), const Color &p_color = Color()) const;
  116. };
  117. mutable RID accessibility_row_element;
  118. mutable bool accessibility_row_dirty = true;
  119. Vector<Cell> cells;
  120. bool collapsed = false; // Won't show children.
  121. bool visible = true;
  122. bool parent_visible_in_tree = true;
  123. bool disable_folding = false;
  124. int custom_min_height = 0;
  125. TreeItem *parent = nullptr; // Parent item.
  126. TreeItem *prev = nullptr; // Previous in list.
  127. TreeItem *next = nullptr; // Next in list.
  128. TreeItem *first_child = nullptr;
  129. TreeItem *last_child = nullptr;
  130. Vector<TreeItem *> children_cache;
  131. bool is_root = false; // For tree root.
  132. Tree *tree = nullptr; // Tree (for reference).
  133. TreeItem(Tree *p_tree);
  134. void _changed_notify(int p_cell);
  135. void _changed_notify();
  136. void _cell_selected(int p_cell);
  137. void _cell_deselected(int p_cell);
  138. void _handle_visibility_changed(bool p_visible);
  139. void _propagate_visibility_changed(bool p_parent_visible_in_tree);
  140. void _change_tree(Tree *p_tree);
  141. _FORCE_INLINE_ void _create_children_cache() {
  142. if (children_cache.is_empty()) {
  143. TreeItem *c = first_child;
  144. while (c) {
  145. children_cache.append(c);
  146. c = c->next;
  147. }
  148. }
  149. }
  150. _FORCE_INLINE_ void _unlink_from_tree() {
  151. if (accessibility_row_element.is_valid()) {
  152. DisplayServer::get_singleton()->accessibility_free_element(accessibility_row_element);
  153. accessibility_row_element = RID();
  154. }
  155. for (Cell &cell : cells) {
  156. if (cell.accessibility_cell_element.is_valid()) {
  157. DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
  158. cell.accessibility_cell_element = RID();
  159. }
  160. for (Cell::Button &btn : cell.buttons) {
  161. if (btn.accessibility_button_element.is_valid()) {
  162. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  163. btn.accessibility_button_element = RID();
  164. }
  165. }
  166. }
  167. TreeItem *p = get_prev();
  168. if (p) {
  169. p->next = next;
  170. }
  171. if (next) {
  172. next->prev = p;
  173. }
  174. if (parent) {
  175. if (!parent->children_cache.is_empty()) {
  176. parent->children_cache.remove_at(get_index());
  177. }
  178. if (parent->first_child == this) {
  179. parent->first_child = next;
  180. }
  181. if (parent->last_child == this) {
  182. parent->last_child = prev;
  183. }
  184. }
  185. }
  186. bool _is_any_collapsed(bool p_only_visible);
  187. protected:
  188. static void _bind_methods();
  189. #ifndef DISABLE_DEPRECATED
  190. void _add_button_bind_compat_76829(int p_column, const Ref<Texture2D> &p_button, int p_id, bool p_disabled, const String &p_tooltip);
  191. static void _bind_compatibility_methods();
  192. #endif
  193. // Bind helpers.
  194. Dictionary _get_range_config(int p_column) {
  195. Dictionary d;
  196. double min = 0.0, max = 0.0, step = 0.0;
  197. get_range_config(p_column, min, max, step);
  198. d["min"] = min;
  199. d["max"] = max;
  200. d["step"] = step;
  201. d["expr"] = false;
  202. return d;
  203. }
  204. void _call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  205. public:
  206. // Cell mode.
  207. void set_cell_mode(int p_column, TreeCellMode p_mode);
  208. TreeCellMode get_cell_mode(int p_column) const;
  209. // Auto translate mode.
  210. void set_auto_translate_mode(int p_column, Node::AutoTranslateMode p_mode);
  211. Node::AutoTranslateMode get_auto_translate_mode(int p_column) const;
  212. // Multiline editable.
  213. void set_edit_multiline(int p_column, bool p_multiline);
  214. bool is_edit_multiline(int p_column) const;
  215. // Check mode.
  216. void set_checked(int p_column, bool p_checked);
  217. void set_indeterminate(int p_column, bool p_indeterminate);
  218. bool is_checked(int p_column) const;
  219. bool is_indeterminate(int p_column) const;
  220. void propagate_check(int p_column, bool p_emit_signal = true);
  221. String atr(int p_column, const String &p_text) const;
  222. private:
  223. // Check helpers.
  224. void _propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal);
  225. void _propagate_check_through_parents(int p_column, bool p_emit_signal);
  226. TreeItem *_get_prev_in_tree(bool p_wrap = false, bool p_include_invisible = false);
  227. TreeItem *_get_next_in_tree(bool p_wrap = false, bool p_include_invisible = false);
  228. public:
  229. void set_text(int p_column, String p_text);
  230. String get_text(int p_column) const;
  231. void set_description(int p_column, String p_text);
  232. String get_description(int p_column) const;
  233. void set_text_direction(int p_column, Control::TextDirection p_text_direction);
  234. Control::TextDirection get_text_direction(int p_column) const;
  235. void set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode);
  236. TextServer::AutowrapMode get_autowrap_mode(int p_column) const;
  237. void set_text_overrun_behavior(int p_column, TextServer::OverrunBehavior p_behavior);
  238. TextServer::OverrunBehavior get_text_overrun_behavior(int p_column) const;
  239. void set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser);
  240. TextServer::StructuredTextParser get_structured_text_bidi_override(int p_column) const;
  241. void set_structured_text_bidi_override_options(int p_column, Array p_args);
  242. Array get_structured_text_bidi_override_options(int p_column) const;
  243. void set_language(int p_column, const String &p_language);
  244. String get_language(int p_column) const;
  245. void set_suffix(int p_column, String p_suffix);
  246. String get_suffix(int p_column) const;
  247. void set_icon(int p_column, const Ref<Texture2D> &p_icon);
  248. Ref<Texture2D> get_icon(int p_column) const;
  249. void set_icon_overlay(int p_column, const Ref<Texture2D> &p_icon_overlay);
  250. Ref<Texture2D> get_icon_overlay(int p_column) const;
  251. void set_icon_region(int p_column, const Rect2 &p_icon_region);
  252. Rect2 get_icon_region(int p_column) const;
  253. void set_icon_modulate(int p_column, const Color &p_modulate);
  254. Color get_icon_modulate(int p_column) const;
  255. void set_icon_max_width(int p_column, int p_max);
  256. int get_icon_max_width(int p_column) const;
  257. void clear_buttons();
  258. void add_button(int p_column, const Ref<Texture2D> &p_button, int p_id = -1, bool p_disabled = false, const String &p_tooltip = "", const String &p_description = "");
  259. int get_button_count(int p_column) const;
  260. String get_button_tooltip_text(int p_column, int p_index) const;
  261. Ref<Texture2D> get_button(int p_column, int p_index) const;
  262. int get_button_id(int p_column, int p_index) const;
  263. void erase_button(int p_column, int p_index);
  264. int get_button_by_id(int p_column, int p_id) const;
  265. Color get_button_color(int p_column, int p_index) const;
  266. void set_button_tooltip_text(int p_column, int p_index, const String &p_tooltip);
  267. void set_button(int p_column, int p_index, const Ref<Texture2D> &p_button);
  268. void set_button_description(int p_column, int p_index, const String &p_description);
  269. void set_button_color(int p_column, int p_index, const Color &p_color);
  270. void set_button_disabled(int p_column, int p_index, bool p_disabled);
  271. bool is_button_disabled(int p_column, int p_index) const;
  272. // Range works for mode number or mode combo.
  273. void set_range(int p_column, double p_value);
  274. double get_range(int p_column) const;
  275. void set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp = false);
  276. void get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const;
  277. bool is_range_exponential(int p_column) const;
  278. void set_metadata(int p_column, const Variant &p_meta);
  279. Variant get_metadata(int p_column) const;
  280. #ifndef DISABLE_DEPRECATED
  281. void set_custom_draw(int p_column, Object *p_object, const StringName &p_callback);
  282. #endif // DISABLE_DEPRECATED
  283. void set_custom_draw_callback(int p_column, const Callable &p_callback);
  284. Callable get_custom_draw_callback(int p_column) const;
  285. void set_collapsed(bool p_collapsed);
  286. bool is_collapsed();
  287. void set_collapsed_recursive(bool p_collapsed);
  288. bool is_any_collapsed(bool p_only_visible = false);
  289. void set_visible(bool p_visible);
  290. bool is_visible();
  291. bool is_visible_in_tree() const;
  292. void uncollapse_tree();
  293. void set_custom_minimum_height(int p_height);
  294. int get_custom_minimum_height() const;
  295. void set_selectable(int p_column, bool p_selectable);
  296. bool is_selectable(int p_column) const;
  297. bool is_selected(int p_column);
  298. void select(int p_column);
  299. void deselect(int p_column);
  300. void set_as_cursor(int p_column);
  301. void set_editable(int p_column, bool p_editable);
  302. bool is_editable(int p_column);
  303. void set_custom_color(int p_column, const Color &p_color);
  304. Color get_custom_color(int p_column) const;
  305. void clear_custom_color(int p_column);
  306. void set_custom_font(int p_column, const Ref<Font> &p_font);
  307. Ref<Font> get_custom_font(int p_column) const;
  308. void set_custom_font_size(int p_column, int p_font_size);
  309. int get_custom_font_size(int p_column) const;
  310. void set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline = false);
  311. void clear_custom_bg_color(int p_column);
  312. Color get_custom_bg_color(int p_column) const;
  313. void set_custom_as_button(int p_column, bool p_button);
  314. bool is_custom_set_as_button(int p_column) const;
  315. void set_tooltip_text(int p_column, const String &p_tooltip);
  316. String get_tooltip_text(int p_column) const;
  317. void set_text_alignment(int p_column, HorizontalAlignment p_alignment);
  318. HorizontalAlignment get_text_alignment(int p_column) const;
  319. void set_expand_right(int p_column, bool p_enable);
  320. bool get_expand_right(int p_column) const;
  321. void set_disable_folding(bool p_disable);
  322. bool is_folding_disabled() const;
  323. Size2 get_minimum_size(int p_column);
  324. // Item manipulation.
  325. TreeItem *create_child(int p_index = -1);
  326. void add_child(TreeItem *p_item);
  327. void remove_child(TreeItem *p_item);
  328. Tree *get_tree() const;
  329. TreeItem *get_prev();
  330. TreeItem *get_next() const;
  331. TreeItem *get_parent() const;
  332. TreeItem *get_first_child() const;
  333. TreeItem *get_prev_in_tree(bool p_wrap = false);
  334. TreeItem *get_next_in_tree(bool p_wrap = false);
  335. TreeItem *get_prev_visible(bool p_wrap = false);
  336. TreeItem *get_next_visible(bool p_wrap = false);
  337. TreeItem *get_child(int p_index);
  338. int get_visible_child_count();
  339. int get_child_count();
  340. TypedArray<TreeItem> get_children();
  341. void clear_children();
  342. int get_index();
  343. #ifdef DEV_ENABLED
  344. // This debugging code can be removed once the current refactoring of this class is complete.
  345. void validate_cache() const;
  346. #else
  347. void validate_cache() const {}
  348. #endif
  349. void move_before(TreeItem *p_item);
  350. void move_after(TreeItem *p_item);
  351. void call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  352. ~TreeItem();
  353. };
  354. VARIANT_ENUM_CAST(TreeItem::TreeCellMode);
  355. class Tree : public Control {
  356. GDCLASS(Tree, Control);
  357. public:
  358. enum SelectMode {
  359. SELECT_SINGLE,
  360. SELECT_ROW,
  361. SELECT_MULTI
  362. };
  363. enum DropModeFlags {
  364. DROP_MODE_DISABLED = 0,
  365. DROP_MODE_ON_ITEM = 1,
  366. DROP_MODE_INBETWEEN = 2
  367. };
  368. private:
  369. friend class TreeItem;
  370. TreeItem *root = nullptr;
  371. TreeItem *popup_edited_item = nullptr;
  372. TreeItem *selected_item = nullptr;
  373. TreeItem *edited_item = nullptr;
  374. TreeItem *shift_anchor = nullptr;
  375. TreeItem *popup_pressing_edited_item = nullptr; // Candidate.
  376. int popup_pressing_edited_item_column = -1;
  377. TreeItem *drop_mode_over = nullptr;
  378. int drop_mode_section = 0;
  379. TreeItem *single_select_defer = nullptr;
  380. int single_select_defer_column = 0;
  381. int pressed_button = -1;
  382. bool pressing_for_editor = false;
  383. String pressing_for_editor_text;
  384. Vector2 pressing_pos;
  385. Rect2 pressing_item_rect;
  386. Vector2 hovered_pos;
  387. bool is_mouse_hovering = false;
  388. float range_drag_base = 0.0;
  389. bool range_drag_enabled = false;
  390. Vector2 range_drag_capture_pos;
  391. bool propagate_mouse_activated = false;
  392. float content_scale_factor = 0.0;
  393. Rect2 custom_popup_rect;
  394. int edited_col = -1;
  395. int selected_col = -1;
  396. int selected_button = -1;
  397. int popup_edited_item_col = -1;
  398. bool hide_root = false;
  399. SelectMode select_mode = SELECT_SINGLE;
  400. int blocked = 0;
  401. int drop_mode_flags = 0;
  402. struct ColumnInfo {
  403. mutable RID accessibility_col_element;
  404. int custom_min_width = 0;
  405. int expand_ratio = 1;
  406. bool expand = true;
  407. bool clip_content = false;
  408. String title;
  409. String xl_title;
  410. HorizontalAlignment title_alignment = HORIZONTAL_ALIGNMENT_CENTER;
  411. Ref<TextParagraph> text_buf;
  412. String language;
  413. Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
  414. mutable int cached_minimum_width = 0;
  415. mutable bool cached_minimum_width_dirty = true;
  416. ColumnInfo() {
  417. text_buf.instantiate();
  418. }
  419. };
  420. bool show_column_titles = false;
  421. bool popup_edit_committed = true;
  422. RID accessibility_scroll_element;
  423. VBoxContainer *popup_editor_vb = nullptr;
  424. Popup *popup_editor = nullptr;
  425. LineEdit *line_editor = nullptr;
  426. TextEdit *text_editor = nullptr;
  427. HSlider *value_editor = nullptr;
  428. bool updating_value_editor = false;
  429. uint64_t focus_in_id = 0;
  430. PopupMenu *popup_menu = nullptr;
  431. Vector<ColumnInfo> columns;
  432. Timer *range_click_timer = nullptr;
  433. TreeItem *range_item_last = nullptr;
  434. bool range_up_last = false;
  435. void _range_click_timeout();
  436. int compute_item_height(TreeItem *p_item) const;
  437. int get_item_height(TreeItem *p_item) const;
  438. void _update_all();
  439. void update_column(int p_col);
  440. void update_item_cell(TreeItem *p_item, int p_col) const;
  441. void update_item_cache(TreeItem *p_item) const;
  442. void draw_item_rect(TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color, int p_ol_size, const Color &p_ol_color);
  443. int draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height);
  444. 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);
  445. int propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod);
  446. void _line_editor_submit(String p_text);
  447. void _apply_multiline_edit();
  448. void _text_editor_popup_modal_close();
  449. void _text_editor_gui_input(const Ref<InputEvent> &p_event);
  450. void value_editor_changed(double p_value);
  451. void _update_popup_menu(const TreeItem::Cell &p_cell);
  452. void _update_value_editor(const TreeItem::Cell &p_cell);
  453. void popup_select(int p_option);
  454. void item_edited(int p_column, TreeItem *p_item, MouseButton p_custom_mouse_index = MouseButton::NONE);
  455. void item_changed(int p_column, TreeItem *p_item);
  456. void item_selected(int p_column, TreeItem *p_item);
  457. void item_deselected(int p_column, TreeItem *p_item);
  458. void propagate_set_columns(TreeItem *p_item);
  459. struct ThemeCache {
  460. Ref<StyleBox> panel_style;
  461. Ref<StyleBox> focus_style;
  462. Ref<Font> font;
  463. Ref<Font> tb_font;
  464. int font_size = 0;
  465. int tb_font_size = 0;
  466. Ref<StyleBox> hovered;
  467. Ref<StyleBox> hovered_dimmed;
  468. Ref<StyleBox> hovered_selected;
  469. Ref<StyleBox> hovered_selected_focus;
  470. Ref<StyleBox> selected;
  471. Ref<StyleBox> selected_focus;
  472. Ref<StyleBox> cursor;
  473. Ref<StyleBox> cursor_unfocus;
  474. Ref<StyleBox> button_hover;
  475. Ref<StyleBox> button_pressed;
  476. Ref<StyleBox> title_button;
  477. Ref<StyleBox> title_button_hover;
  478. Ref<StyleBox> title_button_pressed;
  479. Ref<StyleBox> custom_button;
  480. Ref<StyleBox> custom_button_hover;
  481. Ref<StyleBox> custom_button_pressed;
  482. Color title_button_color;
  483. Ref<Texture2D> checked;
  484. Ref<Texture2D> unchecked;
  485. Ref<Texture2D> checked_disabled;
  486. Ref<Texture2D> unchecked_disabled;
  487. Ref<Texture2D> indeterminate;
  488. Ref<Texture2D> indeterminate_disabled;
  489. Ref<Texture2D> arrow;
  490. Ref<Texture2D> arrow_collapsed;
  491. Ref<Texture2D> arrow_collapsed_mirrored;
  492. Ref<Texture2D> select_arrow;
  493. Ref<Texture2D> updown;
  494. Color font_color;
  495. Color font_hovered_color;
  496. Color font_hovered_dimmed_color;
  497. Color font_hovered_selected_color;
  498. Color font_selected_color;
  499. Color font_disabled_color;
  500. Color guide_color;
  501. Color drop_position_color;
  502. Color relationship_line_color;
  503. Color parent_hl_line_color;
  504. Color children_hl_line_color;
  505. Color custom_button_font_highlight;
  506. Color font_outline_color;
  507. float base_scale = 1.0;
  508. int font_outline_size = 0;
  509. int h_separation = 0;
  510. int v_separation = 0;
  511. int inner_item_margin_bottom = 0;
  512. int inner_item_margin_left = 0;
  513. int inner_item_margin_right = 0;
  514. int inner_item_margin_top = 0;
  515. int item_margin = 0;
  516. int button_margin = 0;
  517. int icon_max_width = 0;
  518. Point2 offset;
  519. int draw_relationship_lines = 0;
  520. int relationship_line_width = 0;
  521. int parent_hl_line_width = 0;
  522. int children_hl_line_width = 0;
  523. int parent_hl_line_margin = 0;
  524. int draw_guides = 0;
  525. int scroll_border = 0;
  526. int scroll_speed = 0;
  527. int scrollbar_margin_top = -1;
  528. int scrollbar_margin_right = -1;
  529. int scrollbar_margin_bottom = -1;
  530. int scrollbar_margin_left = -1;
  531. int scrollbar_h_separation = 0;
  532. int scrollbar_v_separation = 0;
  533. } theme_cache;
  534. struct Cache {
  535. enum ClickType {
  536. CLICK_NONE,
  537. CLICK_TITLE,
  538. CLICK_BUTTON,
  539. };
  540. ClickType click_type = Cache::CLICK_NONE;
  541. int click_index = -1;
  542. int click_id = -1;
  543. TreeItem *click_item = nullptr;
  544. int click_column = 0;
  545. int hover_header_column = -1;
  546. bool hover_header_row = false;
  547. Point2 click_pos;
  548. TreeItem *hover_item = nullptr;
  549. int hover_column = -1;
  550. int hover_button_index_in_column = -1;
  551. bool rtl = false;
  552. } cache;
  553. int _get_title_button_height() const;
  554. Size2 _get_cell_icon_size(const TreeItem::Cell &p_cell) const;
  555. void _scroll_moved(float p_value);
  556. HScrollBar *h_scroll = nullptr;
  557. VScrollBar *v_scroll = nullptr;
  558. bool h_scroll_enabled = true;
  559. bool v_scroll_enabled = true;
  560. Size2 get_internal_min_size() const;
  561. void update_scrollbars();
  562. Rect2 search_item_rect(TreeItem *p_from, TreeItem *p_item);
  563. uint64_t last_keypress = 0;
  564. String incr_search;
  565. bool cursor_can_exit_tree = true;
  566. void _do_incr_search(const String &p_add);
  567. TreeItem *_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards = false);
  568. TreeItem *_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &r_height, int &r_section) const;
  569. void _find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index, int &r_section) const;
  570. struct FindColumnButtonResult {
  571. int column_index = -1;
  572. int button_index = -1;
  573. int column_width = -1;
  574. int column_offset = -1;
  575. int pos_x = -1;
  576. };
  577. FindColumnButtonResult _find_column_and_button_at_pos(int p_x, const TreeItem *p_item, int p_x_ofs, int p_x_limit) const;
  578. float drag_speed = 0.0;
  579. float drag_from = 0.0;
  580. float drag_accum = 0.0;
  581. bool drag_touching = false;
  582. bool drag_touching_deaccel = false;
  583. bool click_handled = false;
  584. bool allow_rmb_select = false;
  585. bool scrolling = false;
  586. bool allow_reselect = false;
  587. bool allow_search = true;
  588. bool force_edit_checkbox_only_on_checkbox = false;
  589. bool hide_folding = false;
  590. bool enable_recursive_folding = true;
  591. bool enable_auto_tooltip = true;
  592. bool hovered_update_queued = false;
  593. void _determine_hovered_item();
  594. void _queue_update_hovered_item();
  595. int _count_selected_items(TreeItem *p_from) const;
  596. bool _is_branch_selected(TreeItem *p_from) const;
  597. bool _is_sibling_branch_selected(TreeItem *p_from) const;
  598. void _go_left();
  599. void _go_right();
  600. void _go_down();
  601. void _go_up();
  602. void _shift_select_range(TreeItem *new_item);
  603. bool _scroll(bool p_horizontal, float p_pages);
  604. Rect2 _get_scrollbar_layout_rect() const;
  605. Rect2 _get_content_rect() const; // Considering the background stylebox and scrollbars.
  606. Rect2 _get_item_focus_rect(const TreeItem *p_item) const;
  607. void _check_item_accessibility(TreeItem *p_item, PackedStringArray &r_warnings, int &r_row) const;
  608. void _accessibility_clean_info(TreeItem *p_item);
  609. void _accessibility_update_item(Point2 &r_ofs, TreeItem *p_item, int &r_row, int p_level);
  610. protected:
  611. virtual void _update_theme_item_cache() override;
  612. void _notification(int p_what);
  613. static void _bind_methods();
  614. void _accessibility_action_scroll_down(const Variant &p_data);
  615. void _accessibility_action_scroll_left(const Variant &p_data);
  616. void _accessibility_action_scroll_right(const Variant &p_data);
  617. void _accessibility_action_scroll_up(const Variant &p_data);
  618. void _accessibility_action_scroll_set(const Variant &p_data);
  619. void _accessibility_action_scroll_into_view(const Variant &p_data, TreeItem *p_item, int p_col);
  620. void _accessibility_action_focus(const Variant &p_data, TreeItem *p_item, int p_col);
  621. void _accessibility_action_blur(const Variant &p_data, TreeItem *p_item, int p_col);
  622. void _accessibility_action_collapse(const Variant &p_data, TreeItem *p_item);
  623. void _accessibility_action_expand(const Variant &p_data, TreeItem *p_item);
  624. void _accessibility_action_set_text_value(const Variant &p_data, TreeItem *p_item, int p_col);
  625. void _accessibility_action_set_num_value(const Variant &p_data, TreeItem *p_item, int p_col);
  626. void _accessibility_action_set_bool_value(const Variant &p_data, TreeItem *p_item, int p_col);
  627. void _accessibility_action_set_inc(const Variant &p_data, TreeItem *p_item, int p_col);
  628. void _accessibility_action_set_dec(const Variant &p_data, TreeItem *p_item, int p_col);
  629. void _accessibility_action_edit_custom(const Variant &p_data, TreeItem *p_item, int p_col);
  630. void _accessibility_action_button_press(const Variant &p_data, TreeItem *p_item, int p_col, int p_btn);
  631. public:
  632. PackedStringArray get_accessibility_configuration_warnings() const override;
  633. virtual RID get_focused_accessibility_element() const override;
  634. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  635. virtual String get_tooltip(const Point2 &p_pos) const override;
  636. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  637. virtual Variant get_drag_data(const Point2 &p_point) override;
  638. TreeItem *get_item_at_position(const Point2 &p_pos) const;
  639. int get_column_at_position(const Point2 &p_pos) const;
  640. int get_drop_section_at_position(const Point2 &p_pos) const;
  641. int get_button_id_at_position(const Point2 &p_pos) const;
  642. void clear();
  643. TreeItem *create_item(TreeItem *p_parent = nullptr, int p_index = -1);
  644. TreeItem *get_root() const;
  645. TreeItem *get_last_item() const;
  646. void set_column_custom_minimum_width(int p_column, int p_min_width);
  647. void set_column_expand(int p_column, bool p_expand);
  648. void set_column_expand_ratio(int p_column, int p_ratio);
  649. void set_column_clip_content(int p_column, bool p_fit);
  650. int get_column_minimum_width(int p_column) const;
  651. int get_column_width(int p_column) const;
  652. int get_column_expand_ratio(int p_column) const;
  653. bool is_column_expanding(int p_column) const;
  654. bool is_column_clipping_content(int p_column) const;
  655. void set_hide_root(bool p_enabled);
  656. bool is_root_hidden() const;
  657. TreeItem *get_next_selected(TreeItem *p_item);
  658. TreeItem *get_selected() const;
  659. void set_selected(TreeItem *p_item, int p_column = 0);
  660. int get_selected_column() const;
  661. int get_pressed_button() const;
  662. void set_select_mode(SelectMode p_mode);
  663. SelectMode get_select_mode() const;
  664. void deselect_all();
  665. bool is_anything_selected();
  666. void set_columns(int p_columns);
  667. int get_columns() const;
  668. void set_column_title(int p_column, const String &p_title);
  669. String get_column_title(int p_column) const;
  670. void set_column_title_alignment(int p_column, HorizontalAlignment p_alignment);
  671. HorizontalAlignment get_column_title_alignment(int p_column) const;
  672. void set_column_title_direction(int p_column, Control::TextDirection p_text_direction);
  673. Control::TextDirection get_column_title_direction(int p_column) const;
  674. void set_column_title_language(int p_column, const String &p_language);
  675. String get_column_title_language(int p_column) const;
  676. void set_column_titles_visible(bool p_show);
  677. bool are_column_titles_visible() const;
  678. TreeItem *get_edited() const;
  679. int get_edited_column() const;
  680. void ensure_cursor_is_visible();
  681. Rect2 get_custom_popup_rect() const;
  682. int get_item_offset(TreeItem *p_item) const;
  683. Rect2 get_item_rect(TreeItem *p_item, int p_column = -1, int p_button = -1) const;
  684. bool edit_selected(bool p_force_edit = false);
  685. bool is_editing();
  686. void set_editor_selection(int p_from_line, int p_to_line, int p_from_column = -1, int p_to_column = -1, int p_caret = 0);
  687. // First item that starts with the text, from the current focused item down and wraps around.
  688. TreeItem *search_item_text(const String &p_find, int *r_col = nullptr, bool p_selectable = false);
  689. // First item that matches the whole text, from the first item down.
  690. TreeItem *get_item_with_text(const String &p_find) const;
  691. TreeItem *get_item_with_metadata(const Variant &p_find, int p_column = -1) const;
  692. Point2 get_scroll() const;
  693. void scroll_to_item(TreeItem *p_item, bool p_center_on_item = false);
  694. void set_h_scroll_enabled(bool p_enable);
  695. bool is_h_scroll_enabled() const;
  696. void set_v_scroll_enabled(bool p_enable);
  697. bool is_v_scroll_enabled() const;
  698. void set_cursor_can_exit_tree(bool p_enable);
  699. VScrollBar *get_vscroll_bar() { return v_scroll; }
  700. void set_hide_folding(bool p_hide);
  701. bool is_folding_hidden() const;
  702. void set_enable_recursive_folding(bool p_enable);
  703. bool is_recursive_folding_enabled() const;
  704. void set_drop_mode_flags(int p_flags);
  705. int get_drop_mode_flags() const;
  706. void set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable);
  707. bool get_edit_checkbox_cell_only_when_checkbox_is_pressed() const;
  708. void set_allow_rmb_select(bool p_allow);
  709. bool get_allow_rmb_select() const;
  710. void set_allow_reselect(bool p_allow);
  711. bool get_allow_reselect() const;
  712. void set_allow_search(bool p_allow);
  713. bool get_allow_search() const;
  714. void set_auto_tooltip(bool p_enable);
  715. bool is_auto_tooltip_enabled() const;
  716. Size2 get_minimum_size() const override;
  717. Tree();
  718. ~Tree();
  719. };
  720. VARIANT_ENUM_CAST(Tree::SelectMode);
  721. VARIANT_ENUM_CAST(Tree::DropModeFlags);