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