control.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <reduzio@gmail.com>
  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. Map<Ref<Font>, int> font_refcount;
  176. } data;
  177. // used internally
  178. Control *_find_control_at_pos(CanvasItem *p_node, const Point2 &p_pos, const Transform2D &p_xform, Transform2D &r_inv_xform);
  179. 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);
  180. Control *_get_focus_neighbour(Margin p_margin, int p_count = 0);
  181. void _set_anchor(Margin p_margin, float p_anchor);
  182. void _propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign = true);
  183. void _theme_changed();
  184. void _change_notify_margins();
  185. void _update_minimum_size();
  186. void _update_scroll();
  187. void _resize(const Size2 &p_size);
  188. Rect2 _compute_child_rect(const float p_anchors[4], const float p_margins[4]) const;
  189. void _compute_margins(Rect2 p_rect, const float p_anchors[4], float (&r_margins)[4]);
  190. void _size_changed();
  191. String _get_tooltip() const;
  192. void _ref_font(Ref<Font> p_sc);
  193. void _unref_font(Ref<Font> p_sc);
  194. void _font_changed();
  195. void _update_canvas_item_transform();
  196. Transform2D _get_internal_transform() const;
  197. friend class Viewport;
  198. void _modal_stack_remove();
  199. void _modal_set_prev_focus_owner(ObjectID p_prev);
  200. void _update_minimum_size_cache();
  201. protected:
  202. virtual void add_child_notify(Node *p_child);
  203. virtual void remove_child_notify(Node *p_child);
  204. //virtual void _window_gui_input(InputEvent p_event);
  205. bool _set(const StringName &p_name, const Variant &p_value);
  206. bool _get(const StringName &p_name, Variant &r_ret) const;
  207. void _get_property_list(List<PropertyInfo> *p_list) const;
  208. void _notification(int p_notification);
  209. static void _bind_methods();
  210. //bind helpers
  211. public:
  212. enum {
  213. /* NOTIFICATION_DRAW=30,
  214. NOTIFICATION_VISIBILITY_CHANGED=38*/
  215. NOTIFICATION_RESIZED = 40,
  216. NOTIFICATION_MOUSE_ENTER = 41,
  217. NOTIFICATION_MOUSE_EXIT = 42,
  218. NOTIFICATION_FOCUS_ENTER = 43,
  219. NOTIFICATION_FOCUS_EXIT = 44,
  220. NOTIFICATION_THEME_CHANGED = 45,
  221. NOTIFICATION_MODAL_CLOSE = 46,
  222. NOTIFICATION_SCROLL_BEGIN = 47,
  223. NOTIFICATION_SCROLL_END = 48,
  224. };
  225. /* EDITOR */
  226. virtual Dictionary _edit_get_state() const;
  227. virtual void _edit_set_state(const Dictionary &p_state);
  228. virtual void _edit_set_position(const Point2 &p_position);
  229. virtual Point2 _edit_get_position() const;
  230. virtual void _edit_set_scale(const Size2 &p_scale);
  231. virtual Size2 _edit_get_scale() const;
  232. virtual void _edit_set_rect(const Rect2 &p_edit_rect);
  233. virtual Rect2 _edit_get_rect() const;
  234. virtual bool _edit_use_rect() const;
  235. virtual void _edit_set_rotation(float p_rotation);
  236. virtual float _edit_get_rotation() const;
  237. virtual bool _edit_use_rotation() const;
  238. virtual void _edit_set_pivot(const Point2 &p_pivot);
  239. virtual Point2 _edit_get_pivot() const;
  240. virtual bool _edit_use_pivot() const;
  241. virtual Size2 _edit_get_minimum_size() const;
  242. void accept_event();
  243. virtual Size2 get_minimum_size() const;
  244. virtual Size2 get_combined_minimum_size() const;
  245. virtual bool has_point(const Point2 &p_point) const;
  246. virtual bool clips_input() const;
  247. virtual void set_drag_forwarding(Control *p_target);
  248. virtual Variant get_drag_data(const Point2 &p_point);
  249. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  250. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  251. void set_drag_preview(Control *p_control);
  252. void force_drag(const Variant &p_data, Control *p_control);
  253. void set_custom_minimum_size(const Size2 &p_custom);
  254. Size2 get_custom_minimum_size() const;
  255. bool is_window_modal_on_top() const;
  256. uint64_t get_modal_frame() const; //frame in which this was made modal
  257. Control *get_parent_control() const;
  258. /* POSITIONING */
  259. void set_anchors_preset(LayoutPreset p_preset, bool p_keep_margin = true);
  260. void set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
  261. void set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
  262. void set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin = true, bool p_push_opposite_anchor = true);
  263. float get_anchor(Margin p_margin) const;
  264. void set_margin(Margin p_margin, float p_value);
  265. float get_margin(Margin p_margin) const;
  266. void set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor = true);
  267. void set_begin(const Point2 &p_point); // helper
  268. void set_end(const Point2 &p_point); // helper
  269. Point2 get_begin() const;
  270. Point2 get_end() const;
  271. void set_position(const Point2 &p_point);
  272. void set_global_position(const Point2 &p_point);
  273. Point2 get_position() const;
  274. Point2 get_global_position() const;
  275. void set_size(const Size2 &p_size);
  276. Size2 get_size() const;
  277. Rect2 get_rect() const;
  278. Rect2 get_global_rect() const;
  279. Rect2 get_window_rect() const; ///< use with care, as it blocks waiting for the visual server
  280. Rect2 get_anchorable_rect() const;
  281. void set_rotation(float p_radians);
  282. void set_rotation_degrees(float p_degrees);
  283. float get_rotation() const;
  284. float get_rotation_degrees() const;
  285. void set_h_grow_direction(GrowDirection p_direction);
  286. GrowDirection get_h_grow_direction() const;
  287. void set_v_grow_direction(GrowDirection p_direction);
  288. GrowDirection get_v_grow_direction() const;
  289. void set_pivot_offset(const Vector2 &p_pivot);
  290. Vector2 get_pivot_offset() const;
  291. void set_scale(const Vector2 &p_scale);
  292. Vector2 get_scale() const;
  293. void show_modal(bool p_exclusive = false);
  294. void set_theme(const Ref<Theme> &p_theme);
  295. Ref<Theme> get_theme() const;
  296. void set_h_size_flags(int p_flags);
  297. int get_h_size_flags() const;
  298. void set_v_size_flags(int p_flags);
  299. int get_v_size_flags() const;
  300. void set_stretch_ratio(float p_ratio);
  301. float get_stretch_ratio() const;
  302. void minimum_size_changed();
  303. /* FOCUS */
  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. Control *find_next_valid_focus() const;
  310. Control *find_prev_valid_focus() const;
  311. void set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour);
  312. NodePath get_focus_neighbour(Margin p_margin) const;
  313. void set_focus_next(const NodePath &p_next);
  314. NodePath get_focus_next() const;
  315. void set_focus_previous(const NodePath &p_prev);
  316. NodePath get_focus_previous() const;
  317. Control *get_focus_owner() const;
  318. void set_mouse_filter(MouseFilter p_filter);
  319. MouseFilter get_mouse_filter() const;
  320. void set_pass_on_modal_close_click(bool p_pass_on);
  321. bool pass_on_modal_close_click() const;
  322. /* SKINNING */
  323. void add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon);
  324. void add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader);
  325. void add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
  326. void add_font_override(const StringName &p_name, const Ref<Font> &p_font);
  327. void add_color_override(const StringName &p_name, const Color &p_color);
  328. void add_constant_override(const StringName &p_name, int p_constant);
  329. Ref<Texture> get_icon(const StringName &p_name, const StringName &p_type = StringName()) const;
  330. Ref<Shader> get_shader(const StringName &p_name, const StringName &p_type = StringName()) const;
  331. Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_type = StringName()) const;
  332. Ref<Font> get_font(const StringName &p_name, const StringName &p_type = StringName()) const;
  333. Color get_color(const StringName &p_name, const StringName &p_type = StringName()) const;
  334. int get_constant(const StringName &p_name, const StringName &p_type = StringName()) const;
  335. bool has_icon_override(const StringName &p_name) const;
  336. bool has_shader_override(const StringName &p_name) const;
  337. bool has_stylebox_override(const StringName &p_name) const;
  338. bool has_font_override(const StringName &p_name) const;
  339. bool has_color_override(const StringName &p_name) const;
  340. bool has_constant_override(const StringName &p_name) const;
  341. bool has_icon(const StringName &p_name, const StringName &p_type = StringName()) const;
  342. bool has_shader(const StringName &p_name, const StringName &p_type = StringName()) const;
  343. bool has_stylebox(const StringName &p_name, const StringName &p_type = StringName()) const;
  344. bool has_font(const StringName &p_name, const StringName &p_type = StringName()) const;
  345. bool has_color(const StringName &p_name, const StringName &p_type = StringName()) const;
  346. bool has_constant(const StringName &p_name, const StringName &p_type = StringName()) const;
  347. /* TOOLTIP */
  348. void set_tooltip(const String &p_tooltip);
  349. virtual String get_tooltip(const Point2 &p_pos) const;
  350. virtual Control *make_custom_tooltip(const String &p_text) const;
  351. /* CURSOR */
  352. void set_default_cursor_shape(CursorShape p_shape);
  353. CursorShape get_default_cursor_shape() const;
  354. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const;
  355. virtual Transform2D get_transform() const;
  356. bool is_toplevel_control() const;
  357. Size2 get_parent_area_size() const;
  358. Rect2 get_parent_anchorable_rect() const;
  359. void grab_click_focus();
  360. void warp_mouse(const Point2 &p_to_pos);
  361. virtual bool is_text_field() const;
  362. Control *get_root_parent_control() const;
  363. void set_clip_contents(bool p_clip);
  364. bool is_clipping_contents();
  365. void set_block_minimum_size_adjust(bool p_block);
  366. bool is_minimum_size_adjust_blocked() const;
  367. void set_disable_visibility_clip(bool p_ignore);
  368. bool is_visibility_clip_disabled() const;
  369. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  370. Control();
  371. ~Control();
  372. };
  373. VARIANT_ENUM_CAST(Control::FocusMode);
  374. VARIANT_ENUM_CAST(Control::SizeFlags);
  375. VARIANT_ENUM_CAST(Control::CursorShape);
  376. VARIANT_ENUM_CAST(Control::LayoutPreset);
  377. VARIANT_ENUM_CAST(Control::LayoutPresetMode);
  378. VARIANT_ENUM_CAST(Control::MouseFilter);
  379. VARIANT_ENUM_CAST(Control::GrowDirection);
  380. VARIANT_ENUM_CAST(Control::Anchor);
  381. #endif