1 /*************************************************************************/
2 /*  control.cpp                                                          */
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 #include "control.h"
32 
33 #include "core/message_queue.h"
34 #include "core/os/keyboard.h"
35 #include "core/os/os.h"
36 #include "core/print_string.h"
37 #include "core/project_settings.h"
38 #include "scene/gui/label.h"
39 #include "scene/gui/panel.h"
40 #include "scene/main/canvas_layer.h"
41 #include "scene/main/viewport.h"
42 #include "scene/scene_string_names.h"
43 #include "servers/visual_server.h"
44 
45 #ifdef TOOLS_ENABLED
46 #include "editor/editor_settings.h"
47 #include "editor/plugins/canvas_item_editor_plugin.h"
48 #endif
49 
50 #ifdef TOOLS_ENABLED
_edit_get_state() const51 Dictionary Control::_edit_get_state() const {
52 
53 	Dictionary s;
54 	s["rotation"] = get_rotation();
55 	s["scale"] = get_scale();
56 	s["pivot"] = get_pivot_offset();
57 	Array anchors;
58 	anchors.push_back(get_anchor(MARGIN_LEFT));
59 	anchors.push_back(get_anchor(MARGIN_TOP));
60 	anchors.push_back(get_anchor(MARGIN_RIGHT));
61 	anchors.push_back(get_anchor(MARGIN_BOTTOM));
62 	s["anchors"] = anchors;
63 	Array margins;
64 	margins.push_back(get_margin(MARGIN_LEFT));
65 	margins.push_back(get_margin(MARGIN_TOP));
66 	margins.push_back(get_margin(MARGIN_RIGHT));
67 	margins.push_back(get_margin(MARGIN_BOTTOM));
68 	s["margins"] = margins;
69 	return s;
70 }
71 
_edit_set_state(const Dictionary & p_state)72 void Control::_edit_set_state(const Dictionary &p_state) {
73 
74 	Dictionary state = p_state;
75 
76 	set_rotation(state["rotation"]);
77 	set_scale(state["scale"]);
78 	set_pivot_offset(state["pivot"]);
79 	Array anchors = state["anchors"];
80 	data.anchor[MARGIN_LEFT] = anchors[0];
81 	data.anchor[MARGIN_TOP] = anchors[1];
82 	data.anchor[MARGIN_RIGHT] = anchors[2];
83 	data.anchor[MARGIN_BOTTOM] = anchors[3];
84 	Array margins = state["margins"];
85 	data.margin[MARGIN_LEFT] = margins[0];
86 	data.margin[MARGIN_TOP] = margins[1];
87 	data.margin[MARGIN_RIGHT] = margins[2];
88 	data.margin[MARGIN_BOTTOM] = margins[3];
89 	_size_changed();
90 }
91 
_edit_set_position(const Point2 & p_position)92 void Control::_edit_set_position(const Point2 &p_position) {
93 #ifdef TOOLS_ENABLED
94 	set_position(p_position, CanvasItemEditor::get_singleton()->is_anchors_mode_enabled());
95 #else
96 	// Unlikely to happen. TODO: enclose all _edit_ functions into TOOLS_ENABLED
97 	set_position(p_position);
98 #endif
99 };
100 
_edit_get_position() const101 Point2 Control::_edit_get_position() const {
102 	return get_position();
103 };
104 
_edit_set_scale(const Size2 & p_scale)105 void Control::_edit_set_scale(const Size2 &p_scale) {
106 	set_scale(p_scale);
107 }
108 
_edit_get_scale() const109 Size2 Control::_edit_get_scale() const {
110 	return data.scale;
111 }
112 
_edit_set_rect(const Rect2 & p_edit_rect)113 void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
114 #ifdef TOOLS_ENABLED
115 	set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snapped(Vector2(1, 1)), CanvasItemEditor::get_singleton()->is_anchors_mode_enabled());
116 	set_size(p_edit_rect.size.snapped(Vector2(1, 1)), CanvasItemEditor::get_singleton()->is_anchors_mode_enabled());
117 #else
118 	// Unlikely to happen. TODO: enclose all _edit_ functions into TOOLS_ENABLED
119 	set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snapped(Vector2(1, 1)));
120 	set_size(p_edit_rect.size.snapped(Vector2(1, 1)));
121 #endif
122 }
123 
_edit_get_rect() const124 Rect2 Control::_edit_get_rect() const {
125 	return Rect2(Point2(), get_size());
126 }
127 
_edit_use_rect() const128 bool Control::_edit_use_rect() const {
129 	return true;
130 }
131 
_edit_set_rotation(float p_rotation)132 void Control::_edit_set_rotation(float p_rotation) {
133 	set_rotation(p_rotation);
134 }
135 
_edit_get_rotation() const136 float Control::_edit_get_rotation() const {
137 	return get_rotation();
138 }
139 
_edit_use_rotation() const140 bool Control::_edit_use_rotation() const {
141 	return true;
142 }
143 
_edit_set_pivot(const Point2 & p_pivot)144 void Control::_edit_set_pivot(const Point2 &p_pivot) {
145 	Vector2 delta_pivot = p_pivot - get_pivot_offset();
146 	Vector2 move = Vector2((cos(data.rotation) - 1.0) * delta_pivot.x - sin(data.rotation) * delta_pivot.y, sin(data.rotation) * delta_pivot.x + (cos(data.rotation) - 1.0) * delta_pivot.y);
147 	set_position(get_position() + move);
148 	set_pivot_offset(p_pivot);
149 }
150 
_edit_get_pivot() const151 Point2 Control::_edit_get_pivot() const {
152 	return get_pivot_offset();
153 }
154 
_edit_use_pivot() const155 bool Control::_edit_use_pivot() const {
156 	return true;
157 }
158 
_edit_get_minimum_size() const159 Size2 Control::_edit_get_minimum_size() const {
160 	return get_combined_minimum_size();
161 }
162 #endif
163 
set_custom_minimum_size(const Size2 & p_custom)164 void Control::set_custom_minimum_size(const Size2 &p_custom) {
165 
166 	if (p_custom == data.custom_minimum_size)
167 		return;
168 	data.custom_minimum_size = p_custom;
169 	minimum_size_changed();
170 }
171 
get_custom_minimum_size() const172 Size2 Control::get_custom_minimum_size() const {
173 
174 	return data.custom_minimum_size;
175 }
176 
_update_minimum_size_cache()177 void Control::_update_minimum_size_cache() {
178 
179 	Size2 minsize = get_minimum_size();
180 	minsize.x = MAX(minsize.x, data.custom_minimum_size.x);
181 	minsize.y = MAX(minsize.y, data.custom_minimum_size.y);
182 
183 	bool size_changed = false;
184 	if (data.minimum_size_cache != minsize)
185 		size_changed = true;
186 
187 	data.minimum_size_cache = minsize;
188 	data.minimum_size_valid = true;
189 
190 	if (size_changed)
191 		minimum_size_changed();
192 }
193 
get_combined_minimum_size() const194 Size2 Control::get_combined_minimum_size() const {
195 
196 	if (!data.minimum_size_valid) {
197 		const_cast<Control *>(this)->_update_minimum_size_cache();
198 	}
199 	return data.minimum_size_cache;
200 }
201 
_get_internal_transform() const202 Transform2D Control::_get_internal_transform() const {
203 
204 	Transform2D rot_scale;
205 	rot_scale.set_rotation_and_scale(data.rotation, data.scale);
206 	Transform2D offset;
207 	offset.set_origin(-data.pivot_offset);
208 
209 	return offset.affine_inverse() * (rot_scale * offset);
210 }
211 
_set(const StringName & p_name,const Variant & p_value)212 bool Control::_set(const StringName &p_name, const Variant &p_value) {
213 
214 	String name = p_name;
215 	if (!name.begins_with("custom")) {
216 		return false;
217 	}
218 
219 	if (p_value.get_type() == Variant::NIL) {
220 
221 		if (name.begins_with("custom_icons/")) {
222 			String dname = name.get_slicec('/', 1);
223 			if (data.icon_override.has(dname)) {
224 				data.icon_override[dname]->disconnect("changed", this, "_override_changed");
225 			}
226 			data.icon_override.erase(dname);
227 			notification(NOTIFICATION_THEME_CHANGED);
228 		} else if (name.begins_with("custom_shaders/")) {
229 			String dname = name.get_slicec('/', 1);
230 			if (data.shader_override.has(dname)) {
231 				data.shader_override[dname]->disconnect("changed", this, "_override_changed");
232 			}
233 			data.shader_override.erase(dname);
234 			notification(NOTIFICATION_THEME_CHANGED);
235 		} else if (name.begins_with("custom_styles/")) {
236 			String dname = name.get_slicec('/', 1);
237 			if (data.style_override.has(dname)) {
238 				data.style_override[dname]->disconnect("changed", this, "_override_changed");
239 			}
240 			data.style_override.erase(dname);
241 			notification(NOTIFICATION_THEME_CHANGED);
242 		} else if (name.begins_with("custom_fonts/")) {
243 			String dname = name.get_slicec('/', 1);
244 			if (data.font_override.has(dname)) {
245 				data.font_override[dname]->disconnect("changed", this, "_override_changed");
246 			}
247 			data.font_override.erase(dname);
248 			notification(NOTIFICATION_THEME_CHANGED);
249 		} else if (name.begins_with("custom_colors/")) {
250 			String dname = name.get_slicec('/', 1);
251 			data.color_override.erase(dname);
252 			notification(NOTIFICATION_THEME_CHANGED);
253 		} else if (name.begins_with("custom_constants/")) {
254 			String dname = name.get_slicec('/', 1);
255 			data.constant_override.erase(dname);
256 			notification(NOTIFICATION_THEME_CHANGED);
257 		} else
258 			return false;
259 
260 	} else {
261 		if (name.begins_with("custom_icons/")) {
262 			String dname = name.get_slicec('/', 1);
263 			add_icon_override(dname, p_value);
264 		} else if (name.begins_with("custom_shaders/")) {
265 			String dname = name.get_slicec('/', 1);
266 			add_shader_override(dname, p_value);
267 		} else if (name.begins_with("custom_styles/")) {
268 			String dname = name.get_slicec('/', 1);
269 			add_style_override(dname, p_value);
270 		} else if (name.begins_with("custom_fonts/")) {
271 			String dname = name.get_slicec('/', 1);
272 			add_font_override(dname, p_value);
273 		} else if (name.begins_with("custom_colors/")) {
274 			String dname = name.get_slicec('/', 1);
275 			add_color_override(dname, p_value);
276 		} else if (name.begins_with("custom_constants/")) {
277 			String dname = name.get_slicec('/', 1);
278 			add_constant_override(dname, p_value);
279 		} else
280 			return false;
281 	}
282 	return true;
283 }
284 
_update_minimum_size()285 void Control::_update_minimum_size() {
286 
287 	if (!is_inside_tree())
288 		return;
289 
290 	Size2 minsize = get_combined_minimum_size();
291 	if (minsize.x > data.size_cache.x ||
292 			minsize.y > data.size_cache.y) {
293 		_size_changed();
294 	}
295 
296 	data.updating_last_minimum_size = false;
297 
298 	if (minsize != data.last_minimum_size) {
299 		data.last_minimum_size = minsize;
300 		emit_signal(SceneStringNames::get_singleton()->minimum_size_changed);
301 	}
302 }
303 
_get(const StringName & p_name,Variant & r_ret) const304 bool Control::_get(const StringName &p_name, Variant &r_ret) const {
305 
306 	String sname = p_name;
307 
308 	if (!sname.begins_with("custom")) {
309 		return false;
310 	}
311 
312 	if (sname.begins_with("custom_icons/")) {
313 		String name = sname.get_slicec('/', 1);
314 
315 		r_ret = data.icon_override.has(name) ? Variant(data.icon_override[name]) : Variant();
316 	} else if (sname.begins_with("custom_shaders/")) {
317 		String name = sname.get_slicec('/', 1);
318 
319 		r_ret = data.shader_override.has(name) ? Variant(data.shader_override[name]) : Variant();
320 	} else if (sname.begins_with("custom_styles/")) {
321 		String name = sname.get_slicec('/', 1);
322 
323 		r_ret = data.style_override.has(name) ? Variant(data.style_override[name]) : Variant();
324 	} else if (sname.begins_with("custom_fonts/")) {
325 		String name = sname.get_slicec('/', 1);
326 
327 		r_ret = data.font_override.has(name) ? Variant(data.font_override[name]) : Variant();
328 	} else if (sname.begins_with("custom_colors/")) {
329 		String name = sname.get_slicec('/', 1);
330 		r_ret = data.color_override.has(name) ? Variant(data.color_override[name]) : Variant();
331 	} else if (sname.begins_with("custom_constants/")) {
332 		String name = sname.get_slicec('/', 1);
333 
334 		r_ret = data.constant_override.has(name) ? Variant(data.constant_override[name]) : Variant();
335 	} else
336 		return false;
337 
338 	return true;
339 }
_get_property_list(List<PropertyInfo> * p_list) const340 void Control::_get_property_list(List<PropertyInfo> *p_list) const {
341 
342 	Ref<Theme> theme = Theme::get_default();
343 	/* Using the default theme since the properties below are meant for editor only
344 	if (data.theme.is_valid()) {
345 
346 		theme = data.theme;
347 	} else {
348 		theme = Theme::get_default();
349 
350 	}*/
351 
352 	{
353 		List<StringName> names;
354 		theme->get_icon_list(get_class_name(), &names);
355 		for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
356 
357 			uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
358 			if (data.icon_override.has(E->get()))
359 				hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
360 
361 			p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_icons/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Texture", hint));
362 		}
363 	}
364 	{
365 		List<StringName> names;
366 		theme->get_shader_list(get_class_name(), &names);
367 		for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
368 
369 			uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
370 			if (data.shader_override.has(E->get()))
371 				hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
372 
373 			p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_shaders/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Shader,VisualShader", hint));
374 		}
375 	}
376 	{
377 		List<StringName> names;
378 		theme->get_stylebox_list(get_class_name(), &names);
379 		for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
380 
381 			uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
382 			if (data.style_override.has(E->get()))
383 				hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
384 
385 			p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_styles/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", hint));
386 		}
387 	}
388 	{
389 		List<StringName> names;
390 		theme->get_font_list(get_class_name(), &names);
391 		for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
392 
393 			uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
394 			if (data.font_override.has(E->get()))
395 				hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
396 
397 			p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_fonts/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Font", hint));
398 		}
399 	}
400 	{
401 		List<StringName> names;
402 		theme->get_color_list(get_class_name(), &names);
403 		for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
404 
405 			uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
406 			if (data.color_override.has(E->get()))
407 				hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
408 
409 			p_list->push_back(PropertyInfo(Variant::COLOR, "custom_colors/" + E->get(), PROPERTY_HINT_NONE, "", hint));
410 		}
411 	}
412 	{
413 		List<StringName> names;
414 		theme->get_constant_list(get_class_name(), &names);
415 		for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
416 
417 			uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
418 			if (data.constant_override.has(E->get()))
419 				hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
420 
421 			p_list->push_back(PropertyInfo(Variant::INT, "custom_constants/" + E->get(), PROPERTY_HINT_RANGE, "-16384,16384", hint));
422 		}
423 	}
424 }
425 
get_parent_control() const426 Control *Control::get_parent_control() const {
427 
428 	return data.parent;
429 }
430 
_resize(const Size2 & p_size)431 void Control::_resize(const Size2 &p_size) {
432 
433 	_size_changed();
434 }
435 
436 //moved theme configuration here, so controls can set up even if still not inside active scene
437 
add_child_notify(Node * p_child)438 void Control::add_child_notify(Node *p_child) {
439 
440 	Control *child_c = Object::cast_to<Control>(p_child);
441 	if (!child_c)
442 		return;
443 
444 	if (child_c->data.theme.is_null() && data.theme_owner) {
445 		_propagate_theme_changed(child_c, data.theme_owner); //need to propagate here, since many controls may require setting up stuff
446 	}
447 }
448 
remove_child_notify(Node * p_child)449 void Control::remove_child_notify(Node *p_child) {
450 
451 	Control *child_c = Object::cast_to<Control>(p_child);
452 	if (!child_c)
453 		return;
454 
455 	if (child_c->data.theme_owner && child_c->data.theme.is_null()) {
456 		_propagate_theme_changed(child_c, NULL);
457 	}
458 }
459 
_update_canvas_item_transform()460 void Control::_update_canvas_item_transform() {
461 
462 	Transform2D xform = _get_internal_transform();
463 	xform[2] += get_position();
464 
465 	// We use a little workaround to avoid flickering when moving the pivot with _edit_set_pivot()
466 	if (is_inside_tree() && Math::abs(Math::sin(data.rotation * 4.0f)) < 0.00001f && get_viewport()->is_snap_controls_to_pixels_enabled()) {
467 		xform[2] = xform[2].round();
468 	}
469 
470 	VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), xform);
471 }
472 
_notification(int p_notification)473 void Control::_notification(int p_notification) {
474 
475 	switch (p_notification) {
476 
477 		case NOTIFICATION_ENTER_TREE: {
478 
479 		} break;
480 		case NOTIFICATION_POST_ENTER_TREE: {
481 			data.minimum_size_valid = false;
482 			_size_changed();
483 		} break;
484 		case NOTIFICATION_EXIT_TREE: {
485 
486 			get_viewport()->_gui_remove_control(this);
487 
488 		} break;
489 
490 		case NOTIFICATION_ENTER_CANVAS: {
491 
492 			data.parent = Object::cast_to<Control>(get_parent());
493 
494 			if (is_set_as_toplevel()) {
495 				data.SI = get_viewport()->_gui_add_subwindow_control(this);
496 
497 				if (data.theme.is_null() && data.parent && data.parent->data.theme_owner) {
498 					data.theme_owner = data.parent->data.theme_owner;
499 					notification(NOTIFICATION_THEME_CHANGED);
500 				}
501 
502 			} else {
503 
504 				Node *parent = this; //meh
505 				Control *parent_control = NULL;
506 				bool subwindow = false;
507 
508 				while (parent) {
509 
510 					parent = parent->get_parent();
511 
512 					if (!parent)
513 						break;
514 
515 					CanvasItem *ci = Object::cast_to<CanvasItem>(parent);
516 					if (ci && ci->is_set_as_toplevel()) {
517 						subwindow = true;
518 						break;
519 					}
520 
521 					parent_control = Object::cast_to<Control>(parent);
522 
523 					if (parent_control) {
524 						break;
525 					} else if (ci) {
526 
527 					} else {
528 						break;
529 					}
530 				}
531 
532 				if (parent_control) {
533 					//do nothing, has a parent control
534 					if (data.theme.is_null() && parent_control->data.theme_owner) {
535 						data.theme_owner = parent_control->data.theme_owner;
536 						notification(NOTIFICATION_THEME_CHANGED);
537 					}
538 				} else if (subwindow) {
539 					//is a subwindow (process input before other controls for that canvas)
540 					data.SI = get_viewport()->_gui_add_subwindow_control(this);
541 				} else {
542 					//is a regular root control
543 					data.RI = get_viewport()->_gui_add_root_control(this);
544 				}
545 
546 				data.parent_canvas_item = get_parent_item();
547 
548 				if (data.parent_canvas_item) {
549 
550 					data.parent_canvas_item->connect("item_rect_changed", this, "_size_changed");
551 				} else {
552 					//connect viewport
553 					get_viewport()->connect("size_changed", this, "_size_changed");
554 				}
555 			}
556 
557 			/*
558 			if (data.theme.is_null() && data.parent && data.parent->data.theme_owner) {
559 				data.theme_owner=data.parent->data.theme_owner;
560 				notification(NOTIFICATION_THEME_CHANGED);
561 			}
562 			*/
563 
564 		} break;
565 		case NOTIFICATION_EXIT_CANVAS: {
566 
567 			if (data.parent_canvas_item) {
568 
569 				data.parent_canvas_item->disconnect("item_rect_changed", this, "_size_changed");
570 				data.parent_canvas_item = NULL;
571 			} else if (!is_set_as_toplevel()) {
572 				//disconnect viewport
573 				get_viewport()->disconnect("size_changed", this, "_size_changed");
574 			}
575 
576 			if (data.MI) {
577 				get_viewport()->_gui_remove_modal_control(data.MI);
578 				data.MI = NULL;
579 			}
580 
581 			if (data.SI) {
582 				get_viewport()->_gui_remove_subwindow_control(data.SI);
583 				data.SI = NULL;
584 			}
585 
586 			if (data.RI) {
587 				get_viewport()->_gui_remove_root_control(data.RI);
588 				data.RI = NULL;
589 			}
590 
591 			data.parent = NULL;
592 			data.parent_canvas_item = NULL;
593 			/*
594 			if (data.theme_owner && data.theme.is_null()) {
595 				data.theme_owner=NULL;
596 				notification(NOTIFICATION_THEME_CHANGED);
597 			}
598 			*/
599 
600 		} break;
601 		case NOTIFICATION_MOVED_IN_PARENT: {
602 			// some parents need to know the order of the childrens to draw (like TabContainer)
603 			// update if necessary
604 			if (data.parent)
605 				data.parent->update();
606 			update();
607 
608 			if (data.SI) {
609 				get_viewport()->_gui_set_subwindow_order_dirty();
610 			}
611 			if (data.RI) {
612 				get_viewport()->_gui_set_root_order_dirty();
613 			}
614 
615 		} break;
616 		case NOTIFICATION_RESIZED: {
617 
618 			emit_signal(SceneStringNames::get_singleton()->resized);
619 		} break;
620 		case NOTIFICATION_DRAW: {
621 
622 			_update_canvas_item_transform();
623 			VisualServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), !data.disable_visibility_clip, Rect2(Point2(), get_size()));
624 			VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), data.clip_contents);
625 			//emit_signal(SceneStringNames::get_singleton()->draw);
626 
627 		} break;
628 		case NOTIFICATION_MOUSE_ENTER: {
629 
630 			emit_signal(SceneStringNames::get_singleton()->mouse_entered);
631 		} break;
632 		case NOTIFICATION_MOUSE_EXIT: {
633 
634 			emit_signal(SceneStringNames::get_singleton()->mouse_exited);
635 		} break;
636 		case NOTIFICATION_FOCUS_ENTER: {
637 
638 			emit_signal(SceneStringNames::get_singleton()->focus_entered);
639 			update();
640 		} break;
641 		case NOTIFICATION_FOCUS_EXIT: {
642 
643 			emit_signal(SceneStringNames::get_singleton()->focus_exited);
644 			update();
645 
646 		} break;
647 		case NOTIFICATION_THEME_CHANGED: {
648 
649 			minimum_size_changed();
650 			update();
651 		} break;
652 		case NOTIFICATION_MODAL_CLOSE: {
653 
654 			emit_signal("modal_closed");
655 		} break;
656 		case NOTIFICATION_VISIBILITY_CHANGED: {
657 
658 			if (!is_visible_in_tree()) {
659 
660 				if (get_viewport() != NULL)
661 					get_viewport()->_gui_hid_control(this);
662 
663 				if (is_inside_tree()) {
664 					_modal_stack_remove();
665 				}
666 
667 				//remove key focus
668 				//remove modalness
669 			} else {
670 				data.minimum_size_valid = false;
671 				_size_changed();
672 			}
673 
674 		} break;
675 		case SceneTree::NOTIFICATION_WM_UNFOCUS_REQUEST: {
676 
677 			get_viewport()->_gui_unfocus_control(this);
678 
679 		} break;
680 	}
681 }
682 
clips_input() const683 bool Control::clips_input() const {
684 
685 	if (get_script_instance()) {
686 		return get_script_instance()->call(SceneStringNames::get_singleton()->_clips_input);
687 	}
688 	return false;
689 }
has_point(const Point2 & p_point) const690 bool Control::has_point(const Point2 &p_point) const {
691 
692 	if (get_script_instance()) {
693 		Variant v = p_point;
694 		const Variant *p = &v;
695 		Variant::CallError ce;
696 		Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->has_point, &p, 1, ce);
697 		if (ce.error == Variant::CallError::CALL_OK) {
698 			return ret;
699 		}
700 	}
701 	/*if (has_stylebox("mask")) {
702 		Ref<StyleBox> mask = get_stylebox("mask");
703 		return mask->test_mask(p_point,Rect2(Point2(),get_size()));
704 	}*/
705 	return Rect2(Point2(), get_size()).has_point(p_point);
706 }
707 
set_drag_forwarding(Control * p_target)708 void Control::set_drag_forwarding(Control *p_target) {
709 
710 	if (p_target)
711 		data.drag_owner = p_target->get_instance_id();
712 	else
713 		data.drag_owner = 0;
714 }
715 
get_drag_data(const Point2 & p_point)716 Variant Control::get_drag_data(const Point2 &p_point) {
717 
718 	if (data.drag_owner) {
719 		Object *obj = ObjectDB::get_instance(data.drag_owner);
720 		if (obj) {
721 			Control *c = Object::cast_to<Control>(obj);
722 			return c->call("get_drag_data_fw", p_point, this);
723 		}
724 	}
725 
726 	if (get_script_instance()) {
727 		Variant v = p_point;
728 		const Variant *p = &v;
729 		Variant::CallError ce;
730 		Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->get_drag_data, &p, 1, ce);
731 		if (ce.error == Variant::CallError::CALL_OK)
732 			return ret;
733 	}
734 
735 	return Variant();
736 }
737 
can_drop_data(const Point2 & p_point,const Variant & p_data) const738 bool Control::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
739 
740 	if (data.drag_owner) {
741 		Object *obj = ObjectDB::get_instance(data.drag_owner);
742 		if (obj) {
743 			Control *c = Object::cast_to<Control>(obj);
744 			return c->call("can_drop_data_fw", p_point, p_data, this);
745 		}
746 	}
747 
748 	if (get_script_instance()) {
749 		Variant v = p_point;
750 		const Variant *p[2] = { &v, &p_data };
751 		Variant::CallError ce;
752 		Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->can_drop_data, p, 2, ce);
753 		if (ce.error == Variant::CallError::CALL_OK)
754 			return ret;
755 	}
756 
757 	return Variant();
758 }
drop_data(const Point2 & p_point,const Variant & p_data)759 void Control::drop_data(const Point2 &p_point, const Variant &p_data) {
760 
761 	if (data.drag_owner) {
762 		Object *obj = ObjectDB::get_instance(data.drag_owner);
763 		if (obj) {
764 			Control *c = Object::cast_to<Control>(obj);
765 			c->call("drop_data_fw", p_point, p_data, this);
766 			return;
767 		}
768 	}
769 
770 	if (get_script_instance()) {
771 		Variant v = p_point;
772 		const Variant *p[2] = { &v, &p_data };
773 		Variant::CallError ce;
774 		Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->drop_data, p, 2, ce);
775 		if (ce.error == Variant::CallError::CALL_OK)
776 			return;
777 	}
778 }
779 
force_drag(const Variant & p_data,Control * p_control)780 void Control::force_drag(const Variant &p_data, Control *p_control) {
781 
782 	ERR_FAIL_COND(!is_inside_tree());
783 	ERR_FAIL_COND(p_data.get_type() == Variant::NIL);
784 
785 	get_viewport()->_gui_force_drag(this, p_data, p_control);
786 }
787 
set_drag_preview(Control * p_control)788 void Control::set_drag_preview(Control *p_control) {
789 
790 	ERR_FAIL_COND(!is_inside_tree());
791 	ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
792 	get_viewport()->_gui_set_drag_preview(this, p_control);
793 }
794 
is_window_modal_on_top() const795 bool Control::is_window_modal_on_top() const {
796 
797 	if (!is_inside_tree())
798 		return false;
799 
800 	return get_viewport()->_gui_is_modal_on_top(this);
801 }
802 
get_modal_frame() const803 uint64_t Control::get_modal_frame() const {
804 
805 	return data.modal_frame;
806 }
807 
get_minimum_size() const808 Size2 Control::get_minimum_size() const {
809 
810 	ScriptInstance *si = const_cast<Control *>(this)->get_script_instance();
811 	if (si) {
812 
813 		Variant::CallError ce;
814 		Variant s = si->call(SceneStringNames::get_singleton()->_get_minimum_size, NULL, 0, ce);
815 		if (ce.error == Variant::CallError::CALL_OK)
816 			return s;
817 	}
818 	return Size2();
819 }
820 
get_icon(const StringName & p_name,const StringName & p_type) const821 Ref<Texture> Control::get_icon(const StringName &p_name, const StringName &p_type) const {
822 
823 	if (p_type == StringName() || p_type == get_class_name()) {
824 
825 		const Ref<Texture> *tex = data.icon_override.getptr(p_name);
826 		if (tex)
827 			return *tex;
828 	}
829 
830 	StringName type = p_type ? p_type : get_class_name();
831 
832 	// try with custom themes
833 	Control *theme_owner = data.theme_owner;
834 
835 	while (theme_owner) {
836 
837 		StringName class_name = type;
838 
839 		while (class_name != StringName()) {
840 			if (theme_owner->data.theme->has_icon(p_name, class_name)) {
841 				return theme_owner->data.theme->get_icon(p_name, class_name);
842 			}
843 
844 			class_name = ClassDB::get_parent_class_nocheck(class_name);
845 		}
846 
847 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
848 
849 		if (parent)
850 			theme_owner = parent->data.theme_owner;
851 		else
852 			theme_owner = NULL;
853 	}
854 
855 	if (Theme::get_project_default().is_valid()) {
856 		if (Theme::get_project_default()->has_icon(p_name, type)) {
857 			return Theme::get_project_default()->get_icon(p_name, type);
858 		}
859 	}
860 
861 	return Theme::get_default()->get_icon(p_name, type);
862 }
863 
get_shader(const StringName & p_name,const StringName & p_type) const864 Ref<Shader> Control::get_shader(const StringName &p_name, const StringName &p_type) const {
865 	if (p_type == StringName() || p_type == get_class_name()) {
866 
867 		const Ref<Shader> *sdr = data.shader_override.getptr(p_name);
868 		if (sdr)
869 			return *sdr;
870 	}
871 
872 	StringName type = p_type ? p_type : get_class_name();
873 
874 	// try with custom themes
875 	Control *theme_owner = data.theme_owner;
876 
877 	while (theme_owner) {
878 
879 		StringName class_name = type;
880 
881 		while (class_name != StringName()) {
882 			if (theme_owner->data.theme->has_shader(p_name, class_name)) {
883 				return theme_owner->data.theme->get_shader(p_name, class_name);
884 			}
885 
886 			class_name = ClassDB::get_parent_class_nocheck(class_name);
887 		}
888 
889 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
890 
891 		if (parent)
892 			theme_owner = parent->data.theme_owner;
893 		else
894 			theme_owner = NULL;
895 	}
896 
897 	if (Theme::get_project_default().is_valid()) {
898 		if (Theme::get_project_default()->has_shader(p_name, type)) {
899 			return Theme::get_project_default()->get_shader(p_name, type);
900 		}
901 	}
902 
903 	return Theme::get_default()->get_shader(p_name, type);
904 }
905 
get_stylebox(const StringName & p_name,const StringName & p_type) const906 Ref<StyleBox> Control::get_stylebox(const StringName &p_name, const StringName &p_type) const {
907 
908 	if (p_type == StringName() || p_type == get_class_name()) {
909 		const Ref<StyleBox> *style = data.style_override.getptr(p_name);
910 		if (style)
911 			return *style;
912 	}
913 
914 	StringName type = p_type ? p_type : get_class_name();
915 
916 	// try with custom themes
917 	Control *theme_owner = data.theme_owner;
918 
919 	StringName class_name = type;
920 
921 	while (theme_owner) {
922 
923 		while (class_name != StringName()) {
924 			if (theme_owner->data.theme->has_stylebox(p_name, class_name)) {
925 				return theme_owner->data.theme->get_stylebox(p_name, class_name);
926 			}
927 
928 			class_name = ClassDB::get_parent_class_nocheck(class_name);
929 		}
930 
931 		class_name = type;
932 
933 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
934 
935 		if (parent)
936 			theme_owner = parent->data.theme_owner;
937 		else
938 			theme_owner = NULL;
939 	}
940 
941 	while (class_name != StringName()) {
942 		if (Theme::get_project_default().is_valid() && Theme::get_project_default()->has_stylebox(p_name, type))
943 			return Theme::get_project_default()->get_stylebox(p_name, type);
944 
945 		if (Theme::get_default()->has_stylebox(p_name, class_name))
946 			return Theme::get_default()->get_stylebox(p_name, class_name);
947 
948 		class_name = ClassDB::get_parent_class_nocheck(class_name);
949 	}
950 	return Theme::get_default()->get_stylebox(p_name, type);
951 }
get_font(const StringName & p_name,const StringName & p_type) const952 Ref<Font> Control::get_font(const StringName &p_name, const StringName &p_type) const {
953 
954 	if (p_type == StringName() || p_type == get_class_name()) {
955 		const Ref<Font> *font = data.font_override.getptr(p_name);
956 		if (font)
957 			return *font;
958 	}
959 
960 	StringName type = p_type ? p_type : get_class_name();
961 
962 	// try with custom themes
963 	Control *theme_owner = data.theme_owner;
964 
965 	while (theme_owner) {
966 
967 		StringName class_name = type;
968 
969 		while (class_name != StringName()) {
970 			if (theme_owner->data.theme->has_font(p_name, class_name)) {
971 				return theme_owner->data.theme->get_font(p_name, class_name);
972 			}
973 
974 			class_name = ClassDB::get_parent_class_nocheck(class_name);
975 		}
976 
977 		if (theme_owner->data.theme->get_default_theme_font().is_valid())
978 			return theme_owner->data.theme->get_default_theme_font();
979 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
980 
981 		if (parent)
982 			theme_owner = parent->data.theme_owner;
983 		else
984 			theme_owner = NULL;
985 	}
986 
987 	return Theme::get_default()->get_font(p_name, type);
988 }
get_color(const StringName & p_name,const StringName & p_type) const989 Color Control::get_color(const StringName &p_name, const StringName &p_type) const {
990 
991 	if (p_type == StringName() || p_type == get_class_name()) {
992 		const Color *color = data.color_override.getptr(p_name);
993 		if (color)
994 			return *color;
995 	}
996 
997 	StringName type = p_type ? p_type : get_class_name();
998 	// try with custom themes
999 	Control *theme_owner = data.theme_owner;
1000 
1001 	while (theme_owner) {
1002 
1003 		StringName class_name = type;
1004 
1005 		while (class_name != StringName()) {
1006 			if (theme_owner->data.theme->has_color(p_name, class_name)) {
1007 				return theme_owner->data.theme->get_color(p_name, class_name);
1008 			}
1009 
1010 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1011 		}
1012 
1013 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1014 
1015 		if (parent)
1016 			theme_owner = parent->data.theme_owner;
1017 		else
1018 			theme_owner = NULL;
1019 	}
1020 
1021 	if (Theme::get_project_default().is_valid()) {
1022 		if (Theme::get_project_default()->has_color(p_name, type)) {
1023 			return Theme::get_project_default()->get_color(p_name, type);
1024 		}
1025 	}
1026 	return Theme::get_default()->get_color(p_name, type);
1027 }
1028 
get_constant(const StringName & p_name,const StringName & p_type) const1029 int Control::get_constant(const StringName &p_name, const StringName &p_type) const {
1030 
1031 	if (p_type == StringName() || p_type == get_class_name()) {
1032 		const int *constant = data.constant_override.getptr(p_name);
1033 		if (constant)
1034 			return *constant;
1035 	}
1036 
1037 	StringName type = p_type ? p_type : get_class_name();
1038 	// try with custom themes
1039 	Control *theme_owner = data.theme_owner;
1040 
1041 	while (theme_owner) {
1042 
1043 		StringName class_name = type;
1044 
1045 		while (class_name != StringName()) {
1046 			if (theme_owner->data.theme->has_constant(p_name, class_name)) {
1047 				return theme_owner->data.theme->get_constant(p_name, class_name);
1048 			}
1049 
1050 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1051 		}
1052 
1053 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1054 
1055 		if (parent)
1056 			theme_owner = parent->data.theme_owner;
1057 		else
1058 			theme_owner = NULL;
1059 	}
1060 
1061 	if (Theme::get_project_default().is_valid()) {
1062 		if (Theme::get_project_default()->has_constant(p_name, type)) {
1063 			return Theme::get_project_default()->get_constant(p_name, type);
1064 		}
1065 	}
1066 	return Theme::get_default()->get_constant(p_name, type);
1067 }
1068 
has_icon_override(const StringName & p_name) const1069 bool Control::has_icon_override(const StringName &p_name) const {
1070 
1071 	const Ref<Texture> *tex = data.icon_override.getptr(p_name);
1072 	return tex != NULL;
1073 }
1074 
has_shader_override(const StringName & p_name) const1075 bool Control::has_shader_override(const StringName &p_name) const {
1076 
1077 	const Ref<Shader> *sdr = data.shader_override.getptr(p_name);
1078 	return sdr != NULL;
1079 }
1080 
has_stylebox_override(const StringName & p_name) const1081 bool Control::has_stylebox_override(const StringName &p_name) const {
1082 
1083 	const Ref<StyleBox> *style = data.style_override.getptr(p_name);
1084 	return style != NULL;
1085 }
1086 
has_font_override(const StringName & p_name) const1087 bool Control::has_font_override(const StringName &p_name) const {
1088 
1089 	const Ref<Font> *font = data.font_override.getptr(p_name);
1090 	return font != NULL;
1091 }
1092 
has_color_override(const StringName & p_name) const1093 bool Control::has_color_override(const StringName &p_name) const {
1094 
1095 	const Color *color = data.color_override.getptr(p_name);
1096 	return color != NULL;
1097 }
1098 
has_constant_override(const StringName & p_name) const1099 bool Control::has_constant_override(const StringName &p_name) const {
1100 
1101 	const int *constant = data.constant_override.getptr(p_name);
1102 	return constant != NULL;
1103 }
1104 
has_icon(const StringName & p_name,const StringName & p_type) const1105 bool Control::has_icon(const StringName &p_name, const StringName &p_type) const {
1106 
1107 	if (p_type == StringName() || p_type == get_class_name()) {
1108 		if (has_icon_override(p_name))
1109 			return true;
1110 	}
1111 
1112 	StringName type = p_type ? p_type : get_class_name();
1113 
1114 	// try with custom themes
1115 	Control *theme_owner = data.theme_owner;
1116 
1117 	while (theme_owner) {
1118 
1119 		StringName class_name = type;
1120 
1121 		while (class_name != StringName()) {
1122 			if (theme_owner->data.theme->has_icon(p_name, class_name)) {
1123 				return true;
1124 			}
1125 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1126 		}
1127 
1128 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1129 
1130 		if (parent)
1131 			theme_owner = parent->data.theme_owner;
1132 		else
1133 			theme_owner = NULL;
1134 	}
1135 
1136 	if (Theme::get_project_default().is_valid()) {
1137 		if (Theme::get_project_default()->has_color(p_name, type)) {
1138 			return true;
1139 		}
1140 	}
1141 	return Theme::get_default()->has_icon(p_name, type);
1142 }
1143 
has_shader(const StringName & p_name,const StringName & p_type) const1144 bool Control::has_shader(const StringName &p_name, const StringName &p_type) const {
1145 
1146 	if (p_type == StringName() || p_type == get_class_name()) {
1147 		if (has_shader_override(p_name))
1148 			return true;
1149 	}
1150 
1151 	StringName type = p_type ? p_type : get_class_name();
1152 
1153 	// try with custom themes
1154 	Control *theme_owner = data.theme_owner;
1155 
1156 	while (theme_owner) {
1157 
1158 		StringName class_name = type;
1159 
1160 		while (class_name != StringName()) {
1161 			if (theme_owner->data.theme->has_shader(p_name, class_name)) {
1162 				return true;
1163 			}
1164 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1165 		}
1166 
1167 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1168 
1169 		if (parent)
1170 			theme_owner = parent->data.theme_owner;
1171 		else
1172 			theme_owner = NULL;
1173 	}
1174 
1175 	if (Theme::get_project_default().is_valid()) {
1176 		if (Theme::get_project_default()->has_shader(p_name, type)) {
1177 			return true;
1178 		}
1179 	}
1180 	return Theme::get_default()->has_shader(p_name, type);
1181 }
has_stylebox(const StringName & p_name,const StringName & p_type) const1182 bool Control::has_stylebox(const StringName &p_name, const StringName &p_type) const {
1183 
1184 	if (p_type == StringName() || p_type == get_class_name()) {
1185 		if (has_stylebox_override(p_name))
1186 			return true;
1187 	}
1188 
1189 	StringName type = p_type ? p_type : get_class_name();
1190 
1191 	// try with custom themes
1192 	Control *theme_owner = data.theme_owner;
1193 
1194 	while (theme_owner) {
1195 
1196 		StringName class_name = type;
1197 
1198 		while (class_name != StringName()) {
1199 			if (theme_owner->data.theme->has_stylebox(p_name, class_name)) {
1200 				return true;
1201 			}
1202 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1203 		}
1204 
1205 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1206 
1207 		if (parent)
1208 			theme_owner = parent->data.theme_owner;
1209 		else
1210 			theme_owner = NULL;
1211 	}
1212 
1213 	if (Theme::get_project_default().is_valid()) {
1214 		if (Theme::get_project_default()->has_stylebox(p_name, type)) {
1215 			return true;
1216 		}
1217 	}
1218 	return Theme::get_default()->has_stylebox(p_name, type);
1219 }
has_font(const StringName & p_name,const StringName & p_type) const1220 bool Control::has_font(const StringName &p_name, const StringName &p_type) const {
1221 
1222 	if (p_type == StringName() || p_type == get_class_name()) {
1223 		if (has_font_override(p_name))
1224 			return true;
1225 	}
1226 
1227 	StringName type = p_type ? p_type : get_class_name();
1228 
1229 	// try with custom themes
1230 	Control *theme_owner = data.theme_owner;
1231 
1232 	while (theme_owner) {
1233 
1234 		StringName class_name = type;
1235 
1236 		while (class_name != StringName()) {
1237 			if (theme_owner->data.theme->has_font(p_name, class_name)) {
1238 				return true;
1239 			}
1240 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1241 		}
1242 
1243 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1244 
1245 		if (parent)
1246 			theme_owner = parent->data.theme_owner;
1247 		else
1248 			theme_owner = NULL;
1249 	}
1250 
1251 	if (Theme::get_project_default().is_valid()) {
1252 		if (Theme::get_project_default()->has_font(p_name, type)) {
1253 			return true;
1254 		}
1255 	}
1256 	return Theme::get_default()->has_font(p_name, type);
1257 }
1258 
has_color(const StringName & p_name,const StringName & p_type) const1259 bool Control::has_color(const StringName &p_name, const StringName &p_type) const {
1260 
1261 	if (p_type == StringName() || p_type == get_class_name()) {
1262 		if (has_color_override(p_name))
1263 			return true;
1264 	}
1265 
1266 	StringName type = p_type ? p_type : get_class_name();
1267 
1268 	// try with custom themes
1269 	Control *theme_owner = data.theme_owner;
1270 
1271 	while (theme_owner) {
1272 
1273 		StringName class_name = type;
1274 
1275 		while (class_name != StringName()) {
1276 			if (theme_owner->data.theme->has_color(p_name, class_name)) {
1277 				return true;
1278 			}
1279 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1280 		}
1281 
1282 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1283 
1284 		if (parent)
1285 			theme_owner = parent->data.theme_owner;
1286 		else
1287 			theme_owner = NULL;
1288 	}
1289 
1290 	if (Theme::get_project_default().is_valid()) {
1291 		if (Theme::get_project_default()->has_color(p_name, type)) {
1292 			return true;
1293 		}
1294 	}
1295 	return Theme::get_default()->has_color(p_name, type);
1296 }
1297 
has_constant(const StringName & p_name,const StringName & p_type) const1298 bool Control::has_constant(const StringName &p_name, const StringName &p_type) const {
1299 
1300 	if (p_type == StringName() || p_type == get_class_name()) {
1301 		if (has_constant_override(p_name))
1302 			return true;
1303 	}
1304 
1305 	StringName type = p_type ? p_type : get_class_name();
1306 
1307 	// try with custom themes
1308 	Control *theme_owner = data.theme_owner;
1309 
1310 	while (theme_owner) {
1311 
1312 		StringName class_name = type;
1313 
1314 		while (class_name != StringName()) {
1315 			if (theme_owner->data.theme->has_constant(p_name, class_name)) {
1316 				return true;
1317 			}
1318 			class_name = ClassDB::get_parent_class_nocheck(class_name);
1319 		}
1320 
1321 		Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
1322 
1323 		if (parent)
1324 			theme_owner = parent->data.theme_owner;
1325 		else
1326 			theme_owner = NULL;
1327 	}
1328 
1329 	if (Theme::get_project_default().is_valid()) {
1330 		if (Theme::get_project_default()->has_constant(p_name, type)) {
1331 			return true;
1332 		}
1333 	}
1334 	return Theme::get_default()->has_constant(p_name, type);
1335 }
1336 
get_parent_anchorable_rect() const1337 Rect2 Control::get_parent_anchorable_rect() const {
1338 	if (!is_inside_tree())
1339 		return Rect2();
1340 
1341 	Rect2 parent_rect;
1342 	if (data.parent_canvas_item) {
1343 		parent_rect = data.parent_canvas_item->get_anchorable_rect();
1344 	} else {
1345 		parent_rect = get_viewport()->get_visible_rect();
1346 	}
1347 
1348 	return parent_rect;
1349 }
1350 
get_parent_area_size() const1351 Size2 Control::get_parent_area_size() const {
1352 
1353 	return get_parent_anchorable_rect().size;
1354 }
1355 
_size_changed()1356 void Control::_size_changed() {
1357 
1358 	Rect2 parent_rect = get_parent_anchorable_rect();
1359 
1360 	float margin_pos[4];
1361 
1362 	for (int i = 0; i < 4; i++) {
1363 
1364 		float area = parent_rect.size[i & 1];
1365 		margin_pos[i] = data.margin[i] + (data.anchor[i] * area);
1366 	}
1367 
1368 	Point2 new_pos_cache = Point2(margin_pos[0], margin_pos[1]);
1369 	Size2 new_size_cache = Point2(margin_pos[2], margin_pos[3]) - new_pos_cache;
1370 
1371 	Size2 minimum_size = get_combined_minimum_size();
1372 
1373 	if (minimum_size.width > new_size_cache.width) {
1374 		if (data.h_grow == GROW_DIRECTION_BEGIN) {
1375 			new_pos_cache.x += new_size_cache.width - minimum_size.width;
1376 		} else if (data.h_grow == GROW_DIRECTION_BOTH) {
1377 			new_pos_cache.x += 0.5 * (new_size_cache.width - minimum_size.width);
1378 		}
1379 
1380 		new_size_cache.width = minimum_size.width;
1381 	}
1382 
1383 	if (minimum_size.height > new_size_cache.height) {
1384 		if (data.v_grow == GROW_DIRECTION_BEGIN) {
1385 			new_pos_cache.y += new_size_cache.height - minimum_size.height;
1386 		} else if (data.v_grow == GROW_DIRECTION_BOTH) {
1387 			new_pos_cache.y += 0.5 * (new_size_cache.height - minimum_size.height);
1388 		}
1389 
1390 		new_size_cache.height = minimum_size.height;
1391 	}
1392 
1393 	bool pos_changed = new_pos_cache != data.pos_cache;
1394 	bool size_changed = new_size_cache != data.size_cache;
1395 
1396 	data.pos_cache = new_pos_cache;
1397 	data.size_cache = new_size_cache;
1398 
1399 	if (is_inside_tree()) {
1400 		if (size_changed) {
1401 			notification(NOTIFICATION_RESIZED);
1402 		}
1403 		if (pos_changed || size_changed) {
1404 			item_rect_changed(size_changed);
1405 			_change_notify_margins();
1406 			_notify_transform();
1407 		}
1408 
1409 		if (pos_changed && !size_changed) {
1410 			_update_canvas_item_transform(); //move because it won't be updated
1411 		}
1412 	}
1413 }
1414 
set_anchor(Margin p_margin,float p_anchor,bool p_keep_margin,bool p_push_opposite_anchor)1415 void Control::set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin, bool p_push_opposite_anchor) {
1416 
1417 	ERR_FAIL_INDEX((int)p_margin, 4);
1418 
1419 	Rect2 parent_rect = get_parent_anchorable_rect();
1420 	float parent_range = (p_margin == MARGIN_LEFT || p_margin == MARGIN_RIGHT) ? parent_rect.size.x : parent_rect.size.y;
1421 	float previous_margin_pos = data.margin[p_margin] + data.anchor[p_margin] * parent_range;
1422 	float previous_opposite_margin_pos = data.margin[(p_margin + 2) % 4] + data.anchor[(p_margin + 2) % 4] * parent_range;
1423 
1424 	data.anchor[p_margin] = p_anchor;
1425 
1426 	if (((p_margin == MARGIN_LEFT || p_margin == MARGIN_TOP) && data.anchor[p_margin] > data.anchor[(p_margin + 2) % 4]) ||
1427 			((p_margin == MARGIN_RIGHT || p_margin == MARGIN_BOTTOM) && data.anchor[p_margin] < data.anchor[(p_margin + 2) % 4])) {
1428 		if (p_push_opposite_anchor) {
1429 			data.anchor[(p_margin + 2) % 4] = data.anchor[p_margin];
1430 		} else {
1431 			data.anchor[p_margin] = data.anchor[(p_margin + 2) % 4];
1432 		}
1433 	}
1434 
1435 	if (!p_keep_margin) {
1436 		data.margin[p_margin] = previous_margin_pos - data.anchor[p_margin] * parent_range;
1437 		if (p_push_opposite_anchor) {
1438 			data.margin[(p_margin + 2) % 4] = previous_opposite_margin_pos - data.anchor[(p_margin + 2) % 4] * parent_range;
1439 		}
1440 	}
1441 	if (is_inside_tree()) {
1442 		_size_changed();
1443 	}
1444 
1445 	update();
1446 	_change_notify("anchor_left");
1447 	_change_notify("anchor_right");
1448 	_change_notify("anchor_top");
1449 	_change_notify("anchor_bottom");
1450 }
1451 
_set_anchor(Margin p_margin,float p_anchor)1452 void Control::_set_anchor(Margin p_margin, float p_anchor) {
1453 	set_anchor(p_margin, p_anchor);
1454 }
1455 
set_anchor_and_margin(Margin p_margin,float p_anchor,float p_pos,bool p_push_opposite_anchor)1456 void Control::set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor) {
1457 
1458 	set_anchor(p_margin, p_anchor, false, p_push_opposite_anchor);
1459 	set_margin(p_margin, p_pos);
1460 }
1461 
set_anchors_preset(LayoutPreset p_preset,bool p_keep_margins)1462 void Control::set_anchors_preset(LayoutPreset p_preset, bool p_keep_margins) {
1463 
1464 	ERR_FAIL_INDEX((int)p_preset, 16);
1465 
1466 	//Left
1467 	switch (p_preset) {
1468 		case PRESET_TOP_LEFT:
1469 		case PRESET_BOTTOM_LEFT:
1470 		case PRESET_CENTER_LEFT:
1471 		case PRESET_TOP_WIDE:
1472 		case PRESET_BOTTOM_WIDE:
1473 		case PRESET_LEFT_WIDE:
1474 		case PRESET_HCENTER_WIDE:
1475 		case PRESET_WIDE:
1476 			set_anchor(MARGIN_LEFT, ANCHOR_BEGIN, p_keep_margins);
1477 			break;
1478 
1479 		case PRESET_CENTER_TOP:
1480 		case PRESET_CENTER_BOTTOM:
1481 		case PRESET_CENTER:
1482 		case PRESET_VCENTER_WIDE:
1483 			set_anchor(MARGIN_LEFT, 0.5, p_keep_margins);
1484 			break;
1485 
1486 		case PRESET_TOP_RIGHT:
1487 		case PRESET_BOTTOM_RIGHT:
1488 		case PRESET_CENTER_RIGHT:
1489 		case PRESET_RIGHT_WIDE:
1490 			set_anchor(MARGIN_LEFT, ANCHOR_END, p_keep_margins);
1491 			break;
1492 	}
1493 
1494 	// Top
1495 	switch (p_preset) {
1496 		case PRESET_TOP_LEFT:
1497 		case PRESET_TOP_RIGHT:
1498 		case PRESET_CENTER_TOP:
1499 		case PRESET_LEFT_WIDE:
1500 		case PRESET_RIGHT_WIDE:
1501 		case PRESET_TOP_WIDE:
1502 		case PRESET_VCENTER_WIDE:
1503 		case PRESET_WIDE:
1504 			set_anchor(MARGIN_TOP, ANCHOR_BEGIN, p_keep_margins);
1505 			break;
1506 
1507 		case PRESET_CENTER_LEFT:
1508 		case PRESET_CENTER_RIGHT:
1509 		case PRESET_CENTER:
1510 		case PRESET_HCENTER_WIDE:
1511 			set_anchor(MARGIN_TOP, 0.5, p_keep_margins);
1512 			break;
1513 
1514 		case PRESET_BOTTOM_LEFT:
1515 		case PRESET_BOTTOM_RIGHT:
1516 		case PRESET_CENTER_BOTTOM:
1517 		case PRESET_BOTTOM_WIDE:
1518 			set_anchor(MARGIN_TOP, ANCHOR_END, p_keep_margins);
1519 			break;
1520 	}
1521 
1522 	// Right
1523 	switch (p_preset) {
1524 		case PRESET_TOP_LEFT:
1525 		case PRESET_BOTTOM_LEFT:
1526 		case PRESET_CENTER_LEFT:
1527 		case PRESET_LEFT_WIDE:
1528 			set_anchor(MARGIN_RIGHT, ANCHOR_BEGIN, p_keep_margins);
1529 			break;
1530 
1531 		case PRESET_CENTER_TOP:
1532 		case PRESET_CENTER_BOTTOM:
1533 		case PRESET_CENTER:
1534 		case PRESET_VCENTER_WIDE:
1535 			set_anchor(MARGIN_RIGHT, 0.5, p_keep_margins);
1536 			break;
1537 
1538 		case PRESET_TOP_RIGHT:
1539 		case PRESET_BOTTOM_RIGHT:
1540 		case PRESET_CENTER_RIGHT:
1541 		case PRESET_TOP_WIDE:
1542 		case PRESET_RIGHT_WIDE:
1543 		case PRESET_BOTTOM_WIDE:
1544 		case PRESET_HCENTER_WIDE:
1545 		case PRESET_WIDE:
1546 			set_anchor(MARGIN_RIGHT, ANCHOR_END, p_keep_margins);
1547 			break;
1548 	}
1549 
1550 	// Bottom
1551 	switch (p_preset) {
1552 		case PRESET_TOP_LEFT:
1553 		case PRESET_TOP_RIGHT:
1554 		case PRESET_CENTER_TOP:
1555 		case PRESET_TOP_WIDE:
1556 			set_anchor(MARGIN_BOTTOM, ANCHOR_BEGIN, p_keep_margins);
1557 			break;
1558 
1559 		case PRESET_CENTER_LEFT:
1560 		case PRESET_CENTER_RIGHT:
1561 		case PRESET_CENTER:
1562 		case PRESET_HCENTER_WIDE:
1563 			set_anchor(MARGIN_BOTTOM, 0.5, p_keep_margins);
1564 			break;
1565 
1566 		case PRESET_BOTTOM_LEFT:
1567 		case PRESET_BOTTOM_RIGHT:
1568 		case PRESET_CENTER_BOTTOM:
1569 		case PRESET_LEFT_WIDE:
1570 		case PRESET_RIGHT_WIDE:
1571 		case PRESET_BOTTOM_WIDE:
1572 		case PRESET_VCENTER_WIDE:
1573 		case PRESET_WIDE:
1574 			set_anchor(MARGIN_BOTTOM, ANCHOR_END, p_keep_margins);
1575 			break;
1576 	}
1577 }
1578 
set_margins_preset(LayoutPreset p_preset,LayoutPresetMode p_resize_mode,int p_margin)1579 void Control::set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
1580 
1581 	ERR_FAIL_INDEX((int)p_preset, 16);
1582 	ERR_FAIL_INDEX((int)p_resize_mode, 4);
1583 
1584 	// Calculate the size if the node is not resized
1585 	Size2 min_size = get_minimum_size();
1586 	Size2 new_size = get_size();
1587 	if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_HEIGHT) {
1588 		new_size.x = min_size.x;
1589 	}
1590 	if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_WIDTH) {
1591 		new_size.y = min_size.y;
1592 	}
1593 
1594 	Rect2 parent_rect = get_parent_anchorable_rect();
1595 
1596 	//Left
1597 	switch (p_preset) {
1598 		case PRESET_TOP_LEFT:
1599 		case PRESET_BOTTOM_LEFT:
1600 		case PRESET_CENTER_LEFT:
1601 		case PRESET_TOP_WIDE:
1602 		case PRESET_BOTTOM_WIDE:
1603 		case PRESET_LEFT_WIDE:
1604 		case PRESET_HCENTER_WIDE:
1605 		case PRESET_WIDE:
1606 			data.margin[0] = parent_rect.size.x * (0.0 - data.anchor[0]) + p_margin + parent_rect.position.x;
1607 			break;
1608 
1609 		case PRESET_CENTER_TOP:
1610 		case PRESET_CENTER_BOTTOM:
1611 		case PRESET_CENTER:
1612 		case PRESET_VCENTER_WIDE:
1613 			data.margin[0] = parent_rect.size.x * (0.5 - data.anchor[0]) - new_size.x / 2 + parent_rect.position.x;
1614 			break;
1615 
1616 		case PRESET_TOP_RIGHT:
1617 		case PRESET_BOTTOM_RIGHT:
1618 		case PRESET_CENTER_RIGHT:
1619 		case PRESET_RIGHT_WIDE:
1620 			data.margin[0] = parent_rect.size.x * (1.0 - data.anchor[0]) - new_size.x - p_margin + parent_rect.position.x;
1621 			break;
1622 	}
1623 
1624 	// Top
1625 	switch (p_preset) {
1626 		case PRESET_TOP_LEFT:
1627 		case PRESET_TOP_RIGHT:
1628 		case PRESET_CENTER_TOP:
1629 		case PRESET_LEFT_WIDE:
1630 		case PRESET_RIGHT_WIDE:
1631 		case PRESET_TOP_WIDE:
1632 		case PRESET_VCENTER_WIDE:
1633 		case PRESET_WIDE:
1634 			data.margin[1] = parent_rect.size.y * (0.0 - data.anchor[1]) + p_margin + parent_rect.position.y;
1635 			break;
1636 
1637 		case PRESET_CENTER_LEFT:
1638 		case PRESET_CENTER_RIGHT:
1639 		case PRESET_CENTER:
1640 		case PRESET_HCENTER_WIDE:
1641 			data.margin[1] = parent_rect.size.y * (0.5 - data.anchor[1]) - new_size.y / 2 + parent_rect.position.y;
1642 			break;
1643 
1644 		case PRESET_BOTTOM_LEFT:
1645 		case PRESET_BOTTOM_RIGHT:
1646 		case PRESET_CENTER_BOTTOM:
1647 		case PRESET_BOTTOM_WIDE:
1648 			data.margin[1] = parent_rect.size.y * (1.0 - data.anchor[1]) - new_size.y - p_margin + parent_rect.position.y;
1649 			break;
1650 	}
1651 
1652 	// Right
1653 	switch (p_preset) {
1654 		case PRESET_TOP_LEFT:
1655 		case PRESET_BOTTOM_LEFT:
1656 		case PRESET_CENTER_LEFT:
1657 		case PRESET_LEFT_WIDE:
1658 			data.margin[2] = parent_rect.size.x * (0.0 - data.anchor[2]) + new_size.x + p_margin + parent_rect.position.x;
1659 			break;
1660 
1661 		case PRESET_CENTER_TOP:
1662 		case PRESET_CENTER_BOTTOM:
1663 		case PRESET_CENTER:
1664 		case PRESET_VCENTER_WIDE:
1665 			data.margin[2] = parent_rect.size.x * (0.5 - data.anchor[2]) + new_size.x / 2 + parent_rect.position.x;
1666 			break;
1667 
1668 		case PRESET_TOP_RIGHT:
1669 		case PRESET_BOTTOM_RIGHT:
1670 		case PRESET_CENTER_RIGHT:
1671 		case PRESET_TOP_WIDE:
1672 		case PRESET_RIGHT_WIDE:
1673 		case PRESET_BOTTOM_WIDE:
1674 		case PRESET_HCENTER_WIDE:
1675 		case PRESET_WIDE:
1676 			data.margin[2] = parent_rect.size.x * (1.0 - data.anchor[2]) - p_margin + parent_rect.position.x;
1677 			break;
1678 	}
1679 
1680 	// Bottom
1681 	switch (p_preset) {
1682 		case PRESET_TOP_LEFT:
1683 		case PRESET_TOP_RIGHT:
1684 		case PRESET_CENTER_TOP:
1685 		case PRESET_TOP_WIDE:
1686 			data.margin[3] = parent_rect.size.y * (0.0 - data.anchor[3]) + new_size.y + p_margin + parent_rect.position.y;
1687 			break;
1688 
1689 		case PRESET_CENTER_LEFT:
1690 		case PRESET_CENTER_RIGHT:
1691 		case PRESET_CENTER:
1692 		case PRESET_HCENTER_WIDE:
1693 			data.margin[3] = parent_rect.size.y * (0.5 - data.anchor[3]) + new_size.y / 2 + parent_rect.position.y;
1694 			break;
1695 
1696 		case PRESET_BOTTOM_LEFT:
1697 		case PRESET_BOTTOM_RIGHT:
1698 		case PRESET_CENTER_BOTTOM:
1699 		case PRESET_LEFT_WIDE:
1700 		case PRESET_RIGHT_WIDE:
1701 		case PRESET_BOTTOM_WIDE:
1702 		case PRESET_VCENTER_WIDE:
1703 		case PRESET_WIDE:
1704 			data.margin[3] = parent_rect.size.y * (1.0 - data.anchor[3]) - p_margin + parent_rect.position.y;
1705 			break;
1706 	}
1707 
1708 	_size_changed();
1709 }
1710 
set_anchors_and_margins_preset(LayoutPreset p_preset,LayoutPresetMode p_resize_mode,int p_margin)1711 void Control::set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
1712 	set_anchors_preset(p_preset);
1713 	set_margins_preset(p_preset, p_resize_mode, p_margin);
1714 }
1715 
get_anchor(Margin p_margin) const1716 float Control::get_anchor(Margin p_margin) const {
1717 
1718 	ERR_FAIL_INDEX_V(int(p_margin), 4, 0.0);
1719 
1720 	return data.anchor[p_margin];
1721 }
1722 
_change_notify_margins()1723 void Control::_change_notify_margins() {
1724 
1725 	// this avoids sending the whole object data again on a change
1726 	_change_notify("margin_left");
1727 	_change_notify("margin_top");
1728 	_change_notify("margin_right");
1729 	_change_notify("margin_bottom");
1730 	_change_notify("rect_position");
1731 	_change_notify("rect_size");
1732 }
1733 
set_margin(Margin p_margin,float p_value)1734 void Control::set_margin(Margin p_margin, float p_value) {
1735 
1736 	ERR_FAIL_INDEX((int)p_margin, 4);
1737 
1738 	data.margin[p_margin] = p_value;
1739 	_size_changed();
1740 }
1741 
set_begin(const Size2 & p_point)1742 void Control::set_begin(const Size2 &p_point) {
1743 
1744 	data.margin[0] = p_point.x;
1745 	data.margin[1] = p_point.y;
1746 	_size_changed();
1747 }
1748 
set_end(const Size2 & p_point)1749 void Control::set_end(const Size2 &p_point) {
1750 
1751 	data.margin[2] = p_point.x;
1752 	data.margin[3] = p_point.y;
1753 	_size_changed();
1754 }
1755 
get_margin(Margin p_margin) const1756 float Control::get_margin(Margin p_margin) const {
1757 
1758 	ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
1759 
1760 	return data.margin[p_margin];
1761 }
1762 
get_begin() const1763 Size2 Control::get_begin() const {
1764 
1765 	return Size2(data.margin[0], data.margin[1]);
1766 }
get_end() const1767 Size2 Control::get_end() const {
1768 
1769 	return Size2(data.margin[2], data.margin[3]);
1770 }
1771 
get_global_position() const1772 Point2 Control::get_global_position() const {
1773 
1774 	return get_global_transform().get_origin();
1775 }
1776 
_set_global_position(const Point2 & p_point)1777 void Control::_set_global_position(const Point2 &p_point) {
1778 	set_global_position(p_point);
1779 }
1780 
set_global_position(const Point2 & p_point,bool p_keep_margins)1781 void Control::set_global_position(const Point2 &p_point, bool p_keep_margins) {
1782 
1783 	Transform2D inv;
1784 
1785 	if (data.parent_canvas_item) {
1786 
1787 		inv = data.parent_canvas_item->get_global_transform().affine_inverse();
1788 	}
1789 
1790 	set_position(inv.xform(p_point), p_keep_margins);
1791 }
1792 
_compute_anchors(Rect2 p_rect,const float p_margins[4],float (& r_anchors)[4])1793 void Control::_compute_anchors(Rect2 p_rect, const float p_margins[4], float (&r_anchors)[4]) {
1794 
1795 	Size2 parent_rect_size = get_parent_anchorable_rect().size;
1796 	ERR_FAIL_COND(parent_rect_size.x == 0.0);
1797 	ERR_FAIL_COND(parent_rect_size.y == 0.0);
1798 
1799 	r_anchors[0] = (p_rect.position.x - p_margins[0]) / parent_rect_size.x;
1800 	r_anchors[1] = (p_rect.position.y - p_margins[1]) / parent_rect_size.y;
1801 	r_anchors[2] = (p_rect.position.x + p_rect.size.x - p_margins[2]) / parent_rect_size.x;
1802 	r_anchors[3] = (p_rect.position.y + p_rect.size.y - p_margins[3]) / parent_rect_size.y;
1803 }
1804 
_compute_margins(Rect2 p_rect,const float p_anchors[4],float (& r_margins)[4])1805 void Control::_compute_margins(Rect2 p_rect, const float p_anchors[4], float (&r_margins)[4]) {
1806 
1807 	Size2 parent_rect_size = get_parent_anchorable_rect().size;
1808 	r_margins[0] = p_rect.position.x - (p_anchors[0] * parent_rect_size.x);
1809 	r_margins[1] = p_rect.position.y - (p_anchors[1] * parent_rect_size.y);
1810 	r_margins[2] = p_rect.position.x + p_rect.size.x - (p_anchors[2] * parent_rect_size.x);
1811 	r_margins[3] = p_rect.position.y + p_rect.size.y - (p_anchors[3] * parent_rect_size.y);
1812 }
1813 
_set_position(const Size2 & p_point)1814 void Control::_set_position(const Size2 &p_point) {
1815 	set_position(p_point);
1816 }
1817 
set_position(const Size2 & p_point,bool p_keep_margins)1818 void Control::set_position(const Size2 &p_point, bool p_keep_margins) {
1819 	if (p_keep_margins) {
1820 		_compute_anchors(Rect2(p_point, data.size_cache), data.margin, data.anchor);
1821 		_change_notify("anchor_left");
1822 		_change_notify("anchor_right");
1823 		_change_notify("anchor_top");
1824 		_change_notify("anchor_bottom");
1825 	} else {
1826 		_compute_margins(Rect2(p_point, data.size_cache), data.anchor, data.margin);
1827 	}
1828 	_size_changed();
1829 }
1830 
_set_size(const Size2 & p_size)1831 void Control::_set_size(const Size2 &p_size) {
1832 	set_size(p_size);
1833 }
1834 
set_size(const Size2 & p_size,bool p_keep_margins)1835 void Control::set_size(const Size2 &p_size, bool p_keep_margins) {
1836 
1837 	Size2 new_size = p_size;
1838 	Size2 min = get_combined_minimum_size();
1839 	if (new_size.x < min.x)
1840 		new_size.x = min.x;
1841 	if (new_size.y < min.y)
1842 		new_size.y = min.y;
1843 
1844 	if (p_keep_margins) {
1845 		_compute_anchors(Rect2(data.pos_cache, new_size), data.margin, data.anchor);
1846 		_change_notify("anchor_left");
1847 		_change_notify("anchor_right");
1848 		_change_notify("anchor_top");
1849 		_change_notify("anchor_bottom");
1850 	} else {
1851 		_compute_margins(Rect2(data.pos_cache, new_size), data.anchor, data.margin);
1852 	}
1853 	_size_changed();
1854 }
1855 
get_position() const1856 Size2 Control::get_position() const {
1857 
1858 	return data.pos_cache;
1859 }
1860 
get_size() const1861 Size2 Control::get_size() const {
1862 
1863 	return data.size_cache;
1864 }
1865 
get_global_rect() const1866 Rect2 Control::get_global_rect() const {
1867 
1868 	return Rect2(get_global_position(), get_size());
1869 }
1870 
get_window_rect() const1871 Rect2 Control::get_window_rect() const {
1872 	ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
1873 	Rect2 gr = get_global_rect();
1874 	gr.position += get_viewport()->get_visible_rect().position;
1875 	return gr;
1876 }
1877 
get_rect() const1878 Rect2 Control::get_rect() const {
1879 
1880 	return Rect2(get_position(), get_size());
1881 }
1882 
get_anchorable_rect() const1883 Rect2 Control::get_anchorable_rect() const {
1884 
1885 	return Rect2(Point2(), get_size());
1886 }
1887 
add_icon_override(const StringName & p_name,const Ref<Texture> & p_icon)1888 void Control::add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon) {
1889 
1890 	if (data.icon_override.has(p_name)) {
1891 		data.icon_override[p_name]->disconnect("changed", this, "_override_changed");
1892 	}
1893 
1894 	// clear if "null" is passed instead of a icon
1895 	if (p_icon.is_null()) {
1896 		data.icon_override.erase(p_name);
1897 	} else {
1898 		data.icon_override[p_name] = p_icon;
1899 		if (data.icon_override[p_name].is_valid()) {
1900 			data.icon_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
1901 		}
1902 	}
1903 	notification(NOTIFICATION_THEME_CHANGED);
1904 }
1905 
add_shader_override(const StringName & p_name,const Ref<Shader> & p_shader)1906 void Control::add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader) {
1907 
1908 	if (data.shader_override.has(p_name)) {
1909 		data.shader_override[p_name]->disconnect("changed", this, "_override_changed");
1910 	}
1911 
1912 	// clear if "null" is passed instead of a shader
1913 	if (p_shader.is_null()) {
1914 		data.shader_override.erase(p_name);
1915 	} else {
1916 		data.shader_override[p_name] = p_shader;
1917 		if (data.shader_override[p_name].is_valid()) {
1918 			data.shader_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
1919 		}
1920 	}
1921 	notification(NOTIFICATION_THEME_CHANGED);
1922 }
add_style_override(const StringName & p_name,const Ref<StyleBox> & p_style)1923 void Control::add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
1924 
1925 	if (data.style_override.has(p_name)) {
1926 		data.style_override[p_name]->disconnect("changed", this, "_override_changed");
1927 	}
1928 
1929 	// clear if "null" is passed instead of a style
1930 	if (p_style.is_null()) {
1931 		data.style_override.erase(p_name);
1932 	} else {
1933 		data.style_override[p_name] = p_style;
1934 		if (data.style_override[p_name].is_valid()) {
1935 			data.style_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
1936 		}
1937 	}
1938 	notification(NOTIFICATION_THEME_CHANGED);
1939 }
1940 
add_font_override(const StringName & p_name,const Ref<Font> & p_font)1941 void Control::add_font_override(const StringName &p_name, const Ref<Font> &p_font) {
1942 
1943 	if (data.font_override.has(p_name)) {
1944 		data.font_override[p_name]->disconnect("changed", this, "_override_changed");
1945 	}
1946 
1947 	// clear if "null" is passed instead of a font
1948 	if (p_font.is_null()) {
1949 		data.font_override.erase(p_name);
1950 	} else {
1951 		data.font_override[p_name] = p_font;
1952 		if (data.font_override[p_name].is_valid()) {
1953 			data.font_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
1954 		}
1955 	}
1956 	notification(NOTIFICATION_THEME_CHANGED);
1957 }
add_color_override(const StringName & p_name,const Color & p_color)1958 void Control::add_color_override(const StringName &p_name, const Color &p_color) {
1959 
1960 	data.color_override[p_name] = p_color;
1961 	notification(NOTIFICATION_THEME_CHANGED);
1962 }
add_constant_override(const StringName & p_name,int p_constant)1963 void Control::add_constant_override(const StringName &p_name, int p_constant) {
1964 
1965 	data.constant_override[p_name] = p_constant;
1966 	notification(NOTIFICATION_THEME_CHANGED);
1967 }
1968 
set_focus_mode(FocusMode p_focus_mode)1969 void Control::set_focus_mode(FocusMode p_focus_mode) {
1970 
1971 	ERR_FAIL_INDEX((int)p_focus_mode, 3);
1972 
1973 	if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode != FOCUS_NONE && has_focus())
1974 		release_focus();
1975 
1976 	data.focus_mode = p_focus_mode;
1977 }
1978 
_next_control(Control * p_from)1979 static Control *_next_control(Control *p_from) {
1980 
1981 	if (p_from->is_set_as_toplevel())
1982 		return NULL; // can't go above
1983 
1984 	Control *parent = Object::cast_to<Control>(p_from->get_parent());
1985 
1986 	if (!parent) {
1987 
1988 		return NULL;
1989 	}
1990 
1991 	int next = p_from->get_position_in_parent();
1992 	ERR_FAIL_INDEX_V(next, parent->get_child_count(), NULL);
1993 	for (int i = (next + 1); i < parent->get_child_count(); i++) {
1994 
1995 		Control *c = Object::cast_to<Control>(parent->get_child(i));
1996 		if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel())
1997 			continue;
1998 
1999 		return c;
2000 	}
2001 
2002 	//no next in parent, try the same in parent
2003 	return _next_control(parent);
2004 }
2005 
find_next_valid_focus() const2006 Control *Control::find_next_valid_focus() const {
2007 
2008 	Control *from = const_cast<Control *>(this);
2009 
2010 	while (true) {
2011 
2012 		// If the focus property is manually overwritten, attempt to use it.
2013 
2014 		if (!data.focus_next.is_empty()) {
2015 			Node *n = get_node(data.focus_next);
2016 			Control *c;
2017 			if (n) {
2018 				c = Object::cast_to<Control>(n);
2019 				ERR_FAIL_COND_V_MSG(!c, NULL, "Next focus node is not a control: " + n->get_name() + ".");
2020 			} else {
2021 				return NULL;
2022 			}
2023 			if (c->is_visible() && c->get_focus_mode() != FOCUS_NONE)
2024 				return c;
2025 		}
2026 
2027 		// find next child
2028 
2029 		Control *next_child = NULL;
2030 
2031 		for (int i = 0; i < from->get_child_count(); i++) {
2032 
2033 			Control *c = Object::cast_to<Control>(from->get_child(i));
2034 			if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) {
2035 				continue;
2036 			}
2037 
2038 			next_child = c;
2039 			break;
2040 		}
2041 
2042 		if (!next_child) {
2043 
2044 			next_child = _next_control(from);
2045 			if (!next_child) { //nothing else.. go up and find either window or subwindow
2046 				next_child = const_cast<Control *>(this);
2047 				while (next_child && !next_child->is_set_as_toplevel()) {
2048 
2049 					next_child = cast_to<Control>(next_child->get_parent());
2050 				}
2051 
2052 				if (!next_child) {
2053 
2054 					next_child = const_cast<Control *>(this);
2055 					while (next_child) {
2056 
2057 						if (next_child->data.SI || next_child->data.RI)
2058 							break;
2059 						next_child = next_child->get_parent_control();
2060 					}
2061 				}
2062 			}
2063 		}
2064 
2065 		if (next_child == this) // no next control->
2066 			return (get_focus_mode() == FOCUS_ALL) ? next_child : NULL;
2067 		if (next_child) {
2068 			if (next_child->get_focus_mode() == FOCUS_ALL)
2069 				return next_child;
2070 			from = next_child;
2071 		} else
2072 			break;
2073 	}
2074 
2075 	return NULL;
2076 }
2077 
_prev_control(Control * p_from)2078 static Control *_prev_control(Control *p_from) {
2079 
2080 	Control *child = NULL;
2081 	for (int i = p_from->get_child_count() - 1; i >= 0; i--) {
2082 
2083 		Control *c = Object::cast_to<Control>(p_from->get_child(i));
2084 		if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel())
2085 			continue;
2086 
2087 		child = c;
2088 		break;
2089 	}
2090 
2091 	if (!child)
2092 		return p_from;
2093 
2094 	//no prev in parent, try the same in parent
2095 	return _prev_control(child);
2096 }
2097 
find_prev_valid_focus() const2098 Control *Control::find_prev_valid_focus() const {
2099 	Control *from = const_cast<Control *>(this);
2100 
2101 	while (true) {
2102 
2103 		// If the focus property is manually overwritten, attempt to use it.
2104 
2105 		if (!data.focus_prev.is_empty()) {
2106 			Node *n = get_node(data.focus_prev);
2107 			Control *c;
2108 			if (n) {
2109 				c = Object::cast_to<Control>(n);
2110 				ERR_FAIL_COND_V_MSG(!c, NULL, "Previous focus node is not a control: " + n->get_name() + ".");
2111 			} else {
2112 				return NULL;
2113 			}
2114 			if (c->is_visible() && c->get_focus_mode() != FOCUS_NONE)
2115 				return c;
2116 		}
2117 
2118 		// find prev child
2119 
2120 		Control *prev_child = NULL;
2121 
2122 		if (from->is_set_as_toplevel() || !Object::cast_to<Control>(from->get_parent())) {
2123 
2124 			//find last of the children
2125 
2126 			prev_child = _prev_control(from);
2127 
2128 		} else {
2129 
2130 			for (int i = (from->get_position_in_parent() - 1); i >= 0; i--) {
2131 
2132 				Control *c = Object::cast_to<Control>(from->get_parent()->get_child(i));
2133 
2134 				if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) {
2135 					continue;
2136 				}
2137 
2138 				prev_child = c;
2139 				break;
2140 			}
2141 
2142 			if (!prev_child) {
2143 
2144 				prev_child = Object::cast_to<Control>(from->get_parent());
2145 			} else {
2146 
2147 				prev_child = _prev_control(prev_child);
2148 			}
2149 		}
2150 
2151 		if (prev_child == this) // no prev control->
2152 			return (get_focus_mode() == FOCUS_ALL) ? prev_child : NULL;
2153 
2154 		if (prev_child->get_focus_mode() == FOCUS_ALL)
2155 			return prev_child;
2156 
2157 		from = prev_child;
2158 	}
2159 
2160 	return NULL;
2161 }
2162 
get_focus_mode() const2163 Control::FocusMode Control::get_focus_mode() const {
2164 
2165 	return data.focus_mode;
2166 }
has_focus() const2167 bool Control::has_focus() const {
2168 
2169 	return is_inside_tree() && get_viewport()->_gui_control_has_focus(this);
2170 }
2171 
grab_focus()2172 void Control::grab_focus() {
2173 
2174 	ERR_FAIL_COND(!is_inside_tree());
2175 
2176 	if (data.focus_mode == FOCUS_NONE) {
2177 		WARN_PRINT("This control can't grab focus. Use set_focus_mode() to allow a control to get focus.");
2178 		return;
2179 	}
2180 
2181 	get_viewport()->_gui_control_grab_focus(this);
2182 }
2183 
release_focus()2184 void Control::release_focus() {
2185 
2186 	ERR_FAIL_COND(!is_inside_tree());
2187 
2188 	if (!has_focus())
2189 		return;
2190 
2191 	get_viewport()->_gui_remove_focus();
2192 	update();
2193 }
2194 
is_toplevel_control() const2195 bool Control::is_toplevel_control() const {
2196 
2197 	return is_inside_tree() && (!data.parent_canvas_item && !data.RI && is_set_as_toplevel());
2198 }
2199 
show_modal(bool p_exclusive)2200 void Control::show_modal(bool p_exclusive) {
2201 
2202 	ERR_FAIL_COND(!is_inside_tree());
2203 	ERR_FAIL_COND(!data.SI);
2204 
2205 	if (is_visible_in_tree())
2206 		hide();
2207 
2208 	ERR_FAIL_COND(data.MI != NULL);
2209 	show();
2210 	raise();
2211 	data.modal_exclusive = p_exclusive;
2212 	data.MI = get_viewport()->_gui_show_modal(this);
2213 	data.modal_frame = Engine::get_singleton()->get_frames_drawn();
2214 }
2215 
_modal_set_prev_focus_owner(ObjectID p_prev)2216 void Control::_modal_set_prev_focus_owner(ObjectID p_prev) {
2217 	data.modal_prev_focus_owner = p_prev;
2218 }
2219 
_modal_stack_remove()2220 void Control::_modal_stack_remove() {
2221 
2222 	ERR_FAIL_COND(!is_inside_tree());
2223 
2224 	if (!data.MI)
2225 		return;
2226 
2227 	List<Control *>::Element *element = data.MI;
2228 	data.MI = NULL;
2229 
2230 	get_viewport()->_gui_remove_from_modal_stack(element, data.modal_prev_focus_owner);
2231 
2232 	data.modal_prev_focus_owner = 0;
2233 }
2234 
_propagate_theme_changed(CanvasItem * p_at,Control * p_owner,bool p_assign)2235 void Control::_propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign) {
2236 
2237 	Control *c = Object::cast_to<Control>(p_at);
2238 
2239 	if (c && c != p_owner && c->data.theme.is_valid()) // has a theme, this can't be propagated
2240 		return;
2241 
2242 	for (int i = 0; i < p_at->get_child_count(); i++) {
2243 
2244 		CanvasItem *child = Object::cast_to<CanvasItem>(p_at->get_child(i));
2245 		if (child) {
2246 			_propagate_theme_changed(child, p_owner, p_assign);
2247 		}
2248 	}
2249 
2250 	if (c) {
2251 
2252 		if (p_assign) {
2253 			c->data.theme_owner = p_owner;
2254 		}
2255 		c->notification(NOTIFICATION_THEME_CHANGED);
2256 	}
2257 }
2258 
_theme_changed()2259 void Control::_theme_changed() {
2260 
2261 	_propagate_theme_changed(this, this, false);
2262 }
2263 
set_theme(const Ref<Theme> & p_theme)2264 void Control::set_theme(const Ref<Theme> &p_theme) {
2265 
2266 	if (data.theme == p_theme)
2267 		return;
2268 
2269 	if (data.theme.is_valid()) {
2270 		data.theme->disconnect("changed", this, "_theme_changed");
2271 	}
2272 
2273 	data.theme = p_theme;
2274 	if (!p_theme.is_null()) {
2275 
2276 		data.theme_owner = this;
2277 		_propagate_theme_changed(this, this);
2278 	} else {
2279 
2280 		Control *parent = cast_to<Control>(get_parent());
2281 		if (parent && parent->data.theme_owner) {
2282 			_propagate_theme_changed(this, parent->data.theme_owner);
2283 		} else {
2284 
2285 			_propagate_theme_changed(this, NULL);
2286 		}
2287 	}
2288 
2289 	if (data.theme.is_valid()) {
2290 		data.theme->connect("changed", this, "_theme_changed", varray(), CONNECT_DEFERRED);
2291 	}
2292 }
2293 
accept_event()2294 void Control::accept_event() {
2295 
2296 	if (is_inside_tree())
2297 		get_viewport()->_gui_accept_event();
2298 }
2299 
get_theme() const2300 Ref<Theme> Control::get_theme() const {
2301 
2302 	return data.theme;
2303 }
2304 
set_tooltip(const String & p_tooltip)2305 void Control::set_tooltip(const String &p_tooltip) {
2306 
2307 	data.tooltip = p_tooltip;
2308 	update_configuration_warning();
2309 }
2310 
get_tooltip(const Point2 & p_pos) const2311 String Control::get_tooltip(const Point2 &p_pos) const {
2312 
2313 	return data.tooltip;
2314 }
make_custom_tooltip(const String & p_text) const2315 Control *Control::make_custom_tooltip(const String &p_text) const {
2316 	if (get_script_instance()) {
2317 		return const_cast<Control *>(this)->call("_make_custom_tooltip", p_text);
2318 	}
2319 	return NULL;
2320 }
2321 
set_default_cursor_shape(CursorShape p_shape)2322 void Control::set_default_cursor_shape(CursorShape p_shape) {
2323 
2324 	ERR_FAIL_INDEX(int(p_shape), CURSOR_MAX);
2325 
2326 	data.default_cursor = p_shape;
2327 }
2328 
get_default_cursor_shape() const2329 Control::CursorShape Control::get_default_cursor_shape() const {
2330 
2331 	return data.default_cursor;
2332 }
get_cursor_shape(const Point2 & p_pos) const2333 Control::CursorShape Control::get_cursor_shape(const Point2 &p_pos) const {
2334 
2335 	return data.default_cursor;
2336 }
2337 
get_transform() const2338 Transform2D Control::get_transform() const {
2339 
2340 	Transform2D xform = _get_internal_transform();
2341 	xform[2] += get_position();
2342 	return xform;
2343 }
2344 
_get_tooltip() const2345 String Control::_get_tooltip() const {
2346 
2347 	return data.tooltip;
2348 }
2349 
set_focus_neighbour(Margin p_margin,const NodePath & p_neighbour)2350 void Control::set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour) {
2351 
2352 	ERR_FAIL_INDEX((int)p_margin, 4);
2353 	data.focus_neighbour[p_margin] = p_neighbour;
2354 }
2355 
get_focus_neighbour(Margin p_margin) const2356 NodePath Control::get_focus_neighbour(Margin p_margin) const {
2357 
2358 	ERR_FAIL_INDEX_V((int)p_margin, 4, NodePath());
2359 	return data.focus_neighbour[p_margin];
2360 }
2361 
set_focus_next(const NodePath & p_next)2362 void Control::set_focus_next(const NodePath &p_next) {
2363 
2364 	data.focus_next = p_next;
2365 }
2366 
get_focus_next() const2367 NodePath Control::get_focus_next() const {
2368 
2369 	return data.focus_next;
2370 }
2371 
set_focus_previous(const NodePath & p_prev)2372 void Control::set_focus_previous(const NodePath &p_prev) {
2373 
2374 	data.focus_prev = p_prev;
2375 }
2376 
get_focus_previous() const2377 NodePath Control::get_focus_previous() const {
2378 
2379 	return data.focus_prev;
2380 }
2381 
2382 #define MAX_NEIGHBOUR_SEARCH_COUNT 512
2383 
_get_focus_neighbour(Margin p_margin,int p_count)2384 Control *Control::_get_focus_neighbour(Margin p_margin, int p_count) {
2385 
2386 	ERR_FAIL_INDEX_V((int)p_margin, 4, NULL);
2387 
2388 	if (p_count >= MAX_NEIGHBOUR_SEARCH_COUNT)
2389 		return NULL;
2390 	if (!data.focus_neighbour[p_margin].is_empty()) {
2391 
2392 		Control *c = NULL;
2393 		Node *n = get_node(data.focus_neighbour[p_margin]);
2394 		if (n) {
2395 			c = Object::cast_to<Control>(n);
2396 			ERR_FAIL_COND_V_MSG(!c, NULL, "Neighbor focus node is not a control: " + n->get_name() + ".");
2397 		} else {
2398 			return NULL;
2399 		}
2400 		bool valid = true;
2401 		if (!c->is_visible())
2402 			valid = false;
2403 		if (c->get_focus_mode() == FOCUS_NONE)
2404 			valid = false;
2405 		if (valid)
2406 			return c;
2407 
2408 		c = c->_get_focus_neighbour(p_margin, p_count + 1);
2409 		return c;
2410 	}
2411 
2412 	float dist = 1e7;
2413 	Control *result = NULL;
2414 
2415 	Point2 points[4];
2416 
2417 	Transform2D xform = get_global_transform();
2418 
2419 	points[0] = xform.xform(Point2());
2420 	points[1] = xform.xform(Point2(get_size().x, 0));
2421 	points[2] = xform.xform(get_size());
2422 	points[3] = xform.xform(Point2(0, get_size().y));
2423 
2424 	const Vector2 dir[4] = {
2425 		Vector2(-1, 0),
2426 		Vector2(0, -1),
2427 		Vector2(1, 0),
2428 		Vector2(0, 1)
2429 	};
2430 
2431 	Vector2 vdir = dir[p_margin];
2432 
2433 	float maxd = -1e7;
2434 
2435 	for (int i = 0; i < 4; i++) {
2436 
2437 		float d = vdir.dot(points[i]);
2438 		if (d > maxd)
2439 			maxd = d;
2440 	}
2441 
2442 	Node *base = this;
2443 
2444 	while (base) {
2445 
2446 		Control *c = Object::cast_to<Control>(base);
2447 		if (c) {
2448 			if (c->data.SI)
2449 				break;
2450 			if (c->data.RI)
2451 				break;
2452 		}
2453 		base = base->get_parent();
2454 	}
2455 
2456 	if (!base)
2457 		return NULL;
2458 
2459 	_window_find_focus_neighbour(vdir, base, points, maxd, dist, &result);
2460 
2461 	return result;
2462 }
2463 
_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)2464 void Control::_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) {
2465 
2466 	if (Object::cast_to<Viewport>(p_at))
2467 		return; //bye
2468 
2469 	Control *c = Object::cast_to<Control>(p_at);
2470 
2471 	if (c && c != this && c->get_focus_mode() == FOCUS_ALL && c->is_visible_in_tree()) {
2472 
2473 		Point2 points[4];
2474 
2475 		Transform2D xform = c->get_global_transform();
2476 
2477 		points[0] = xform.xform(Point2());
2478 		points[1] = xform.xform(Point2(c->get_size().x, 0));
2479 		points[2] = xform.xform(c->get_size());
2480 		points[3] = xform.xform(Point2(0, c->get_size().y));
2481 
2482 		float min = 1e7;
2483 
2484 		for (int i = 0; i < 4; i++) {
2485 
2486 			float d = p_dir.dot(points[i]);
2487 			if (d < min)
2488 				min = d;
2489 		}
2490 
2491 		if (min > (p_min - CMP_EPSILON)) {
2492 
2493 			for (int i = 0; i < 4; i++) {
2494 
2495 				Vector2 la = p_points[i];
2496 				Vector2 lb = p_points[(i + 1) % 4];
2497 
2498 				for (int j = 0; j < 4; j++) {
2499 
2500 					Vector2 fa = points[j];
2501 					Vector2 fb = points[(j + 1) % 4];
2502 
2503 					Vector2 pa, pb;
2504 					float d = Geometry::get_closest_points_between_segments(la, lb, fa, fb, pa, pb);
2505 					//float d = Geometry::get_closest_distance_between_segments(Vector3(la.x,la.y,0),Vector3(lb.x,lb.y,0),Vector3(fa.x,fa.y,0),Vector3(fb.x,fb.y,0));
2506 					if (d < r_closest_dist) {
2507 						r_closest_dist = d;
2508 						*r_closest = c;
2509 					}
2510 				}
2511 			}
2512 		}
2513 	}
2514 
2515 	for (int i = 0; i < p_at->get_child_count(); i++) {
2516 
2517 		Node *child = p_at->get_child(i);
2518 		Control *childc = Object::cast_to<Control>(child);
2519 		if (childc && childc->data.SI)
2520 			continue; //subwindow, ignore
2521 		_window_find_focus_neighbour(p_dir, p_at->get_child(i), p_points, p_min, r_closest_dist, r_closest);
2522 	}
2523 }
2524 
set_h_size_flags(int p_flags)2525 void Control::set_h_size_flags(int p_flags) {
2526 
2527 	if (data.h_size_flags == p_flags)
2528 		return;
2529 	data.h_size_flags = p_flags;
2530 	emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
2531 }
2532 
get_h_size_flags() const2533 int Control::get_h_size_flags() const {
2534 	return data.h_size_flags;
2535 }
set_v_size_flags(int p_flags)2536 void Control::set_v_size_flags(int p_flags) {
2537 
2538 	if (data.v_size_flags == p_flags)
2539 		return;
2540 	data.v_size_flags = p_flags;
2541 	emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
2542 }
2543 
set_stretch_ratio(float p_ratio)2544 void Control::set_stretch_ratio(float p_ratio) {
2545 
2546 	if (data.expand == p_ratio)
2547 		return;
2548 
2549 	data.expand = p_ratio;
2550 	emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
2551 }
2552 
get_stretch_ratio() const2553 float Control::get_stretch_ratio() const {
2554 
2555 	return data.expand;
2556 }
2557 
grab_click_focus()2558 void Control::grab_click_focus() {
2559 
2560 	ERR_FAIL_COND(!is_inside_tree());
2561 
2562 	get_viewport()->_gui_grab_click_focus(this);
2563 }
2564 
minimum_size_changed()2565 void Control::minimum_size_changed() {
2566 
2567 	if (!is_inside_tree() || data.block_minimum_size_adjust)
2568 		return;
2569 
2570 	Control *invalidate = this;
2571 
2572 	//invalidate cache upwards
2573 	while (invalidate && invalidate->data.minimum_size_valid) {
2574 		invalidate->data.minimum_size_valid = false;
2575 		if (invalidate->is_set_as_toplevel())
2576 			break; // do not go further up
2577 		invalidate = invalidate->data.parent;
2578 	}
2579 
2580 	if (!is_visible_in_tree())
2581 		return;
2582 
2583 	if (data.updating_last_minimum_size)
2584 		return;
2585 
2586 	data.updating_last_minimum_size = true;
2587 
2588 	MessageQueue::get_singleton()->push_call(this, "_update_minimum_size");
2589 }
2590 
get_v_size_flags() const2591 int Control::get_v_size_flags() const {
2592 	return data.v_size_flags;
2593 }
2594 
set_mouse_filter(MouseFilter p_filter)2595 void Control::set_mouse_filter(MouseFilter p_filter) {
2596 
2597 	ERR_FAIL_INDEX(p_filter, 3);
2598 	data.mouse_filter = p_filter;
2599 	update_configuration_warning();
2600 }
2601 
get_mouse_filter() const2602 Control::MouseFilter Control::get_mouse_filter() const {
2603 
2604 	return data.mouse_filter;
2605 }
2606 
set_pass_on_modal_close_click(bool p_pass_on)2607 void Control::set_pass_on_modal_close_click(bool p_pass_on) {
2608 
2609 	data.pass_on_modal_close_click = p_pass_on;
2610 }
2611 
pass_on_modal_close_click() const2612 bool Control::pass_on_modal_close_click() const {
2613 
2614 	return data.pass_on_modal_close_click;
2615 }
2616 
get_focus_owner() const2617 Control *Control::get_focus_owner() const {
2618 
2619 	ERR_FAIL_COND_V(!is_inside_tree(), NULL);
2620 	return get_viewport()->_gui_get_focus_owner();
2621 }
2622 
warp_mouse(const Point2 & p_to_pos)2623 void Control::warp_mouse(const Point2 &p_to_pos) {
2624 	ERR_FAIL_COND(!is_inside_tree());
2625 	get_viewport()->warp_mouse(get_global_transform().xform(p_to_pos));
2626 }
2627 
is_text_field() const2628 bool Control::is_text_field() const {
2629 	/*
2630     if (get_script_instance()) {
2631         Variant v=p_point;
2632         const Variant *p[2]={&v,&p_data};
2633         Variant::CallError ce;
2634         Variant ret = get_script_instance()->call("is_text_field",p,2,ce);
2635         if (ce.error==Variant::CallError::CALL_OK)
2636             return ret;
2637     }
2638   */
2639 	return false;
2640 }
2641 
set_rotation(float p_radians)2642 void Control::set_rotation(float p_radians) {
2643 
2644 	data.rotation = p_radians;
2645 	update();
2646 	_notify_transform();
2647 	_change_notify("rect_rotation");
2648 }
2649 
get_rotation() const2650 float Control::get_rotation() const {
2651 
2652 	return data.rotation;
2653 }
2654 
set_rotation_degrees(float p_degrees)2655 void Control::set_rotation_degrees(float p_degrees) {
2656 	set_rotation(Math::deg2rad(p_degrees));
2657 }
2658 
get_rotation_degrees() const2659 float Control::get_rotation_degrees() const {
2660 	return Math::rad2deg(get_rotation());
2661 }
2662 
_override_changed()2663 void Control::_override_changed() {
2664 
2665 	notification(NOTIFICATION_THEME_CHANGED);
2666 	minimum_size_changed(); // overrides are likely to affect minimum size
2667 }
2668 
set_pivot_offset(const Vector2 & p_pivot)2669 void Control::set_pivot_offset(const Vector2 &p_pivot) {
2670 
2671 	data.pivot_offset = p_pivot;
2672 	update();
2673 	_notify_transform();
2674 	_change_notify("rect_pivot_offset");
2675 }
2676 
get_pivot_offset() const2677 Vector2 Control::get_pivot_offset() const {
2678 
2679 	return data.pivot_offset;
2680 }
2681 
set_scale(const Vector2 & p_scale)2682 void Control::set_scale(const Vector2 &p_scale) {
2683 
2684 	data.scale = p_scale;
2685 	// Avoid having 0 scale values, can lead to errors in physics and rendering.
2686 	if (data.scale.x == 0)
2687 		data.scale.x = CMP_EPSILON;
2688 	if (data.scale.y == 0)
2689 		data.scale.y = CMP_EPSILON;
2690 	update();
2691 	_notify_transform();
2692 }
get_scale() const2693 Vector2 Control::get_scale() const {
2694 
2695 	return data.scale;
2696 }
2697 
get_root_parent_control() const2698 Control *Control::get_root_parent_control() const {
2699 
2700 	const CanvasItem *ci = this;
2701 	const Control *root = this;
2702 
2703 	while (ci) {
2704 
2705 		const Control *c = Object::cast_to<Control>(ci);
2706 		if (c) {
2707 			root = c;
2708 
2709 			if (c->data.RI || c->data.MI || c->is_toplevel_control())
2710 				break;
2711 		}
2712 
2713 		ci = ci->get_parent_item();
2714 	}
2715 
2716 	return const_cast<Control *>(root);
2717 }
2718 
set_block_minimum_size_adjust(bool p_block)2719 void Control::set_block_minimum_size_adjust(bool p_block) {
2720 	data.block_minimum_size_adjust = p_block;
2721 }
2722 
is_minimum_size_adjust_blocked() const2723 bool Control::is_minimum_size_adjust_blocked() const {
2724 
2725 	return data.block_minimum_size_adjust;
2726 }
2727 
set_disable_visibility_clip(bool p_ignore)2728 void Control::set_disable_visibility_clip(bool p_ignore) {
2729 
2730 	data.disable_visibility_clip = p_ignore;
2731 	update();
2732 }
2733 
is_visibility_clip_disabled() const2734 bool Control::is_visibility_clip_disabled() const {
2735 
2736 	return data.disable_visibility_clip;
2737 }
2738 
get_argument_options(const StringName & p_function,int p_idx,List<String> * r_options) const2739 void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
2740 
2741 #ifdef TOOLS_ENABLED
2742 	const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
2743 #else
2744 	const String quote_style = "\"";
2745 #endif
2746 
2747 	Node::get_argument_options(p_function, p_idx, r_options);
2748 
2749 	if (p_idx == 0) {
2750 		List<StringName> sn;
2751 		String pf = p_function;
2752 		if (pf == "add_color_override" || pf == "has_color" || pf == "has_color_override" || pf == "get_color") {
2753 			Theme::get_default()->get_color_list(get_class(), &sn);
2754 		} else if (pf == "add_style_override" || pf == "has_style" || pf == "has_style_override" || pf == "get_style") {
2755 			Theme::get_default()->get_stylebox_list(get_class(), &sn);
2756 		} else if (pf == "add_font_override" || pf == "has_font" || pf == "has_font_override" || pf == "get_font") {
2757 			Theme::get_default()->get_font_list(get_class(), &sn);
2758 		} else if (pf == "add_constant_override" || pf == "has_constant" || pf == "has_constant_override" || pf == "get_constant") {
2759 			Theme::get_default()->get_constant_list(get_class(), &sn);
2760 		}
2761 
2762 		sn.sort_custom<StringName::AlphCompare>();
2763 		for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
2764 			r_options->push_back(quote_style + E->get() + quote_style);
2765 		}
2766 	}
2767 }
2768 
get_configuration_warning() const2769 String Control::get_configuration_warning() const {
2770 	String warning = CanvasItem::get_configuration_warning();
2771 
2772 	if (data.mouse_filter == MOUSE_FILTER_IGNORE && data.tooltip != "") {
2773 		if (warning != String()) {
2774 			warning += "\n\n";
2775 		}
2776 		warning += TTR("The Hint Tooltip won't be displayed as the control's Mouse Filter is set to \"Ignore\". To solve this, set the Mouse Filter to \"Stop\" or \"Pass\".");
2777 	}
2778 
2779 	return warning;
2780 }
2781 
set_clip_contents(bool p_clip)2782 void Control::set_clip_contents(bool p_clip) {
2783 
2784 	data.clip_contents = p_clip;
2785 	update();
2786 }
2787 
is_clipping_contents()2788 bool Control::is_clipping_contents() {
2789 
2790 	return data.clip_contents;
2791 }
2792 
set_h_grow_direction(GrowDirection p_direction)2793 void Control::set_h_grow_direction(GrowDirection p_direction) {
2794 
2795 	ERR_FAIL_INDEX((int)p_direction, 3);
2796 
2797 	data.h_grow = p_direction;
2798 	_size_changed();
2799 }
2800 
get_h_grow_direction() const2801 Control::GrowDirection Control::get_h_grow_direction() const {
2802 
2803 	return data.h_grow;
2804 }
2805 
set_v_grow_direction(GrowDirection p_direction)2806 void Control::set_v_grow_direction(GrowDirection p_direction) {
2807 
2808 	ERR_FAIL_INDEX((int)p_direction, 3);
2809 
2810 	data.v_grow = p_direction;
2811 	_size_changed();
2812 }
get_v_grow_direction() const2813 Control::GrowDirection Control::get_v_grow_direction() const {
2814 
2815 	return data.v_grow;
2816 }
2817 
_bind_methods()2818 void Control::_bind_methods() {
2819 
2820 	//ClassDB::bind_method(D_METHOD("_window_resize_event"),&Control::_window_resize_event);
2821 	ClassDB::bind_method(D_METHOD("_size_changed"), &Control::_size_changed);
2822 	ClassDB::bind_method(D_METHOD("_update_minimum_size"), &Control::_update_minimum_size);
2823 
2824 	ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event);
2825 	ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size);
2826 	ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size);
2827 	ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_margins"), &Control::set_anchors_preset, DEFVAL(false));
2828 	ClassDB::bind_method(D_METHOD("set_margins_preset", "preset", "resize_mode", "margin"), &Control::set_margins_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
2829 	ClassDB::bind_method(D_METHOD("set_anchors_and_margins_preset", "preset", "resize_mode", "margin"), &Control::set_anchors_and_margins_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
2830 	ClassDB::bind_method(D_METHOD("_set_anchor", "margin", "anchor"), &Control::_set_anchor);
2831 	ClassDB::bind_method(D_METHOD("set_anchor", "margin", "anchor", "keep_margin", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true));
2832 	ClassDB::bind_method(D_METHOD("get_anchor", "margin"), &Control::get_anchor);
2833 	ClassDB::bind_method(D_METHOD("set_margin", "margin", "offset"), &Control::set_margin);
2834 	ClassDB::bind_method(D_METHOD("set_anchor_and_margin", "margin", "anchor", "offset", "push_opposite_anchor"), &Control::set_anchor_and_margin, DEFVAL(false));
2835 	ClassDB::bind_method(D_METHOD("set_begin", "position"), &Control::set_begin);
2836 	ClassDB::bind_method(D_METHOD("set_end", "position"), &Control::set_end);
2837 	ClassDB::bind_method(D_METHOD("set_position", "position", "keep_margins"), &Control::set_position, DEFVAL(false));
2838 	ClassDB::bind_method(D_METHOD("_set_position", "margin"), &Control::_set_position);
2839 	ClassDB::bind_method(D_METHOD("set_size", "size", "keep_margins"), &Control::set_size, DEFVAL(false));
2840 	ClassDB::bind_method(D_METHOD("_set_size", "size"), &Control::_set_size);
2841 	ClassDB::bind_method(D_METHOD("set_custom_minimum_size", "size"), &Control::set_custom_minimum_size);
2842 	ClassDB::bind_method(D_METHOD("set_global_position", "position", "keep_margins"), &Control::set_global_position, DEFVAL(false));
2843 	ClassDB::bind_method(D_METHOD("_set_global_position", "position"), &Control::_set_global_position);
2844 	ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Control::set_rotation);
2845 	ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
2846 	ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
2847 	ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
2848 	ClassDB::bind_method(D_METHOD("get_margin", "margin"), &Control::get_margin);
2849 	ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
2850 	ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
2851 	ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
2852 	ClassDB::bind_method(D_METHOD("get_size"), &Control::get_size);
2853 	ClassDB::bind_method(D_METHOD("get_rotation"), &Control::get_rotation);
2854 	ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
2855 	ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
2856 	ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
2857 	ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
2858 	ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
2859 	ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
2860 	ClassDB::bind_method(D_METHOD("get_rect"), &Control::get_rect);
2861 	ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect);
2862 	ClassDB::bind_method(D_METHOD("show_modal", "exclusive"), &Control::show_modal, DEFVAL(false));
2863 	ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode);
2864 	ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode);
2865 	ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus);
2866 	ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus);
2867 	ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
2868 	ClassDB::bind_method(D_METHOD("get_focus_owner"), &Control::get_focus_owner);
2869 
2870 	ClassDB::bind_method(D_METHOD("set_h_size_flags", "flags"), &Control::set_h_size_flags);
2871 	ClassDB::bind_method(D_METHOD("get_h_size_flags"), &Control::get_h_size_flags);
2872 
2873 	ClassDB::bind_method(D_METHOD("set_stretch_ratio", "ratio"), &Control::set_stretch_ratio);
2874 	ClassDB::bind_method(D_METHOD("get_stretch_ratio"), &Control::get_stretch_ratio);
2875 
2876 	ClassDB::bind_method(D_METHOD("set_v_size_flags", "flags"), &Control::set_v_size_flags);
2877 	ClassDB::bind_method(D_METHOD("get_v_size_flags"), &Control::get_v_size_flags);
2878 
2879 	ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Control::set_theme);
2880 	ClassDB::bind_method(D_METHOD("get_theme"), &Control::get_theme);
2881 
2882 	ClassDB::bind_method(D_METHOD("add_icon_override", "name", "texture"), &Control::add_icon_override);
2883 	ClassDB::bind_method(D_METHOD("add_shader_override", "name", "shader"), &Control::add_shader_override);
2884 	ClassDB::bind_method(D_METHOD("add_stylebox_override", "name", "stylebox"), &Control::add_style_override);
2885 	ClassDB::bind_method(D_METHOD("add_font_override", "name", "font"), &Control::add_font_override);
2886 	ClassDB::bind_method(D_METHOD("add_color_override", "name", "color"), &Control::add_color_override);
2887 	ClassDB::bind_method(D_METHOD("add_constant_override", "name", "constant"), &Control::add_constant_override);
2888 
2889 	ClassDB::bind_method(D_METHOD("get_icon", "name", "type"), &Control::get_icon, DEFVAL(""));
2890 	ClassDB::bind_method(D_METHOD("get_stylebox", "name", "type"), &Control::get_stylebox, DEFVAL(""));
2891 	ClassDB::bind_method(D_METHOD("get_font", "name", "type"), &Control::get_font, DEFVAL(""));
2892 	ClassDB::bind_method(D_METHOD("get_color", "name", "type"), &Control::get_color, DEFVAL(""));
2893 	ClassDB::bind_method(D_METHOD("get_constant", "name", "type"), &Control::get_constant, DEFVAL(""));
2894 
2895 	ClassDB::bind_method(D_METHOD("has_icon_override", "name"), &Control::has_icon_override);
2896 	ClassDB::bind_method(D_METHOD("has_shader_override", "name"), &Control::has_shader_override);
2897 	ClassDB::bind_method(D_METHOD("has_stylebox_override", "name"), &Control::has_stylebox_override);
2898 	ClassDB::bind_method(D_METHOD("has_font_override", "name"), &Control::has_font_override);
2899 	ClassDB::bind_method(D_METHOD("has_color_override", "name"), &Control::has_color_override);
2900 	ClassDB::bind_method(D_METHOD("has_constant_override", "name"), &Control::has_constant_override);
2901 
2902 	ClassDB::bind_method(D_METHOD("has_icon", "name", "type"), &Control::has_icon, DEFVAL(""));
2903 	ClassDB::bind_method(D_METHOD("has_stylebox", "name", "type"), &Control::has_stylebox, DEFVAL(""));
2904 	ClassDB::bind_method(D_METHOD("has_font", "name", "type"), &Control::has_font, DEFVAL(""));
2905 	ClassDB::bind_method(D_METHOD("has_color", "name", "type"), &Control::has_color, DEFVAL(""));
2906 	ClassDB::bind_method(D_METHOD("has_constant", "name", "type"), &Control::has_constant, DEFVAL(""));
2907 
2908 	ClassDB::bind_method(D_METHOD("get_parent_control"), &Control::get_parent_control);
2909 
2910 	ClassDB::bind_method(D_METHOD("set_h_grow_direction", "direction"), &Control::set_h_grow_direction);
2911 	ClassDB::bind_method(D_METHOD("get_h_grow_direction"), &Control::get_h_grow_direction);
2912 
2913 	ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction);
2914 	ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction);
2915 
2916 	ClassDB::bind_method(D_METHOD("set_tooltip", "tooltip"), &Control::set_tooltip);
2917 	ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));
2918 	ClassDB::bind_method(D_METHOD("_get_tooltip"), &Control::_get_tooltip);
2919 
2920 	ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape);
2921 	ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape);
2922 	ClassDB::bind_method(D_METHOD("get_cursor_shape", "position"), &Control::get_cursor_shape, DEFVAL(Point2()));
2923 
2924 	ClassDB::bind_method(D_METHOD("set_focus_neighbour", "margin", "neighbour"), &Control::set_focus_neighbour);
2925 	ClassDB::bind_method(D_METHOD("get_focus_neighbour", "margin"), &Control::get_focus_neighbour);
2926 
2927 	ClassDB::bind_method(D_METHOD("set_focus_next", "next"), &Control::set_focus_next);
2928 	ClassDB::bind_method(D_METHOD("get_focus_next"), &Control::get_focus_next);
2929 
2930 	ClassDB::bind_method(D_METHOD("set_focus_previous", "previous"), &Control::set_focus_previous);
2931 	ClassDB::bind_method(D_METHOD("get_focus_previous"), &Control::get_focus_previous);
2932 
2933 	ClassDB::bind_method(D_METHOD("force_drag", "data", "preview"), &Control::force_drag);
2934 
2935 	ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter);
2936 	ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter);
2937 
2938 	ClassDB::bind_method(D_METHOD("set_clip_contents", "enable"), &Control::set_clip_contents);
2939 	ClassDB::bind_method(D_METHOD("is_clipping_contents"), &Control::is_clipping_contents);
2940 
2941 	ClassDB::bind_method(D_METHOD("grab_click_focus"), &Control::grab_click_focus);
2942 
2943 	ClassDB::bind_method(D_METHOD("set_drag_forwarding", "target"), &Control::set_drag_forwarding);
2944 	ClassDB::bind_method(D_METHOD("set_drag_preview", "control"), &Control::set_drag_preview);
2945 
2946 	ClassDB::bind_method(D_METHOD("warp_mouse", "to_position"), &Control::warp_mouse);
2947 
2948 	ClassDB::bind_method(D_METHOD("minimum_size_changed"), &Control::minimum_size_changed);
2949 
2950 	ClassDB::bind_method(D_METHOD("_theme_changed"), &Control::_theme_changed);
2951 
2952 	ClassDB::bind_method(D_METHOD("_override_changed"), &Control::_override_changed);
2953 
2954 	BIND_VMETHOD(MethodInfo("_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
2955 	BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "_get_minimum_size"));
2956 
2957 	MethodInfo get_drag_data = MethodInfo("get_drag_data", PropertyInfo(Variant::VECTOR2, "position"));
2958 	get_drag_data.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
2959 	BIND_VMETHOD(get_drag_data);
2960 
2961 	BIND_VMETHOD(MethodInfo(Variant::BOOL, "can_drop_data", PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::NIL, "data")));
2962 	BIND_VMETHOD(MethodInfo("drop_data", PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::NIL, "data")));
2963 	BIND_VMETHOD(MethodInfo(Variant::OBJECT, "_make_custom_tooltip", PropertyInfo(Variant::STRING, "for_text")));
2964 	BIND_VMETHOD(MethodInfo(Variant::BOOL, "_clips_input"));
2965 
2966 	ADD_GROUP("Anchor", "anchor_");
2967 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_left", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_LEFT);
2968 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_top", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_TOP);
2969 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_right", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_RIGHT);
2970 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_bottom", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_BOTTOM);
2971 
2972 	ADD_GROUP("Margin", "margin_");
2973 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_left", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_LEFT);
2974 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_top", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_TOP);
2975 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_right", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_RIGHT);
2976 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_bottom", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_BOTTOM);
2977 
2978 	ADD_GROUP("Grow Direction", "grow_");
2979 	ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_horizontal", PROPERTY_HINT_ENUM, "Begin,End,Both"), "set_h_grow_direction", "get_h_grow_direction");
2980 	ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_vertical", PROPERTY_HINT_ENUM, "Begin,End,Both"), "set_v_grow_direction", "get_v_grow_direction");
2981 
2982 	ADD_GROUP("Rect", "rect_");
2983 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_position", "get_position");
2984 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_global_position", PROPERTY_HINT_NONE, "", 0), "_set_global_position", "get_global_position");
2985 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_size", "get_size");
2986 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_min_size"), "set_custom_minimum_size", "get_custom_minimum_size");
2987 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "rect_rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_lesser,or_greater"), "set_rotation_degrees", "get_rotation_degrees");
2988 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_scale"), "set_scale", "get_scale");
2989 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_pivot_offset"), "set_pivot_offset", "get_pivot_offset");
2990 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rect_clip_content"), "set_clip_contents", "is_clipping_contents");
2991 
2992 	ADD_GROUP("Hint", "hint_");
2993 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "hint_tooltip", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip", "_get_tooltip");
2994 
2995 	ADD_GROUP("Focus", "focus_");
2996 	ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_LEFT);
2997 	ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_top", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_TOP);
2998 	ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_right", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_RIGHT);
2999 	ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_bottom", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_BOTTOM);
3000 	ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_next", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_next", "get_focus_next");
3001 	ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_previous", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_previous", "get_focus_previous");
3002 	ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode");
3003 
3004 	ADD_GROUP("Mouse", "mouse_");
3005 	ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass,Ignore"), "set_mouse_filter", "get_mouse_filter");
3006 	ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_default_cursor_shape", PROPERTY_HINT_ENUM, "Arrow,Ibeam,Pointing hand,Cross,Wait,Busy,Drag,Can drop,Forbidden,Vertical resize,Horizontal resize,Secondary diagonal resize,Main diagonal resize,Move,Vertical split,Horizontal split,Help"), "set_default_cursor_shape", "get_default_cursor_shape");
3007 
3008 	ADD_GROUP("Size Flags", "size_flags_");
3009 	ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill,Expand,Shrink Center,Shrink End"), "set_h_size_flags", "get_h_size_flags");
3010 	ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill,Expand,Shrink Center,Shrink End"), "set_v_size_flags", "get_v_size_flags");
3011 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "0,20,0.01,or_greater"), "set_stretch_ratio", "get_stretch_ratio");
3012 	ADD_GROUP("Theme", "");
3013 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
3014 	ADD_GROUP("", "");
3015 
3016 	BIND_ENUM_CONSTANT(FOCUS_NONE);
3017 	BIND_ENUM_CONSTANT(FOCUS_CLICK);
3018 	BIND_ENUM_CONSTANT(FOCUS_ALL);
3019 
3020 	BIND_CONSTANT(NOTIFICATION_RESIZED);
3021 	BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER);
3022 	BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT);
3023 	BIND_CONSTANT(NOTIFICATION_FOCUS_ENTER);
3024 	BIND_CONSTANT(NOTIFICATION_FOCUS_EXIT);
3025 	BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
3026 	BIND_CONSTANT(NOTIFICATION_MODAL_CLOSE);
3027 	BIND_CONSTANT(NOTIFICATION_SCROLL_BEGIN);
3028 	BIND_CONSTANT(NOTIFICATION_SCROLL_END);
3029 
3030 	BIND_ENUM_CONSTANT(CURSOR_ARROW);
3031 	BIND_ENUM_CONSTANT(CURSOR_IBEAM);
3032 	BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
3033 	BIND_ENUM_CONSTANT(CURSOR_CROSS);
3034 	BIND_ENUM_CONSTANT(CURSOR_WAIT);
3035 	BIND_ENUM_CONSTANT(CURSOR_BUSY);
3036 	BIND_ENUM_CONSTANT(CURSOR_DRAG);
3037 	BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
3038 	BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
3039 	BIND_ENUM_CONSTANT(CURSOR_VSIZE);
3040 	BIND_ENUM_CONSTANT(CURSOR_HSIZE);
3041 	BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
3042 	BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
3043 	BIND_ENUM_CONSTANT(CURSOR_MOVE);
3044 	BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
3045 	BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
3046 	BIND_ENUM_CONSTANT(CURSOR_HELP);
3047 
3048 	BIND_ENUM_CONSTANT(PRESET_TOP_LEFT);
3049 	BIND_ENUM_CONSTANT(PRESET_TOP_RIGHT);
3050 	BIND_ENUM_CONSTANT(PRESET_BOTTOM_LEFT);
3051 	BIND_ENUM_CONSTANT(PRESET_BOTTOM_RIGHT);
3052 	BIND_ENUM_CONSTANT(PRESET_CENTER_LEFT);
3053 	BIND_ENUM_CONSTANT(PRESET_CENTER_TOP);
3054 	BIND_ENUM_CONSTANT(PRESET_CENTER_RIGHT);
3055 	BIND_ENUM_CONSTANT(PRESET_CENTER_BOTTOM);
3056 	BIND_ENUM_CONSTANT(PRESET_CENTER);
3057 	BIND_ENUM_CONSTANT(PRESET_LEFT_WIDE);
3058 	BIND_ENUM_CONSTANT(PRESET_TOP_WIDE);
3059 	BIND_ENUM_CONSTANT(PRESET_RIGHT_WIDE);
3060 	BIND_ENUM_CONSTANT(PRESET_BOTTOM_WIDE);
3061 	BIND_ENUM_CONSTANT(PRESET_VCENTER_WIDE);
3062 	BIND_ENUM_CONSTANT(PRESET_HCENTER_WIDE);
3063 	BIND_ENUM_CONSTANT(PRESET_WIDE);
3064 
3065 	BIND_ENUM_CONSTANT(PRESET_MODE_MINSIZE);
3066 	BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_WIDTH);
3067 	BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_HEIGHT);
3068 	BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_SIZE);
3069 
3070 	BIND_ENUM_CONSTANT(SIZE_FILL);
3071 	BIND_ENUM_CONSTANT(SIZE_EXPAND);
3072 	BIND_ENUM_CONSTANT(SIZE_EXPAND_FILL);
3073 	BIND_ENUM_CONSTANT(SIZE_SHRINK_CENTER);
3074 	BIND_ENUM_CONSTANT(SIZE_SHRINK_END);
3075 
3076 	BIND_ENUM_CONSTANT(MOUSE_FILTER_STOP);
3077 	BIND_ENUM_CONSTANT(MOUSE_FILTER_PASS);
3078 	BIND_ENUM_CONSTANT(MOUSE_FILTER_IGNORE);
3079 
3080 	BIND_ENUM_CONSTANT(GROW_DIRECTION_BEGIN);
3081 	BIND_ENUM_CONSTANT(GROW_DIRECTION_END);
3082 	BIND_ENUM_CONSTANT(GROW_DIRECTION_BOTH);
3083 
3084 	BIND_ENUM_CONSTANT(ANCHOR_BEGIN);
3085 	BIND_ENUM_CONSTANT(ANCHOR_END);
3086 
3087 	ADD_SIGNAL(MethodInfo("resized"));
3088 	ADD_SIGNAL(MethodInfo("gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
3089 	ADD_SIGNAL(MethodInfo("mouse_entered"));
3090 	ADD_SIGNAL(MethodInfo("mouse_exited"));
3091 	ADD_SIGNAL(MethodInfo("focus_entered"));
3092 	ADD_SIGNAL(MethodInfo("focus_exited"));
3093 	ADD_SIGNAL(MethodInfo("size_flags_changed"));
3094 	ADD_SIGNAL(MethodInfo("minimum_size_changed"));
3095 	ADD_SIGNAL(MethodInfo("modal_closed"));
3096 
3097 	BIND_VMETHOD(MethodInfo(Variant::BOOL, "has_point", PropertyInfo(Variant::VECTOR2, "point")));
3098 }
Control()3099 Control::Control() {
3100 
3101 	data.parent = NULL;
3102 
3103 	data.mouse_filter = MOUSE_FILTER_STOP;
3104 	data.pass_on_modal_close_click = true;
3105 
3106 	data.SI = NULL;
3107 	data.MI = NULL;
3108 	data.RI = NULL;
3109 	data.theme_owner = NULL;
3110 	data.modal_exclusive = false;
3111 	data.default_cursor = CURSOR_ARROW;
3112 	data.h_size_flags = SIZE_FILL;
3113 	data.v_size_flags = SIZE_FILL;
3114 	data.expand = 1;
3115 	data.rotation = 0;
3116 	data.parent_canvas_item = NULL;
3117 	data.scale = Vector2(1, 1);
3118 	data.drag_owner = 0;
3119 	data.modal_frame = 0;
3120 	data.block_minimum_size_adjust = false;
3121 	data.disable_visibility_clip = false;
3122 	data.h_grow = GROW_DIRECTION_END;
3123 	data.v_grow = GROW_DIRECTION_END;
3124 	data.minimum_size_valid = false;
3125 	data.updating_last_minimum_size = false;
3126 
3127 	data.clip_contents = false;
3128 	for (int i = 0; i < 4; i++) {
3129 		data.anchor[i] = ANCHOR_BEGIN;
3130 		data.margin[i] = 0;
3131 	}
3132 	data.focus_mode = FOCUS_NONE;
3133 	data.modal_prev_focus_owner = 0;
3134 }
3135 
~Control()3136 Control::~Control() {
3137 }
3138