2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* canvas_layer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "canvas_layer.h"
|
|
|
|
#include "viewport.h"
|
|
|
|
|
|
|
|
void CanvasLayer::set_layer(int p_xform) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
layer = p_xform;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (viewport.is_valid())
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_layer(viewport, canvas, layer);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int CanvasLayer::get_layer() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void CanvasLayer::set_transform(const Transform2D &p_xform) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
transform = p_xform;
|
|
|
|
locrotscale_dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (viewport.is_valid())
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D CanvasLayer::get_transform() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasLayer::_update_xform() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
transform.set_rotation_and_scale(rot, scale);
|
2014-02-10 02:10:30 +01:00
|
|
|
transform.set_origin(ofs);
|
|
|
|
if (viewport.is_valid())
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasLayer::_update_locrotscale() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ofs = transform.elements[2];
|
|
|
|
rot = transform.get_rotation();
|
|
|
|
scale = transform.get_scale();
|
|
|
|
locrotscale_dirty = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void CanvasLayer::set_offset(const Vector2 &p_offset) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (locrotscale_dirty)
|
|
|
|
_update_locrotscale();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ofs = p_offset;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_xform();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 CanvasLayer::get_offset() const {
|
|
|
|
|
|
|
|
if (locrotscale_dirty)
|
2017-03-05 16:44:50 +01:00
|
|
|
const_cast<CanvasLayer *>(this)->_update_locrotscale();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ofs;
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:38:08 +02:00
|
|
|
void CanvasLayer::set_rotation(real_t p_radians) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (locrotscale_dirty)
|
|
|
|
_update_locrotscale();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rot = p_radians;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_xform();
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t CanvasLayer::get_rotation() const {
|
|
|
|
|
|
|
|
if (locrotscale_dirty)
|
2017-03-05 16:44:50 +01:00
|
|
|
const_cast<CanvasLayer *>(this)->_update_locrotscale();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return rot;
|
|
|
|
}
|
|
|
|
|
2017-11-10 11:07:52 +01:00
|
|
|
void CanvasLayer::set_rotation_degrees(real_t p_degrees) {
|
2016-05-06 23:38:08 +02:00
|
|
|
|
|
|
|
set_rotation(Math::deg2rad(p_degrees));
|
|
|
|
}
|
|
|
|
|
2017-11-10 11:07:52 +01:00
|
|
|
real_t CanvasLayer::get_rotation_degrees() const {
|
2016-05-06 23:38:08 +02:00
|
|
|
|
|
|
|
return Math::rad2deg(get_rotation());
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void CanvasLayer::set_scale(const Vector2 &p_scale) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (locrotscale_dirty)
|
|
|
|
_update_locrotscale();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
scale = p_scale;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_xform();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 CanvasLayer::get_scale() const {
|
|
|
|
|
|
|
|
if (locrotscale_dirty)
|
2017-03-05 16:44:50 +01:00
|
|
|
const_cast<CanvasLayer *>(this)->_update_locrotscale();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasLayer::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-09-14 20:55:14 +02:00
|
|
|
if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
vp = custom_viewport;
|
2016-09-14 20:55:14 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
vp = Node::get_viewport();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
ERR_FAIL_COND(!vp);
|
2017-03-05 16:44:50 +01:00
|
|
|
viewport = vp->get_viewport_rid();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_attach_canvas(viewport, canvas);
|
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_layer(viewport, canvas, layer);
|
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
|
2017-03-05 16:44:50 +01:00
|
|
|
viewport = RID();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 CanvasLayer::get_viewport_size() const {
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
if (!is_inside_tree())
|
2017-03-05 16:44:50 +01:00
|
|
|
return Size2(1, 1);
|
2016-09-14 20:55:14 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Rect2 r = vp->get_visible_rect();
|
|
|
|
return r.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID CanvasLayer::get_viewport() const {
|
|
|
|
|
|
|
|
return viewport;
|
|
|
|
}
|
|
|
|
|
2016-09-14 20:55:14 +02:00
|
|
|
void CanvasLayer::set_custom_viewport(Node *p_viewport) {
|
|
|
|
ERR_FAIL_NULL(p_viewport);
|
|
|
|
if (is_inside_tree()) {
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
|
2017-03-05 16:44:50 +01:00
|
|
|
viewport = RID();
|
2016-09-14 20:55:14 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
custom_viewport = Object::cast_to<Viewport>(p_viewport);
|
2016-09-14 20:55:14 +02:00
|
|
|
|
|
|
|
if (custom_viewport) {
|
2017-08-07 12:17:31 +02:00
|
|
|
custom_viewport_id = custom_viewport->get_instance_id();
|
2016-09-14 20:55:14 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
custom_viewport_id = 0;
|
2016-09-14 20:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
|
|
|
|
if (custom_viewport)
|
2017-03-05 16:44:50 +01:00
|
|
|
vp = custom_viewport;
|
2016-09-14 20:55:14 +02:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
vp = Node::get_viewport();
|
2016-09-14 20:55:14 +02:00
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
viewport = vp->get_viewport_rid();
|
2016-09-14 20:55:14 +02:00
|
|
|
|
2018-04-08 00:42:11 +02:00
|
|
|
VisualServer::get_singleton()->viewport_attach_canvas(viewport, canvas);
|
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_layer(viewport, canvas, layer);
|
|
|
|
VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
|
2016-09-14 20:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Node *CanvasLayer::get_custom_viewport() const {
|
2016-09-14 20:55:14 +02:00
|
|
|
|
|
|
|
return custom_viewport;
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
void CanvasLayer::reset_sort_index() {
|
2017-03-05 16:44:50 +01:00
|
|
|
sort_index = 0;
|
2016-10-03 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CanvasLayer::get_sort_index() {
|
|
|
|
|
|
|
|
return sort_index++;
|
|
|
|
}
|
|
|
|
|
2018-04-08 00:42:11 +02:00
|
|
|
RID CanvasLayer::get_canvas() const {
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
void CanvasLayer::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_layer", "layer"), &CanvasLayer::set_layer);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_layer"), &CanvasLayer::get_layer);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CanvasLayer::set_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_transform"), &CanvasLayer::get_transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_offset", "offset"), &CanvasLayer::set_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_offset"), &CanvasLayer::get_offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &CanvasLayer::set_rotation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rotation"), &CanvasLayer::get_rotation);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-11-10 11:07:52 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &CanvasLayer::set_rotation_degrees);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &CanvasLayer::get_rotation_degrees);
|
2016-05-06 23:38:08 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &CanvasLayer::set_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_scale"), &CanvasLayer::get_scale);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &CanvasLayer::set_custom_viewport);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_custom_viewport"), &CanvasLayer::get_custom_viewport);
|
2016-09-14 20:55:14 +02:00
|
|
|
|
2018-04-08 00:42:11 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasLayer::get_canvas);
|
2017-02-13 12:47:24 +01:00
|
|
|
//ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasLayer::get_viewport);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "layer", PROPERTY_HINT_RANGE, "-128,128,1"), "set_layer", "get_layer");
|
2017-02-12 01:11:37 +01:00
|
|
|
//ADD_PROPERTY( PropertyInfo(Variant::MATRIX32,"transform",PROPERTY_HINT_RANGE),"set_transform","get_transform") ;
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "rotation_degrees", PROPERTY_HINT_RANGE, "-1440,1440,0.1", PROPERTY_USAGE_EDITOR), "set_rotation_degrees", "get_rotation_degrees");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_rotation", "get_rotation");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", 0), "set_custom_viewport", "get_custom_viewport");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CanvasLayer::CanvasLayer() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
vp = NULL;
|
|
|
|
scale = Vector2(1, 1);
|
|
|
|
rot = 0;
|
|
|
|
locrotscale_dirty = false;
|
|
|
|
layer = 1;
|
2018-04-08 00:42:11 +02:00
|
|
|
canvas = VS::get_singleton()->canvas_create();
|
2017-03-05 16:44:50 +01:00
|
|
|
custom_viewport = NULL;
|
|
|
|
custom_viewport_id = 0;
|
|
|
|
sort_index = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2018-04-08 00:42:11 +02:00
|
|
|
|
|
|
|
CanvasLayer::~CanvasLayer() {
|
|
|
|
|
|
|
|
VS::get_singleton()->free(canvas);
|
|
|
|
}
|