tree.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*************************************************************************/
  2. /* tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef TREE_H
  30. #define TREE_H
  31. #include "scene/gui/control.h"
  32. #include "scene/gui/popup_menu.h"
  33. #include "scene/gui/line_edit.h"
  34. #include "scene/gui/scroll_bar.h"
  35. #include "scene/gui/slider.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. class Tree;
  40. class TreeItem : public Object {
  41. OBJ_TYPE(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 a 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;
  54. Ref<Texture> icon;
  55. Rect2i icon_region;
  56. String text;
  57. double min,max,step,val;
  58. int icon_max_w;
  59. bool expr;
  60. bool checked;
  61. bool editable;
  62. bool selected;
  63. bool selectable;
  64. bool custom_color;
  65. Color color;
  66. bool custom_bg_color;
  67. Color bg_color;
  68. Variant meta;
  69. String tooltip;
  70. ObjectID custom_draw_obj;
  71. StringName custom_draw_callback;
  72. struct Button {
  73. int id;
  74. bool disabled;
  75. Ref<Texture> texture;
  76. Button() { id=0; disabled=false; }
  77. };
  78. Vector< Button > buttons;
  79. Cell() {
  80. custom_draw_obj=0;
  81. mode=TreeItem::CELL_MODE_STRING;
  82. min=0;
  83. max=100;
  84. step=1;
  85. val=0;
  86. checked=false;
  87. editable=false;
  88. selected=false;
  89. selectable=true;
  90. custom_color=false;
  91. custom_bg_color=false;
  92. expr=false;
  93. icon_max_w=0;
  94. }
  95. Size2 get_icon_size() const;
  96. void draw_icon(const RID& p_where, const Point2& p_pos, const Size2& p_size=Size2()) const;
  97. };
  98. Vector<Cell> cells;
  99. bool collapsed; // wont show childs
  100. TreeItem *parent; // parent item
  101. TreeItem *next; // next in list
  102. TreeItem *childs; //child items
  103. Tree *tree; //tree (for reference)
  104. TreeItem(Tree *p_tree);
  105. void _changed_notify(int p_cell);
  106. void _changed_notify();
  107. void _cell_selected(int p_cell);
  108. void _cell_deselected(int p_cell);
  109. protected:
  110. static void _bind_methods();
  111. //bind helpers
  112. Dictionary _get_range_config( int p_column ) {
  113. Dictionary d;
  114. double min,max,step;
  115. get_range_config(p_column,min,max,step);
  116. d["min"]=min;
  117. d["max"]=max;
  118. d["step"]=step;
  119. d["expr"]=false;
  120. return d;
  121. }
  122. void _remove_child(Object *p_child) { remove_child( p_child->cast_to<TreeItem>() ); }
  123. public:
  124. /* cell mode */
  125. void set_cell_mode( int p_column, TreeCellMode p_mode );
  126. TreeCellMode get_cell_mode( int p_column ) const;
  127. /* check mode */
  128. void set_checked(int p_column,bool p_checked);
  129. bool is_checked(int p_column) const;
  130. void set_text(int p_column,String p_text);
  131. String get_text(int p_column) const;
  132. void set_icon(int p_column,const Ref<Texture>& p_icon);
  133. Ref<Texture> get_icon(int p_column) const;
  134. void set_icon_region(int p_column,const Rect2& p_icon_region);
  135. Rect2 get_icon_region(int p_column) const;
  136. void set_icon_max_width(int p_column,int p_max);
  137. int get_icon_max_width(int p_column) const;
  138. void add_button(int p_column,const Ref<Texture>& p_button,int p_id=-1,bool p_disabled=false);
  139. int get_button_count(int p_column) const;
  140. Ref<Texture> get_button(int p_column,int p_idx) const;
  141. int get_button_id(int p_column,int p_idx) const;
  142. void erase_button(int p_column,int p_idx);
  143. int get_button_by_id(int p_column,int p_id) const;
  144. bool is_button_disabled(int p_column,int p_idx) const;
  145. void set_button(int p_column,int p_idx,const Ref<Texture>& p_button);
  146. /* range works for mode number or mode combo */
  147. void set_range(int p_column,double p_value);
  148. double get_range(int p_column) const;
  149. void set_range_config(int p_column,double p_min,double p_max,double p_step,bool p_exp=false);
  150. void get_range_config(int p_column,double& r_min,double& r_max,double &r_step) const;
  151. bool is_range_exponential(int p_column) const;
  152. void set_metadata(int p_column,const Variant& p_meta);
  153. Variant get_metadata(int p_column) const;
  154. void set_custom_draw(int p_column,Object *p_object,const StringName& p_callback);
  155. void set_collapsed(bool p_collapsed);
  156. bool is_collapsed();
  157. TreeItem *get_prev();
  158. TreeItem *get_next();
  159. TreeItem *get_parent();
  160. TreeItem *get_children();
  161. TreeItem *get_prev_visible();
  162. TreeItem *get_next_visible();
  163. void remove_child(TreeItem *p_item);
  164. void set_selectable(int p_column,bool p_selectable);
  165. bool is_selectable(int p_column) const;
  166. bool is_selected(int p_column);
  167. void select(int p_column);
  168. void deselect(int p_column);
  169. void set_as_cursor(int p_column);
  170. void set_editable(int p_column,bool p_editable);
  171. bool is_editable(int p_column);
  172. void set_custom_color(int p_column,const Color& p_color);
  173. void clear_custom_color(int p_column);
  174. void set_custom_bg_color(int p_column,const Color& p_color);
  175. void clear_custom_bg_color(int p_column);
  176. Color get_custom_bg_color(int p_column) const;
  177. void set_tooltip(int p_column, const String& p_tooltip);
  178. String get_tooltip(int p_column) const;
  179. void clear_children();
  180. void move_to_top();
  181. void move_to_bottom();
  182. ~TreeItem();
  183. };
  184. VARIANT_ENUM_CAST( TreeItem::TreeCellMode );
  185. class Tree : public Control {
  186. OBJ_TYPE( Tree, Control );
  187. public:
  188. enum SelectMode {
  189. SELECT_SINGLE,
  190. SELECT_ROW,
  191. SELECT_MULTI
  192. };
  193. private:
  194. friend class TreeItem;
  195. TreeItem *root;
  196. TreeItem *popup_edited_item;
  197. TreeItem *selected_item;
  198. TreeItem *edited_item;
  199. int pressed_button;
  200. bool pressing_for_editor;
  201. String pressing_for_editor_text;
  202. Vector2 pressing_pos;
  203. Rect2 pressing_item_rect;
  204. float range_drag_base;
  205. bool range_drag_enabled;
  206. Vector2 range_drag_capture_pos;
  207. //TreeItem *cursor_item;
  208. //int cursor_column;
  209. Rect2 custom_popup_rect;
  210. int edited_col;
  211. int selected_col;
  212. int popup_edited_item_col;
  213. bool hide_root;
  214. SelectMode select_mode;
  215. int blocked;
  216. struct ColumnInfo {
  217. int min_width;
  218. bool expand;
  219. String title;
  220. ColumnInfo() { min_width=1; expand=true; }
  221. };
  222. bool show_column_titles;
  223. LineEdit *text_editor;
  224. HSlider *value_editor;
  225. bool updating_value_editor;
  226. uint32_t focus_in_id;
  227. PopupMenu *popup_menu;
  228. Vector<ColumnInfo> columns;
  229. Timer *range_click_timer;
  230. TreeItem *range_item_last;
  231. bool range_up_last;
  232. void _range_click_timeout();
  233. int compute_item_height(TreeItem *p_item) const;
  234. int get_item_height(TreeItem *p_item) const;
  235. // 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);
  236. void draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color);
  237. int draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item);
  238. void select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev=NULL,bool *r_in_range=NULL);
  239. int propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_doubleclick,TreeItem *p_item,int p_button,const InputModifierState& p_mod);
  240. void text_editor_enter(String p_text);
  241. void _text_editor_modal_close();
  242. void value_editor_changed(double p_value);
  243. void popup_select(int p_option);
  244. void _input_event(InputEvent p_event);
  245. void _notification(int p_what);
  246. Size2 get_minimum_size() const;
  247. void item_edited(int p_column,TreeItem *p_item);
  248. void item_changed(int p_column,TreeItem *p_item);
  249. void item_selected(int p_column,TreeItem *p_item);
  250. void item_deselected(int p_column,TreeItem *p_item);
  251. void propagate_set_columns(TreeItem *p_item);
  252. struct Cache {
  253. Ref<Font> font;
  254. Ref<Font> tb_font;
  255. Ref<StyleBox> bg;
  256. Ref<StyleBox> selected;
  257. Ref<StyleBox> selected_focus;
  258. Ref<StyleBox> cursor;
  259. Ref<StyleBox> cursor_unfocus;
  260. Ref<StyleBox> button_pressed;
  261. Ref<StyleBox> title_button;
  262. Ref<StyleBox> title_button_hover;
  263. Ref<StyleBox> title_button_pressed;
  264. Color title_button_color;
  265. Ref<Texture> checked;
  266. Ref<Texture> unchecked;
  267. Ref<Texture> arrow_collapsed;
  268. Ref<Texture> arrow;
  269. Ref<Texture> select_arrow;
  270. Ref<Texture> updown;
  271. Color font_color;
  272. Color font_color_selected;
  273. Color guide_color;
  274. int hseparation;
  275. int vseparation;
  276. int item_margin;
  277. int guide_width;
  278. int button_margin;
  279. Point2 offset;
  280. enum ClickType {
  281. CLICK_NONE,
  282. CLICK_TITLE,
  283. CLICK_BUTTON,
  284. };
  285. ClickType click_type;
  286. ClickType hover_type;
  287. int click_index;
  288. int click_id;
  289. TreeItem *click_item;
  290. int click_column;
  291. int hover_index;
  292. } cache;
  293. int _get_title_button_height() const;
  294. void _scroll_moved(float p_value);
  295. HScrollBar *h_scroll;
  296. VScrollBar *v_scroll;
  297. Size2 get_internal_min_size() const;
  298. void update_cache();
  299. void update_scrollbars();
  300. Rect2 search_item_rect(TreeItem *p_from, TreeItem *p_item);
  301. //Rect2 get_item_rect(TreeItem *p_item);
  302. uint64_t last_keypress;
  303. String incr_search;
  304. bool cursor_can_exit_tree;
  305. void _do_incr_search(const String& p_add);
  306. TreeItem* _search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards=false);
  307. TreeItem* _find_item_at_pos(TreeItem *p_current, const Point2& p_pos,int& r_column,int &h) const;
  308. /* float drag_speed;
  309. float drag_accum;
  310. float last_drag_accum;
  311. float last_drag_time;
  312. float time_since_motion;*/
  313. float drag_speed;
  314. float drag_from;
  315. float drag_accum;
  316. Vector2 last_speed;
  317. bool drag_touching;
  318. bool drag_touching_deaccel;
  319. bool click_handled;
  320. bool hide_folding;
  321. protected:
  322. static void _bind_methods();
  323. //bind helpers
  324. Object* _create_item(Object *p_parent) { return create_item(p_parent->cast_to<TreeItem>() ); }
  325. TreeItem *_get_next_selected(Object *p_item) { return get_next_selected(p_item->cast_to<TreeItem>() ); }
  326. Rect2 _get_item_rect(Object *p_item,int p_column) const { return get_item_rect(p_item->cast_to<TreeItem>(),p_column ); }
  327. public:
  328. virtual String get_tooltip(const Point2& p_pos) const;
  329. void clear();
  330. TreeItem* create_item(TreeItem *p_parent=0);
  331. TreeItem* get_root();
  332. TreeItem* get_last_item();
  333. void set_column_min_width(int p_column,int p_min_width);
  334. void set_column_expand(int p_column,bool p_expand);
  335. int get_column_width(int p_column) const;
  336. void set_hide_root(bool p_eanbled);
  337. TreeItem *get_next_selected( TreeItem* p_item);
  338. TreeItem *get_selected() const;
  339. int get_selected_column() const;
  340. int get_pressed_button() const;
  341. void set_select_mode(SelectMode p_mode);
  342. void set_columns(int p_columns);
  343. int get_columns() const;
  344. void set_column_title(int p_column,const String& p_title);
  345. String get_column_title(int p_column) const;
  346. void set_column_titles_visible(bool p_show);
  347. bool are_column_titles_visible() const;
  348. TreeItem *get_edited() const;
  349. int get_edited_column() const;
  350. void ensure_cursor_is_visible();
  351. Rect2 get_custom_popup_rect() const;
  352. int get_item_offset(TreeItem *p_item) const;
  353. Rect2 get_item_rect(TreeItem *p_item,int p_column=-1) const;
  354. bool edit_selected();
  355. TreeItem* search_item_text(const String& p_find,int *r_col=NULL,bool p_selectable=false);
  356. Point2 get_scroll() const;
  357. void set_cursor_can_exit_tree(bool p_enable);
  358. bool can_cursor_exit_tree() const;
  359. VScrollBar *get_vscroll_bar() { return v_scroll; }
  360. void set_hide_folding(bool p_hide);
  361. bool is_folding_hidden() const;
  362. Tree();
  363. ~Tree();
  364. };
  365. VARIANT_ENUM_CAST( Tree::SelectMode );
  366. #endif