control.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*************************************************************************/
  2. /* control.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 CONTROL_H
  30. #define CONTROL_H
  31. #include "scene/main/node.h"
  32. #include "scene/resources/theme.h"
  33. #include "scene/main/timer.h"
  34. #include "scene/2d/canvas_item.h"
  35. #include "math_2d.h"
  36. #include "rid.h"
  37. /**
  38. @author Juan Linietsky <[email protected]>
  39. */
  40. class Viewport;
  41. class Label;
  42. class Panel;
  43. class Control : public CanvasItem {
  44. OBJ_TYPE( Control, CanvasItem );
  45. OBJ_CATEGORY("GUI Nodes");
  46. public:
  47. enum AnchorType {
  48. ANCHOR_BEGIN,
  49. ANCHOR_END,
  50. ANCHOR_RATIO,
  51. ANCHOR_CENTER,
  52. };
  53. enum FocusMode {
  54. FOCUS_NONE,
  55. FOCUS_CLICK,
  56. FOCUS_ALL
  57. };
  58. enum SizeFlags {
  59. SIZE_EXPAND=1,
  60. SIZE_FILL=2,
  61. SIZE_EXPAND_FILL=SIZE_EXPAND|SIZE_FILL
  62. };
  63. enum CursorShape {
  64. CURSOR_ARROW,
  65. CURSOR_IBEAM,
  66. CURSOR_POINTING_HAND,
  67. CURSOR_CROSS,
  68. CURSOR_WAIT,
  69. CURSOR_BUSY,
  70. CURSOR_DRAG,
  71. CURSOR_CAN_DROP,
  72. CURSOR_FORBIDDEN,
  73. CURSOR_VSIZE,
  74. CURSOR_HSIZE,
  75. CURSOR_BDIAGSIZE,
  76. CURSOR_FDIAGSIZE,
  77. CURSOR_MOVE,
  78. CURSOR_VSPLIT,
  79. CURSOR_HSPLIT,
  80. CURSOR_HELP,
  81. CURSOR_MAX
  82. };
  83. private:
  84. struct CComparator {
  85. bool operator()(const Control* p_a, const Control* p_b) const { return p_b->is_greater_than(p_a); }
  86. };
  87. struct Data {
  88. Point2 pos_cache;
  89. Size2 size_cache;
  90. float margin[4];
  91. AnchorType anchor[4];
  92. FocusMode focus_mode;
  93. bool pending_resize;
  94. int h_size_flags;
  95. int v_size_flags;
  96. float expand;
  97. bool pending_min_size_update;
  98. Point2 custom_minimum_size;
  99. bool ignore_mouse;
  100. bool stop_mouse;
  101. Control *parent;
  102. Control *window;
  103. bool modal;
  104. bool modal_exclusive;
  105. Ref<Theme> theme;
  106. Control *theme_owner;
  107. String tooltip;
  108. CursorShape default_cursor;
  109. List<Control*>::Element *MI; //modal item
  110. List<Control*>::Element *SI;
  111. CanvasItem *parent_canvas_item;
  112. Viewport *viewport;
  113. ObjectID modal_prev_focus_owner;
  114. NodePath focus_neighbour[4];
  115. HashMap<StringName, Ref<Texture>, StringNameHasher > icon_override;
  116. HashMap<StringName, Ref<StyleBox>, StringNameHasher > style_override;
  117. HashMap<StringName, Ref<Font>, StringNameHasher > font_override;
  118. HashMap<StringName, Color, StringNameHasher > color_override;
  119. HashMap<StringName, int, StringNameHasher > constant_override;
  120. } data;
  121. struct Window {
  122. // info used when this is a window
  123. bool key_event_accepted;
  124. Control *mouse_focus;
  125. int mouse_focus_button;
  126. Control *key_focus;
  127. Control *mouse_over;
  128. Control *tooltip;
  129. Panel *tooltip_popup;
  130. Label *tooltip_label;
  131. Point2 tooltip_pos;
  132. Point2 last_mouse_pos;
  133. Point2 drag_accum;
  134. bool drag_attempted;
  135. Variant drag_data;
  136. Control *drag_preview;
  137. Timer *tooltip_timer;
  138. List<Control*> modal_stack;
  139. unsigned int cancelled_input_ID;
  140. Matrix32 focus_inv_xform;
  141. bool subwindow_order_dirty;
  142. List<Control*> subwindows;
  143. bool disable_input;
  144. Window();
  145. };
  146. Window *window;
  147. // used internally
  148. Control* _find_next_visible_control_at_pos(Node* p_node,const Point2& p_global,Matrix32& r_xform) const;
  149. Control* _find_control_at_pos(CanvasItem* p_node,const Point2& p_pos,const Matrix32& p_xform,Matrix32& r_inv_xform);
  150. void _window_sort_subwindows();
  151. void _window_accept_event();
  152. void _window_remove_focus();
  153. void _window_cancel_input_ID(int p_input);
  154. void _window_sort_modal_stack();
  155. 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);
  156. Control *_get_focus_neighbour(Margin p_margin,int p_count=0);
  157. void _window_call_input(Control *p_control,const InputEvent& p_input);
  158. float _get_parent_range(int p_idx) const;
  159. float _get_range(int p_idx) const;
  160. float _s2a(float p_val, AnchorType p_anchor,float p_range) const;
  161. float _a2s(float p_val, AnchorType p_anchor,float p_range) const;
  162. void _modal_stack_remove();
  163. void _propagate_theme_changed(Control *p_owner);
  164. void _change_notify_margins();
  165. void _window_cancel_tooltip();
  166. void _window_show_tooltip();
  167. void _update_minimum_size();
  168. void _update_scroll();
  169. void _gui_input(const InputEvent& p_event); //used by scene main loop
  170. void _input_text(const String& p_text);
  171. void _resize(const Size2& p_size);
  172. void _size_changed();
  173. String _get_tooltip() const;
  174. protected:
  175. bool window_has_modal_stack() const;
  176. virtual void _window_input_event(InputEvent p_event);
  177. bool _set(const StringName& p_name, const Variant& p_value);
  178. bool _get(const StringName& p_name,Variant &r_ret) const;
  179. void _get_property_list( List<PropertyInfo> *p_list) const;
  180. void _notification(int p_notification);
  181. static void _bind_methods();
  182. //bind helpers
  183. public:
  184. enum {
  185. /* NOTIFICATION_DRAW=30,
  186. NOTIFICATION_VISIBILITY_CHANGED=38*/
  187. NOTIFICATION_RESIZED=40,
  188. NOTIFICATION_MOUSE_ENTER=41,
  189. NOTIFICATION_MOUSE_EXIT=42,
  190. NOTIFICATION_FOCUS_ENTER=43,
  191. NOTIFICATION_FOCUS_EXIT=44,
  192. NOTIFICATION_THEME_CHANGED=45,
  193. NOTIFICATION_MODAL_CLOSE=46,
  194. };
  195. virtual Variant edit_get_state() const;
  196. virtual void edit_set_state(const Variant& p_state);
  197. virtual void edit_set_rect(const Rect2& p_edit_rect);
  198. virtual Size2 edit_get_minimum_size() const;
  199. void accept_event();
  200. virtual Size2 get_minimum_size() const;
  201. virtual Size2 get_combined_minimum_size() const;
  202. virtual bool has_point(const Point2& p_point) const;
  203. virtual bool clips_input() const;
  204. virtual Variant get_drag_data(const Point2& p_point);
  205. virtual bool can_drop_data(const Point2& p_point,const Variant& p_data) const;
  206. virtual void drop_data(const Point2& p_point,const Variant& p_data);
  207. void set_drag_preview(Control *p_control);
  208. void force_drag(const Variant& p_data,Control *p_control);
  209. void set_custom_minimum_size(const Size2& p_custom);
  210. Size2 get_custom_minimum_size() const;
  211. bool is_window() const;
  212. Control *get_window() const;
  213. Control *get_parent_control() const;
  214. /* POSITIONING */
  215. void set_anchor(Margin p_margin,AnchorType p_anchor);
  216. void set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos);
  217. AnchorType get_anchor(Margin p_margin) const;
  218. void set_margin(Margin p_margin,float p_value);
  219. void set_begin(const Point2& p_point); // helper
  220. void set_end(const Point2& p_point); // helper
  221. float get_margin(Margin p_margin) const;
  222. Point2 get_begin() const;
  223. Point2 get_end() const;
  224. void set_pos(const Point2& p_point);
  225. void set_size(const Size2& p_size);
  226. void set_global_pos(const Point2& p_point);
  227. Point2 get_pos() const;
  228. Point2 get_global_pos() const;
  229. Size2 get_size() const;
  230. Rect2 get_rect() const;
  231. Rect2 get_global_rect() const;
  232. Rect2 get_window_rect() const; ///< use with care, as it blocks waiting for the visual server
  233. void set_area_as_parent_rect(int p_margin=0);
  234. void show_modal(bool p_exclusive=false);
  235. void set_theme(const Ref<Theme>& p_theme);
  236. Ref<Theme> get_theme() const;
  237. void set_h_size_flags(int p_flags);
  238. int get_h_size_flags() const;
  239. void set_v_size_flags(int p_flags);
  240. int get_v_size_flags() const;
  241. void set_stretch_ratio(float p_ratio);
  242. float get_stretch_ratio() const;
  243. void minimum_size_changed();
  244. /* FOCUS */
  245. void set_focus_mode(FocusMode p_focus_mode);
  246. FocusMode get_focus_mode() const;
  247. bool has_focus() const;
  248. void grab_focus();
  249. void release_focus();
  250. Control *find_next_valid_focus() const;
  251. Control *find_prev_valid_focus() const;
  252. void set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour);
  253. NodePath get_focus_neighbour(Margin p_margin) const;
  254. Control *get_focus_owner() const;
  255. void set_ignore_mouse(bool p_ignore);
  256. bool is_ignoring_mouse() const;
  257. void set_stop_mouse(bool p_stop);
  258. bool is_stopping_mouse() const;
  259. /* SKINNING */
  260. void add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon);
  261. void add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style);
  262. void add_font_override(const StringName& p_name, const Ref<Font>& p_font);
  263. void add_color_override(const StringName& p_name, const Color& p_color);
  264. void add_constant_override(const StringName& p_name, int p_constant);
  265. Ref<Texture> get_icon(const StringName& p_name,const StringName& p_type=StringName()) const;
  266. Ref<StyleBox> get_stylebox(const StringName& p_name,const StringName& p_type=StringName()) const;
  267. Ref<Font> get_font(const StringName& p_name,const StringName& p_type=StringName()) const;
  268. Color get_color(const StringName& p_name,const StringName& p_type=StringName()) const;
  269. int get_constant(const StringName& p_name,const StringName& p_type=StringName()) const;
  270. bool has_icon(const StringName& p_name,const StringName& p_type=StringName()) const;
  271. bool has_stylebox(const StringName& p_name,const StringName& p_type=StringName()) const;
  272. bool has_font(const StringName& p_name,const StringName& p_type=StringName()) const;
  273. bool has_color(const StringName& p_name,const StringName& p_type=StringName()) const;
  274. bool has_constant(const StringName& p_name,const StringName& p_type=StringName()) const;
  275. /* TOOLTIP */
  276. void set_tooltip(const String& p_tooltip);
  277. virtual String get_tooltip(const Point2& p_pos) const;
  278. /* CURSOR */
  279. void set_default_cursor_shape(CursorShape p_shape);
  280. CursorShape get_default_cursor_shape() const;
  281. virtual CursorShape get_cursor_shape(const Point2& p_pos=Point2i()) const;
  282. virtual Rect2 get_item_rect() const;
  283. virtual Matrix32 get_transform() const;
  284. bool is_toplevel_control() const;
  285. Size2 get_parent_area_size() const;
  286. void grab_click_focus();
  287. void warp_mouse(const Point2& p_to_pos);
  288. Control();
  289. ~Control();
  290. };
  291. VARIANT_ENUM_CAST(Control::AnchorType);
  292. VARIANT_ENUM_CAST(Control::FocusMode);
  293. VARIANT_ENUM_CAST(Control::SizeFlags);
  294. VARIANT_ENUM_CAST(Control::CursorShape);
  295. #endif