control.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*************************************************************************/
  2. /* control.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef CONTROL_H
  31. #define CONTROL_H
  32. #include "core/math/transform_2d.h"
  33. #include "core/rid.h"
  34. #include "scene/2d/canvas_item.h"
  35. #include "scene/gui/shortcut.h"
  36. #include "scene/main/node.h"
  37. #include "scene/main/timer.h"
  38. #include "scene/resources/theme.h"
  39. /**
  40. @author Juan Linietsky <[email protected]>
  41. */
  42. class Viewport;
  43. class Label;
  44. class Panel;
  45. class Control : public CanvasItem {
  46. GDCLASS(Control, CanvasItem);
  47. OBJ_CATEGORY("GUI Nodes");
  48. public:
  49. enum Anchor {
  50. ANCHOR_BEGIN = 0,
  51. ANCHOR_END = 1
  52. };
  53. enum GrowDirection {
  54. GROW_DIRECTION_BEGIN,
  55. GROW_DIRECTION_END,
  56. GROW_DIRECTION_BOTH
  57. };
  58. enum FocusMode {
  59. FOCUS_NONE,
  60. FOCUS_CLICK,
  61. FOCUS_ALL
  62. };
  63. enum SizeFlags {
  64. SIZE_FILL = 1,
  65. SIZE_EXPAND = 2,
  66. SIZE_EXPAND_FILL = SIZE_EXPAND | SIZE_FILL,
  67. SIZE_SHRINK_CENTER = 4, //ignored by expand or fill
  68. SIZE_SHRINK_END = 8, //ignored by expand or fill
  69. };
  70. enum MouseFilter {
  71. MOUSE_FILTER_STOP,
  72. MOUSE_FILTER_PASS,
  73. MOUSE_FILTER_IGNORE
  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. enum LayoutPreset {
  96. PRESET_TOP_LEFT,
  97. PRESET_TOP_RIGHT,
  98. PRESET_BOTTOM_LEFT,
  99. PRESET_BOTTOM_RIGHT,
  100. PRESET_CENTER_LEFT,
  101. PRESET_CENTER_TOP,
  102. PRESET_CENTER_RIGHT,
  103. PRESET_CENTER_BOTTOM,
  104. PRESET_CENTER,
  105. PRESET_LEFT_WIDE,
  106. PRESET_TOP_WIDE,
  107. PRESET_RIGHT_WIDE,
  108. PRESET_BOTTOM_WIDE,
  109. PRESET_VCENTER_WIDE,
  110. PRESET_HCENTER_WIDE,
  111. PRESET_WIDE
  112. };
  113. enum LayoutPresetMode {
  114. PRESET_MODE_MINSIZE,
  115. PRESET_MODE_KEEP_WIDTH,
  116. PRESET_MODE_KEEP_HEIGHT,
  117. PRESET_MODE_KEEP_SIZE
  118. };
  119. private:
  120. struct CComparator {
  121. bool operator()(const Control *p_a, const Control *p_b) const {
  122. if (p_a->get_canvas_layer() == p_b->get_canvas_layer())
  123. return p_b->is_greater_than(p_a);
  124. else
  125. return p_a->get_canvas_layer() < p_b->get_canvas_layer();
  126. }
  127. };
  128. struct Data {
  129. Point2 pos_cache;
  130. Size2 size_cache;
  131. Size2 minimum_size_cache;
  132. bool minimum_size_valid;
  133. Size2 last_minimum_size;
  134. bool updating_last_minimum_size;
  135. float margin[4];
  136. float anchor[4];
  137. FocusMode focus_mode;
  138. GrowDirection h_grow;
  139. GrowDirection v_grow;
  140. float rotation;
  141. Vector2 scale;
  142. Vector2 pivot_offset;
  143. bool pending_resize;
  144. int h_size_flags;
  145. int v_size_flags;
  146. float expand;
  147. Point2 custom_minimum_size;
  148. bool pass_on_modal_close_click;
  149. MouseFilter mouse_filter;
  150. bool clip_contents;
  151. bool block_minimum_size_adjust;
  152. bool disable_visibility_clip;
  153. Control *parent;
  154. ObjectID drag_owner;
  155. bool modal_exclusive;
  156. uint64_t modal_frame; //frame used to put something as modal
  157. Ref<Theme> theme;
  158. Control *theme_owner;
  159. String tooltip;
  160. CursorShape default_cursor;
  161. List<Control *>::Element *MI; //modal item
  162. List<Control *>::Element *SI;
  163. List<Control *>::Element *RI;
  164. CanvasItem *parent_canvas_item;
  165. ObjectID modal_prev_focus_owner;
  166. NodePath focus_neighbour[4];
  167. NodePath focus_next;
  168. NodePath focus_prev;
  169. HashMap<StringName, Ref<Texture> > icon_override;
  170. HashMap<StringName, Ref<Shader> > shader_override;
  171. HashMap<StringName, Ref<StyleBox> > style_override;
  172. HashMap<StringName, Ref<Font> > font_override;
  173. HashMap<StringName, Color> color_override;
  174. HashMap<StringName, int> constant_override;
  175. } data;
  176. // used internally
  177. Control *_find_control_at_pos(CanvasItem *p_node, const Point2 &p_pos, const Transform2D &p_xform, Transform2D &r_inv_xform);
  178. 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);
  179. Control *_get_focus_neighbour(Margin p_margin, int p_count = 0);
  180. void _set_anchor(Margin p_margin, float p_anchor);
  181. void _propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign = true);
  182. void _theme_changed();
  183. void _change_notify_margins();
  184. void _update_minimum_size();
  185. void _update_scroll();
  186. void _resize(const Size2 &p_size);
  187. Rect2 _compute_child_rect(const float p_anchors[4], const float p_margins[4]) const;
  188. void _compute_margins(Rect2 p_rect, const float p_anchors[4], float (&r_margins)[4]);
  189. void _size_changed();
  190. String _get_tooltip() const;
  191. void _override_changed();
  192. void _update_canvas_item_transform();
  193. Transform2D _get_internal_transform() const;
  194. friend class Viewport;
  195. void _modal_stack_remove();
  196. void _modal_set_prev_focus_owner(ObjectID p_prev);
  197. void _update_minimum_size_cache();
  198. protected:
  199. virtual void add_child_notify(Node *p_child);
  200. virtual void remove_child_notify(Node *p_child);
  201. //virtual void _window_gui_input(InputEvent p_event);
  202. bool _set(const StringName &p_name, const Variant &p_value);
  203. bool _get(const StringName &p_name, Variant &r_ret) const;
  204. void _get_property_list(List<PropertyInfo> *p_list) const;
  205. void _notification(int p_notification);
  206. static void _bind_methods();
  207. //bind helpers
  208. public:
  209. enum {
  210. /* NOTIFICATION_DRAW=30,
  211. NOTIFICATION_VISIBILITY_CHANGED=38*/
  212. NOTIFICATION_RESIZED = 40,
  213. NOTIFICATION_MOUSE_ENTER = 41,
  214. NOTIFICATION_MOUSE_EXIT = 42,
  215. NOTIFICATION_FOCUS_ENTER = 43,
  216. NOTIFICATION_FOCUS_EXIT = 44,
  217. NOTIFICATION_THEME_CHANGED = 45,
  218. NOTIFICATION_MODAL_CLOSE = 46,
  219. NOTIFICATION_SCROLL_BEGIN = 47,
  220. NOTIFICATION_SCROLL_END = 48,
  221. };
  222. /* EDITOR */
  223. virtual Dictionary _edit_get_state() const;
  224. virtual void _edit_set_state(const Dictionary &p_state);
  225. virtual void _edit_set_position(const Point2 &p_position);
  226. virtual Point2 _edit_get_position() const;
  227. virtual void _edit_set_scale(const Size2 &p_scale);
  228. virtual Size2 _edit_get_scale() const;
  229. virtual void _edit_set_rect(const Rect2 &p_edit_rect);
  230. virtual Rect2 _edit_get_rect() const;
  231. virtual bool _edit_use_rect() const;
  232. virtual void _edit_set_rotation(float p_rotation);
  233. virtual float _edit_get_rotation() const;
  234. virtual bool _edit_use_rotation() const;
  235. virtual void _edit_set_pivot(const Point2 &p_pivot);
  236. virtual Point2 _edit_get_pivot() const;
  237. virtual bool _edit_use_pivot() const;
  238. virtual Size2 _edit_get_minimum_size() const;
  239. void accept_event();
  240. virtual Size2 get_minimum_size() const;
  241. virtual Size2 get_combined_minimum_size() const;
  242. virtual bool has_point(const Point2 &p_point) const;
  243. virtual bool clips_input() const;
  244. virtual void set_drag_forwarding(Control *p_target);
  245. virtual Variant get_drag_data(const Point2 &p_point);
  246. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  247. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  248. void set_drag_preview(Control *p_control);
  249. void force_drag(const Variant &p_data, Control *p_control);
  250. void set_custom_minimum_size(const Size2 &p_custom);
  251. Size2 get_custom_minimum_size() const;
  252. bool is_window_modal_on_top() const;
  253. uint64_t get_modal_frame() const; //frame in which this was made modal
  254. Control *get_parent_control() const;
  255. /* POSITIONING */
  256. void set_anchors_preset(LayoutPreset p_preset, bool p_keep_margin = true);
  257. void set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
  258. void set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
  259. void set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin = true, bool p_push_opposite_anchor = true);
  260. float get_anchor(Margin p_margin) const;
  261. void set_margin(Margin p_margin, float p_value);
  262. float get_margin(Margin p_margin) const;
  263. void set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor = true);
  264. void set_begin(const Point2 &p_point); // helper
  265. void set_end(const Point2 &p_point); // helper
  266. Point2 get_begin() const;
  267. Point2 get_end() const;
  268. void set_position(const Point2 &p_point);
  269. void set_global_position(const Point2 &p_point);
  270. Point2 get_position() const;
  271. Point2 get_global_position() const;
  272. void set_size(const Size2 &p_size);
  273. Size2 get_size() const;
  274. Rect2 get_rect() const;
  275. Rect2 get_global_rect() const;
  276. Rect2 get_window_rect() const; ///< use with care, as it blocks waiting for the visual server
  277. Rect2 get_anchorable_rect() const;
  278. void set_rotation(float p_radians);
  279. void set_rotation_degrees(float p_degrees);
  280. float get_rotation() const;
  281. float get_rotation_degrees() const;
  282. void set_h_grow_direction(GrowDirection p_direction);
  283. GrowDirection get_h_grow_direction() const;
  284. void set_v_grow_direction(GrowDirection p_direction);
  285. GrowDirection get_v_grow_direction() const;
  286. void set_pivot_offset(const Vector2 &p_pivot);
  287. Vector2 get_pivot_offset() const;
  288. void set_scale(const Vector2 &p_scale);
  289. Vector2 get_scale() const;
  290. void show_modal(bool p_exclusive = false);
  291. void set_theme(const Ref<Theme> &p_theme);
  292. Ref<Theme> get_theme() const;
  293. void set_h_size_flags(int p_flags);
  294. int get_h_size_flags() const;
  295. void set_v_size_flags(int p_flags);
  296. int get_v_size_flags() const;
  297. void set_stretch_ratio(float p_ratio);
  298. float get_stretch_ratio() const;
  299. void minimum_size_changed();
  300. /* FOCUS */
  301. void set_focus_mode(FocusMode p_focus_mode);
  302. FocusMode get_focus_mode() const;
  303. bool has_focus() const;
  304. void grab_focus();
  305. void release_focus();
  306. Control *find_next_valid_focus() const;
  307. Control *find_prev_valid_focus() const;
  308. void set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour);
  309. NodePath get_focus_neighbour(Margin p_margin) const;
  310. void set_focus_next(const NodePath &p_next);
  311. NodePath get_focus_next() const;
  312. void set_focus_previous(const NodePath &p_prev);
  313. NodePath get_focus_previous() const;
  314. Control *get_focus_owner() const;
  315. void set_mouse_filter(MouseFilter p_filter);
  316. MouseFilter get_mouse_filter() const;
  317. void set_pass_on_modal_close_click(bool p_pass_on);
  318. bool pass_on_modal_close_click() const;
  319. /* SKINNING */
  320. void add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon);
  321. void add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader);
  322. void add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
  323. void add_font_override(const StringName &p_name, const Ref<Font> &p_font);
  324. void add_color_override(const StringName &p_name, const Color &p_color);
  325. void add_constant_override(const StringName &p_name, int p_constant);
  326. Ref<Texture> get_icon(const StringName &p_name, const StringName &p_type = StringName()) const;
  327. Ref<Shader> get_shader(const StringName &p_name, const StringName &p_type = StringName()) const;
  328. Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_type = StringName()) const;
  329. Ref<Font> get_font(const StringName &p_name, const StringName &p_type = StringName()) const;
  330. Color get_color(const StringName &p_name, const StringName &p_type = StringName()) const;
  331. int get_constant(const StringName &p_name, const StringName &p_type = StringName()) const;
  332. bool has_icon_override(const StringName &p_name) const;
  333. bool has_shader_override(const StringName &p_name) const;
  334. bool has_stylebox_override(const StringName &p_name) const;
  335. bool has_font_override(const StringName &p_name) const;
  336. bool has_color_override(const StringName &p_name) const;
  337. bool has_constant_override(const StringName &p_name) const;
  338. bool has_icon(const StringName &p_name, const StringName &p_type = StringName()) const;
  339. bool has_shader(const StringName &p_name, const StringName &p_type = StringName()) const;
  340. bool has_stylebox(const StringName &p_name, const StringName &p_type = StringName()) const;
  341. bool has_font(const StringName &p_name, const StringName &p_type = StringName()) const;
  342. bool has_color(const StringName &p_name, const StringName &p_type = StringName()) const;
  343. bool has_constant(const StringName &p_name, const StringName &p_type = StringName()) const;
  344. /* TOOLTIP */
  345. void set_tooltip(const String &p_tooltip);
  346. virtual String get_tooltip(const Point2 &p_pos) const;
  347. virtual Control *make_custom_tooltip(const String &p_text) const;
  348. /* CURSOR */
  349. void set_default_cursor_shape(CursorShape p_shape);
  350. CursorShape get_default_cursor_shape() const;
  351. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const;
  352. virtual Transform2D get_transform() const;
  353. bool is_toplevel_control() const;
  354. Size2 get_parent_area_size() const;
  355. Rect2 get_parent_anchorable_rect() const;
  356. void grab_click_focus();
  357. void warp_mouse(const Point2 &p_to_pos);
  358. virtual bool is_text_field() const;
  359. Control *get_root_parent_control() const;
  360. void set_clip_contents(bool p_clip);
  361. bool is_clipping_contents();
  362. void set_block_minimum_size_adjust(bool p_block);
  363. bool is_minimum_size_adjust_blocked() const;
  364. void set_disable_visibility_clip(bool p_ignore);
  365. bool is_visibility_clip_disabled() const;
  366. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  367. Control();
  368. ~Control();
  369. };
  370. VARIANT_ENUM_CAST(Control::FocusMode);
  371. VARIANT_ENUM_CAST(Control::SizeFlags);
  372. VARIANT_ENUM_CAST(Control::CursorShape);
  373. VARIANT_ENUM_CAST(Control::LayoutPreset);
  374. VARIANT_ENUM_CAST(Control::LayoutPresetMode);
  375. VARIANT_ENUM_CAST(Control::MouseFilter);
  376. VARIANT_ENUM_CAST(Control::GrowDirection);
  377. VARIANT_ENUM_CAST(Control::Anchor);
  378. #endif