control.h
1 /*************************************************************************/
2 /* control.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 CONTROL_H
30 #define CONTROL_H
31 
32 #include "scene/main/node.h"
33 #include "scene/resources/theme.h"
34 #include "scene/main/timer.h"
35 #include "scene/2d/canvas_item.h"
36 #include "math_2d.h"
37 #include "rid.h"
38 
43 class Viewport;
44 class Label;
45 class Panel;
46 
47 class Control : public CanvasItem {
48 
49  OBJ_TYPE( Control, CanvasItem );
50  OBJ_CATEGORY("GUI Nodes");
51 
52 public:
53 
54  enum AnchorType {
55  ANCHOR_BEGIN,
56  ANCHOR_END,
57  ANCHOR_RATIO,
58  ANCHOR_CENTER,
59  };
60 
61  enum FocusMode {
62  FOCUS_NONE,
63  FOCUS_CLICK,
64  FOCUS_ALL
65  };
66 
67  enum SizeFlags {
68 
69  SIZE_EXPAND=1,
70  SIZE_FILL=2,
71  SIZE_EXPAND_FILL=SIZE_EXPAND|SIZE_FILL
72 
73  };
74 
75  enum CursorShape {
76  CURSOR_ARROW,
77  CURSOR_IBEAM,
78  CURSOR_POINTING_HAND,
79  CURSOR_CROSS,
80  CURSOR_WAIT,
81  CURSOR_BUSY,
82  CURSOR_DRAG,
83  CURSOR_CAN_DROP,
84  CURSOR_FORBIDDEN,
85  CURSOR_VSIZE,
86  CURSOR_HSIZE,
87  CURSOR_BDIAGSIZE,
88  CURSOR_FDIAGSIZE,
89  CURSOR_MOVE,
90  CURSOR_VSPLIT,
91  CURSOR_HSPLIT,
92  CURSOR_HELP,
93  CURSOR_MAX
94  };
95 
96 private:
97 
98  struct CComparator {
99 
100  bool operator()(const Control* p_a, const Control* p_b) const {
101  if (p_a->get_canvas_layer()==p_b->get_canvas_layer())
102  return p_b->is_greater_than(p_a);
103  else
104  return p_a->get_canvas_layer() < p_b->get_canvas_layer();
105  }
106  };
107 
108  struct Data {
109 
110  Point2 pos_cache;
111  Size2 size_cache;
112 
113  float margin[4];
114  AnchorType anchor[4];
115  FocusMode focus_mode;
116 
117  float rotation;
118  Vector2 scale;
119 
120  bool pending_resize;
121 
122  int h_size_flags;
123  int v_size_flags;
124  float expand;
125  bool pending_min_size_update;
126  Point2 custom_minimum_size;
127 
128  bool ignore_mouse;
129  bool stop_mouse;
130 
131  Control *parent;
132  bool modal;
133  bool modal_exclusive;
134  Ref<Theme> theme;
135  Control *theme_owner;
136  String tooltip;
137  CursorShape default_cursor;
138 
139  List<Control*>::Element *MI; //modal item
142 
143  CanvasItem *parent_canvas_item;
144 
145  ObjectID modal_prev_focus_owner;
146 
147  NodePath focus_neighbour[4];
148 
155  } data;
156 
157  // used internally
158  Control* _find_control_at_pos(CanvasItem* p_node,const Point2& p_pos,const Matrix32& p_xform,Matrix32& r_inv_xform);
159 
160 
161  void _window_find_focus_neighbour(const Vector2& p_dir, Node *p_at, const Point2* p_points ,float p_min,float &r_closest_dist,Control **r_closest);
162  Control *_get_focus_neighbour(Margin p_margin,int p_count=0);
163 
164 
165  float _get_parent_range(int p_idx) const;
166  float _get_range(int p_idx) const;
167  float _s2a(float p_val, AnchorType p_anchor,float p_range) const;
168  float _a2s(float p_val, AnchorType p_anchor,float p_range) const;
169  void _propagate_theme_changed(Control *p_owner);
170 
171  void _change_notify_margins();
172  void _update_minimum_size();
173 
174  void _update_scroll();
175  void _resize(const Size2& p_size);
176 
177  void _size_changed();
178  String _get_tooltip() const;
179 
180  // Deprecated, should be removed in a future version.
181  void _set_rotation_deg(float p_degrees);
182  float _get_rotation_deg() const;
183 
184 friend class Viewport;
185  void _modal_stack_remove();
186  void _modal_set_prev_focus_owner(ObjectID p_prev);
187 
188 protected:
189 
190  //virtual void _window_input_event(InputEvent p_event);
191 
192  bool _set(const StringName& p_name, const Variant& p_value);
193  bool _get(const StringName& p_name,Variant &r_ret) const;
194  void _get_property_list( List<PropertyInfo> *p_list) const;
195 
196  void _notification(int p_notification);
197 
198 
199  static void _bind_methods();
200 
201  //bind helpers
202 
203 public:
204 
205  enum {
206 
207 /* NOTIFICATION_DRAW=30,
208  NOTIFICATION_VISIBILITY_CHANGED=38*/
209  NOTIFICATION_RESIZED=40,
210  NOTIFICATION_MOUSE_ENTER=41,
211  NOTIFICATION_MOUSE_EXIT=42,
212  NOTIFICATION_FOCUS_ENTER=43,
213  NOTIFICATION_FOCUS_EXIT=44,
214  NOTIFICATION_THEME_CHANGED=45,
215  NOTIFICATION_MODAL_CLOSE=46,
216 
217 
218  };
219 
220  virtual Variant edit_get_state() const;
221  virtual void edit_set_state(const Variant& p_state);
222  virtual void edit_set_rect(const Rect2& p_edit_rect);
223  virtual Size2 edit_get_minimum_size() const;
224 
225  void accept_event();
226 
227  virtual Size2 get_minimum_size() const;
228  virtual Size2 get_combined_minimum_size() const;
229  virtual bool has_point(const Point2& p_point) const;
230  virtual bool clips_input() const;
231  virtual Variant get_drag_data(const Point2& p_point);
232  virtual bool can_drop_data(const Point2& p_point,const Variant& p_data) const;
233  virtual void drop_data(const Point2& p_point,const Variant& p_data);
234  void set_drag_preview(Control *p_control);
235  void force_drag(const Variant& p_data,Control *p_control);
236 
237  void set_custom_minimum_size(const Size2& p_custom);
238  Size2 get_custom_minimum_size() const;
239 
240  bool is_window_modal_on_top() const;
241 
242  Control *get_parent_control() const;
243 
244 
245 
246  /* POSITIONING */
247 
248  void set_anchor(Margin p_margin,AnchorType p_anchor);
249  void set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos);
250 
251  AnchorType get_anchor(Margin p_margin) const;
252 
253  void set_margin(Margin p_margin,float p_value);
254 
255  void set_begin(const Point2& p_point); // helper
256  void set_end(const Point2& p_point); // helper
257 
258 
259 
260  float get_margin(Margin p_margin) const;
261  Point2 get_begin() const;
262  Point2 get_end() const;
263 
264  void set_pos(const Point2& p_point);
265  void set_size(const Size2& p_size);
266  void set_global_pos(const Point2& p_point);
267 
268  Point2 get_pos() const;
269  Point2 get_global_pos() const;
270  Size2 get_size() const;
271  Rect2 get_rect() const;
272  Rect2 get_global_rect() const;
273  Rect2 get_window_rect() const;
274 
275  void set_rotation(float p_radians);
276  void set_rotation_deg(float p_degrees);
277  float get_rotation() const;
278  float get_rotation_deg() const;
279 
280  void set_scale(const Vector2& p_scale);
281  Vector2 get_scale() const;
282 
283 
284  void set_area_as_parent_rect(int p_margin=0);
285 
286  void show_modal(bool p_exclusive=false);
287 
288  void set_theme(const Ref<Theme>& p_theme);
289  Ref<Theme> get_theme() const;
290 
291  void set_h_size_flags(int p_flags);
292  int get_h_size_flags() const;
293 
294  void set_v_size_flags(int p_flags);
295  int get_v_size_flags() const;
296 
297  void set_stretch_ratio(float p_ratio);
298  float get_stretch_ratio() const;
299 
300  void minimum_size_changed();
301 
302  /* FOCUS */
303 
304  void set_focus_mode(FocusMode p_focus_mode);
305  FocusMode get_focus_mode() const;
306  bool has_focus() const;
307  void grab_focus();
308  void release_focus();
309 
310  Control *find_next_valid_focus() const;
311  Control *find_prev_valid_focus() const;
312 
313  void set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour);
314  NodePath get_focus_neighbour(Margin p_margin) const;
315 
316  Control *get_focus_owner() const;
317 
318  void set_ignore_mouse(bool p_ignore);
319  bool is_ignoring_mouse() const;
320 
321  void set_stop_mouse(bool p_stop);
322  bool is_stopping_mouse() const;
323 
324  /* SKINNING */
325 
326  void add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon);
327  void add_shader_override(const StringName& p_name, const Ref<Shader>& p_shader);
328  void add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style);
329  void add_font_override(const StringName& p_name, const Ref<Font>& p_font);
330  void add_color_override(const StringName& p_name, const Color& p_color);
331  void add_constant_override(const StringName& p_name, int p_constant);
332 
333  Ref<Texture> get_icon(const StringName& p_name,const StringName& p_type=StringName()) const;
334  Ref<Shader> get_shader(const StringName &p_name, const StringName &p_type=StringName()) const;
335  Ref<StyleBox> get_stylebox(const StringName& p_name,const StringName& p_type=StringName()) const;
336  Ref<Font> get_font(const StringName& p_name,const StringName& p_type=StringName()) const;
337  Color get_color(const StringName& p_name,const StringName& p_type=StringName()) const;
338  int get_constant(const StringName& p_name,const StringName& p_type=StringName()) const;
339 
340  bool has_icon(const StringName& p_name,const StringName& p_type=StringName()) const;
341  bool has_shader(const StringName& p_name,const StringName& p_type=StringName()) const;
342  bool has_stylebox(const StringName& p_name,const StringName& p_type=StringName()) const;
343  bool has_font(const StringName& p_name,const StringName& p_type=StringName()) const;
344  bool has_color(const StringName& p_name,const StringName& p_type=StringName()) const;
345  bool has_constant(const StringName& p_name,const StringName& p_type=StringName()) const;
346 
347  /* TOOLTIP */
348 
349  void set_tooltip(const String& p_tooltip);
350  virtual String get_tooltip(const Point2& p_pos) const;
351 
352  /* CURSOR */
353 
354  void set_default_cursor_shape(CursorShape p_shape);
355  CursorShape get_default_cursor_shape() const;
356  virtual CursorShape get_cursor_shape(const Point2& p_pos=Point2i()) const;
357 
358  virtual Rect2 get_item_rect() const;
359  virtual Matrix32 get_transform() const;
360 
361  bool is_toplevel_control() const;
362 
363  Size2 get_parent_area_size() const;
364 
365  void grab_click_focus();
366 
367  void warp_mouse(const Point2& p_to_pos);
368 
369  virtual bool is_text_field() const;
370 
371  Control *get_root_parent_control() const;
372 
373  Control();
374  ~Control();
375 
376 };
377 
378 VARIANT_ENUM_CAST(Control::AnchorType);
379 VARIANT_ENUM_CAST(Control::FocusMode);
380 VARIANT_ENUM_CAST(Control::SizeFlags);
381 VARIANT_ENUM_CAST(Control::CursorShape);
382 
383 #endif
Definition: viewport.h:75
Definition: variant.h:74
Definition: string_db.h:150
Definition: path_db.h:41
Definition: node.h:42
Definition: math_2d.h:369
Definition: color.h:37
Definition: math_2d.h:204
Definition: string_db.h:48
Rect2 get_window_rect() const
use with care, as it blocks waiting for the visual server
Definition: control.cpp:1396
Definition: canvas_item.h:87
Definition: hash_map.h:84
Definition: list.h:44
Definition: panel.h:36
Definition: math_2d.h:65
Definition: math_2d.h:554
Definition: control.h:47
Definition: ustring.h:64
Definition: label.h:36