tree.h
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 
32 #include "scene/gui/control.h"
33 #include "scene/gui/popup_menu.h"
34 #include "scene/gui/line_edit.h"
35 #include "scene/gui/scroll_bar.h"
36 #include "scene/gui/slider.h"
37 
43 class Tree;
44 
45 class TreeItem : public Object {
46 
47  OBJ_TYPE(TreeItem,Object);
48 public:
49 
50  enum TreeCellMode {
51 
57  };
58 
59 private:
60 friend class Tree;
61 
62 
63  struct Cell {
64 
65  TreeCellMode mode;
66 
67  Ref<Texture> icon;
68  Rect2i icon_region;
69  String text;
70  double min,max,step,val;
71  int icon_max_w;
72  bool expr;
73  bool checked;
74  bool editable;
75  bool selected;
76  bool selectable;
77  bool custom_color;
78  Color color;
79  bool custom_bg_color;
80  Color bg_color;
81  Variant meta;
82  String tooltip;
83 
84  ObjectID custom_draw_obj;
85  StringName custom_draw_callback;
86 
87  struct Button {
88  int id;
89  bool disabled;
90  Ref<Texture> texture;
91  Button() { id=0; disabled=false; }
92  };
93 
94  Vector< Button > buttons;
95 
96  Cell() {
97 
98  custom_draw_obj=0;
100  min=0;
101  max=100;
102  step=1;
103  val=0;
104  checked=false;
105  editable=false;
106  selected=false;
107  selectable=true;
108  custom_color=false;
109  custom_bg_color=false;
110  expr=false;
111  icon_max_w=0;
112  }
113 
114 
115  Size2 get_icon_size() const;
116  void draw_icon(const RID& p_where, const Point2& p_pos, const Size2& p_size=Size2()) const;
117 
118  };
119 
120  Vector<Cell> cells;
121 
122  bool collapsed; // wont show childs
123 
124  TreeItem *parent; // parent item
125  TreeItem *next; // next in list
126  TreeItem *childs; //child items
127  Tree *tree; //tree (for reference)
128 
129 
130 
131  TreeItem(Tree *p_tree);
132 
133 
134  void _changed_notify(int p_cell);
135  void _changed_notify();
136  void _cell_selected(int p_cell);
137  void _cell_deselected(int p_cell);
138 protected:
139 
140  static void _bind_methods();
141  //bind helpers
142  Dictionary _get_range_config( int p_column ) {
143  Dictionary d;
144  double min,max,step;
145  get_range_config(p_column,min,max,step);
146  d["min"]=min;
147  d["max"]=max;
148  d["step"]=step;
149  d["expr"]=false;
150 
151  return d;
152  }
153  void _remove_child(Object *p_child) { remove_child( p_child->cast_to<TreeItem>() ); }
154 public:
155 
156  /* cell mode */
157  void set_cell_mode( int p_column, TreeCellMode p_mode );
158  TreeCellMode get_cell_mode( int p_column ) const;
159 
160  /* check mode */
161  void set_checked(int p_column,bool p_checked);
162  bool is_checked(int p_column) const;
163 
164  void set_text(int p_column,String p_text);
165  String get_text(int p_column) const;
166 
167  void set_icon(int p_column,const Ref<Texture>& p_icon);
168  Ref<Texture> get_icon(int p_column) const;
169 
170  void set_icon_region(int p_column,const Rect2& p_icon_region);
171  Rect2 get_icon_region(int p_column) const;
172 
173  void set_icon_max_width(int p_column,int p_max);
174  int get_icon_max_width(int p_column) const;
175 
176  void add_button(int p_column,const Ref<Texture>& p_button,int p_id=-1,bool p_disabled=false);
177  int get_button_count(int p_column) const;
178  Ref<Texture> get_button(int p_column,int p_idx) const;
179  int get_button_id(int p_column,int p_idx) const;
180  void erase_button(int p_column,int p_idx);
181  int get_button_by_id(int p_column,int p_id) const;
182  bool is_button_disabled(int p_column,int p_idx) const;
183  void set_button(int p_column,int p_idx,const Ref<Texture>& p_button);
184 
185  /* range works for mode number or mode combo */
186 
187  void set_range(int p_column,double p_value);
188  double get_range(int p_column) const;
189 
190  void set_range_config(int p_column,double p_min,double p_max,double p_step,bool p_exp=false);
191  void get_range_config(int p_column,double& r_min,double& r_max,double &r_step) const;
192  bool is_range_exponential(int p_column) const;
193 
194  void set_metadata(int p_column,const Variant& p_meta);
195  Variant get_metadata(int p_column) const;
196 
197  void set_custom_draw(int p_column,Object *p_object,const StringName& p_callback);
198 
199  void set_collapsed(bool p_collapsed);
200  bool is_collapsed();
201 
202  TreeItem *get_prev();
203  TreeItem *get_next();
204  TreeItem *get_parent();
205  TreeItem *get_children();
206 
207  TreeItem *get_prev_visible();
208  TreeItem *get_next_visible();
209 
210  void remove_child(TreeItem *p_item);
211 
212  void set_selectable(int p_column,bool p_selectable);
213  bool is_selectable(int p_column) const;
214 
215  bool is_selected(int p_column);
216  void select(int p_column);
217  void deselect(int p_column);
218  void set_as_cursor(int p_column);
219 
220  void set_editable(int p_column,bool p_editable);
221  bool is_editable(int p_column);
222 
223  void set_custom_color(int p_column,const Color& p_color);
224  void clear_custom_color(int p_column);
225 
226  void set_custom_bg_color(int p_column,const Color& p_color);
227  void clear_custom_bg_color(int p_column);
228  Color get_custom_bg_color(int p_column) const;
229 
230  void set_tooltip(int p_column, const String& p_tooltip);
231  String get_tooltip(int p_column) const;
232 
233 
234  void clear_children();
235 
236  void move_to_top();
237  void move_to_bottom();
238 
239  ~TreeItem();
240 
241 };
242 
243 
244 VARIANT_ENUM_CAST( TreeItem::TreeCellMode );
245 
246 
247 class Tree : public Control {
248 
249  OBJ_TYPE( Tree, Control );
250 public:
251  enum SelectMode {
252  SELECT_SINGLE,
253  SELECT_ROW,
254  SELECT_MULTI
255  };
256 
257 private:
258 friend class TreeItem;
259 
260  TreeItem *root;
261  TreeItem *popup_edited_item;
262  TreeItem *selected_item;
263  TreeItem *edited_item;
264 
265 
266  int pressed_button;
267  bool pressing_for_editor;
268  String pressing_for_editor_text;
269  Vector2 pressing_pos;
270  Rect2 pressing_item_rect;
271 
272  float range_drag_base;
273  bool range_drag_enabled;
274  Vector2 range_drag_capture_pos;
275 
276 
277  //TreeItem *cursor_item;
278  //int cursor_column;
279 
280  Rect2 custom_popup_rect;
281  int edited_col;
282  int selected_col;
283  int popup_edited_item_col;
284  bool hide_root;
285  SelectMode select_mode;
286 
287  int blocked;
288 
289  struct ColumnInfo {
290 
291  int min_width;
292  bool expand;
293  String title;
294  ColumnInfo() { min_width=1; expand=true; }
295  };
296 
297  bool show_column_titles;
298  LineEdit *text_editor;
299  HSlider *value_editor;
300  bool updating_value_editor;
301  uint32_t focus_in_id;
302  PopupMenu *popup_menu;
303 
304  Vector<ColumnInfo> columns;
305 
306  Timer *range_click_timer;
307  TreeItem *range_item_last;
308  bool range_up_last;
309  void _range_click_timeout();
310 
311  int compute_item_height(TreeItem *p_item) const;
312  int get_item_height(TreeItem *p_item) const;
313 // 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);
314  void draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color);
315  int draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item);
316  void select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev=NULL,bool *r_in_range=NULL);
317  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);
318  void text_editor_enter(String p_text);
319  void _text_editor_modal_close();
320  void value_editor_changed(double p_value);
321 
322  void popup_select(int p_option);
323 
324  void _input_event(InputEvent p_event);
325  void _notification(int p_what);
326 
327  Size2 get_minimum_size() const;
328 
329  void item_edited(int p_column,TreeItem *p_item);
330  void item_changed(int p_column,TreeItem *p_item);
331  void item_selected(int p_column,TreeItem *p_item);
332  void item_deselected(int p_column,TreeItem *p_item);
333 
334  void propagate_set_columns(TreeItem *p_item);
335 
336  struct Cache {
337 
338  Ref<Font> font;
339  Ref<Font> tb_font;
340  Ref<StyleBox> bg;
341  Ref<StyleBox> selected;
342  Ref<StyleBox> selected_focus;
343  Ref<StyleBox> cursor;
344  Ref<StyleBox> cursor_unfocus;
345  Ref<StyleBox> button_pressed;
346  Ref<StyleBox> title_button;
347  Ref<StyleBox> title_button_hover;
348  Ref<StyleBox> title_button_pressed;
349  Color title_button_color;
350 
351  Ref<Texture> checked;
352  Ref<Texture> unchecked;
353  Ref<Texture> arrow_collapsed;
354  Ref<Texture> arrow;
355  Ref<Texture> select_arrow;
356  Ref<Texture> updown;
357 
358  Color font_color;
359  Color font_color_selected;
360  Color guide_color;
361  int hseparation;
362  int vseparation;
363  int item_margin;
364  int guide_width;
365  int button_margin;
366  Point2 offset;
367 
368  enum ClickType {
369  CLICK_NONE,
370  CLICK_TITLE,
371  CLICK_BUTTON,
372 
373  };
374 
375  ClickType click_type;
376  ClickType hover_type;
377  int click_index;
378  int click_id;
379  TreeItem *click_item;
380  int click_column;
381  int hover_index;
382 
383  } cache;
384 
385 
386  int _get_title_button_height() const;
387 
388  void _scroll_moved(float p_value);
389  HScrollBar *h_scroll;
390  VScrollBar *v_scroll;
391 
392  Size2 get_internal_min_size() const;
393  void update_cache();
394  void update_scrollbars();
395 
396  Rect2 search_item_rect(TreeItem *p_from, TreeItem *p_item);
397  //Rect2 get_item_rect(TreeItem *p_item);
398  uint64_t last_keypress;
399  String incr_search;
400  bool cursor_can_exit_tree;
401  void _do_incr_search(const String& p_add);
402 
403  TreeItem* _search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards=false);
404 
405  TreeItem* _find_item_at_pos(TreeItem *p_current, const Point2& p_pos,int& r_column,int &h) const;
406 
407 /* float drag_speed;
408  float drag_accum;
409 
410  float last_drag_accum;
411  float last_drag_time;
412  float time_since_motion;*/
413 
414  float drag_speed;
415  float drag_from;
416  float drag_accum;
417  Vector2 last_speed;
418  bool drag_touching;
419  bool drag_touching_deaccel;
420  bool click_handled;
421 
422  bool hide_folding;
423 
424 protected:
425  static void _bind_methods();
426 
427  //bind helpers
428  Object* _create_item(Object *p_parent) { return create_item(p_parent->cast_to<TreeItem>() ); }
429  TreeItem *_get_next_selected(Object *p_item) { return get_next_selected(p_item->cast_to<TreeItem>() ); }
430  Rect2 _get_item_rect(Object *p_item,int p_column) const { return get_item_rect(p_item->cast_to<TreeItem>(),p_column ); }
431 public:
432 
433  virtual String get_tooltip(const Point2& p_pos) const;
434 
435  void clear();
436 
437  TreeItem* create_item(TreeItem *p_parent=0);
438  TreeItem* get_root();
439  TreeItem* get_last_item();
440 
441  void set_column_min_width(int p_column,int p_min_width);
442  void set_column_expand(int p_column,bool p_expand);
443  int get_column_width(int p_column) const;
444 
445  void set_hide_root(bool p_eanbled);
446  TreeItem *get_next_selected( TreeItem* p_item);
447  TreeItem *get_selected() const;
448  int get_selected_column() const;
449  int get_pressed_button() const;
450  void set_select_mode(SelectMode p_mode);
451 
452  void set_columns(int p_columns);
453  int get_columns() const;
454 
455  void set_column_title(int p_column,const String& p_title);
456  String get_column_title(int p_column) const;
457 
458  void set_column_titles_visible(bool p_show);
459  bool are_column_titles_visible() const;
460 
461  TreeItem *get_edited() const;
462  int get_edited_column() const;
463 
464  void ensure_cursor_is_visible();
465 
466  Rect2 get_custom_popup_rect() const;
467 
468  int get_item_offset(TreeItem *p_item) const;
469  Rect2 get_item_rect(TreeItem *p_item,int p_column=-1) const;
470  bool edit_selected();
471 
472  TreeItem* search_item_text(const String& p_find,int *r_col=NULL,bool p_selectable=false);
473 
474  Point2 get_scroll() const;
475 
476  void set_cursor_can_exit_tree(bool p_enable);
477  bool can_cursor_exit_tree() const;
478 
479  VScrollBar *get_vscroll_bar() { return v_scroll; }
480 
481  void set_hide_folding(bool p_hide);
482  bool is_folding_hidden() const;
483 
484 
485 
486  Tree();
487  ~Tree();
488 
489 };
490 
491 VARIANT_ENUM_CAST( Tree::SelectMode );
492 #endif
493 
Contains a range.
Definition: tree.h:54
Definition: scroll_bar.h:110
Definition: timer.h:34
Definition: tree.h:87
Definition: variant.h:74
Definition: math_2d.h:422
Definition: math_2d.h:369
Definition: input_event.h:158
Definition: color.h:37
Definition: math_2d.h:204
just a string
Definition: tree.h:52
Definition: string_db.h:48
Definition: scroll_bar.h:118
Definition: slider.h:75
Definition: input_event.h:263
string + check
Definition: tree.h:53
Definition: tree.h:247
Contains a custom value, show a string, and an edit button.
Definition: tree.h:56
Definition: rid.h:47
Definition: dictionary.h:42
Contains a icon, not editable.
Definition: tree.h:55
Definition: math_2d.h:65
Definition: control.h:47
Definition: ustring.h:64
Definition: object.h:317
TreeCellMode
Definition: tree.h:50
Definition: popup_menu.h:37
Definition: line_edit.h:36
Definition: tree.h:45