tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*************************************************************************/
  2. /* tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef 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. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class Tree;
  41. class TreeItem : public Object {
  42. GDCLASS(TreeItem, Object);
  43. public:
  44. enum TreeCellMode {
  45. CELL_MODE_STRING, ///< just a string
  46. CELL_MODE_CHECK, ///< string + check
  47. CELL_MODE_RANGE, ///< Contains a range
  48. CELL_MODE_ICON, ///< Contains an icon, not editable
  49. CELL_MODE_CUSTOM, ///< Contains a custom value, show a string, and an edit button
  50. };
  51. enum TextAlign {
  52. ALIGN_LEFT,
  53. ALIGN_CENTER,
  54. ALIGN_RIGHT
  55. };
  56. private:
  57. friend class Tree;
  58. struct Cell {
  59. TreeCellMode mode;
  60. Ref<Texture> icon;
  61. Rect2i icon_region;
  62. String text;
  63. String suffix;
  64. double min, max, step, val;
  65. int icon_max_w;
  66. bool expr;
  67. bool checked;
  68. bool editable;
  69. bool selected;
  70. bool selectable;
  71. bool custom_color;
  72. Color color;
  73. bool custom_bg_color;
  74. bool custom_bg_outline;
  75. Color bg_color;
  76. bool custom_button;
  77. bool expand_right;
  78. Color icon_color;
  79. TextAlign text_align;
  80. Variant meta;
  81. String tooltip;
  82. ObjectID custom_draw_obj;
  83. StringName custom_draw_callback;
  84. struct Button {
  85. int id;
  86. bool disabled;
  87. Ref<Texture> texture;
  88. Color color;
  89. String tooltip;
  90. Button() {
  91. id = 0;
  92. disabled = false;
  93. color = Color(1, 1, 1, 1);
  94. tooltip = "";
  95. }
  96. };
  97. Vector<Button> buttons;
  98. Cell() {
  99. custom_draw_obj = 0;
  100. custom_button = false;
  101. mode = TreeItem::CELL_MODE_STRING;
  102. min = 0;
  103. max = 100;
  104. step = 1;
  105. val = 0;
  106. checked = false;
  107. editable = false;
  108. selected = false;
  109. selectable = true;
  110. custom_color = false;
  111. custom_bg_color = false;
  112. expr = false;
  113. icon_max_w = 0;
  114. text_align = ALIGN_LEFT;
  115. expand_right = false;
  116. icon_color = Color(1, 1, 1);
  117. }
  118. Size2 get_icon_size() const;
  119. void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2(), const Color &p_color = Color()) const;
  120. };
  121. Vector<Cell> cells;
  122. bool collapsed; // won't show children
  123. bool disable_folding;
  124. int custom_min_height;
  125. TreeItem *parent; // parent item
  126. TreeItem *next; // next in list
  127. TreeItem *children; //child items
  128. Tree *tree; //tree (for reference)
  129. TreeItem(Tree *p_tree);
  130. void _changed_notify(int p_cell);
  131. void _changed_notify();
  132. void _cell_selected(int p_cell);
  133. void _cell_deselected(int p_cell);
  134. protected:
  135. static void _bind_methods();
  136. //bind helpers
  137. Dictionary _get_range_config(int p_column) {
  138. Dictionary d;
  139. double min, max, step;
  140. get_range_config(p_column, min, max, step);
  141. d["min"] = min;
  142. d["max"] = max;
  143. d["step"] = step;
  144. d["expr"] = false;
  145. return d;
  146. }
  147. void _remove_child(Object *p_child) {
  148. remove_child(Object::cast_to<TreeItem>(p_child));
  149. }
  150. public:
  151. /* cell mode */
  152. void set_cell_mode(int p_column, TreeCellMode p_mode);
  153. TreeCellMode get_cell_mode(int p_column) const;
  154. /* check mode */
  155. void set_checked(int p_column, bool p_checked);
  156. bool is_checked(int p_column) const;
  157. void set_text(int p_column, String p_text);
  158. String get_text(int p_column) const;
  159. void set_suffix(int p_column, String p_suffix);
  160. String get_suffix(int p_column) const;
  161. void set_icon(int p_column, const Ref<Texture> &p_icon);
  162. Ref<Texture> get_icon(int p_column) const;
  163. void set_icon_region(int p_column, const Rect2 &p_icon_region);
  164. Rect2 get_icon_region(int p_column) const;
  165. void set_icon_color(int p_column, const Color &p_icon_color);
  166. Color get_icon_color(int p_column) const;
  167. void set_icon_max_width(int p_column, int p_max);
  168. int get_icon_max_width(int p_column) const;
  169. void add_button(int p_column, const Ref<Texture> &p_button, int p_id = -1, bool p_disabled = false, const String &p_tooltip = "");
  170. int get_button_count(int p_column) const;
  171. Ref<Texture> get_button(int p_column, int p_idx) const;
  172. int get_button_id(int p_column, int p_idx) const;
  173. void erase_button(int p_column, int p_idx);
  174. int get_button_by_id(int p_column, int p_id) const;
  175. bool is_button_disabled(int p_column, int p_idx) const;
  176. void set_button(int p_column, int p_idx, const Ref<Texture> &p_button);
  177. void set_button_color(int p_column, int p_idx, const Color &p_color);
  178. /* range works for mode number or mode combo */
  179. void set_range(int p_column, double p_value);
  180. double get_range(int p_column) const;
  181. void set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp = false);
  182. void get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const;
  183. bool is_range_exponential(int p_column) const;
  184. void set_metadata(int p_column, const Variant &p_meta);
  185. Variant get_metadata(int p_column) const;
  186. void set_custom_draw(int p_column, Object *p_object, const StringName &p_callback);
  187. void set_collapsed(bool p_collapsed);
  188. bool is_collapsed();
  189. void set_custom_minimum_height(int p_height);
  190. int get_custom_minimum_height() const;
  191. TreeItem *get_prev();
  192. TreeItem *get_next();
  193. TreeItem *get_parent();
  194. TreeItem *get_children();
  195. TreeItem *get_prev_visible();
  196. TreeItem *get_next_visible();
  197. void remove_child(TreeItem *p_item);
  198. void set_selectable(int p_column, bool p_selectable);
  199. bool is_selectable(int p_column) const;
  200. bool is_selected(int p_column);
  201. void select(int p_column);
  202. void deselect(int p_column);
  203. void set_as_cursor(int p_column);
  204. void set_editable(int p_column, bool p_editable);
  205. bool is_editable(int p_column);
  206. void set_custom_color(int p_column, const Color &p_color);
  207. Color get_custom_color(int p_column) const;
  208. void clear_custom_color(int p_column);
  209. void set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline = false);
  210. void clear_custom_bg_color(int p_column);
  211. Color get_custom_bg_color(int p_column) const;
  212. void set_custom_as_button(int p_column, bool p_button);
  213. bool is_custom_set_as_button(int p_column) const;
  214. void set_tooltip(int p_column, const String &p_tooltip);
  215. String get_tooltip(int p_column) const;
  216. void clear_children();
  217. void set_text_align(int p_column, TextAlign p_align);
  218. TextAlign get_text_align(int p_column) const;
  219. void set_expand_right(int p_column, bool p_enable);
  220. bool get_expand_right(int p_column) const;
  221. void move_to_top();
  222. void move_to_bottom();
  223. void set_disable_folding(bool p_disable);
  224. bool is_folding_disabled() const;
  225. ~TreeItem();
  226. };
  227. VARIANT_ENUM_CAST(TreeItem::TreeCellMode);
  228. VARIANT_ENUM_CAST(TreeItem::TextAlign);
  229. class Tree : public Control {
  230. GDCLASS(Tree, Control);
  231. public:
  232. enum SelectMode {
  233. SELECT_SINGLE,
  234. SELECT_ROW,
  235. SELECT_MULTI
  236. };
  237. enum DropModeFlags {
  238. DROP_MODE_DISABLED = 0,
  239. DROP_MODE_ON_ITEM = 1,
  240. DROP_MODE_INBETWEEN = 2
  241. };
  242. private:
  243. friend class TreeItem;
  244. TreeItem *root;
  245. TreeItem *popup_edited_item;
  246. TreeItem *selected_item;
  247. TreeItem *edited_item;
  248. TreeItem *drop_mode_over;
  249. int drop_mode_section;
  250. TreeItem *single_select_defer;
  251. int single_select_defer_column;
  252. int pressed_button;
  253. bool pressing_for_editor;
  254. String pressing_for_editor_text;
  255. Vector2 pressing_pos;
  256. Rect2 pressing_item_rect;
  257. float range_drag_base;
  258. bool range_drag_enabled;
  259. Vector2 range_drag_capture_pos;
  260. //TreeItem *cursor_item;
  261. //int cursor_column;
  262. Rect2 custom_popup_rect;
  263. int edited_col;
  264. int selected_col;
  265. int popup_edited_item_col;
  266. bool hide_root;
  267. SelectMode select_mode;
  268. int blocked;
  269. int drop_mode_flags;
  270. struct ColumnInfo {
  271. int min_width;
  272. bool expand;
  273. String title;
  274. ColumnInfo() {
  275. min_width = 1;
  276. expand = true;
  277. }
  278. };
  279. bool show_column_titles;
  280. LineEdit *text_editor;
  281. HSlider *value_editor;
  282. bool updating_value_editor;
  283. int64_t focus_in_id;
  284. PopupMenu *popup_menu;
  285. Vector<ColumnInfo> columns;
  286. Timer *range_click_timer;
  287. TreeItem *range_item_last;
  288. bool range_up_last;
  289. void _range_click_timeout();
  290. int compute_item_height(TreeItem *p_item) const;
  291. int get_item_height(TreeItem *p_item) const;
  292. //void draw_item_text(String p_text,const Ref<Texture>& p_icon,int p_icon_max_w,bool p_tool,Rect2i p_rect,const Color& p_color);
  293. void draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color);
  294. int draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item);
  295. void select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev = NULL, bool *r_in_range = NULL, bool p_force_deselect = false);
  296. 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);
  297. void text_editor_enter(String p_text);
  298. void _text_editor_modal_close();
  299. void value_editor_changed(double p_value);
  300. void popup_select(int p_option);
  301. void _gui_input(Ref<InputEvent> p_event);
  302. void _notification(int p_what);
  303. Size2 get_minimum_size() const;
  304. void item_edited(int p_column, TreeItem *p_item, bool p_lmb = true);
  305. void item_changed(int p_column, TreeItem *p_item);
  306. void item_selected(int p_column, TreeItem *p_item);
  307. void item_deselected(int p_column, TreeItem *p_item);
  308. void propagate_set_columns(TreeItem *p_item);
  309. struct Cache {
  310. Ref<Font> font;
  311. Ref<Font> tb_font;
  312. Ref<StyleBox> bg;
  313. Ref<StyleBox> selected;
  314. Ref<StyleBox> selected_focus;
  315. Ref<StyleBox> cursor;
  316. Ref<StyleBox> cursor_unfocus;
  317. Ref<StyleBox> button_pressed;
  318. Ref<StyleBox> title_button;
  319. Ref<StyleBox> title_button_hover;
  320. Ref<StyleBox> title_button_pressed;
  321. Ref<StyleBox> custom_button;
  322. Ref<StyleBox> custom_button_hover;
  323. Ref<StyleBox> custom_button_pressed;
  324. Color title_button_color;
  325. Ref<Texture> checked;
  326. Ref<Texture> unchecked;
  327. Ref<Texture> arrow_collapsed;
  328. Ref<Texture> arrow;
  329. Ref<Texture> select_arrow;
  330. Ref<Texture> select_option;
  331. Ref<Texture> updown;
  332. Color font_color;
  333. Color font_color_selected;
  334. Color guide_color;
  335. Color drop_position_color;
  336. Color relationship_line_color;
  337. Color custom_button_font_highlight;
  338. int hseparation;
  339. int vseparation;
  340. int item_margin;
  341. int guide_width;
  342. int button_margin;
  343. Point2 offset;
  344. int draw_relationship_lines;
  345. int draw_guides;
  346. int scroll_border;
  347. int scroll_speed;
  348. enum ClickType {
  349. CLICK_NONE,
  350. CLICK_TITLE,
  351. CLICK_BUTTON,
  352. };
  353. ClickType click_type;
  354. ClickType hover_type;
  355. int click_index;
  356. int click_id;
  357. TreeItem *click_item;
  358. int click_column;
  359. int hover_index;
  360. Point2 click_pos;
  361. TreeItem *hover_item;
  362. int hover_cell;
  363. Point2i text_editor_position;
  364. } cache;
  365. int _get_title_button_height() const;
  366. void _scroll_moved(float p_value);
  367. HScrollBar *h_scroll;
  368. VScrollBar *v_scroll;
  369. Size2 get_internal_min_size() const;
  370. void update_cache();
  371. void update_scrollbars();
  372. Rect2 search_item_rect(TreeItem *p_from, TreeItem *p_item);
  373. //Rect2 get_item_rect(TreeItem *p_item);
  374. uint64_t last_keypress;
  375. String incr_search;
  376. bool cursor_can_exit_tree;
  377. void _do_incr_search(const String &p_add);
  378. TreeItem *_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards = false);
  379. TreeItem *_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const;
  380. /* float drag_speed;
  381. float drag_accum;
  382. float last_drag_accum;
  383. float last_drag_time;
  384. float time_since_motion;*/
  385. float drag_speed;
  386. float drag_from;
  387. float drag_accum;
  388. Vector2 last_speed;
  389. bool drag_touching;
  390. bool drag_touching_deaccel;
  391. bool click_handled;
  392. bool allow_rmb_select;
  393. bool scrolling;
  394. bool allow_reselect;
  395. bool force_edit_checkbox_only_on_checkbox;
  396. bool hide_folding;
  397. int _count_selected_items(TreeItem *p_from) const;
  398. void _go_left();
  399. void _go_right();
  400. void _go_down();
  401. void _go_up();
  402. protected:
  403. static void _bind_methods();
  404. //bind helpers
  405. TreeItem *_create_item(Object *p_parent, int p_idx = -1) {
  406. return create_item(Object::cast_to<TreeItem>(p_parent), p_idx);
  407. }
  408. TreeItem *_get_next_selected(Object *p_item) {
  409. return get_next_selected(Object::cast_to<TreeItem>(p_item));
  410. }
  411. Rect2 _get_item_rect(Object *p_item, int p_column) const {
  412. return get_item_rect(Object::cast_to<TreeItem>(p_item), p_column);
  413. }
  414. public:
  415. virtual String get_tooltip(const Point2 &p_pos) const;
  416. TreeItem *get_item_at_position(const Point2 &p_pos) const;
  417. int get_column_at_position(const Point2 &p_pos) const;
  418. int get_drop_section_at_position(const Point2 &p_pos) const;
  419. void clear();
  420. TreeItem *create_item(TreeItem *p_parent = 0, int p_idx = -1);
  421. TreeItem *get_root();
  422. TreeItem *get_last_item();
  423. void set_column_min_width(int p_column, int p_min_width);
  424. void set_column_expand(int p_column, bool p_expand);
  425. int get_column_width(int p_column) const;
  426. void set_hide_root(bool p_enabled);
  427. bool is_root_hidden() const;
  428. TreeItem *get_next_selected(TreeItem *p_item);
  429. TreeItem *get_selected() const;
  430. int get_selected_column() const;
  431. int get_pressed_button() const;
  432. void set_select_mode(SelectMode p_mode);
  433. SelectMode get_select_mode() const;
  434. void deselect_all();
  435. bool is_anything_selected();
  436. void set_columns(int p_columns);
  437. int get_columns() const;
  438. void set_column_title(int p_column, const String &p_title);
  439. String get_column_title(int p_column) const;
  440. void set_column_titles_visible(bool p_show);
  441. bool are_column_titles_visible() const;
  442. TreeItem *get_edited() const;
  443. int get_edited_column() const;
  444. void ensure_cursor_is_visible();
  445. Rect2 get_custom_popup_rect() const;
  446. int get_item_offset(TreeItem *p_item) const;
  447. Rect2 get_item_rect(TreeItem *p_item, int p_column = -1) const;
  448. bool edit_selected();
  449. TreeItem *search_item_text(const String &p_find, int *r_col = NULL, bool p_selectable = false);
  450. Point2 get_scroll() const;
  451. void scroll_to_item(TreeItem *p_item);
  452. void set_cursor_can_exit_tree(bool p_enable);
  453. bool can_cursor_exit_tree() const;
  454. VScrollBar *get_vscroll_bar() { return v_scroll; }
  455. void set_hide_folding(bool p_hide);
  456. bool is_folding_hidden() const;
  457. void set_drop_mode_flags(int p_flags);
  458. int get_drop_mode_flags() const;
  459. void set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable);
  460. bool get_edit_checkbox_cell_only_when_checkbox_is_pressed() const;
  461. void set_allow_rmb_select(bool p_allow);
  462. bool get_allow_rmb_select() const;
  463. void set_allow_reselect(bool p_allow);
  464. bool get_allow_reselect() const;
  465. Tree();
  466. ~Tree();
  467. };
  468. VARIANT_ENUM_CAST(Tree::SelectMode);
  469. VARIANT_ENUM_CAST(Tree::DropModeFlags);
  470. #endif