tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*************************************************************************/
  2. /* tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "core/helper/value_evaluator.h"
  33. #include "scene/gui/control.h"
  34. #include "scene/gui/line_edit.h"
  35. #include "scene/gui/popup_menu.h"
  36. #include "scene/gui/scroll_bar.h"
  37. #include "scene/gui/slider.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. class Tree;
  42. class TreeItem : public Object {
  43. GDCLASS(TreeItem, Object);
  44. public:
  45. enum TreeCellMode {
  46. CELL_MODE_STRING, ///< just a string
  47. CELL_MODE_CHECK, ///< string + check
  48. CELL_MODE_RANGE, ///< Contains a range
  49. CELL_MODE_RANGE_EXPRESSION, ///< Contains a range
  50. CELL_MODE_ICON, ///< Contains an icon, not editable
  51. CELL_MODE_CUSTOM, ///< Contains a custom value, show a string, and an edit button
  52. };
  53. enum TextAlign {
  54. ALIGN_LEFT,
  55. ALIGN_CENTER,
  56. ALIGN_RIGHT
  57. };
  58. private:
  59. friend class Tree;
  60. struct Cell {
  61. TreeCellMode mode;
  62. Ref<Texture> icon;
  63. Rect2i icon_region;
  64. String text;
  65. String suffix;
  66. double min, max, step, val;
  67. int icon_max_w;
  68. bool expr;
  69. bool checked;
  70. bool editable;
  71. bool selected;
  72. bool selectable;
  73. bool custom_color;
  74. Color color;
  75. bool custom_bg_color;
  76. bool custom_bg_outline;
  77. Color bg_color;
  78. bool custom_button;
  79. bool expand_right;
  80. Color icon_color;
  81. TextAlign text_align;
  82. Variant meta;
  83. String tooltip;
  84. ObjectID custom_draw_obj;
  85. StringName custom_draw_callback;
  86. struct Button {
  87. int id;
  88. bool disabled;
  89. Ref<Texture> texture;
  90. Color color;
  91. String tooltip;
  92. Button() {
  93. id = 0;
  94. disabled = false;
  95. color = Color(1, 1, 1, 1);
  96. tooltip = "";
  97. }
  98. };
  99. Vector<Button> buttons;
  100. Cell() {
  101. custom_draw_obj = 0;
  102. custom_button = false;
  103. mode = TreeItem::CELL_MODE_STRING;
  104. min = 0;
  105. max = 100;
  106. step = 1;
  107. val = 0;
  108. checked = false;
  109. editable = false;
  110. selected = false;
  111. selectable = true;
  112. custom_color = false;
  113. custom_bg_color = false;
  114. expr = false;
  115. icon_max_w = 0;
  116. text_align = ALIGN_LEFT;
  117. expand_right = false;
  118. icon_color = Color(1, 1, 1);
  119. }
  120. Size2 get_icon_size() const;
  121. void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2(), const Color &p_color = Color()) const;
  122. };
  123. Vector<Cell> cells;
  124. bool collapsed; // wont show childs
  125. bool disable_folding;
  126. int custom_min_height;
  127. TreeItem *parent; // parent item
  128. TreeItem *next; // next in list
  129. TreeItem *childs; //child items
  130. Tree *tree; //tree (for reference)
  131. TreeItem(Tree *p_tree);
  132. void _changed_notify(int p_cell);
  133. void _changed_notify();
  134. void _cell_selected(int p_cell);
  135. void _cell_deselected(int p_cell);
  136. protected:
  137. static void _bind_methods();
  138. //bind helpers
  139. Dictionary _get_range_config(int p_column) {
  140. Dictionary d;
  141. double min, max, step;
  142. get_range_config(p_column, min, max, step);
  143. d["min"] = min;
  144. d["max"] = max;
  145. d["step"] = step;
  146. d["expr"] = false;
  147. return d;
  148. }
  149. void _remove_child(Object *p_child) {
  150. remove_child(Object::cast_to<TreeItem>(p_child));
  151. }
  152. public:
  153. /* cell mode */
  154. void set_cell_mode(int p_column, TreeCellMode p_mode);
  155. TreeCellMode get_cell_mode(int p_column) const;
  156. /* check mode */
  157. void set_checked(int p_column, bool p_checked);
  158. bool is_checked(int p_column) const;
  159. void set_text(int p_column, String p_text);
  160. String get_text(int p_column) const;
  161. void set_suffix(int p_column, String p_suffix);
  162. String get_suffix(int p_column) const;
  163. void set_icon(int p_column, const Ref<Texture> &p_icon);
  164. Ref<Texture> get_icon(int p_column) const;
  165. void set_icon_region(int p_column, const Rect2 &p_icon_region);
  166. Rect2 get_icon_region(int p_column) const;
  167. void set_icon_color(int p_column, const Color &p_icon_color);
  168. Color get_icon_color(int p_column) const;
  169. void set_icon_max_width(int p_column, int p_max);
  170. int get_icon_max_width(int p_column) const;
  171. void add_button(int p_column, const Ref<Texture> &p_button, int p_id = -1, bool p_disabled = false, const String &p_tooltip = "");
  172. int get_button_count(int p_column) const;
  173. Ref<Texture> get_button(int p_column, int p_idx) const;
  174. int get_button_id(int p_column, int p_idx) const;
  175. void erase_button(int p_column, int p_idx);
  176. int get_button_by_id(int p_column, int p_id) const;
  177. bool is_button_disabled(int p_column, int p_idx) const;
  178. void set_button(int p_column, int p_idx, const Ref<Texture> &p_button);
  179. void set_button_color(int p_column, int p_idx, const Color &p_color);
  180. /* range works for mode number or mode combo */
  181. void set_range(int p_column, double p_value);
  182. double get_range(int p_column) const;
  183. void set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp = false);
  184. void get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const;
  185. bool is_range_exponential(int p_column) const;
  186. void set_metadata(int p_column, const Variant &p_meta);
  187. Variant get_metadata(int p_column) const;
  188. void set_custom_draw(int p_column, Object *p_object, const StringName &p_callback);
  189. void set_collapsed(bool p_collapsed);
  190. bool is_collapsed();
  191. void set_custom_minimum_height(int p_height);
  192. int get_custom_minimum_height() const;
  193. TreeItem *get_prev();
  194. TreeItem *get_next();
  195. TreeItem *get_parent();
  196. TreeItem *get_children();
  197. TreeItem *get_prev_visible();
  198. TreeItem *get_next_visible();
  199. void remove_child(TreeItem *p_item);
  200. void set_selectable(int p_column, bool p_selectable);
  201. bool is_selectable(int p_column) const;
  202. bool is_selected(int p_column);
  203. void select(int p_column);
  204. void deselect(int p_column);
  205. void set_as_cursor(int p_column);
  206. void set_editable(int p_column, bool p_editable);
  207. bool is_editable(int p_column);
  208. void set_custom_color(int p_column, const Color &p_color);
  209. Color get_custom_color(int p_column) const;
  210. void clear_custom_color(int p_column);
  211. void set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline = false);
  212. void clear_custom_bg_color(int p_column);
  213. Color get_custom_bg_color(int p_column) const;
  214. void set_custom_as_button(int p_column, bool p_button);
  215. bool is_custom_set_as_button(int p_column) const;
  216. void set_tooltip(int p_column, const String &p_tooltip);
  217. String get_tooltip(int p_column) const;
  218. void clear_children();
  219. void set_text_align(int p_column, TextAlign p_align);
  220. TextAlign get_text_align(int p_column) const;
  221. void set_expand_right(int p_column, bool p_enable);
  222. bool get_expand_right(int p_column) const;
  223. void move_to_top();
  224. void move_to_bottom();
  225. void set_disable_folding(bool p_disable);
  226. bool is_folding_disabled() const;
  227. ~TreeItem();
  228. };
  229. VARIANT_ENUM_CAST(TreeItem::TreeCellMode);
  230. VARIANT_ENUM_CAST(TreeItem::TextAlign);
  231. class Tree : public Control {
  232. GDCLASS(Tree, Control);
  233. public:
  234. enum SelectMode {
  235. SELECT_SINGLE,
  236. SELECT_ROW,
  237. SELECT_MULTI
  238. };
  239. enum DropModeFlags {
  240. DROP_MODE_DISABLED = 0,
  241. DROP_MODE_ON_ITEM = 1,
  242. DROP_MODE_INBETWEEN = 2
  243. };
  244. private:
  245. friend class TreeItem;
  246. TreeItem *root;
  247. TreeItem *popup_edited_item;
  248. TreeItem *selected_item;
  249. TreeItem *edited_item;
  250. TreeItem *drop_mode_over;
  251. int drop_mode_section;
  252. TreeItem *single_select_defer;
  253. int single_select_defer_column;
  254. int pressed_button;
  255. bool pressing_for_editor;
  256. String pressing_for_editor_text;
  257. Vector2 pressing_pos;
  258. Rect2 pressing_item_rect;
  259. float range_drag_base;
  260. bool range_drag_enabled;
  261. Vector2 range_drag_capture_pos;
  262. //TreeItem *cursor_item;
  263. //int cursor_column;
  264. Rect2 custom_popup_rect;
  265. int edited_col;
  266. int selected_col;
  267. int popup_edited_item_col;
  268. bool hide_root;
  269. SelectMode select_mode;
  270. int blocked;
  271. int drop_mode_flags;
  272. struct ColumnInfo {
  273. int min_width;
  274. bool expand;
  275. String title;
  276. ColumnInfo() {
  277. min_width = 1;
  278. expand = true;
  279. }
  280. };
  281. bool show_column_titles;
  282. LineEdit *text_editor;
  283. HSlider *value_editor;
  284. bool updating_value_editor;
  285. uint32_t focus_in_id;
  286. PopupMenu *popup_menu;
  287. Vector<ColumnInfo> columns;
  288. Timer *range_click_timer;
  289. TreeItem *range_item_last;
  290. bool range_up_last;
  291. void _range_click_timeout();
  292. int compute_item_height(TreeItem *p_item) const;
  293. int get_item_height(TreeItem *p_item) const;
  294. //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);
  295. void draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color);
  296. int draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item);
  297. 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);
  298. 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);
  299. void text_editor_enter(String p_text);
  300. void _text_editor_modal_close();
  301. void value_editor_changed(double p_value);
  302. void popup_select(int p_option);
  303. void _gui_input(Ref<InputEvent> p_event);
  304. void _notification(int p_what);
  305. Size2 get_minimum_size() const;
  306. void item_edited(int p_column, TreeItem *p_item, bool p_lmb = true);
  307. void item_changed(int p_column, TreeItem *p_item);
  308. void item_selected(int p_column, TreeItem *p_item);
  309. void item_deselected(int p_column, TreeItem *p_item);
  310. void propagate_set_columns(TreeItem *p_item);
  311. struct Cache {
  312. Ref<Font> font;
  313. Ref<Font> tb_font;
  314. Ref<StyleBox> bg;
  315. Ref<StyleBox> selected;
  316. Ref<StyleBox> selected_focus;
  317. Ref<StyleBox> cursor;
  318. Ref<StyleBox> cursor_unfocus;
  319. Ref<StyleBox> button_pressed;
  320. Ref<StyleBox> title_button;
  321. Ref<StyleBox> title_button_hover;
  322. Ref<StyleBox> title_button_pressed;
  323. Ref<StyleBox> custom_button;
  324. Ref<StyleBox> custom_button_hover;
  325. Ref<StyleBox> custom_button_pressed;
  326. Color title_button_color;
  327. Ref<Texture> checked;
  328. Ref<Texture> unchecked;
  329. Ref<Texture> arrow_collapsed;
  330. Ref<Texture> arrow;
  331. Ref<Texture> select_arrow;
  332. Ref<Texture> select_option;
  333. Ref<Texture> updown;
  334. Color font_color;
  335. Color font_color_selected;
  336. Color guide_color;
  337. Color drop_position_color;
  338. Color relationship_line_color;
  339. Color custom_button_font_highlight;
  340. int hseparation;
  341. int vseparation;
  342. int item_margin;
  343. int guide_width;
  344. int button_margin;
  345. Point2 offset;
  346. int draw_relationship_lines;
  347. int scroll_border;
  348. int scroll_speed;
  349. enum ClickType {
  350. CLICK_NONE,
  351. CLICK_TITLE,
  352. CLICK_BUTTON,
  353. };
  354. ClickType click_type;
  355. ClickType hover_type;
  356. int click_index;
  357. int click_id;
  358. TreeItem *click_item;
  359. int click_column;
  360. int hover_index;
  361. Point2 click_pos;
  362. TreeItem *hover_item;
  363. int hover_cell;
  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. ValueEvaluator *evaluator;
  398. int _count_selected_items(TreeItem *p_from) const;
  399. protected:
  400. static void _bind_methods();
  401. //bind helpers
  402. Object *_create_item(Object *p_parent) {
  403. return create_item(Object::cast_to<TreeItem>(p_parent));
  404. }
  405. TreeItem *_get_next_selected(Object *p_item) {
  406. return get_next_selected(Object::cast_to<TreeItem>(p_item));
  407. }
  408. Rect2 _get_item_rect(Object *p_item, int p_column) const {
  409. return get_item_rect(Object::cast_to<TreeItem>(p_item), p_column);
  410. }
  411. public:
  412. virtual String get_tooltip(const Point2 &p_pos) const;
  413. TreeItem *get_item_at_position(const Point2 &p_pos) const;
  414. int get_column_at_position(const Point2 &p_pos) const;
  415. int get_drop_section_at_position(const Point2 &p_pos) const;
  416. void clear();
  417. TreeItem *create_item(TreeItem *p_parent = 0);
  418. TreeItem *get_root();
  419. TreeItem *get_last_item();
  420. void set_column_min_width(int p_column, int p_min_width);
  421. void set_column_expand(int p_column, bool p_expand);
  422. int get_column_width(int p_column) const;
  423. void set_hide_root(bool p_enabled);
  424. TreeItem *get_next_selected(TreeItem *p_item);
  425. TreeItem *get_selected() const;
  426. int get_selected_column() const;
  427. int get_pressed_button() const;
  428. void set_select_mode(SelectMode p_mode);
  429. void set_columns(int p_columns);
  430. int get_columns() const;
  431. void set_column_title(int p_column, const String &p_title);
  432. String get_column_title(int p_column) const;
  433. void set_column_titles_visible(bool p_show);
  434. bool are_column_titles_visible() const;
  435. TreeItem *get_edited() const;
  436. int get_edited_column() const;
  437. void ensure_cursor_is_visible();
  438. Rect2 get_custom_popup_rect() const;
  439. int get_item_offset(TreeItem *p_item) const;
  440. Rect2 get_item_rect(TreeItem *p_item, int p_column = -1) const;
  441. bool edit_selected();
  442. TreeItem *search_item_text(const String &p_find, int *r_col = NULL, bool p_selectable = false);
  443. Point2 get_scroll() const;
  444. void scroll_to_item(TreeItem *p_item);
  445. void set_cursor_can_exit_tree(bool p_enable);
  446. bool can_cursor_exit_tree() const;
  447. VScrollBar *get_vscroll_bar() { return v_scroll; }
  448. void set_hide_folding(bool p_hide);
  449. bool is_folding_hidden() const;
  450. void set_drop_mode_flags(int p_flags);
  451. int get_drop_mode_flags() const;
  452. void set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable);
  453. bool get_edit_checkbox_cell_only_when_checkbox_is_pressed() const;
  454. void set_allow_rmb_select(bool p_allow);
  455. bool get_allow_rmb_select() const;
  456. void set_allow_reselect(bool p_allow);
  457. bool get_allow_reselect() const;
  458. void set_value_evaluator(ValueEvaluator *p_evaluator);
  459. Tree();
  460. ~Tree();
  461. };
  462. VARIANT_ENUM_CAST(Tree::SelectMode);
  463. VARIANT_ENUM_CAST(Tree::DropModeFlags);
  464. #endif