tree.h 26 KB

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