2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* node_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2021-01-02 03:13:46 +08:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08: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 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "node_2d.h"
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/object/message_queue.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "scene/gui/control.h"
|
2020-03-04 09:51:12 +08:00
|
|
|
#include "scene/main/window.h"
|
2020-03-28 02:21:27 +08:00
|
|
|
#include "servers/rendering_server.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2019-10-22 05:37:07 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-11-16 06:03:25 +08:00
|
|
|
Dictionary Node2D::_edit_get_state() const {
|
|
|
|
Dictionary state;
|
|
|
|
state["position"] = get_position();
|
|
|
|
state["rotation"] = get_rotation();
|
|
|
|
state["scale"] = get_scale();
|
2020-05-02 00:38:04 +08:00
|
|
|
state["skew"] = get_skew();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
2019-10-22 05:37:07 +08:00
|
|
|
|
2017-11-16 06:03:25 +08:00
|
|
|
void Node2D::_edit_set_state(const Dictionary &p_state) {
|
2017-11-07 15:58:35 +08:00
|
|
|
pos = p_state["position"];
|
|
|
|
angle = p_state["rotation"];
|
|
|
|
_scale = p_state["scale"];
|
2020-05-02 00:38:04 +08:00
|
|
|
skew = p_state["skew"];
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2017-11-16 06:03:25 +08:00
|
|
|
void Node2D::_edit_set_position(const Point2 &p_position) {
|
2018-03-09 04:35:41 +08:00
|
|
|
set_position(p_position);
|
2017-11-16 06:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Point2 Node2D::_edit_get_position() const {
|
|
|
|
return pos;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-03-09 04:35:41 +08:00
|
|
|
void Node2D::_edit_set_scale(const Size2 &p_scale) {
|
|
|
|
set_scale(p_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 Node2D::_edit_get_scale() const {
|
|
|
|
return _scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::_edit_set_rotation(float p_rotation) {
|
|
|
|
angle = p_rotation;
|
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Node2D::_edit_get_rotation() const {
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Node2D::_edit_use_rotation() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-16 06:03:25 +08:00
|
|
|
void Node2D::_edit_set_rect(const Rect2 &p_edit_rect) {
|
2018-03-09 04:35:41 +08:00
|
|
|
ERR_FAIL_COND(!_edit_use_rect());
|
|
|
|
|
2017-11-16 06:03:25 +08:00
|
|
|
Rect2 r = _edit_get_rect();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
Vector2 zero_offset;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (r.size.x != 0) {
|
2017-06-04 06:25:13 +08:00
|
|
|
zero_offset.x = -r.position.x / r.size.x;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (r.size.y != 0) {
|
2017-06-04 06:25:13 +08:00
|
|
|
zero_offset.y = -r.position.y / r.size.y;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Size2 new_scale(1, 1);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (r.size.x != 0) {
|
2014-02-10 09:10:30 +08:00
|
|
|
new_scale.x = p_edit_rect.size.x / r.size.x;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (r.size.y != 0) {
|
2014-02-10 09:10:30 +08:00
|
|
|
new_scale.y = p_edit_rect.size.y / r.size.y;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-03-09 04:35:41 +08:00
|
|
|
Point2 new_pos = p_edit_rect.position + p_edit_rect.size * zero_offset;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D postxf;
|
2020-05-02 00:38:04 +08:00
|
|
|
postxf.set_rotation_scale_and_skew(angle, _scale, skew);
|
2014-02-10 09:10:30 +08:00
|
|
|
new_pos = postxf.xform(new_pos);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
pos += new_pos;
|
|
|
|
_scale *= new_scale;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_update_transform();
|
|
|
|
}
|
2019-10-22 05:37:07 +08:00
|
|
|
#endif
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
void Node2D::_update_xform_values() {
|
2017-03-05 23:44:50 +08:00
|
|
|
pos = _mat.elements[2];
|
|
|
|
angle = _mat.get_rotation();
|
|
|
|
_scale = _mat.get_scale();
|
2020-05-02 00:38:04 +08:00
|
|
|
skew = _mat.get_skew();
|
2017-03-05 23:44:50 +08:00
|
|
|
_xform_dirty = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::_update_transform() {
|
2020-05-02 00:38:04 +08:00
|
|
|
_mat.set_rotation_scale_and_skew(angle, _scale, skew);
|
2017-03-05 23:44:50 +08:00
|
|
|
_mat.elements[2] = pos;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), _mat);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_notify_transform();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::set_position(const Point2 &p_pos) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2017-03-05 23:44:50 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
pos = p_pos;
|
2014-02-10 09:10:30 +08:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
void Node2D::set_rotation(float p_radians) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2017-03-05 23:44:50 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
angle = p_radians;
|
2014-02-10 09:10:30 +08:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2020-05-02 00:38:04 +08:00
|
|
|
void Node2D::set_skew(float p_radians) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2020-05-02 00:38:04 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-05-02 00:38:04 +08:00
|
|
|
skew = p_radians;
|
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:07:52 +08:00
|
|
|
void Node2D::set_rotation_degrees(float p_degrees) {
|
2017-01-04 12:16:14 +08:00
|
|
|
set_rotation(Math::deg2rad(p_degrees));
|
2016-05-07 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-02 00:38:04 +08:00
|
|
|
void Node2D::set_skew_degrees(float p_degrees) {
|
|
|
|
set_skew(Math::deg2rad(p_degrees));
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::set_scale(const Size2 &p_scale) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2017-03-05 23:44:50 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
_scale = p_scale;
|
2020-01-13 19:13:45 +08:00
|
|
|
// Avoid having 0 scale values, can lead to errors in physics and rendering.
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_scale.x == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
_scale.x = CMP_EPSILON;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (_scale.y == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
_scale.y = CMP_EPSILON;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
Point2 Node2D::get_position() const {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2017-03-05 23:44:50 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
return pos;
|
|
|
|
}
|
2017-11-10 16:03:15 +08:00
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
float Node2D::get_rotation() const {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2017-03-05 23:44:50 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return angle;
|
|
|
|
}
|
2017-11-10 16:03:15 +08:00
|
|
|
|
2020-05-02 00:38:04 +08:00
|
|
|
float Node2D::get_skew() const {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2020-05-02 00:38:04 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-05-02 00:38:04 +08:00
|
|
|
|
|
|
|
return skew;
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:07:52 +08:00
|
|
|
float Node2D::get_rotation_degrees() const {
|
2017-01-04 12:16:14 +08:00
|
|
|
return Math::rad2deg(get_rotation());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-05-02 00:38:04 +08:00
|
|
|
float Node2D::get_skew_degrees() const {
|
|
|
|
return Math::rad2deg(get_skew());
|
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2016-05-07 05:38:08 +08:00
|
|
|
Size2 Node2D::get_scale() const {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_xform_dirty) {
|
2017-03-05 23:44:50 +08:00
|
|
|
((Node2D *)this)->_update_xform_values();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-05-07 05:38:08 +08:00
|
|
|
return _scale;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D Node2D::get_transform() const {
|
2014-02-10 09:10:30 +08:00
|
|
|
return _mat;
|
|
|
|
}
|
|
|
|
|
2015-03-22 21:33:58 +08:00
|
|
|
void Node2D::rotate(float p_radians) {
|
2017-03-05 23:44:50 +08:00
|
|
|
set_rotation(get_rotation() + p_radians);
|
2014-05-14 12:22:15 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::translate(const Vector2 &p_amount) {
|
|
|
|
set_position(get_position() + p_amount);
|
2015-03-22 21:33:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::global_translate(const Vector2 &p_amount) {
|
|
|
|
set_global_position(get_global_position() + p_amount);
|
2015-03-22 21:33:58 +08:00
|
|
|
}
|
|
|
|
|
2017-08-06 04:52:58 +08:00
|
|
|
void Node2D::apply_scale(const Size2 &p_amount) {
|
2017-03-05 23:44:50 +08:00
|
|
|
set_scale(get_scale() * p_amount);
|
2015-03-22 21:33:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::move_x(float p_delta, bool p_scaled) {
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D t = get_transform();
|
2014-05-14 12:22:15 +08:00
|
|
|
Vector2 m = t[0];
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!p_scaled) {
|
2014-05-14 12:22:15 +08:00
|
|
|
m.normalize();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
set_position(t[2] + m * p_delta);
|
2014-05-14 12:22:15 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::move_y(float p_delta, bool p_scaled) {
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D t = get_transform();
|
2014-05-14 12:22:15 +08:00
|
|
|
Vector2 m = t[1];
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!p_scaled) {
|
2014-05-14 12:22:15 +08:00
|
|
|
m.normalize();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
set_position(t[2] + m * p_delta);
|
2014-05-14 12:22:15 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
Point2 Node2D::get_global_position() const {
|
2014-02-10 09:10:30 +08:00
|
|
|
return get_global_transform().get_origin();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::set_global_position(const Point2 &p_pos) {
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D inv;
|
2014-07-01 22:07:04 +08:00
|
|
|
CanvasItem *pi = get_parent_item();
|
|
|
|
if (pi) {
|
|
|
|
inv = pi->get_global_transform().affine_inverse();
|
2017-01-04 12:16:14 +08:00
|
|
|
set_position(inv.xform(p_pos));
|
2014-07-01 22:07:04 +08:00
|
|
|
} else {
|
2017-01-04 12:16:14 +08:00
|
|
|
set_position(p_pos);
|
2014-07-01 22:07:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
float Node2D::get_global_rotation() const {
|
2016-08-15 17:39:19 +08:00
|
|
|
return get_global_transform().get_rotation();
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
void Node2D::set_global_rotation(float p_radians) {
|
2016-08-15 17:39:19 +08:00
|
|
|
CanvasItem *pi = get_parent_item();
|
|
|
|
if (pi) {
|
|
|
|
const float parent_global_rot = pi->get_global_transform().get_rotation();
|
2017-01-04 12:16:14 +08:00
|
|
|
set_rotation(p_radians - parent_global_rot);
|
2016-08-15 17:39:19 +08:00
|
|
|
} else {
|
2017-01-04 12:16:14 +08:00
|
|
|
set_rotation(p_radians);
|
2016-08-15 17:39:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:07:52 +08:00
|
|
|
float Node2D::get_global_rotation_degrees() const {
|
2017-01-04 12:16:14 +08:00
|
|
|
return Math::rad2deg(get_global_rotation());
|
2016-08-15 17:39:19 +08:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:07:52 +08:00
|
|
|
void Node2D::set_global_rotation_degrees(float p_degrees) {
|
2017-01-04 12:16:14 +08:00
|
|
|
set_global_rotation(Math::deg2rad(p_degrees));
|
2016-08-15 17:39:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 Node2D::get_global_scale() const {
|
|
|
|
return get_global_transform().get_scale();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::set_global_scale(const Size2 &p_scale) {
|
2016-08-15 17:39:19 +08:00
|
|
|
CanvasItem *pi = get_parent_item();
|
|
|
|
if (pi) {
|
|
|
|
const Size2 parent_global_scale = pi->get_global_transform().get_scale();
|
2017-07-04 01:28:26 +08:00
|
|
|
set_scale(p_scale / parent_global_scale);
|
2016-08-15 17:39:19 +08:00
|
|
|
} else {
|
|
|
|
set_scale(p_scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::set_transform(const Transform2D &p_transform) {
|
|
|
|
_mat = p_transform;
|
|
|
|
_xform_dirty = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), _mat);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_notify_transform();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::set_global_transform(const Transform2D &p_transform) {
|
2014-02-10 09:10:30 +08:00
|
|
|
CanvasItem *pi = get_parent_item();
|
2020-05-14 22:41:43 +08:00
|
|
|
if (pi) {
|
2017-03-05 23:44:50 +08:00
|
|
|
set_transform(pi->get_global_transform().affine_inverse() * p_transform);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2014-02-10 09:10:30 +08:00
|
|
|
set_transform(p_transform);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-12-30 06:06:03 +08:00
|
|
|
void Node2D::set_z_index(int p_z) {
|
2020-03-28 02:21:27 +08:00
|
|
|
ERR_FAIL_COND(p_z < RS::CANVAS_ITEM_Z_MIN);
|
|
|
|
ERR_FAIL_COND(p_z > RS::CANVAS_ITEM_Z_MAX);
|
2017-12-30 06:06:03 +08:00
|
|
|
z_index = p_z;
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_item_set_z_index(get_canvas_item(), z_index);
|
2015-01-11 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 21:49:26 +08:00
|
|
|
void Node2D::set_z_as_relative(bool p_enabled) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (z_relative == p_enabled) {
|
2015-01-13 21:49:26 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
z_relative = p_enabled;
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_item_set_z_as_relative_to_parent(get_canvas_item(), p_enabled);
|
2015-01-13 21:49:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Node2D::is_z_relative() const {
|
|
|
|
return z_relative;
|
|
|
|
}
|
|
|
|
|
2017-12-30 06:06:03 +08:00
|
|
|
int Node2D::get_z_index() const {
|
|
|
|
return z_index;
|
2015-01-11 04:35:26 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D Node2D::get_relative_transform_to_parent(const Node *p_parent) const {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_parent == this) {
|
2017-01-11 11:52:51 +08:00
|
|
|
return Transform2D();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-02-14 23:09:52 +08:00
|
|
|
|
2017-08-25 04:58:51 +08:00
|
|
|
Node2D *parent_2d = Object::cast_to<Node2D>(get_parent());
|
2015-12-29 08:05:57 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(!parent_2d, Transform2D());
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_parent == parent_2d) {
|
2015-02-14 23:09:52 +08:00
|
|
|
return get_transform();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2015-12-29 08:05:57 +08:00
|
|
|
return parent_2d->get_relative_transform_to_parent(p_parent) * get_transform();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-02-14 23:09:52 +08:00
|
|
|
}
|
2015-01-11 04:35:26 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Node2D::look_at(const Vector2 &p_pos) {
|
2015-05-12 19:17:09 +08:00
|
|
|
rotate(get_angle_to(p_pos));
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
float Node2D::get_angle_to(const Vector2 &p_pos) const {
|
2019-01-31 02:14:43 +08:00
|
|
|
return (to_local(p_pos) * get_scale()).angle();
|
2015-05-12 19:17:09 +08:00
|
|
|
}
|
|
|
|
|
2017-07-23 19:37:26 +08:00
|
|
|
Point2 Node2D::to_local(Point2 p_global) const {
|
|
|
|
return get_global_transform().affine_inverse().xform(p_global);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 Node2D::to_global(Point2 p_local) const {
|
|
|
|
return get_global_transform().xform(p_local);
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void Node2D::_bind_methods() {
|
2017-09-10 21:37:49 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_position", "position"), &Node2D::set_position);
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Node2D::set_rotation);
|
2017-11-10 18:07:52 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Node2D::set_rotation_degrees);
|
2020-05-02 00:38:04 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_skew", "radians"), &Node2D::set_skew);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_skew_degrees", "degrees"), &Node2D::set_skew_degrees);
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node2D::set_scale);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_position"), &Node2D::get_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rotation"), &Node2D::get_rotation);
|
2017-11-10 18:07:52 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Node2D::get_rotation_degrees);
|
2020-05-02 00:38:04 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_skew"), &Node2D::get_skew);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_skew_degrees"), &Node2D::get_skew_degrees);
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_scale"), &Node2D::get_scale);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("rotate", "radians"), &Node2D::rotate);
|
|
|
|
ClassDB::bind_method(D_METHOD("move_local_x", "delta", "scaled"), &Node2D::move_x, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("move_local_y", "delta", "scaled"), &Node2D::move_y, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("translate", "offset"), &Node2D::translate);
|
|
|
|
ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Node2D::global_translate);
|
2017-08-06 04:52:58 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("apply_scale", "ratio"), &Node2D::apply_scale);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-09-10 21:37:49 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node2D::set_global_position);
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_global_position"), &Node2D::get_global_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_global_rotation", "radians"), &Node2D::set_global_rotation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node2D::get_global_rotation);
|
2017-11-10 18:07:52 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_global_rotation_degrees", "degrees"), &Node2D::set_global_rotation_degrees);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_global_rotation_degrees"), &Node2D::get_global_rotation_degrees);
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_global_scale", "scale"), &Node2D::set_global_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_global_scale"), &Node2D::get_global_scale);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_transform", "xform"), &Node2D::set_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_global_transform", "xform"), &Node2D::set_global_transform);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("look_at", "point"), &Node2D::look_at);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_angle_to", "point"), &Node2D::get_angle_to);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-07-23 19:37:26 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Node2D::to_local);
|
|
|
|
ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Node2D::to_global);
|
|
|
|
|
2017-12-30 06:06:03 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &Node2D::set_z_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_z_index"), &Node2D::get_z_index);
|
2017-02-13 19:47:24 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_z_as_relative", "enable"), &Node2D::set_z_as_relative);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_z_relative"), &Node2D::is_z_relative);
|
2015-02-14 23:09:52 +08:00
|
|
|
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_relative_transform_to_parent", "parent"), &Node2D::get_relative_transform_to_parent);
|
2017-01-04 12:16:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_GROUP("Transform", "");
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-25 02:20:53 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_rotation", "get_rotation");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_RANGE, "-360,360,0.1,or_lesser,or_greater", PROPERTY_USAGE_EDITOR), "set_rotation_degrees", "get_rotation_degrees");
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
|
2020-05-02 00:38:04 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "skew", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_skew", "get_skew");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "skew_degrees", PROPERTY_HINT_RANGE, "-89.9,89.9,0.1", PROPERTY_USAGE_EDITOR), "set_skew_degrees", "get_skew_degrees");
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "", 0), "set_transform", "get_transform");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-04-24 11:50:05 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "", 0), "set_global_position", "get_global_position");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-25 02:20:53 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rotation", PROPERTY_HINT_NONE, "", 0), "set_global_rotation", "get_global_rotation");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rotation_degrees", PROPERTY_HINT_NONE, "", 0), "set_global_rotation_degrees", "get_global_rotation_degrees");
|
2018-05-01 16:46:05 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_scale", PROPERTY_HINT_NONE, "", 0), "set_global_scale", "get_global_scale");
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-12-30 06:06:03 +08:00
|
|
|
ADD_GROUP("Z Index", "");
|
2020-03-28 02:21:27 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_index", "get_z_index");
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "z_as_relative"), "set_z_as_relative", "is_z_relative");
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|