1/**************************************************************************/
2/* canvas_layer.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "canvas_layer.h"
32
33#include "scene/main/canvas_item.h"
34#include "scene/main/viewport.h"
35#include "scene/resources/world_2d.h"
36
37void CanvasLayer::set_layer(int p_xform) {
38 layer = p_xform;
39 if (viewport.is_valid()) {
40 RenderingServer::get_singleton()->viewport_set_canvas_stacking(viewport, canvas, layer, get_index());
41 vp->gui_set_root_order_dirty();
42 }
43}
44
45int CanvasLayer::get_layer() const {
46 return layer;
47}
48
49void CanvasLayer::set_visible(bool p_visible) {
50 if (p_visible == visible) {
51 return;
52 }
53
54 visible = p_visible;
55 emit_signal(SNAME("visibility_changed"));
56
57 for (int i = 0; i < get_child_count(); i++) {
58 CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
59 if (c) {
60 RenderingServer::get_singleton()->canvas_item_set_visible(c->get_canvas_item(), p_visible && c->is_visible());
61
62 c->_propagate_visibility_changed(p_visible);
63 }
64 }
65}
66
67void CanvasLayer::show() {
68 set_visible(true);
69}
70
71void CanvasLayer::hide() {
72 set_visible(false);
73}
74
75bool CanvasLayer::is_visible() const {
76 return visible;
77}
78
79void CanvasLayer::set_transform(const Transform2D &p_xform) {
80 transform = p_xform;
81 locrotscale_dirty = true;
82 if (viewport.is_valid()) {
83 RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
84 }
85}
86
87Transform2D CanvasLayer::get_transform() const {
88 return transform;
89}
90
91Transform2D CanvasLayer::get_final_transform() const {
92 if (is_following_viewport()) {
93 Transform2D follow;
94 follow.scale(Vector2(get_follow_viewport_scale(), get_follow_viewport_scale()));
95 if (vp) {
96 follow = vp->get_canvas_transform() * follow;
97 }
98 return follow * transform;
99 }
100 return transform;
101}
102
103void CanvasLayer::_update_xform() {
104 transform.set_rotation_and_scale(rot, scale);
105 transform.set_origin(ofs);
106 if (viewport.is_valid()) {
107 RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
108 }
109}
110
111void CanvasLayer::_update_locrotscale() {
112 ofs = transform.columns[2];
113 rot = transform.get_rotation();
114 scale = transform.get_scale();
115 locrotscale_dirty = false;
116}
117
118void CanvasLayer::set_offset(const Vector2 &p_offset) {
119 if (locrotscale_dirty) {
120 _update_locrotscale();
121 }
122
123 ofs = p_offset;
124 _update_xform();
125}
126
127Vector2 CanvasLayer::get_offset() const {
128 if (locrotscale_dirty) {
129 const_cast<CanvasLayer *>(this)->_update_locrotscale();
130 }
131
132 return ofs;
133}
134
135void CanvasLayer::set_rotation(real_t p_radians) {
136 if (locrotscale_dirty) {
137 _update_locrotscale();
138 }
139
140 rot = p_radians;
141 _update_xform();
142}
143
144real_t CanvasLayer::get_rotation() const {
145 if (locrotscale_dirty) {
146 const_cast<CanvasLayer *>(this)->_update_locrotscale();
147 }
148
149 return rot;
150}
151
152void CanvasLayer::set_scale(const Vector2 &p_scale) {
153 if (locrotscale_dirty) {
154 _update_locrotscale();
155 }
156
157 scale = p_scale;
158 _update_xform();
159}
160
161Vector2 CanvasLayer::get_scale() const {
162 if (locrotscale_dirty) {
163 const_cast<CanvasLayer *>(this)->_update_locrotscale();
164 }
165
166 return scale;
167}
168
169void CanvasLayer::_notification(int p_what) {
170 switch (p_what) {
171 case NOTIFICATION_ENTER_TREE: {
172 if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
173 vp = custom_viewport;
174 } else {
175 vp = Node::get_viewport();
176 }
177 ERR_FAIL_NULL_MSG(vp, "Viewport is not initialized.");
178
179 vp->_canvas_layer_add(this);
180 viewport = vp->get_viewport_rid();
181
182 RenderingServer::get_singleton()->viewport_attach_canvas(viewport, canvas);
183 RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
184 _update_follow_viewport();
185
186 if (vp) {
187 get_parent()->connect(SNAME("child_order_changed"), callable_mp(vp, &Viewport::canvas_parent_mark_dirty).bind(get_parent()), CONNECT_REFERENCE_COUNTED);
188 vp->canvas_parent_mark_dirty(get_parent());
189 }
190 } break;
191
192 case NOTIFICATION_EXIT_TREE: {
193 ERR_FAIL_NULL_MSG(vp, "Viewport is not initialized.");
194 get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(vp, &Viewport::canvas_parent_mark_dirty).bind(get_parent()));
195
196 vp->_canvas_layer_remove(this);
197 RenderingServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
198 viewport = RID();
199 _update_follow_viewport(false);
200 } break;
201 }
202}
203
204void CanvasLayer::update_draw_order() {
205 if (is_inside_tree()) {
206 RenderingServer::get_singleton()->viewport_set_canvas_stacking(viewport, canvas, layer, get_index());
207 }
208}
209
210Size2 CanvasLayer::get_viewport_size() const {
211 if (!is_inside_tree()) {
212 return Size2(1, 1);
213 }
214
215 ERR_FAIL_NULL_V_MSG(vp, Size2(1, 1), "Viewport is not initialized.");
216
217 Rect2 r = vp->get_visible_rect();
218 return r.size;
219}
220
221RID CanvasLayer::get_viewport() const {
222 return viewport;
223}
224
225void CanvasLayer::set_custom_viewport(Node *p_viewport) {
226 ERR_FAIL_NULL_MSG(p_viewport, "Cannot set viewport to nullptr.");
227 if (is_inside_tree()) {
228 vp->_canvas_layer_remove(this);
229 RenderingServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
230 viewport = RID();
231 }
232
233 custom_viewport = Object::cast_to<Viewport>(p_viewport);
234
235 if (custom_viewport) {
236 custom_viewport_id = custom_viewport->get_instance_id();
237 } else {
238 custom_viewport_id = ObjectID();
239 }
240
241 if (is_inside_tree()) {
242 if (custom_viewport) {
243 vp = custom_viewport;
244 } else {
245 vp = Node::get_viewport();
246 }
247
248 vp->_canvas_layer_add(this);
249 viewport = vp->get_viewport_rid();
250
251 RenderingServer::get_singleton()->viewport_attach_canvas(viewport, canvas);
252 RenderingServer::get_singleton()->viewport_set_canvas_stacking(viewport, canvas, layer, get_index());
253 RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
254 }
255}
256
257Node *CanvasLayer::get_custom_viewport() const {
258 return custom_viewport;
259}
260
261void CanvasLayer::reset_sort_index() {
262 sort_index = 0;
263}
264
265int CanvasLayer::get_sort_index() {
266 return sort_index++;
267}
268
269RID CanvasLayer::get_canvas() const {
270 return canvas;
271}
272
273void CanvasLayer::set_follow_viewport(bool p_enable) {
274 if (follow_viewport == p_enable) {
275 return;
276 }
277
278 follow_viewport = p_enable;
279 _update_follow_viewport();
280 notify_property_list_changed();
281}
282
283bool CanvasLayer::is_following_viewport() const {
284 return follow_viewport;
285}
286
287void CanvasLayer::set_follow_viewport_scale(float p_ratio) {
288 follow_viewport_scale = p_ratio;
289 _update_follow_viewport();
290}
291
292float CanvasLayer::get_follow_viewport_scale() const {
293 return follow_viewport_scale;
294}
295
296void CanvasLayer::_update_follow_viewport(bool p_force_exit) {
297 if (!is_inside_tree()) {
298 return;
299 }
300 if (p_force_exit || !follow_viewport) {
301 RS::get_singleton()->canvas_set_parent(canvas, RID(), 1.0);
302 } else {
303 RS::get_singleton()->canvas_set_parent(canvas, vp->get_world_2d()->get_canvas(), follow_viewport_scale);
304 }
305}
306
307void CanvasLayer::_validate_property(PropertyInfo &p_property) const {
308 if (!follow_viewport && p_property.name == "follow_viewport_scale") {
309 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
310 }
311}
312
313void CanvasLayer::_bind_methods() {
314 ClassDB::bind_method(D_METHOD("set_layer", "layer"), &CanvasLayer::set_layer);
315 ClassDB::bind_method(D_METHOD("get_layer"), &CanvasLayer::get_layer);
316
317 ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasLayer::set_visible);
318 ClassDB::bind_method(D_METHOD("is_visible"), &CanvasLayer::is_visible);
319 ClassDB::bind_method(D_METHOD("show"), &CanvasLayer::show);
320 ClassDB::bind_method(D_METHOD("hide"), &CanvasLayer::hide);
321
322 ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CanvasLayer::set_transform);
323 ClassDB::bind_method(D_METHOD("get_transform"), &CanvasLayer::get_transform);
324 ClassDB::bind_method(D_METHOD("get_final_transform"), &CanvasLayer::get_final_transform);
325
326 ClassDB::bind_method(D_METHOD("set_offset", "offset"), &CanvasLayer::set_offset);
327 ClassDB::bind_method(D_METHOD("get_offset"), &CanvasLayer::get_offset);
328
329 ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &CanvasLayer::set_rotation);
330 ClassDB::bind_method(D_METHOD("get_rotation"), &CanvasLayer::get_rotation);
331
332 ClassDB::bind_method(D_METHOD("set_scale", "scale"), &CanvasLayer::set_scale);
333 ClassDB::bind_method(D_METHOD("get_scale"), &CanvasLayer::get_scale);
334
335 ClassDB::bind_method(D_METHOD("set_follow_viewport", "enable"), &CanvasLayer::set_follow_viewport);
336 ClassDB::bind_method(D_METHOD("is_following_viewport"), &CanvasLayer::is_following_viewport);
337
338 ClassDB::bind_method(D_METHOD("set_follow_viewport_scale", "scale"), &CanvasLayer::set_follow_viewport_scale);
339 ClassDB::bind_method(D_METHOD("get_follow_viewport_scale"), &CanvasLayer::get_follow_viewport_scale);
340
341 ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &CanvasLayer::set_custom_viewport);
342 ClassDB::bind_method(D_METHOD("get_custom_viewport"), &CanvasLayer::get_custom_viewport);
343
344 ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasLayer::get_canvas);
345
346 ADD_GROUP("Layer", "");
347 ADD_PROPERTY(PropertyInfo(Variant::INT, "layer", PROPERTY_HINT_RANGE, "-128,128,1"), "set_layer", "get_layer");
348 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
349 ADD_GROUP("Transform", "");
350 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
351 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-1080,1080,0.1,or_less,or_greater,radians"), "set_rotation", "get_rotation");
352 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale", PROPERTY_HINT_LINK), "set_scale", "get_scale");
353 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "suffix:px"), "set_transform", "get_transform");
354 ADD_GROUP("", "");
355 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport");
356 ADD_GROUP("Follow Viewport", "follow_viewport");
357 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_viewport_enabled"), "set_follow_viewport", "is_following_viewport");
358 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "follow_viewport_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001,or_greater,or_less"), "set_follow_viewport_scale", "get_follow_viewport_scale");
359
360 ADD_SIGNAL(MethodInfo("visibility_changed"));
361}
362
363CanvasLayer::CanvasLayer() {
364 canvas = RS::get_singleton()->canvas_create();
365}
366
367CanvasLayer::~CanvasLayer() {
368 ERR_FAIL_NULL(RenderingServer::get_singleton());
369 RS::get_singleton()->free(canvas);
370}
371