1 /*************************************************************************/
2 /*  canvas_item.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 "canvas_item.h"
32 #include "core/message_queue.h"
33 #include "core/method_bind_ext.gen.inc"
34 #include "core/os/input.h"
35 #include "scene/main/canvas_layer.h"
36 #include "scene/main/viewport.h"
37 #include "scene/resources/font.h"
38 #include "scene/resources/style_box.h"
39 #include "scene/resources/texture.h"
40 #include "scene/scene_string_names.h"
41 #include "servers/visual/visual_server_raster.h"
42 #include "servers/visual_server.h"
43 
44 Mutex *CanvasItemMaterial::material_mutex = NULL;
45 SelfList<CanvasItemMaterial>::List *CanvasItemMaterial::dirty_materials = NULL;
46 Map<CanvasItemMaterial::MaterialKey, CanvasItemMaterial::ShaderData> CanvasItemMaterial::shader_map;
47 CanvasItemMaterial::ShaderNames *CanvasItemMaterial::shader_names = NULL;
48 
init_shaders()49 void CanvasItemMaterial::init_shaders() {
50 
51 #ifndef NO_THREADS
52 	material_mutex = Mutex::create();
53 #endif
54 
55 	dirty_materials = memnew(SelfList<CanvasItemMaterial>::List);
56 
57 	shader_names = memnew(ShaderNames);
58 
59 	shader_names->particles_anim_h_frames = "particles_anim_h_frames";
60 	shader_names->particles_anim_v_frames = "particles_anim_v_frames";
61 	shader_names->particles_anim_loop = "particles_anim_loop";
62 }
63 
finish_shaders()64 void CanvasItemMaterial::finish_shaders() {
65 
66 	memdelete(dirty_materials);
67 	memdelete(shader_names);
68 	dirty_materials = NULL;
69 
70 #ifndef NO_THREADS
71 	memdelete(material_mutex);
72 #endif
73 }
74 
_update_shader()75 void CanvasItemMaterial::_update_shader() {
76 
77 	dirty_materials->remove(&element);
78 
79 	MaterialKey mk = _compute_key();
80 	if (mk.key == current_key.key)
81 		return; //no update required in the end
82 
83 	if (shader_map.has(current_key)) {
84 		shader_map[current_key].users--;
85 		if (shader_map[current_key].users == 0) {
86 			//deallocate shader, as it's no longer in use
87 			VS::get_singleton()->free(shader_map[current_key].shader);
88 			shader_map.erase(current_key);
89 		}
90 	}
91 
92 	current_key = mk;
93 
94 	if (shader_map.has(mk)) {
95 
96 		VS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
97 		shader_map[mk].users++;
98 		return;
99 	}
100 
101 	//must create a shader!
102 
103 	String code = "shader_type canvas_item;\nrender_mode ";
104 	switch (blend_mode) {
105 		case BLEND_MODE_MIX: code += "blend_mix"; break;
106 		case BLEND_MODE_ADD: code += "blend_add"; break;
107 		case BLEND_MODE_SUB: code += "blend_sub"; break;
108 		case BLEND_MODE_MUL: code += "blend_mul"; break;
109 		case BLEND_MODE_PREMULT_ALPHA: code += "blend_premul_alpha"; break;
110 		case BLEND_MODE_DISABLED: code += "blend_disabled"; break;
111 	}
112 
113 	switch (light_mode) {
114 		case LIGHT_MODE_NORMAL: break;
115 		case LIGHT_MODE_UNSHADED: code += ",unshaded"; break;
116 		case LIGHT_MODE_LIGHT_ONLY: code += ",light_only"; break;
117 	}
118 
119 	code += ";\n";
120 
121 	if (particles_animation) {
122 
123 		code += "uniform int particles_anim_h_frames;\n";
124 		code += "uniform int particles_anim_v_frames;\n";
125 		code += "uniform bool particles_anim_loop;\n";
126 
127 		code += "void vertex() {\n";
128 
129 		code += "\tfloat h_frames = float(particles_anim_h_frames);\n";
130 		code += "\tfloat v_frames = float(particles_anim_v_frames);\n";
131 
132 		code += "\tVERTEX.xy /= vec2(h_frames, v_frames);\n";
133 
134 		code += "\tfloat particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);\n";
135 		code += "\tfloat particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));\n";
136 		code += "\tif (!particles_anim_loop) {\n";
137 		code += "\t\tparticle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);\n";
138 		code += "\t} else {\n";
139 		code += "\t\tparticle_frame = mod(particle_frame, particle_total_frames);\n";
140 		code += "\t}";
141 		code += "\tUV /= vec2(h_frames, v_frames);\n";
142 		code += "\tUV += vec2(mod(particle_frame, h_frames) / h_frames, floor(particle_frame / h_frames) / v_frames);\n";
143 		code += "}\n";
144 	}
145 
146 	ShaderData shader_data;
147 	shader_data.shader = VS::get_singleton()->shader_create();
148 	shader_data.users = 1;
149 
150 	VS::get_singleton()->shader_set_code(shader_data.shader, code);
151 
152 	shader_map[mk] = shader_data;
153 
154 	VS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
155 }
156 
flush_changes()157 void CanvasItemMaterial::flush_changes() {
158 
159 	if (material_mutex)
160 		material_mutex->lock();
161 
162 	while (dirty_materials->first()) {
163 
164 		dirty_materials->first()->self()->_update_shader();
165 	}
166 
167 	if (material_mutex)
168 		material_mutex->unlock();
169 }
170 
_queue_shader_change()171 void CanvasItemMaterial::_queue_shader_change() {
172 
173 	if (material_mutex)
174 		material_mutex->lock();
175 
176 	if (!element.in_list()) {
177 		dirty_materials->add(&element);
178 	}
179 
180 	if (material_mutex)
181 		material_mutex->unlock();
182 }
183 
_is_shader_dirty() const184 bool CanvasItemMaterial::_is_shader_dirty() const {
185 
186 	bool dirty = false;
187 
188 	if (material_mutex)
189 		material_mutex->lock();
190 
191 	dirty = element.in_list();
192 
193 	if (material_mutex)
194 		material_mutex->unlock();
195 
196 	return dirty;
197 }
set_blend_mode(BlendMode p_blend_mode)198 void CanvasItemMaterial::set_blend_mode(BlendMode p_blend_mode) {
199 
200 	blend_mode = p_blend_mode;
201 	_queue_shader_change();
202 }
203 
get_blend_mode() const204 CanvasItemMaterial::BlendMode CanvasItemMaterial::get_blend_mode() const {
205 	return blend_mode;
206 }
207 
set_light_mode(LightMode p_light_mode)208 void CanvasItemMaterial::set_light_mode(LightMode p_light_mode) {
209 
210 	light_mode = p_light_mode;
211 	_queue_shader_change();
212 }
213 
get_light_mode() const214 CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const {
215 
216 	return light_mode;
217 }
218 
set_particles_animation(bool p_particles_anim)219 void CanvasItemMaterial::set_particles_animation(bool p_particles_anim) {
220 	particles_animation = p_particles_anim;
221 	_queue_shader_change();
222 	_change_notify();
223 }
224 
get_particles_animation() const225 bool CanvasItemMaterial::get_particles_animation() const {
226 	return particles_animation;
227 }
228 
set_particles_anim_h_frames(int p_frames)229 void CanvasItemMaterial::set_particles_anim_h_frames(int p_frames) {
230 
231 	particles_anim_h_frames = p_frames;
232 	VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_h_frames, p_frames);
233 }
234 
get_particles_anim_h_frames() const235 int CanvasItemMaterial::get_particles_anim_h_frames() const {
236 
237 	return particles_anim_h_frames;
238 }
set_particles_anim_v_frames(int p_frames)239 void CanvasItemMaterial::set_particles_anim_v_frames(int p_frames) {
240 
241 	particles_anim_v_frames = p_frames;
242 	VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_v_frames, p_frames);
243 }
244 
get_particles_anim_v_frames() const245 int CanvasItemMaterial::get_particles_anim_v_frames() const {
246 
247 	return particles_anim_v_frames;
248 }
249 
set_particles_anim_loop(bool p_loop)250 void CanvasItemMaterial::set_particles_anim_loop(bool p_loop) {
251 
252 	particles_anim_loop = p_loop;
253 	VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_loop, particles_anim_loop);
254 }
255 
get_particles_anim_loop() const256 bool CanvasItemMaterial::get_particles_anim_loop() const {
257 
258 	return particles_anim_loop;
259 }
260 
_validate_property(PropertyInfo & property) const261 void CanvasItemMaterial::_validate_property(PropertyInfo &property) const {
262 	if (property.name.begins_with("particles_anim_") && !particles_animation) {
263 		property.usage = 0;
264 	}
265 }
266 
get_shader_rid() const267 RID CanvasItemMaterial::get_shader_rid() const {
268 
269 	ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
270 	return shader_map[current_key].shader;
271 }
272 
get_shader_mode() const273 Shader::Mode CanvasItemMaterial::get_shader_mode() const {
274 
275 	return Shader::MODE_CANVAS_ITEM;
276 }
277 
_bind_methods()278 void CanvasItemMaterial::_bind_methods() {
279 
280 	ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode);
281 	ClassDB::bind_method(D_METHOD("get_blend_mode"), &CanvasItemMaterial::get_blend_mode);
282 
283 	ClassDB::bind_method(D_METHOD("set_light_mode", "light_mode"), &CanvasItemMaterial::set_light_mode);
284 	ClassDB::bind_method(D_METHOD("get_light_mode"), &CanvasItemMaterial::get_light_mode);
285 
286 	ClassDB::bind_method(D_METHOD("set_particles_animation", "particles_anim"), &CanvasItemMaterial::set_particles_animation);
287 	ClassDB::bind_method(D_METHOD("get_particles_animation"), &CanvasItemMaterial::get_particles_animation);
288 
289 	ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &CanvasItemMaterial::set_particles_anim_h_frames);
290 	ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &CanvasItemMaterial::get_particles_anim_h_frames);
291 
292 	ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &CanvasItemMaterial::set_particles_anim_v_frames);
293 	ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &CanvasItemMaterial::get_particles_anim_v_frames);
294 
295 	ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &CanvasItemMaterial::set_particles_anim_loop);
296 	ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &CanvasItemMaterial::get_particles_anim_loop);
297 
298 	ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Sub,Mul,Premult Alpha"), "set_blend_mode", "get_blend_mode");
299 	ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mode", PROPERTY_HINT_ENUM, "Normal,Unshaded,Light Only"), "set_light_mode", "get_light_mode");
300 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_animation"), "set_particles_animation", "get_particles_animation");
301 
302 	ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames");
303 	ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames");
304 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
305 
306 	BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
307 	BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
308 	BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
309 	BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
310 	BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
311 
312 	BIND_ENUM_CONSTANT(LIGHT_MODE_NORMAL);
313 	BIND_ENUM_CONSTANT(LIGHT_MODE_UNSHADED);
314 	BIND_ENUM_CONSTANT(LIGHT_MODE_LIGHT_ONLY);
315 }
316 
CanvasItemMaterial()317 CanvasItemMaterial::CanvasItemMaterial() :
318 		element(this) {
319 
320 	blend_mode = BLEND_MODE_MIX;
321 	light_mode = LIGHT_MODE_NORMAL;
322 	particles_animation = false;
323 
324 	set_particles_anim_h_frames(1);
325 	set_particles_anim_v_frames(1);
326 	set_particles_anim_loop(false);
327 
328 	current_key.key = 0;
329 	current_key.invalid_key = 1;
330 	_queue_shader_change();
331 }
332 
~CanvasItemMaterial()333 CanvasItemMaterial::~CanvasItemMaterial() {
334 
335 	if (material_mutex)
336 		material_mutex->lock();
337 
338 	if (shader_map.has(current_key)) {
339 		shader_map[current_key].users--;
340 		if (shader_map[current_key].users == 0) {
341 			//deallocate shader, as it's no longer in use
342 			VS::get_singleton()->free(shader_map[current_key].shader);
343 			shader_map.erase(current_key);
344 		}
345 
346 		VS::get_singleton()->material_set_shader(_get_material(), RID());
347 	}
348 
349 	if (material_mutex)
350 		material_mutex->unlock();
351 }
352 
353 ///////////////////////////////////////////////////////////////////
354 #ifdef TOOLS_ENABLED
_edit_is_selected_on_click(const Point2 & p_point,double p_tolerance) const355 bool CanvasItem::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
356 	if (_edit_use_rect()) {
357 		return _edit_get_rect().has_point(p_point);
358 	} else {
359 		return p_point.length() < p_tolerance;
360 	}
361 }
362 
_edit_get_transform() const363 Transform2D CanvasItem::_edit_get_transform() const {
364 	return Transform2D(_edit_get_rotation(), _edit_get_position() + _edit_get_pivot());
365 }
366 #endif
367 
is_visible_in_tree() const368 bool CanvasItem::is_visible_in_tree() const {
369 
370 	if (!is_inside_tree())
371 		return false;
372 
373 	const CanvasItem *p = this;
374 
375 	while (p) {
376 		if (!p->visible)
377 			return false;
378 		p = p->get_parent_item();
379 	}
380 
381 	return true;
382 }
383 
_propagate_visibility_changed(bool p_visible)384 void CanvasItem::_propagate_visibility_changed(bool p_visible) {
385 
386 	if (p_visible && first_draw) { //avoid propagating it twice
387 		first_draw = false;
388 	}
389 	notification(NOTIFICATION_VISIBILITY_CHANGED);
390 
391 	if (p_visible)
392 		update(); //todo optimize
393 	else
394 		emit_signal(SceneStringNames::get_singleton()->hide);
395 	_block();
396 
397 	for (int i = 0; i < get_child_count(); i++) {
398 
399 		CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
400 
401 		if (c && c->visible) //should the toplevels stop propagation? i think so but..
402 			c->_propagate_visibility_changed(p_visible);
403 	}
404 
405 	_unblock();
406 }
407 
show()408 void CanvasItem::show() {
409 
410 	if (visible)
411 		return;
412 
413 	visible = true;
414 	VisualServer::get_singleton()->canvas_item_set_visible(canvas_item, true);
415 
416 	if (!is_inside_tree())
417 		return;
418 
419 	_propagate_visibility_changed(true);
420 	_change_notify("visible");
421 }
422 
hide()423 void CanvasItem::hide() {
424 
425 	if (!visible)
426 		return;
427 
428 	visible = false;
429 	VisualServer::get_singleton()->canvas_item_set_visible(canvas_item, false);
430 
431 	if (!is_inside_tree())
432 		return;
433 
434 	_propagate_visibility_changed(false);
435 	_change_notify("visible");
436 }
437 
438 CanvasItem *CanvasItem::current_item_drawn = NULL;
get_current_item_drawn()439 CanvasItem *CanvasItem::get_current_item_drawn() {
440 	return current_item_drawn;
441 }
442 
_update_callback()443 void CanvasItem::_update_callback() {
444 
445 	if (!is_inside_tree()) {
446 		pending_update = false;
447 		return;
448 	}
449 
450 	VisualServer::get_singleton()->canvas_item_clear(get_canvas_item());
451 	//todo updating = true - only allow drawing here
452 	if (is_visible_in_tree()) { //todo optimize this!!
453 		if (first_draw) {
454 			notification(NOTIFICATION_VISIBILITY_CHANGED);
455 			first_draw = false;
456 		}
457 		drawing = true;
458 		current_item_drawn = this;
459 		notification(NOTIFICATION_DRAW);
460 		emit_signal(SceneStringNames::get_singleton()->draw);
461 		if (get_script_instance()) {
462 			get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_draw, NULL, 0);
463 		}
464 		current_item_drawn = NULL;
465 		drawing = false;
466 	}
467 	//todo updating = false
468 	pending_update = false; // don't change to false until finished drawing (avoid recursive update)
469 }
470 
get_global_transform_with_canvas() const471 Transform2D CanvasItem::get_global_transform_with_canvas() const {
472 
473 	if (canvas_layer)
474 		return canvas_layer->get_transform() * get_global_transform();
475 	else if (is_inside_tree())
476 		return get_viewport()->get_canvas_transform() * get_global_transform();
477 	else
478 		return get_global_transform();
479 }
480 
get_global_transform() const481 Transform2D CanvasItem::get_global_transform() const {
482 #ifdef DEBUG_ENABLED
483 	ERR_FAIL_COND_V(!is_inside_tree(), get_transform());
484 #endif
485 	if (global_invalid) {
486 
487 		const CanvasItem *pi = get_parent_item();
488 		if (pi)
489 			global_transform = pi->get_global_transform() * get_transform();
490 		else
491 			global_transform = get_transform();
492 
493 		global_invalid = false;
494 	}
495 
496 	return global_transform;
497 }
498 
_toplevel_raise_self()499 void CanvasItem::_toplevel_raise_self() {
500 
501 	if (!is_inside_tree())
502 		return;
503 
504 	if (canvas_layer)
505 		VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, canvas_layer->get_sort_index());
506 	else
507 		VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_viewport()->gui_get_canvas_sort_index());
508 }
509 
_enter_canvas()510 void CanvasItem::_enter_canvas() {
511 
512 	if ((!Object::cast_to<CanvasItem>(get_parent())) || toplevel) {
513 
514 		Node *n = this;
515 
516 		canvas_layer = NULL;
517 
518 		while (n) {
519 
520 			canvas_layer = Object::cast_to<CanvasLayer>(n);
521 			if (canvas_layer) {
522 				break;
523 			}
524 			if (Object::cast_to<Viewport>(n)) {
525 				break;
526 			}
527 			n = n->get_parent();
528 		}
529 
530 		RID canvas;
531 		if (canvas_layer)
532 			canvas = canvas_layer->get_canvas();
533 		else
534 			canvas = get_viewport()->find_world_2d()->get_canvas();
535 
536 		VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
537 
538 		group = "root_canvas" + itos(canvas.get_id());
539 
540 		add_to_group(group);
541 		if (canvas_layer)
542 			canvas_layer->reset_sort_index();
543 		else
544 			get_viewport()->gui_reset_canvas_sort_index();
545 
546 		get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE, group, "_toplevel_raise_self");
547 
548 	} else {
549 
550 		CanvasItem *parent = get_parent_item();
551 		canvas_layer = parent->canvas_layer;
552 		VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, parent->get_canvas_item());
553 		VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
554 	}
555 
556 	pending_update = false;
557 	update();
558 
559 	notification(NOTIFICATION_ENTER_CANVAS);
560 }
561 
_exit_canvas()562 void CanvasItem::_exit_canvas() {
563 
564 	notification(NOTIFICATION_EXIT_CANVAS, true); //reverse the notification
565 	VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, RID());
566 	canvas_layer = NULL;
567 	group = "";
568 }
569 
_notification(int p_what)570 void CanvasItem::_notification(int p_what) {
571 
572 	switch (p_what) {
573 		case NOTIFICATION_ENTER_TREE: {
574 
575 			first_draw = true;
576 			if (get_parent()) {
577 				CanvasItem *ci = Object::cast_to<CanvasItem>(get_parent());
578 				if (ci)
579 					C = ci->children_items.push_back(this);
580 			}
581 			_enter_canvas();
582 			if (!block_transform_notify && !xform_change.in_list()) {
583 				get_tree()->xform_change_list.add(&xform_change);
584 			}
585 		} break;
586 		case NOTIFICATION_MOVED_IN_PARENT: {
587 
588 			if (!is_inside_tree())
589 				break;
590 
591 			if (group != "") {
592 				get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE, group, "_toplevel_raise_self");
593 			} else {
594 				CanvasItem *p = get_parent_item();
595 				ERR_FAIL_COND(!p);
596 				VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
597 			}
598 
599 		} break;
600 		case NOTIFICATION_EXIT_TREE: {
601 			if (xform_change.in_list())
602 				get_tree()->xform_change_list.remove(&xform_change);
603 			_exit_canvas();
604 			if (C) {
605 				Object::cast_to<CanvasItem>(get_parent())->children_items.erase(C);
606 				C = NULL;
607 			}
608 			global_invalid = true;
609 		} break;
610 		case NOTIFICATION_DRAW:
611 		case NOTIFICATION_TRANSFORM_CHANGED: {
612 
613 		} break;
614 		case NOTIFICATION_VISIBILITY_CHANGED: {
615 
616 			emit_signal(SceneStringNames::get_singleton()->visibility_changed);
617 		} break;
618 	}
619 }
620 
set_visible(bool p_visible)621 void CanvasItem::set_visible(bool p_visible) {
622 
623 	if (p_visible)
624 		show();
625 	else
626 		hide();
627 }
is_visible() const628 bool CanvasItem::is_visible() const {
629 
630 	return visible;
631 }
632 
update()633 void CanvasItem::update() {
634 
635 	if (!is_inside_tree())
636 		return;
637 	if (pending_update)
638 		return;
639 
640 	pending_update = true;
641 
642 	MessageQueue::get_singleton()->push_call(this, "_update_callback");
643 }
644 
set_modulate(const Color & p_modulate)645 void CanvasItem::set_modulate(const Color &p_modulate) {
646 
647 	if (modulate == p_modulate)
648 		return;
649 
650 	modulate = p_modulate;
651 	VisualServer::get_singleton()->canvas_item_set_modulate(canvas_item, modulate);
652 }
get_modulate() const653 Color CanvasItem::get_modulate() const {
654 
655 	return modulate;
656 }
657 
set_as_toplevel(bool p_toplevel)658 void CanvasItem::set_as_toplevel(bool p_toplevel) {
659 
660 	if (toplevel == p_toplevel)
661 		return;
662 
663 	if (!is_inside_tree()) {
664 		toplevel = p_toplevel;
665 		return;
666 	}
667 
668 	_exit_canvas();
669 	toplevel = p_toplevel;
670 	_enter_canvas();
671 }
672 
is_set_as_toplevel() const673 bool CanvasItem::is_set_as_toplevel() const {
674 
675 	return toplevel;
676 }
677 
get_parent_item() const678 CanvasItem *CanvasItem::get_parent_item() const {
679 
680 	if (toplevel)
681 		return NULL;
682 
683 	return Object::cast_to<CanvasItem>(get_parent());
684 }
685 
set_self_modulate(const Color & p_self_modulate)686 void CanvasItem::set_self_modulate(const Color &p_self_modulate) {
687 
688 	if (self_modulate == p_self_modulate)
689 		return;
690 
691 	self_modulate = p_self_modulate;
692 	VisualServer::get_singleton()->canvas_item_set_self_modulate(canvas_item, self_modulate);
693 }
get_self_modulate() const694 Color CanvasItem::get_self_modulate() const {
695 
696 	return self_modulate;
697 }
698 
set_light_mask(int p_light_mask)699 void CanvasItem::set_light_mask(int p_light_mask) {
700 
701 	if (light_mask == p_light_mask)
702 		return;
703 
704 	light_mask = p_light_mask;
705 	VS::get_singleton()->canvas_item_set_light_mask(canvas_item, p_light_mask);
706 }
707 
get_light_mask() const708 int CanvasItem::get_light_mask() const {
709 
710 	return light_mask;
711 }
712 
item_rect_changed(bool p_size_changed)713 void CanvasItem::item_rect_changed(bool p_size_changed) {
714 
715 	if (p_size_changed)
716 		update();
717 	emit_signal(SceneStringNames::get_singleton()->item_rect_changed);
718 }
719 
draw_line(const Point2 & p_from,const Point2 & p_to,const Color & p_color,float p_width,bool p_antialiased)720 void CanvasItem::draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width, bool p_antialiased) {
721 
722 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
723 
724 	VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width, p_antialiased);
725 }
726 
draw_polyline(const Vector<Point2> & p_points,const Color & p_color,float p_width,bool p_antialiased)727 void CanvasItem::draw_polyline(const Vector<Point2> &p_points, const Color &p_color, float p_width, bool p_antialiased) {
728 
729 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
730 
731 	Vector<Color> colors;
732 	colors.push_back(p_color);
733 	VisualServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, colors, p_width, p_antialiased);
734 }
735 
draw_polyline_colors(const Vector<Point2> & p_points,const Vector<Color> & p_colors,float p_width,bool p_antialiased)736 void CanvasItem::draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
737 
738 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
739 
740 	VisualServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, p_colors, p_width, p_antialiased);
741 }
742 
draw_arc(const Vector2 & p_center,float p_radius,float p_start_angle,float p_end_angle,int p_point_count,const Color & p_color,float p_width,bool p_antialiased)743 void CanvasItem::draw_arc(const Vector2 &p_center, float p_radius, float p_start_angle, float p_end_angle, int p_point_count, const Color &p_color, float p_width, bool p_antialiased) {
744 
745 	Vector<Point2> points;
746 	points.resize(p_point_count);
747 	const float delta_angle = p_end_angle - p_start_angle;
748 	for (int i = 0; i < p_point_count; i++) {
749 		float theta = (i / (p_point_count - 1.0f)) * delta_angle + p_start_angle;
750 		points.set(i, p_center + Vector2(Math::cos(theta), Math::sin(theta)) * p_radius);
751 	}
752 
753 	draw_polyline(points, p_color, p_width, p_antialiased);
754 }
755 
draw_multiline(const Vector<Point2> & p_points,const Color & p_color,float p_width,bool p_antialiased)756 void CanvasItem::draw_multiline(const Vector<Point2> &p_points, const Color &p_color, float p_width, bool p_antialiased) {
757 
758 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
759 
760 	Vector<Color> colors;
761 	colors.push_back(p_color);
762 	VisualServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, colors, p_width, p_antialiased);
763 }
764 
draw_multiline_colors(const Vector<Point2> & p_points,const Vector<Color> & p_colors,float p_width,bool p_antialiased)765 void CanvasItem::draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
766 
767 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
768 
769 	VisualServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, p_colors, p_width, p_antialiased);
770 }
771 
draw_rect(const Rect2 & p_rect,const Color & p_color,bool p_filled,float p_width,bool p_antialiased)772 void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled, float p_width, bool p_antialiased) {
773 
774 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
775 
776 	if (p_filled) {
777 		if (p_width != 1.0) {
778 			WARN_PRINT("The draw_rect() \"width\" argument has no effect when \"filled\" is \"true\".");
779 		}
780 
781 		if (p_antialiased) {
782 			WARN_PRINT("The draw_rect() \"antialiased\" argument has no effect when \"filled\" is \"true\".");
783 		}
784 
785 		VisualServer::get_singleton()->canvas_item_add_rect(canvas_item, p_rect, p_color);
786 	} else {
787 		// Thick lines are offset depending on their width to avoid partial overlapping.
788 		// Thin lines don't require an offset, so don't apply one in this case
789 		float offset;
790 		if (p_width >= 2) {
791 			offset = p_width / 2.0;
792 		} else {
793 			offset = 0.0;
794 		}
795 
796 		VisualServer::get_singleton()->canvas_item_add_line(
797 				canvas_item,
798 				p_rect.position + Size2(-offset, 0),
799 				p_rect.position + Size2(p_rect.size.width + offset, 0),
800 				p_color,
801 				p_width,
802 				p_antialiased);
803 		VisualServer::get_singleton()->canvas_item_add_line(
804 				canvas_item,
805 				p_rect.position + Size2(p_rect.size.width, offset),
806 				p_rect.position + Size2(p_rect.size.width, p_rect.size.height - offset),
807 				p_color,
808 				p_width,
809 				p_antialiased);
810 		VisualServer::get_singleton()->canvas_item_add_line(
811 				canvas_item,
812 				p_rect.position + Size2(p_rect.size.width + offset, p_rect.size.height),
813 				p_rect.position + Size2(-offset, p_rect.size.height),
814 				p_color,
815 				p_width,
816 				p_antialiased);
817 		VisualServer::get_singleton()->canvas_item_add_line(
818 				canvas_item,
819 				p_rect.position + Size2(0, p_rect.size.height - offset),
820 				p_rect.position + Size2(0, offset),
821 				p_color,
822 				p_width,
823 				p_antialiased);
824 	}
825 }
826 
draw_circle(const Point2 & p_pos,float p_radius,const Color & p_color)827 void CanvasItem::draw_circle(const Point2 &p_pos, float p_radius, const Color &p_color) {
828 
829 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
830 
831 	VisualServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color);
832 }
833 
draw_texture(const Ref<Texture> & p_texture,const Point2 & p_pos,const Color & p_modulate,const Ref<Texture> & p_normal_map)834 void CanvasItem::draw_texture(const Ref<Texture> &p_texture, const Point2 &p_pos, const Color &p_modulate, const Ref<Texture> &p_normal_map) {
835 
836 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
837 
838 	ERR_FAIL_COND(p_texture.is_null());
839 
840 	p_texture->draw(canvas_item, p_pos, p_modulate, false, p_normal_map);
841 }
842 
draw_texture_rect(const Ref<Texture> & p_texture,const Rect2 & p_rect,bool p_tile,const Color & p_modulate,bool p_transpose,const Ref<Texture> & p_normal_map)843 void CanvasItem::draw_texture_rect(const Ref<Texture> &p_texture, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) {
844 
845 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
846 
847 	ERR_FAIL_COND(p_texture.is_null());
848 	p_texture->draw_rect(canvas_item, p_rect, p_tile, p_modulate, p_transpose, p_normal_map);
849 }
draw_texture_rect_region(const Ref<Texture> & p_texture,const Rect2 & p_rect,const Rect2 & p_src_rect,const Color & p_modulate,bool p_transpose,const Ref<Texture> & p_normal_map,bool p_clip_uv)850 void CanvasItem::draw_texture_rect_region(const Ref<Texture> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) {
851 
852 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
853 	ERR_FAIL_COND(p_texture.is_null());
854 	p_texture->draw_rect_region(canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_normal_map, p_clip_uv);
855 }
856 
draw_style_box(const Ref<StyleBox> & p_style_box,const Rect2 & p_rect)857 void CanvasItem::draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect) {
858 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
859 
860 	ERR_FAIL_COND(p_style_box.is_null());
861 
862 	p_style_box->draw(canvas_item, p_rect);
863 }
draw_primitive(const Vector<Point2> & p_points,const Vector<Color> & p_colors,const Vector<Point2> & p_uvs,Ref<Texture> p_texture,float p_width,const Ref<Texture> & p_normal_map)864 void CanvasItem::draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture> p_texture, float p_width, const Ref<Texture> &p_normal_map) {
865 
866 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
867 
868 	RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
869 	RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
870 
871 	VisualServer::get_singleton()->canvas_item_add_primitive(canvas_item, p_points, p_colors, p_uvs, rid, p_width, rid_normal);
872 }
draw_set_transform(const Point2 & p_offset,float p_rot,const Size2 & p_scale)873 void CanvasItem::draw_set_transform(const Point2 &p_offset, float p_rot, const Size2 &p_scale) {
874 
875 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
876 
877 	Transform2D xform(p_rot, p_offset);
878 	xform.scale_basis(p_scale);
879 	VisualServer::get_singleton()->canvas_item_add_set_transform(canvas_item, xform);
880 }
881 
draw_set_transform_matrix(const Transform2D & p_matrix)882 void CanvasItem::draw_set_transform_matrix(const Transform2D &p_matrix) {
883 
884 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
885 
886 	VisualServer::get_singleton()->canvas_item_add_set_transform(canvas_item, p_matrix);
887 }
888 
draw_polygon(const Vector<Point2> & p_points,const Vector<Color> & p_colors,const Vector<Point2> & p_uvs,Ref<Texture> p_texture,const Ref<Texture> & p_normal_map,bool p_antialiased)889 void CanvasItem::draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture> p_texture, const Ref<Texture> &p_normal_map, bool p_antialiased) {
890 
891 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
892 
893 	RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
894 	RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
895 
896 	VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid, rid_normal, p_antialiased);
897 }
898 
draw_colored_polygon(const Vector<Point2> & p_points,const Color & p_color,const Vector<Point2> & p_uvs,Ref<Texture> p_texture,const Ref<Texture> & p_normal_map,bool p_antialiased)899 void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs, Ref<Texture> p_texture, const Ref<Texture> &p_normal_map, bool p_antialiased) {
900 
901 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
902 
903 	Vector<Color> colors;
904 	colors.push_back(p_color);
905 	RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
906 	RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
907 
908 	VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid, rid_normal, p_antialiased);
909 }
910 
draw_mesh(const Ref<Mesh> & p_mesh,const Ref<Texture> & p_texture,const Ref<Texture> & p_normal_map,const Transform2D & p_transform,const Color & p_modulate)911 void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, const Transform2D &p_transform, const Color &p_modulate) {
912 
913 	ERR_FAIL_COND(p_mesh.is_null());
914 	RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
915 	RID normal_map_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
916 
917 	VisualServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), p_transform, p_modulate, texture_rid, normal_map_rid);
918 }
draw_multimesh(const Ref<MultiMesh> & p_multimesh,const Ref<Texture> & p_texture,const Ref<Texture> & p_normal_map)919 void CanvasItem::draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map) {
920 
921 	ERR_FAIL_COND(p_multimesh.is_null());
922 	RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
923 	RID normal_map_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
924 	VisualServer::get_singleton()->canvas_item_add_multimesh(canvas_item, p_multimesh->get_rid(), texture_rid, normal_map_rid);
925 }
926 
draw_string(const Ref<Font> & p_font,const Point2 & p_pos,const String & p_text,const Color & p_modulate,int p_clip_w)927 void CanvasItem::draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, const Color &p_modulate, int p_clip_w) {
928 
929 	ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
930 
931 	ERR_FAIL_COND(p_font.is_null());
932 	p_font->draw(canvas_item, p_pos, p_text, p_modulate, p_clip_w);
933 }
934 
draw_char(const Ref<Font> & p_font,const Point2 & p_pos,const String & p_char,const String & p_next,const Color & p_modulate)935 float CanvasItem::draw_char(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, const String &p_next, const Color &p_modulate) {
936 
937 	ERR_FAIL_COND_V_MSG(!drawing, 0, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
938 
939 	ERR_FAIL_COND_V(p_char.length() != 1, 0);
940 	ERR_FAIL_COND_V(p_font.is_null(), 0);
941 
942 	if (p_font->has_outline()) {
943 		p_font->draw_char(canvas_item, p_pos, p_char[0], p_next.c_str()[0], Color(1, 1, 1), true);
944 	}
945 	return p_font->draw_char(canvas_item, p_pos, p_char[0], p_next.c_str()[0], p_modulate);
946 }
947 
_notify_transform(CanvasItem * p_node)948 void CanvasItem::_notify_transform(CanvasItem *p_node) {
949 
950 	/* This check exists to avoid re-propagating the transform
951 	 * notification down the tree on dirty nodes. It provides
952 	 * optimization by avoiding redundancy (nodes are dirty, will get the
953 	 * notification anyway).
954 	 */
955 
956 	if (/*p_node->xform_change.in_list() &&*/ p_node->global_invalid) {
957 		return; //nothing to do
958 	}
959 
960 	p_node->global_invalid = true;
961 
962 	if (p_node->notify_transform && !p_node->xform_change.in_list()) {
963 		if (!p_node->block_transform_notify) {
964 			if (p_node->is_inside_tree())
965 				get_tree()->xform_change_list.add(&p_node->xform_change);
966 		}
967 	}
968 
969 	for (List<CanvasItem *>::Element *E = p_node->children_items.front(); E; E = E->next()) {
970 
971 		CanvasItem *ci = E->get();
972 		if (ci->toplevel)
973 			continue;
974 		_notify_transform(ci);
975 	}
976 }
977 
get_viewport_rect() const978 Rect2 CanvasItem::get_viewport_rect() const {
979 
980 	ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
981 	return get_viewport()->get_visible_rect();
982 }
983 
get_canvas() const984 RID CanvasItem::get_canvas() const {
985 
986 	ERR_FAIL_COND_V(!is_inside_tree(), RID());
987 
988 	if (canvas_layer)
989 		return canvas_layer->get_canvas();
990 	else
991 		return get_viewport()->find_world_2d()->get_canvas();
992 }
993 
get_canvas_layer_instance_id() const994 ObjectID CanvasItem::get_canvas_layer_instance_id() const {
995 
996 	if (canvas_layer) {
997 		return canvas_layer->get_instance_id();
998 	} else {
999 		return 0;
1000 	}
1001 }
1002 
get_toplevel() const1003 CanvasItem *CanvasItem::get_toplevel() const {
1004 
1005 	CanvasItem *ci = const_cast<CanvasItem *>(this);
1006 	while (!ci->toplevel && Object::cast_to<CanvasItem>(ci->get_parent())) {
1007 		ci = Object::cast_to<CanvasItem>(ci->get_parent());
1008 	}
1009 
1010 	return ci;
1011 }
1012 
get_world_2d() const1013 Ref<World2D> CanvasItem::get_world_2d() const {
1014 
1015 	ERR_FAIL_COND_V(!is_inside_tree(), Ref<World2D>());
1016 
1017 	CanvasItem *tl = get_toplevel();
1018 
1019 	if (tl->get_viewport()) {
1020 		return tl->get_viewport()->find_world_2d();
1021 	} else {
1022 		return Ref<World2D>();
1023 	}
1024 }
1025 
get_viewport_rid() const1026 RID CanvasItem::get_viewport_rid() const {
1027 
1028 	ERR_FAIL_COND_V(!is_inside_tree(), RID());
1029 	return get_viewport()->get_viewport_rid();
1030 }
1031 
set_block_transform_notify(bool p_enable)1032 void CanvasItem::set_block_transform_notify(bool p_enable) {
1033 	block_transform_notify = p_enable;
1034 }
1035 
is_block_transform_notify_enabled() const1036 bool CanvasItem::is_block_transform_notify_enabled() const {
1037 
1038 	return block_transform_notify;
1039 }
1040 
set_draw_behind_parent(bool p_enable)1041 void CanvasItem::set_draw_behind_parent(bool p_enable) {
1042 
1043 	if (behind == p_enable)
1044 		return;
1045 	behind = p_enable;
1046 	VisualServer::get_singleton()->canvas_item_set_draw_behind_parent(canvas_item, behind);
1047 }
1048 
is_draw_behind_parent_enabled() const1049 bool CanvasItem::is_draw_behind_parent_enabled() const {
1050 
1051 	return behind;
1052 }
1053 
set_material(const Ref<Material> & p_material)1054 void CanvasItem::set_material(const Ref<Material> &p_material) {
1055 
1056 	material = p_material;
1057 	RID rid;
1058 	if (material.is_valid())
1059 		rid = material->get_rid();
1060 	VS::get_singleton()->canvas_item_set_material(canvas_item, rid);
1061 	_change_notify(); //properties for material exposed
1062 }
1063 
set_use_parent_material(bool p_use_parent_material)1064 void CanvasItem::set_use_parent_material(bool p_use_parent_material) {
1065 
1066 	use_parent_material = p_use_parent_material;
1067 	VS::get_singleton()->canvas_item_set_use_parent_material(canvas_item, p_use_parent_material);
1068 }
1069 
get_use_parent_material() const1070 bool CanvasItem::get_use_parent_material() const {
1071 
1072 	return use_parent_material;
1073 }
1074 
get_material() const1075 Ref<Material> CanvasItem::get_material() const {
1076 
1077 	return material;
1078 }
1079 
make_canvas_position_local(const Vector2 & screen_point) const1080 Vector2 CanvasItem::make_canvas_position_local(const Vector2 &screen_point) const {
1081 
1082 	ERR_FAIL_COND_V(!is_inside_tree(), screen_point);
1083 
1084 	Transform2D local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();
1085 
1086 	return local_matrix.xform(screen_point);
1087 }
1088 
make_input_local(const Ref<InputEvent> & p_event) const1089 Ref<InputEvent> CanvasItem::make_input_local(const Ref<InputEvent> &p_event) const {
1090 
1091 	ERR_FAIL_COND_V(p_event.is_null(), p_event);
1092 	ERR_FAIL_COND_V(!is_inside_tree(), p_event);
1093 
1094 	return p_event->xformed_by((get_canvas_transform() * get_global_transform()).affine_inverse());
1095 }
1096 
get_global_mouse_position() const1097 Vector2 CanvasItem::get_global_mouse_position() const {
1098 
1099 	ERR_FAIL_COND_V(!get_viewport(), Vector2());
1100 	return get_canvas_transform().affine_inverse().xform(get_viewport()->get_mouse_position());
1101 }
1102 
get_local_mouse_position() const1103 Vector2 CanvasItem::get_local_mouse_position() const {
1104 
1105 	ERR_FAIL_COND_V(!get_viewport(), Vector2());
1106 
1107 	return get_global_transform().affine_inverse().xform(get_global_mouse_position());
1108 }
1109 
force_update_transform()1110 void CanvasItem::force_update_transform() {
1111 	ERR_FAIL_COND(!is_inside_tree());
1112 	if (!xform_change.in_list()) {
1113 		return;
1114 	}
1115 
1116 	get_tree()->xform_change_list.remove(&xform_change);
1117 
1118 	notification(NOTIFICATION_TRANSFORM_CHANGED);
1119 }
1120 
_bind_methods()1121 void CanvasItem::_bind_methods() {
1122 
1123 	ClassDB::bind_method(D_METHOD("_toplevel_raise_self"), &CanvasItem::_toplevel_raise_self);
1124 	ClassDB::bind_method(D_METHOD("_update_callback"), &CanvasItem::_update_callback);
1125 
1126 #ifdef TOOLS_ENABLED
1127 	ClassDB::bind_method(D_METHOD("_edit_set_state", "state"), &CanvasItem::_edit_set_state);
1128 	ClassDB::bind_method(D_METHOD("_edit_get_state"), &CanvasItem::_edit_get_state);
1129 	ClassDB::bind_method(D_METHOD("_edit_set_position", "position"), &CanvasItem::_edit_set_position);
1130 	ClassDB::bind_method(D_METHOD("_edit_get_position"), &CanvasItem::_edit_get_position);
1131 	ClassDB::bind_method(D_METHOD("_edit_set_scale", "scale"), &CanvasItem::_edit_set_scale);
1132 	ClassDB::bind_method(D_METHOD("_edit_get_scale"), &CanvasItem::_edit_get_scale);
1133 	ClassDB::bind_method(D_METHOD("_edit_set_rect", "rect"), &CanvasItem::_edit_set_rect);
1134 	ClassDB::bind_method(D_METHOD("_edit_get_rect"), &CanvasItem::_edit_get_rect);
1135 	ClassDB::bind_method(D_METHOD("_edit_use_rect"), &CanvasItem::_edit_use_rect);
1136 	ClassDB::bind_method(D_METHOD("_edit_set_rotation", "degrees"), &CanvasItem::_edit_set_rotation);
1137 	ClassDB::bind_method(D_METHOD("_edit_get_rotation"), &CanvasItem::_edit_get_rotation);
1138 	ClassDB::bind_method(D_METHOD("_edit_use_rotation"), &CanvasItem::_edit_use_rotation);
1139 	ClassDB::bind_method(D_METHOD("_edit_set_pivot", "pivot"), &CanvasItem::_edit_set_pivot);
1140 	ClassDB::bind_method(D_METHOD("_edit_get_pivot"), &CanvasItem::_edit_get_pivot);
1141 	ClassDB::bind_method(D_METHOD("_edit_use_pivot"), &CanvasItem::_edit_use_pivot);
1142 	ClassDB::bind_method(D_METHOD("_edit_get_transform"), &CanvasItem::_edit_get_transform);
1143 #endif
1144 
1145 	ClassDB::bind_method(D_METHOD("get_canvas_item"), &CanvasItem::get_canvas_item);
1146 
1147 	ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasItem::set_visible);
1148 	ClassDB::bind_method(D_METHOD("is_visible"), &CanvasItem::is_visible);
1149 	ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &CanvasItem::is_visible_in_tree);
1150 	ClassDB::bind_method(D_METHOD("show"), &CanvasItem::show);
1151 	ClassDB::bind_method(D_METHOD("hide"), &CanvasItem::hide);
1152 
1153 	ClassDB::bind_method(D_METHOD("update"), &CanvasItem::update);
1154 
1155 	ClassDB::bind_method(D_METHOD("set_as_toplevel", "enable"), &CanvasItem::set_as_toplevel);
1156 	ClassDB::bind_method(D_METHOD("is_set_as_toplevel"), &CanvasItem::is_set_as_toplevel);
1157 
1158 	ClassDB::bind_method(D_METHOD("set_light_mask", "light_mask"), &CanvasItem::set_light_mask);
1159 	ClassDB::bind_method(D_METHOD("get_light_mask"), &CanvasItem::get_light_mask);
1160 
1161 	ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &CanvasItem::set_modulate);
1162 	ClassDB::bind_method(D_METHOD("get_modulate"), &CanvasItem::get_modulate);
1163 	ClassDB::bind_method(D_METHOD("set_self_modulate", "self_modulate"), &CanvasItem::set_self_modulate);
1164 	ClassDB::bind_method(D_METHOD("get_self_modulate"), &CanvasItem::get_self_modulate);
1165 
1166 	ClassDB::bind_method(D_METHOD("set_draw_behind_parent", "enable"), &CanvasItem::set_draw_behind_parent);
1167 	ClassDB::bind_method(D_METHOD("is_draw_behind_parent_enabled"), &CanvasItem::is_draw_behind_parent_enabled);
1168 
1169 	ClassDB::bind_method(D_METHOD("_set_on_top", "on_top"), &CanvasItem::_set_on_top);
1170 	ClassDB::bind_method(D_METHOD("_is_on_top"), &CanvasItem::_is_on_top);
1171 	//ClassDB::bind_method(D_METHOD("get_transform"),&CanvasItem::get_transform);
1172 
1173 	ClassDB::bind_method(D_METHOD("draw_line", "from", "to", "color", "width", "antialiased"), &CanvasItem::draw_line, DEFVAL(1.0), DEFVAL(false));
1174 	ClassDB::bind_method(D_METHOD("draw_polyline", "points", "color", "width", "antialiased"), &CanvasItem::draw_polyline, DEFVAL(1.0), DEFVAL(false));
1175 	ClassDB::bind_method(D_METHOD("draw_polyline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_polyline_colors, DEFVAL(1.0), DEFVAL(false));
1176 	ClassDB::bind_method(D_METHOD("draw_arc", "center", "radius", "start_angle", "end_angle", "point_count", "color", "width", "antialiased"), &CanvasItem::draw_arc, DEFVAL(1.0), DEFVAL(false));
1177 	ClassDB::bind_method(D_METHOD("draw_multiline", "points", "color", "width", "antialiased"), &CanvasItem::draw_multiline, DEFVAL(1.0), DEFVAL(false));
1178 	ClassDB::bind_method(D_METHOD("draw_multiline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_multiline_colors, DEFVAL(1.0), DEFVAL(false));
1179 	ClassDB::bind_method(D_METHOD("draw_rect", "rect", "color", "filled", "width", "antialiased"), &CanvasItem::draw_rect, DEFVAL(true), DEFVAL(1.0), DEFVAL(false));
1180 	ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color"), &CanvasItem::draw_circle);
1181 	ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate", "normal_map"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(Variant()));
1182 	ClassDB::bind_method(D_METHOD("draw_texture_rect", "texture", "rect", "tile", "modulate", "transpose", "normal_map"), &CanvasItem::draw_texture_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()));
1183 	ClassDB::bind_method(D_METHOD("draw_texture_rect_region", "texture", "rect", "src_rect", "modulate", "transpose", "normal_map", "clip_uv"), &CanvasItem::draw_texture_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()), DEFVAL(true));
1184 	ClassDB::bind_method(D_METHOD("draw_style_box", "style_box", "rect"), &CanvasItem::draw_style_box);
1185 	ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture", "width", "normal_map"), &CanvasItem::draw_primitive, DEFVAL(Variant()), DEFVAL(1.0), DEFVAL(Variant()));
1186 	ClassDB::bind_method(D_METHOD("draw_polygon", "points", "colors", "uvs", "texture", "normal_map", "antialiased"), &CanvasItem::draw_polygon, DEFVAL(PoolVector2Array()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(false));
1187 	ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture", "normal_map", "antialiased"), &CanvasItem::draw_colored_polygon, DEFVAL(PoolVector2Array()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(false));
1188 	ClassDB::bind_method(D_METHOD("draw_string", "font", "position", "text", "modulate", "clip_w"), &CanvasItem::draw_string, DEFVAL(Color(1, 1, 1)), DEFVAL(-1));
1189 	ClassDB::bind_method(D_METHOD("draw_char", "font", "position", "char", "next", "modulate"), &CanvasItem::draw_char, DEFVAL(Color(1, 1, 1)));
1190 	ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "normal_map", "transform", "modulate"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()), DEFVAL(Transform2D()), DEFVAL(Color(1, 1, 1)));
1191 	ClassDB::bind_method(D_METHOD("draw_multimesh", "multimesh", "texture", "normal_map"), &CanvasItem::draw_multimesh, DEFVAL(Ref<Texture>()));
1192 
1193 	ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform);
1194 	ClassDB::bind_method(D_METHOD("draw_set_transform_matrix", "xform"), &CanvasItem::draw_set_transform_matrix);
1195 	ClassDB::bind_method(D_METHOD("get_transform"), &CanvasItem::get_transform);
1196 	ClassDB::bind_method(D_METHOD("get_global_transform"), &CanvasItem::get_global_transform);
1197 	ClassDB::bind_method(D_METHOD("get_global_transform_with_canvas"), &CanvasItem::get_global_transform_with_canvas);
1198 	ClassDB::bind_method(D_METHOD("get_viewport_transform"), &CanvasItem::get_viewport_transform);
1199 	ClassDB::bind_method(D_METHOD("get_viewport_rect"), &CanvasItem::get_viewport_rect);
1200 	ClassDB::bind_method(D_METHOD("get_canvas_transform"), &CanvasItem::get_canvas_transform);
1201 	ClassDB::bind_method(D_METHOD("get_local_mouse_position"), &CanvasItem::get_local_mouse_position);
1202 	ClassDB::bind_method(D_METHOD("get_global_mouse_position"), &CanvasItem::get_global_mouse_position);
1203 	ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasItem::get_canvas);
1204 	ClassDB::bind_method(D_METHOD("get_world_2d"), &CanvasItem::get_world_2d);
1205 	//ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasItem::get_viewport);
1206 
1207 	ClassDB::bind_method(D_METHOD("set_material", "material"), &CanvasItem::set_material);
1208 	ClassDB::bind_method(D_METHOD("get_material"), &CanvasItem::get_material);
1209 
1210 	ClassDB::bind_method(D_METHOD("set_use_parent_material", "enable"), &CanvasItem::set_use_parent_material);
1211 	ClassDB::bind_method(D_METHOD("get_use_parent_material"), &CanvasItem::get_use_parent_material);
1212 
1213 	ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &CanvasItem::set_notify_local_transform);
1214 	ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &CanvasItem::is_local_transform_notification_enabled);
1215 
1216 	ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &CanvasItem::set_notify_transform);
1217 	ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &CanvasItem::is_transform_notification_enabled);
1218 
1219 	ClassDB::bind_method(D_METHOD("force_update_transform"), &CanvasItem::force_update_transform);
1220 
1221 	ClassDB::bind_method(D_METHOD("make_canvas_position_local", "screen_point"), &CanvasItem::make_canvas_position_local);
1222 	ClassDB::bind_method(D_METHOD("make_input_local", "event"), &CanvasItem::make_input_local);
1223 
1224 	BIND_VMETHOD(MethodInfo("_draw"));
1225 
1226 	ADD_GROUP("Visibility", "");
1227 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
1228 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
1229 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "self_modulate"), "set_self_modulate", "get_self_modulate");
1230 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_behind_parent"), "set_draw_behind_parent", "is_draw_behind_parent_enabled");
1231 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_on_top", PROPERTY_HINT_NONE, "", 0), "_set_on_top", "_is_on_top"); //compatibility
1232 	ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_light_mask", "get_light_mask");
1233 
1234 	ADD_GROUP("Material", "");
1235 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial"), "set_material", "get_material");
1236 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_parent_material"), "set_use_parent_material", "get_use_parent_material");
1237 	//exporting these things doesn't really make much sense i think
1238 	// ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toplevel", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_as_toplevel", "is_set_as_toplevel");
1239 	// ADD_PROPERTY(PropertyInfo(Variant::BOOL,"transform/notify"),"set_transform_notify","is_transform_notify_enabled");
1240 
1241 	ADD_SIGNAL(MethodInfo("draw"));
1242 	ADD_SIGNAL(MethodInfo("visibility_changed"));
1243 	ADD_SIGNAL(MethodInfo("hide"));
1244 	ADD_SIGNAL(MethodInfo("item_rect_changed"));
1245 
1246 	BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
1247 	BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
1248 	BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
1249 	BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
1250 	BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
1251 	BIND_ENUM_CONSTANT(BLEND_MODE_DISABLED);
1252 
1253 	BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
1254 	BIND_CONSTANT(NOTIFICATION_DRAW);
1255 	BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
1256 	BIND_CONSTANT(NOTIFICATION_ENTER_CANVAS);
1257 	BIND_CONSTANT(NOTIFICATION_EXIT_CANVAS);
1258 }
1259 
get_canvas_transform() const1260 Transform2D CanvasItem::get_canvas_transform() const {
1261 
1262 	ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
1263 
1264 	if (canvas_layer)
1265 		return canvas_layer->get_transform();
1266 	else if (Object::cast_to<CanvasItem>(get_parent()))
1267 		return Object::cast_to<CanvasItem>(get_parent())->get_canvas_transform();
1268 	else
1269 		return get_viewport()->get_canvas_transform();
1270 }
1271 
get_viewport_transform() const1272 Transform2D CanvasItem::get_viewport_transform() const {
1273 
1274 	ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
1275 
1276 	if (canvas_layer) {
1277 
1278 		if (get_viewport()) {
1279 			return get_viewport()->get_final_transform() * canvas_layer->get_transform();
1280 		} else {
1281 			return canvas_layer->get_transform();
1282 		}
1283 
1284 	} else {
1285 		return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
1286 	}
1287 }
1288 
set_notify_local_transform(bool p_enable)1289 void CanvasItem::set_notify_local_transform(bool p_enable) {
1290 	notify_local_transform = p_enable;
1291 }
1292 
is_local_transform_notification_enabled() const1293 bool CanvasItem::is_local_transform_notification_enabled() const {
1294 	return notify_local_transform;
1295 }
1296 
set_notify_transform(bool p_enable)1297 void CanvasItem::set_notify_transform(bool p_enable) {
1298 	if (notify_transform == p_enable)
1299 		return;
1300 
1301 	notify_transform = p_enable;
1302 
1303 	if (notify_transform && is_inside_tree()) {
1304 		//this ensures that invalid globals get resolved, so notifications can be received
1305 		get_global_transform();
1306 	}
1307 }
1308 
is_transform_notification_enabled() const1309 bool CanvasItem::is_transform_notification_enabled() const {
1310 	return notify_transform;
1311 }
1312 
get_canvas_layer() const1313 int CanvasItem::get_canvas_layer() const {
1314 
1315 	if (canvas_layer)
1316 		return canvas_layer->get_layer();
1317 	else
1318 		return 0;
1319 }
1320 
CanvasItem()1321 CanvasItem::CanvasItem() :
1322 		xform_change(this) {
1323 
1324 	canvas_item = VisualServer::get_singleton()->canvas_item_create();
1325 	visible = true;
1326 	pending_update = false;
1327 	modulate = Color(1, 1, 1, 1);
1328 	self_modulate = Color(1, 1, 1, 1);
1329 	toplevel = false;
1330 	first_draw = false;
1331 	drawing = false;
1332 	behind = false;
1333 	block_transform_notify = false;
1334 	//viewport=NULL;
1335 	canvas_layer = NULL;
1336 	use_parent_material = false;
1337 	global_invalid = true;
1338 	notify_local_transform = false;
1339 	notify_transform = false;
1340 	light_mask = 1;
1341 
1342 	C = NULL;
1343 }
1344 
~CanvasItem()1345 CanvasItem::~CanvasItem() {
1346 
1347 	VisualServer::get_singleton()->free(canvas_item);
1348 }
1349