control.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**************************************************************************/
  2. /* control.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. class Viewport;
  40. class Label;
  41. class Panel;
  42. class Control : public CanvasItem {
  43. GDCLASS(Control, CanvasItem);
  44. OBJ_CATEGORY("GUI Nodes");
  45. public:
  46. enum Anchor {
  47. ANCHOR_BEGIN = 0,
  48. ANCHOR_END = 1
  49. };
  50. enum GrowDirection {
  51. GROW_DIRECTION_BEGIN,
  52. GROW_DIRECTION_END,
  53. GROW_DIRECTION_BOTH
  54. };
  55. enum FocusMode {
  56. FOCUS_NONE,
  57. FOCUS_CLICK,
  58. FOCUS_ALL
  59. };
  60. enum SizeFlags {
  61. SIZE_FILL = 1,
  62. SIZE_EXPAND = 2,
  63. SIZE_EXPAND_FILL = SIZE_EXPAND | SIZE_FILL,
  64. SIZE_SHRINK_CENTER = 4, //ignored by expand or fill
  65. SIZE_SHRINK_END = 8, //ignored by expand or fill
  66. };
  67. enum MouseFilter {
  68. MOUSE_FILTER_STOP,
  69. MOUSE_FILTER_PASS,
  70. MOUSE_FILTER_IGNORE
  71. };
  72. enum CursorShape {
  73. CURSOR_ARROW,
  74. CURSOR_IBEAM,
  75. CURSOR_POINTING_HAND,
  76. CURSOR_CROSS,
  77. CURSOR_WAIT,
  78. CURSOR_BUSY,
  79. CURSOR_DRAG,
  80. CURSOR_CAN_DROP,
  81. CURSOR_FORBIDDEN,
  82. CURSOR_VSIZE,
  83. CURSOR_HSIZE,
  84. CURSOR_BDIAGSIZE,
  85. CURSOR_FDIAGSIZE,
  86. CURSOR_MOVE,
  87. CURSOR_VSPLIT,
  88. CURSOR_HSPLIT,
  89. CURSOR_HELP,
  90. CURSOR_MAX
  91. };
  92. enum LayoutPreset {
  93. PRESET_TOP_LEFT,
  94. PRESET_TOP_RIGHT,
  95. PRESET_BOTTOM_LEFT,
  96. PRESET_BOTTOM_RIGHT,
  97. PRESET_CENTER_LEFT,
  98. PRESET_CENTER_TOP,
  99. PRESET_CENTER_RIGHT,
  100. PRESET_CENTER_BOTTOM,
  101. PRESET_CENTER,
  102. PRESET_LEFT_WIDE,
  103. PRESET_TOP_WIDE,
  104. PRESET_RIGHT_WIDE,
  105. PRESET_BOTTOM_WIDE,
  106. PRESET_VCENTER_WIDE,
  107. PRESET_HCENTER_WIDE,
  108. PRESET_WIDE
  109. };
  110. enum LayoutPresetMode {
  111. PRESET_MODE_MINSIZE,
  112. PRESET_MODE_KEEP_WIDTH,
  113. PRESET_MODE_KEEP_HEIGHT,
  114. PRESET_MODE_KEEP_SIZE
  115. };
  116. private:
  117. struct CComparator {
  118. bool operator()(const Control *p_a, const Control *p_b) const {
  119. if (p_a->get_canvas_layer() == p_b->get_canvas_layer()) {
  120. return p_b->is_greater_than(p_a);
  121. }
  122. return p_a->get_canvas_layer() < p_b->get_canvas_layer();
  123. }
  124. };
  125. struct Data {
  126. Point2 pos_cache;
  127. Size2 size_cache;
  128. Size2 minimum_size_cache;
  129. bool minimum_size_valid;
  130. Size2 last_minimum_size;
  131. bool updating_last_minimum_size;
  132. float margin[4];
  133. float anchor[4];
  134. FocusMode focus_mode;
  135. GrowDirection h_grow;
  136. GrowDirection v_grow;
  137. float rotation;
  138. Vector2 scale;
  139. Vector2 pivot_offset;
  140. bool pending_resize;
  141. int h_size_flags;
  142. int v_size_flags;
  143. float expand;
  144. Point2 custom_minimum_size;
  145. bool pass_on_modal_close_click;
  146. MouseFilter mouse_filter;
  147. bool clip_contents;
  148. bool block_minimum_size_adjust;
  149. bool disable_visibility_clip;
  150. Control *parent;
  151. ObjectID drag_owner;
  152. bool modal_exclusive;
  153. uint64_t modal_frame; //frame used to put something as modal
  154. Ref<Theme> theme;
  155. Control *theme_owner;
  156. StringName theme_type_variation;
  157. String tooltip;
  158. CursorShape default_cursor;
  159. List<Control *>::Element *MI; //modal item
  160. List<Control *>::Element *SI;
  161. List<Control *>::Element *RI;
  162. CanvasItem *parent_canvas_item;
  163. ObjectID modal_prev_focus_owner;
  164. NodePath focus_neighbour[4];
  165. NodePath focus_next;
  166. NodePath focus_prev;
  167. HashMap<StringName, Ref<Texture>> icon_override;
  168. HashMap<StringName, Ref<Shader>> shader_override;
  169. HashMap<StringName, Ref<StyleBox>> style_override;
  170. HashMap<StringName, Ref<Font>> font_override;
  171. HashMap<StringName, Color> color_override;
  172. HashMap<StringName, int> constant_override;
  173. mutable HashMap<StringName, HashMap<StringName, Ref<Texture>>> theme_icon_cache;
  174. mutable HashMap<StringName, HashMap<StringName, Ref<StyleBox>>> theme_style_cache;
  175. mutable HashMap<StringName, HashMap<StringName, Ref<Font>>> theme_font_cache;
  176. mutable HashMap<StringName, HashMap<StringName, Color>> theme_color_cache;
  177. mutable HashMap<StringName, HashMap<StringName, int>> theme_constant_cache;
  178. } data;
  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 _set_position(const Point2 &p_point);
  183. void _set_global_position(const Point2 &p_point);
  184. void _set_size(const Size2 &p_size);
  185. void _propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign = true);
  186. void _theme_changed();
  187. void _invalidate_theme_cache();
  188. void _change_notify_margins();
  189. void _update_minimum_size();
  190. void _resize(const Size2 &p_size);
  191. void _compute_margins(Rect2 p_rect, const float p_anchors[4], float (&r_margins)[4]);
  192. void _compute_anchors(Rect2 p_rect, const float p_margins[4], float (&r_anchors)[4]);
  193. void _size_changed();
  194. String _get_tooltip() const;
  195. void _override_changed();
  196. void _update_canvas_item_transform();
  197. Transform2D _get_internal_transform() const;
  198. friend class Viewport;
  199. void _modal_stack_remove();
  200. void _modal_set_prev_focus_owner(ObjectID p_prev);
  201. void _update_minimum_size_cache();
  202. template <class T>
  203. static T get_theme_item_in_types(Control *p_theme_owner, Theme::DataType p_data_type, const StringName &p_name, List<StringName> p_theme_types);
  204. static bool has_theme_item_in_types(Control *p_theme_owner, Theme::DataType p_data_type, const StringName &p_name, List<StringName> p_theme_types);
  205. _FORCE_INLINE_ void _get_theme_type_dependencies(const StringName &p_theme_type, List<StringName> *p_list) const;
  206. protected:
  207. virtual void add_child_notify(Node *p_child);
  208. virtual void remove_child_notify(Node *p_child);
  209. //virtual void _window_gui_input(InputEvent p_event);
  210. void set_modal_exclusive(bool p_exclusive);
  211. bool _set(const StringName &p_name, const Variant &p_value);
  212. bool _get(const StringName &p_name, Variant &r_ret) const;
  213. void _get_property_list(List<PropertyInfo> *p_list) const;
  214. void _notification(int p_notification);
  215. static void _bind_methods();
  216. virtual void _validate_property(PropertyInfo &property) const;
  217. //bind helpers
  218. public:
  219. enum {
  220. /* NOTIFICATION_DRAW=30,
  221. NOTIFICATION_VISIBILITY_CHANGED=38*/
  222. NOTIFICATION_RESIZED = 40,
  223. NOTIFICATION_MOUSE_ENTER = 41,
  224. NOTIFICATION_MOUSE_EXIT = 42,
  225. NOTIFICATION_FOCUS_ENTER = 43,
  226. NOTIFICATION_FOCUS_EXIT = 44,
  227. NOTIFICATION_THEME_CHANGED = 45,
  228. NOTIFICATION_MODAL_CLOSE = 46,
  229. NOTIFICATION_SCROLL_BEGIN = 47,
  230. NOTIFICATION_SCROLL_END = 48,
  231. };
  232. /* EDITOR */
  233. #ifdef TOOLS_ENABLED
  234. virtual Dictionary _edit_get_state() const;
  235. virtual void _edit_set_state(const Dictionary &p_state);
  236. virtual void _edit_set_position(const Point2 &p_position);
  237. virtual Point2 _edit_get_position() const;
  238. virtual void _edit_set_scale(const Size2 &p_scale);
  239. virtual Size2 _edit_get_scale() const;
  240. virtual void _edit_set_rect(const Rect2 &p_edit_rect);
  241. virtual Rect2 _edit_get_rect() const;
  242. virtual bool _edit_use_rect() const;
  243. virtual void _edit_set_rotation(float p_rotation);
  244. virtual float _edit_get_rotation() const;
  245. virtual bool _edit_use_rotation() const;
  246. virtual void _edit_set_pivot(const Point2 &p_pivot);
  247. virtual Point2 _edit_get_pivot() const;
  248. virtual bool _edit_use_pivot() const;
  249. virtual Size2 _edit_get_minimum_size() const;
  250. #endif
  251. void accept_event();
  252. virtual Size2 get_minimum_size() const;
  253. virtual Size2 get_combined_minimum_size() const;
  254. virtual bool has_point(const Point2 &p_point) const;
  255. virtual bool clips_input() const;
  256. virtual void set_drag_forwarding(Control *p_target);
  257. virtual Variant get_drag_data(const Point2 &p_point);
  258. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  259. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  260. void set_drag_preview(Control *p_control);
  261. void force_drag(const Variant &p_data, Control *p_control);
  262. bool is_drag_successful() const;
  263. void set_custom_minimum_size(const Size2 &p_custom);
  264. Size2 get_custom_minimum_size() const;
  265. bool is_window_modal_on_top() const;
  266. uint64_t get_modal_frame() const; //frame in which this was made modal
  267. Control *get_parent_control() const;
  268. /* POSITIONING */
  269. void set_anchors_preset(LayoutPreset p_preset, bool p_keep_margins = true);
  270. void set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
  271. void set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
  272. void set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin = true, bool p_push_opposite_anchor = true);
  273. float get_anchor(Margin p_margin) const;
  274. void set_margin(Margin p_margin, float p_value);
  275. float get_margin(Margin p_margin) const;
  276. void set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor = true);
  277. void set_begin(const Point2 &p_point); // helper
  278. void set_end(const Point2 &p_point); // helper
  279. Point2 get_begin() const;
  280. Point2 get_end() const;
  281. void set_position(const Point2 &p_point, bool p_keep_margins = false);
  282. void set_global_position(const Point2 &p_point, bool p_keep_margins = false);
  283. Point2 get_position() const;
  284. Point2 get_global_position() const;
  285. void set_size(const Size2 &p_size, bool p_keep_margins = false);
  286. Size2 get_size() const;
  287. Rect2 get_rect() const;
  288. Rect2 get_global_rect() const;
  289. Rect2 get_window_rect() const; ///< use with care, as it blocks waiting for the visual server
  290. Rect2 get_anchorable_rect() const;
  291. void set_rotation(float p_radians);
  292. void set_rotation_degrees(float p_degrees);
  293. float get_rotation() const;
  294. float get_rotation_degrees() const;
  295. void set_h_grow_direction(GrowDirection p_direction);
  296. GrowDirection get_h_grow_direction() const;
  297. void set_v_grow_direction(GrowDirection p_direction);
  298. GrowDirection get_v_grow_direction() const;
  299. void set_pivot_offset(const Vector2 &p_pivot);
  300. Vector2 get_pivot_offset() const;
  301. void set_scale(const Vector2 &p_scale);
  302. Vector2 get_scale() const;
  303. void show_modal(bool p_exclusive = false);
  304. void set_theme(const Ref<Theme> &p_theme);
  305. Ref<Theme> get_theme() const;
  306. void set_theme_type_variation(const StringName &p_theme_type);
  307. StringName get_theme_type_variation() const;
  308. void set_h_size_flags(int p_flags);
  309. int get_h_size_flags() const;
  310. void set_v_size_flags(int p_flags);
  311. int get_v_size_flags() const;
  312. void set_stretch_ratio(float p_ratio);
  313. float get_stretch_ratio() const;
  314. void minimum_size_changed();
  315. /* FOCUS */
  316. void set_focus_mode(FocusMode p_focus_mode);
  317. FocusMode get_focus_mode() const;
  318. bool has_focus() const;
  319. void grab_focus();
  320. void release_focus();
  321. Control *find_next_valid_focus() const;
  322. Control *find_prev_valid_focus() const;
  323. void set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour);
  324. NodePath get_focus_neighbour(Margin p_margin) const;
  325. void set_focus_next(const NodePath &p_next);
  326. NodePath get_focus_next() const;
  327. void set_focus_previous(const NodePath &p_prev);
  328. NodePath get_focus_previous() const;
  329. Control *get_focus_owner() const;
  330. void set_mouse_filter(MouseFilter p_filter);
  331. MouseFilter get_mouse_filter() const;
  332. void set_pass_on_modal_close_click(bool p_pass_on);
  333. bool get_pass_on_modal_close_click() const;
  334. /* SKINNING */
  335. void add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon);
  336. void add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader);
  337. void add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
  338. void add_font_override(const StringName &p_name, const Ref<Font> &p_font);
  339. void add_color_override(const StringName &p_name, const Color &p_color);
  340. void add_constant_override(const StringName &p_name, int p_constant);
  341. void remove_icon_override(const StringName &p_name);
  342. void remove_shader_override(const StringName &p_name);
  343. void remove_stylebox_override(const StringName &p_name);
  344. void remove_font_override(const StringName &p_name);
  345. void remove_color_override(const StringName &p_name);
  346. void remove_constant_override(const StringName &p_name);
  347. Ref<Texture> get_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  348. Ref<Shader> get_shader(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  349. Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  350. Ref<Font> get_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  351. Color get_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  352. int get_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  353. bool has_icon_override(const StringName &p_name) const;
  354. bool has_shader_override(const StringName &p_name) const;
  355. bool has_stylebox_override(const StringName &p_name) const;
  356. bool has_font_override(const StringName &p_name) const;
  357. bool has_color_override(const StringName &p_name) const;
  358. bool has_constant_override(const StringName &p_name) const;
  359. bool has_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  360. bool has_shader(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  361. bool has_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  362. bool has_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  363. bool has_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  364. bool has_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  365. Ref<Font> get_theme_default_font() const;
  366. /* TOOLTIP */
  367. void set_tooltip(const String &p_tooltip);
  368. virtual String get_tooltip(const Point2 &p_pos) const;
  369. virtual Control *make_custom_tooltip(const String &p_text) const;
  370. /* CURSOR */
  371. void set_default_cursor_shape(CursorShape p_shape);
  372. CursorShape get_default_cursor_shape() const;
  373. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const;
  374. virtual Transform2D get_transform() const;
  375. bool is_toplevel_control() const;
  376. Size2 get_parent_area_size() const;
  377. Rect2 get_parent_anchorable_rect() const;
  378. void grab_click_focus();
  379. void warp_mouse(const Point2 &p_to_pos);
  380. virtual bool is_text_field() const;
  381. Control *get_root_parent_control() const;
  382. void set_clip_contents(bool p_clip);
  383. bool is_clipping_contents();
  384. void set_block_minimum_size_adjust(bool p_block);
  385. bool is_minimum_size_adjust_blocked() const;
  386. void set_disable_visibility_clip(bool p_ignore);
  387. bool is_visibility_clip_disabled() const;
  388. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  389. virtual String get_configuration_warning() const;
  390. void _query_order_update(bool &r_subwindow_order_dirty, bool &r_root_order_dirty) const;
  391. Control();
  392. ~Control();
  393. };
  394. VARIANT_ENUM_CAST(Control::FocusMode);
  395. VARIANT_ENUM_CAST(Control::SizeFlags);
  396. VARIANT_ENUM_CAST(Control::CursorShape);
  397. VARIANT_ENUM_CAST(Control::LayoutPreset);
  398. VARIANT_ENUM_CAST(Control::LayoutPresetMode);
  399. VARIANT_ENUM_CAST(Control::MouseFilter);
  400. VARIANT_ENUM_CAST(Control::GrowDirection);
  401. VARIANT_ENUM_CAST(Control::Anchor);
  402. #endif // CONTROL_H