tree.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*************************************************************************/
  2. /* tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 <[email protected]>
  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. Ref<Texture> texture;
  75. Button() { id=0; }
  76. };
  77. Vector< Button > buttons;
  78. Cell() {
  79. custom_draw_obj=0;
  80. mode=TreeItem::CELL_MODE_STRING;
  81. min=0;
  82. max=100;
  83. step=1;
  84. val=0;
  85. checked=false;
  86. editable=false;
  87. selected=false;
  88. selectable=true;
  89. custom_color=false;
  90. custom_bg_color=false;
  91. expr=false;
  92. icon_max_w=0;
  93. }
  94. Size2 get_icon_size() const;
  95. void draw_icon(const RID& p_where, const Point2& p_pos, const Size2& p_size=Size2()) const;
  96. };
  97. Vector<Cell> cells;
  98. bool collapsed; // wont show childs
  99. TreeItem *parent; // parent item
  100. TreeItem *next; // next in list
  101. TreeItem *childs; //child items
  102. Tree *tree; //tree (for reference)
  103. TreeItem(Tree *p_tree);
  104. void _changed_notify(int p_cell);
  105. void _changed_notify();
  106. void _cell_selected(int p_cell);
  107. void _cell_deselected(int p_cell);
  108. protected:
  109. static void _bind_methods();
  110. //bind helpers
  111. Dictionary _get_range_config( int p_column ) {
  112. Dictionary d;
  113. double min,max,step;
  114. get_range_config(p_column,min,max,step);
  115. d["min"]=min;
  116. d["max"]=max;
  117. d["step"]=step;
  118. d["expr"]=false;
  119. return d;
  120. }
  121. void _remove_child(Object *p_child) { remove_child( p_child->cast_to<TreeItem>() ); }
  122. public:
  123. /* cell mode */
  124. void set_cell_mode( int p_column, TreeCellMode p_mode );
  125. TreeCellMode get_cell_mode( int p_column ) const;
  126. /* check mode */
  127. void set_checked(int p_column,bool p_checked);
  128. bool is_checked(int p_column) const;
  129. void set_text(int p_column,String p_text);
  130. String get_text(int p_column) const;
  131. void set_icon(int p_column,const Ref<Texture>& p_icon);
  132. Ref<Texture> get_icon(int p_column) const;
  133. void set_icon_region(int p_column,const Rect2& p_icon_region);
  134. Rect2 get_icon_region(int p_column) const;
  135. void set_icon_max_width(int p_column,int p_max);
  136. int get_icon_max_width(int p_column) const;
  137. void add_button(int p_column,const Ref<Texture>& p_button,int p_id=-1);
  138. int get_button_count(int p_column) const;
  139. Ref<Texture> get_button(int p_column,int p_idx) const;
  140. int get_button_id(int p_column,int p_idx) const;
  141. void erase_button(int p_column,int p_idx);
  142. int get_button_by_id(int p_column,int p_id) const;
  143. void set_button(int p_column,int p_idx,const Ref<Texture>& p_button);
  144. /* range works for mode number or mode combo */
  145. void set_range(int p_column,double p_value);
  146. double get_range(int p_column) const;
  147. void set_range_config(int p_column,double p_min,double p_max,double p_step,bool p_exp=false);
  148. void get_range_config(int p_column,double& r_min,double& r_max,double &r_step) const;
  149. bool is_range_exponential(int p_column) const;
  150. void set_metadata(int p_column,const Variant& p_meta);
  151. Variant get_metadata(int p_column) const;
  152. void set_custom_draw(int p_column,Object *p_object,const StringName& p_callback);
  153. void set_collapsed(bool p_collapsed);
  154. bool is_collapsed();
  155. TreeItem *get_prev();
  156. TreeItem *get_next();
  157. TreeItem *get_parent();
  158. TreeItem *get_children();
  159. TreeItem *get_prev_visible();
  160. TreeItem *get_next_visible();
  161. void remove_child(TreeItem *p_item);
  162. void set_selectable(int p_column,bool p_selectable);
  163. bool is_selectable(int p_column) const;
  164. bool is_selected(int p_column);
  165. void select(int p_column);
  166. void deselect(int p_column);
  167. void set_as_cursor(int p_column);
  168. void set_editable(int p_column,bool p_editable);
  169. bool is_editable(int p_column);
  170. void set_custom_color(int p_column,const Color& p_color);
  171. void clear_custom_color(int p_column);
  172. void set_custom_bg_color(int p_column,const Color& p_color);
  173. void clear_custom_bg_color(int p_column);
  174. Color get_custom_bg_color(int p_column) const;
  175. void set_tooltip(int p_column, const String& p_tooltip);
  176. String get_tooltip(int p_column) const;
  177. void clear_children();
  178. void move_to_top();
  179. void move_to_bottom();
  180. ~TreeItem();
  181. };
  182. VARIANT_ENUM_CAST( TreeItem::TreeCellMode );
  183. class Tree : public Control {
  184. OBJ_TYPE( Tree, Control );
  185. public:
  186. enum SelectMode {
  187. SELECT_SINGLE,
  188. SELECT_ROW,
  189. SELECT_MULTI
  190. };
  191. private:
  192. friend class TreeItem;
  193. TreeItem *root;
  194. TreeItem *popup_edited_item;
  195. TreeItem *selected_item;
  196. TreeItem *edited_item;
  197. int pressed_button;
  198. //TreeItem *cursor_item;
  199. //int cursor_column;
  200. Rect2 custom_popup_rect;
  201. int edited_col;
  202. int selected_col;
  203. int popup_edited_item_col;
  204. bool hide_root;
  205. SelectMode select_mode;
  206. int blocked;
  207. struct ColumnInfo {
  208. int min_width;
  209. bool expand;
  210. String title;
  211. ColumnInfo() { min_width=1; expand=true; }
  212. };
  213. bool show_column_titles;
  214. LineEdit *text_editor;
  215. HSlider *value_editor;
  216. bool updating_value_editor;
  217. uint32_t focus_in_id;
  218. PopupMenu *popup_menu;
  219. Vector<ColumnInfo> columns;
  220. int compute_item_height(TreeItem *p_item) const;
  221. int get_item_height(TreeItem *p_item) const;
  222. // 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);
  223. void draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color);
  224. int draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item);
  225. void select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev=NULL,bool *r_in_range=NULL);
  226. 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);
  227. void text_editor_enter(String p_text);
  228. void value_editor_changed(double p_value);
  229. void popup_select(int p_option);
  230. void _input_event(InputEvent p_event);
  231. void _notification(int p_what);
  232. Size2 get_minimum_size() const;
  233. void item_edited(int p_column,TreeItem *p_item);
  234. void item_changed(int p_column,TreeItem *p_item);
  235. void item_selected(int p_column,TreeItem *p_item);
  236. void item_deselected(int p_column,TreeItem *p_item);
  237. void propagate_set_columns(TreeItem *p_item);
  238. struct Cache {
  239. Ref<Font> font;
  240. Ref<Font> tb_font;
  241. Ref<StyleBox> bg;
  242. Ref<StyleBox> selected;
  243. Ref<StyleBox> selected_focus;
  244. Ref<StyleBox> cursor;
  245. Ref<StyleBox> cursor_unfocus;
  246. Ref<StyleBox> button_pressed;
  247. Ref<StyleBox> title_button;
  248. Ref<StyleBox> title_button_hover;
  249. Ref<StyleBox> title_button_pressed;
  250. Color title_button_color;
  251. Ref<Texture> checked;
  252. Ref<Texture> unchecked;
  253. Ref<Texture> arrow_collapsed;
  254. Ref<Texture> arrow;
  255. Ref<Texture> select_arrow;
  256. Ref<Texture> updown;
  257. Color font_color;
  258. Color font_color_selected;
  259. Color guide_color;
  260. int hseparation;
  261. int vseparation;
  262. int item_margin;
  263. int guide_width;
  264. int button_margin;
  265. Point2 offset;
  266. enum ClickType {
  267. CLICK_NONE,
  268. CLICK_TITLE,
  269. CLICK_BUTTON,
  270. };
  271. ClickType click_type;
  272. ClickType hover_type;
  273. int click_index;
  274. int click_id;
  275. TreeItem *click_item;
  276. int click_column;
  277. int hover_index;
  278. } cache;
  279. int _get_title_button_height() const;
  280. void _scroll_moved(float p_value);
  281. HScrollBar *h_scroll;
  282. VScrollBar *v_scroll;
  283. Size2 get_internal_min_size() const;
  284. void update_cache();
  285. void update_scrollbars();
  286. Rect2 search_item_rect(TreeItem *p_from, TreeItem *p_item);
  287. //Rect2 get_item_rect(TreeItem *p_item);
  288. uint64_t last_keypress;
  289. String incr_search;
  290. bool cursor_can_exit_tree;
  291. void _do_incr_search(const String& p_add);
  292. TreeItem* _search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards=false);
  293. TreeItem* _find_item_at_pos(TreeItem *p_current, const Point2& p_pos,int& r_column,int &h) const;
  294. /* float drag_speed;
  295. float drag_accum;
  296. float last_drag_accum;
  297. float last_drag_time;
  298. float time_since_motion;*/
  299. float drag_speed;
  300. float drag_from;
  301. float drag_accum;
  302. Vector2 last_speed;
  303. bool drag_touching;
  304. bool drag_touching_deaccel;
  305. bool click_handled;
  306. protected:
  307. static void _bind_methods();
  308. //bind helpers
  309. Object* _create_item(Object *p_parent) { return create_item(p_parent->cast_to<TreeItem>() ); }
  310. TreeItem *_get_next_selected(Object *p_item) { return get_next_selected(p_item->cast_to<TreeItem>() ); }
  311. Rect2 _get_item_rect(Object *p_item,int p_column) const { return get_item_rect(p_item->cast_to<TreeItem>(),p_column ); }
  312. public:
  313. virtual String get_tooltip(const Point2& p_pos) const;
  314. void clear();
  315. TreeItem* create_item(TreeItem *p_parent=0);
  316. TreeItem* get_root();
  317. TreeItem* get_last_item();
  318. void set_column_min_width(int p_column,int p_min_width);
  319. void set_column_expand(int p_column,bool p_expand);
  320. int get_column_width(int p_column) const;
  321. void set_hide_root(bool p_eanbled);
  322. TreeItem *get_next_selected( TreeItem* p_item);
  323. TreeItem *get_selected() const;
  324. int get_selected_column() const;
  325. int get_pressed_button() const;
  326. void set_select_mode(SelectMode p_mode);
  327. void set_columns(int p_columns);
  328. int get_columns() const;
  329. void set_column_title(int p_column,const String& p_title);
  330. String get_column_title(int p_column) const;
  331. void set_column_titles_visible(bool p_show);
  332. bool are_column_titles_visible() const;
  333. TreeItem *get_edited() const;
  334. int get_edited_column() const;
  335. void ensure_cursor_is_visible();
  336. Rect2 get_custom_popup_rect() const;
  337. int get_item_offset(TreeItem *p_item) const;
  338. Rect2 get_item_rect(TreeItem *p_item,int p_column=-1) const;
  339. bool edit_selected();
  340. TreeItem* search_item_text(const String& p_find,int *r_col=NULL,bool p_selectable=false);
  341. Point2 get_scroll() const;
  342. void set_cursor_can_exit_tree(bool p_enable);
  343. bool can_cursor_exit_tree() const;
  344. Tree();
  345. ~Tree();
  346. };
  347. VARIANT_ENUM_CAST( Tree::SelectMode );
  348. #endif