2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* node_2d.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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 "node_2d.h"
|
|
|
|
|
2022-04-01 21:18:16 +02:00
|
|
|
#include "scene/main/viewport.h"
|
|
|
|
|
2019-10-21 23:37:07 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-11-15 23:03:25 +01:00
|
|
|
Dictionary Node2D::_edit_get_state() const {
|
|
|
|
Dictionary state;
|
|
|
|
state["position"] = get_position();
|
|
|
|
state["rotation"] = get_rotation();
|
|
|
|
state["scale"] = get_scale();
|
2020-05-01 18:38:04 +02:00
|
|
|
state["skew"] = get_skew();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
2019-10-21 23:37:07 +02:00
|
|
|
|
2017-11-15 23:03:25 +01:00
|
|
|
void Node2D::_edit_set_state(const Dictionary &p_state) {
|
2021-09-24 08:39:30 +02:00
|
|
|
position = p_state["position"];
|
|
|
|
rotation = p_state["rotation"];
|
|
|
|
scale = p_state["scale"];
|
2020-05-01 18:38:04 +02:00
|
|
|
skew = p_state["skew"];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:03:25 +01:00
|
|
|
void Node2D::_edit_set_position(const Point2 &p_position) {
|
2018-03-08 21:35:41 +01:00
|
|
|
set_position(p_position);
|
2017-11-15 23:03:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Point2 Node2D::_edit_get_position() const {
|
2021-09-24 08:39:30 +02:00
|
|
|
return position;
|
2017-11-15 23:03:25 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-03-08 21:35:41 +01:00
|
|
|
void Node2D::_edit_set_scale(const Size2 &p_scale) {
|
|
|
|
set_scale(p_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 Node2D::_edit_get_scale() const {
|
2021-09-24 08:39:30 +02:00
|
|
|
return scale;
|
2018-03-08 21:35:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void Node2D::_edit_set_rotation(real_t p_rotation) {
|
2021-09-24 08:39:30 +02:00
|
|
|
rotation = p_rotation;
|
2018-03-08 21:35:41 +01:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
real_t Node2D::_edit_get_rotation() const {
|
2021-09-24 08:39:30 +02:00
|
|
|
return rotation;
|
2018-03-08 21:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Node2D::_edit_use_rotation() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:03:25 +01:00
|
|
|
void Node2D::_edit_set_rect(const Rect2 &p_edit_rect) {
|
2018-03-08 21:35:41 +01:00
|
|
|
ERR_FAIL_COND(!_edit_use_rect());
|
|
|
|
|
2017-11-15 23:03:25 +01:00
|
|
|
Rect2 r = _edit_get_rect();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector2 zero_offset;
|
|
|
|
Size2 new_scale(1, 1);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r.size.x != 0) {
|
2021-09-24 08:39:30 +02:00
|
|
|
zero_offset.x = -r.position.x / r.size.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
new_scale.x = p_edit_rect.size.x / r.size.x;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-09-24 08:39:30 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r.size.y != 0) {
|
2021-09-24 08:39:30 +02:00
|
|
|
zero_offset.y = -r.position.y / r.size.y;
|
2014-02-10 02:10:30 +01:00
|
|
|
new_scale.y = p_edit_rect.size.y / r.size.y;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-03-08 21:35:41 +01:00
|
|
|
Point2 new_pos = p_edit_rect.position + p_edit_rect.size * zero_offset;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D postxf;
|
2021-09-24 08:39:30 +02:00
|
|
|
postxf.set_rotation_scale_and_skew(rotation, scale, skew);
|
2014-02-10 02:10:30 +01:00
|
|
|
new_pos = postxf.xform(new_pos);
|
|
|
|
|
2021-09-24 08:39:30 +02:00
|
|
|
position += new_pos;
|
|
|
|
scale *= new_scale;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_update_transform();
|
|
|
|
}
|
2019-10-21 23:37:07 +02:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-05-17 01:44:41 +02:00
|
|
|
void Node2D::_set_xform_dirty(bool p_dirty) const {
|
|
|
|
if (is_group_processing()) {
|
|
|
|
if (p_dirty) {
|
|
|
|
xform_dirty.mt.set();
|
|
|
|
} else {
|
|
|
|
xform_dirty.mt.clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xform_dirty.st = p_dirty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::_update_xform_values() const {
|
2021-09-24 08:39:30 +02:00
|
|
|
rotation = transform.get_rotation();
|
|
|
|
skew = transform.get_skew();
|
2023-05-12 13:53:15 +02:00
|
|
|
position = transform.columns[2];
|
|
|
|
scale = transform.get_scale();
|
2023-05-17 01:44:41 +02:00
|
|
|
_set_xform_dirty(false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::_update_transform() {
|
2021-09-24 08:39:30 +02:00
|
|
|
transform.set_rotation_scale_and_skew(rotation, scale, skew);
|
2022-04-24 23:59:24 +02:00
|
|
|
transform.columns[2] = position;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-09-24 08:39:30 +02:00
|
|
|
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_notify_transform();
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:29:14 +01:00
|
|
|
void Node2D::reparent(Node *p_parent, bool p_keep_global_transform) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2020-02-17 18:29:14 +01:00
|
|
|
Transform2D temp = get_global_transform();
|
|
|
|
Node::reparent(p_parent);
|
|
|
|
if (p_keep_global_transform) {
|
|
|
|
set_global_transform(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 05:16:14 +01:00
|
|
|
void Node2D::set_position(const Point2 &p_pos) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-09-24 08:39:30 +02:00
|
|
|
position = p_pos;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void Node2D::set_rotation(real_t p_radians) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-09-24 08:39:30 +02:00
|
|
|
rotation = p_radians;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:58:19 +01:00
|
|
|
void Node2D::set_rotation_degrees(real_t p_degrees) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2022-12-18 16:58:19 +01:00
|
|
|
set_rotation(Math::deg_to_rad(p_degrees));
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void Node2D::set_skew(real_t p_radians) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-05-01 18:38:04 +02:00
|
|
|
skew = p_radians;
|
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Node2D::set_scale(const Size2 &p_scale) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-09-24 08:39:30 +02:00
|
|
|
scale = p_scale;
|
2020-01-13 12:13:45 +01:00
|
|
|
// Avoid having 0 scale values, can lead to errors in physics and rendering.
|
2021-09-24 08:39:30 +02:00
|
|
|
if (Math::is_zero_approx(scale.x)) {
|
|
|
|
scale.x = CMP_EPSILON;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-09-24 08:39:30 +02:00
|
|
|
if (Math::is_zero_approx(scale.y)) {
|
|
|
|
scale.y = CMP_EPSILON;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_transform();
|
|
|
|
}
|
|
|
|
|
2017-01-04 05:16:14 +01:00
|
|
|
Point2 Node2D::get_position() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Point2());
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-05-12 13:53:15 +02:00
|
|
|
|
2021-09-24 08:39:30 +02:00
|
|
|
return position;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-11-10 09:03:15 +01:00
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
real_t Node2D::get_rotation() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-09-24 08:39:30 +02:00
|
|
|
return rotation;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-11-10 09:03:15 +01:00
|
|
|
|
2022-12-18 16:58:19 +01:00
|
|
|
real_t Node2D::get_rotation_degrees() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2022-12-18 16:58:19 +01:00
|
|
|
return Math::rad_to_deg(get_rotation());
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
real_t Node2D::get_skew() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-05-01 18:38:04 +02:00
|
|
|
|
|
|
|
return skew;
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:38:08 +02:00
|
|
|
Size2 Node2D::get_scale() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Size2());
|
2023-05-17 01:44:41 +02:00
|
|
|
if (_is_xform_dirty()) {
|
|
|
|
_update_xform_values();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-09-24 08:39:30 +02:00
|
|
|
return scale;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D Node2D::get_transform() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Transform2D());
|
2021-09-24 08:39:30 +02:00
|
|
|
return transform;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void Node2D::rotate(real_t p_radians) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2017-01-04 05:16:14 +01:00
|
|
|
set_rotation(get_rotation() + p_radians);
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2015-03-22 14:33:58 +01:00
|
|
|
void Node2D::translate(const Vector2 &p_amount) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2017-01-04 05:16:14 +01:00
|
|
|
set_position(get_position() + p_amount);
|
2015-03-22 14:33:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::global_translate(const Vector2 &p_amount) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2017-01-04 05:16:14 +01:00
|
|
|
set_global_position(get_global_position() + p_amount);
|
2015-03-22 14:33:58 +01:00
|
|
|
}
|
|
|
|
|
2017-08-05 22:52:58 +02:00
|
|
|
void Node2D::apply_scale(const Size2 &p_amount) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2015-03-22 14:33:58 +01:00
|
|
|
set_scale(get_scale() * p_amount);
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void Node2D::move_x(real_t p_delta, bool p_scaled) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D t = get_transform();
|
2014-05-14 06:22:15 +02:00
|
|
|
Vector2 m = t[0];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!p_scaled) {
|
2014-05-14 06:22:15 +02:00
|
|
|
m.normalize();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-04 05:16:14 +01:00
|
|
|
set_position(t[2] + m * p_delta);
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void Node2D::move_y(real_t p_delta, bool p_scaled) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D t = get_transform();
|
2014-05-14 06:22:15 +02:00
|
|
|
Vector2 m = t[1];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!p_scaled) {
|
2014-05-14 06:22:15 +02:00
|
|
|
m.normalize();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-04 05:16:14 +01:00
|
|
|
set_position(t[2] + m * p_delta);
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 05:16:14 +01:00
|
|
|
Point2 Node2D::get_global_position() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Point2());
|
2014-02-10 02:10:30 +01:00
|
|
|
return get_global_transform().get_origin();
|
|
|
|
}
|
|
|
|
|
2017-01-04 05:16:14 +01:00
|
|
|
void Node2D::set_global_position(const Point2 &p_pos) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2021-08-12 19:48:34 +02:00
|
|
|
CanvasItem *parent = get_parent_item();
|
|
|
|
if (parent) {
|
|
|
|
Transform2D inv = parent->get_global_transform().affine_inverse();
|
2017-01-04 05:16:14 +01:00
|
|
|
set_position(inv.xform(p_pos));
|
2014-07-01 16:07:04 +02:00
|
|
|
} else {
|
2017-01-04 05:16:14 +01:00
|
|
|
set_position(p_pos);
|
2014-07-01 16:07:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
real_t Node2D::get_global_rotation() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2016-08-15 11:39:19 +02:00
|
|
|
return get_global_transform().get_rotation();
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:58:19 +01:00
|
|
|
real_t Node2D::get_global_rotation_degrees() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2022-12-18 16:58:19 +01:00
|
|
|
return Math::rad_to_deg(get_global_rotation());
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:48:34 +02:00
|
|
|
real_t Node2D::get_global_skew() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2021-08-12 19:48:34 +02:00
|
|
|
return get_global_transform().get_skew();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::set_global_rotation(const real_t p_radians) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2021-08-12 19:48:34 +02:00
|
|
|
CanvasItem *parent = get_parent_item();
|
|
|
|
if (parent) {
|
|
|
|
Transform2D parent_global_transform = parent->get_global_transform();
|
|
|
|
Transform2D new_transform = parent_global_transform * get_transform();
|
|
|
|
new_transform.set_rotation(p_radians);
|
|
|
|
new_transform = parent_global_transform.affine_inverse() * new_transform;
|
|
|
|
set_rotation(new_transform.get_rotation());
|
2016-08-15 11:39:19 +02:00
|
|
|
} else {
|
2017-01-04 05:16:14 +01:00
|
|
|
set_rotation(p_radians);
|
2016-08-15 11:39:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:58:19 +01:00
|
|
|
void Node2D::set_global_rotation_degrees(const real_t p_degrees) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2022-12-18 16:58:19 +01:00
|
|
|
set_global_rotation(Math::deg_to_rad(p_degrees));
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:48:34 +02:00
|
|
|
void Node2D::set_global_skew(const real_t p_radians) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2021-08-12 19:48:34 +02:00
|
|
|
CanvasItem *parent = get_parent_item();
|
|
|
|
if (parent) {
|
|
|
|
Transform2D parent_global_transform = parent->get_global_transform();
|
|
|
|
Transform2D new_transform = parent_global_transform * get_transform();
|
|
|
|
new_transform.set_skew(p_radians);
|
|
|
|
new_transform = parent_global_transform.affine_inverse() * new_transform;
|
|
|
|
set_skew(new_transform.get_skew());
|
|
|
|
} else {
|
|
|
|
set_skew(p_radians);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 11:39:19 +02:00
|
|
|
Size2 Node2D::get_global_scale() const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Size2());
|
2016-08-15 11:39:19 +02:00
|
|
|
return get_global_transform().get_scale();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node2D::set_global_scale(const Size2 &p_scale) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2021-08-12 19:48:34 +02:00
|
|
|
CanvasItem *parent = get_parent_item();
|
|
|
|
if (parent) {
|
|
|
|
Transform2D parent_global_transform = parent->get_global_transform();
|
|
|
|
Transform2D new_transform = parent_global_transform * get_transform();
|
|
|
|
new_transform.set_scale(p_scale);
|
|
|
|
new_transform = parent_global_transform.affine_inverse() * new_transform;
|
|
|
|
set_scale(new_transform.get_scale());
|
2016-08-15 11:39:19 +02:00
|
|
|
} else {
|
|
|
|
set_scale(p_scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
void Node2D::set_transform(const Transform2D &p_transform) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2021-09-24 08:39:30 +02:00
|
|
|
transform = p_transform;
|
2023-05-17 01:44:41 +02:00
|
|
|
_set_xform_dirty(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-09-24 08:39:30 +02:00
|
|
|
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_notify_transform();
|
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
void Node2D::set_global_transform(const Transform2D &p_transform) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2021-08-12 19:48:34 +02:00
|
|
|
CanvasItem *parent = get_parent_item();
|
|
|
|
if (parent) {
|
|
|
|
set_transform(parent->get_global_transform().affine_inverse() * p_transform);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
set_transform(p_transform);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D Node2D::get_relative_transform_to_parent(const Node *p_parent) const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Transform2D());
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_parent == this) {
|
2017-01-11 04:52:51 +01:00
|
|
|
return Transform2D();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
Node2D *parent_2d = Object::cast_to<Node2D>(get_parent());
|
2015-12-29 01:05:57 +01:00
|
|
|
|
2023-06-06 14:59:54 +02:00
|
|
|
ERR_FAIL_NULL_V(parent_2d, Transform2D());
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_parent == parent_2d) {
|
2015-02-14 16:09:52 +01:00
|
|
|
return get_transform();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2015-12-29 01:05:57 +01:00
|
|
|
return parent_2d->get_relative_transform_to_parent(p_parent) * get_transform();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
2015-01-10 21:35:26 +01:00
|
|
|
|
2015-05-12 13:17:09 +02:00
|
|
|
void Node2D::look_at(const Vector2 &p_pos) {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_THREAD_GUARD;
|
2015-05-12 13:17:09 +02:00
|
|
|
rotate(get_angle_to(p_pos));
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
real_t Node2D::get_angle_to(const Vector2 &p_pos) const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(0);
|
2019-01-30 19:14:43 +01:00
|
|
|
return (to_local(p_pos) * get_scale()).angle();
|
2015-05-12 13:17:09 +02:00
|
|
|
}
|
|
|
|
|
2017-07-23 13:37:26 +02:00
|
|
|
Point2 Node2D::to_local(Point2 p_global) const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Point2());
|
2017-07-23 13:37:26 +02:00
|
|
|
return get_global_transform().affine_inverse().xform(p_global);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 Node2D::to_global(Point2 p_local) const {
|
2023-05-12 13:53:15 +02:00
|
|
|
ERR_READ_THREAD_GUARD_V(Point2());
|
2017-07-23 13:37:26 +02:00
|
|
|
return get_global_transform().xform(p_local);
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:18:16 +02:00
|
|
|
void Node2D::_notification(int p_notification) {
|
|
|
|
switch (p_notification) {
|
2023-04-05 18:53:32 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2023-08-18 12:32:53 +02:00
|
|
|
ERR_MAIN_THREAD_GUARD;
|
|
|
|
|
2022-04-01 21:18:16 +02:00
|
|
|
if (get_viewport()) {
|
2023-04-05 18:53:32 +02:00
|
|
|
get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty), CONNECT_REFERENCE_COUNTED);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2023-08-18 12:32:53 +02:00
|
|
|
ERR_MAIN_THREAD_GUARD;
|
|
|
|
|
2023-04-05 18:53:32 +02:00
|
|
|
if (get_viewport()) {
|
|
|
|
get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty));
|
2022-04-01 21:18:16 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Node2D::_bind_methods() {
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_position", "position"), &Node2D::set_position);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Node2D::set_rotation);
|
2022-12-18 16:58:19 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Node2D::set_rotation_degrees);
|
2020-05-01 18:38:04 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_skew", "radians"), &Node2D::set_skew);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node2D::set_scale);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_position"), &Node2D::get_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rotation"), &Node2D::get_rotation);
|
2022-12-18 16:58:19 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Node2D::get_rotation_degrees);
|
2020-05-01 18:38:04 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_skew"), &Node2D::get_skew);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_scale"), &Node2D::get_scale);
|
|
|
|
|
|
|
|
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-05 22:52:58 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("apply_scale", "ratio"), &Node2D::apply_scale);
|
2017-02-13 12:47:24 +01:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node2D::set_global_position);
|
2017-02-13 12:47:24 +01: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);
|
2022-12-18 16:58:19 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_global_rotation_degrees", "degrees"), &Node2D::set_global_rotation_degrees);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node2D::get_global_rotation);
|
2022-12-18 16:58:19 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_global_rotation_degrees"), &Node2D::get_global_rotation_degrees);
|
2021-08-12 19:48:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_global_skew", "radians"), &Node2D::set_global_skew);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_global_skew"), &Node2D::get_global_skew);
|
2017-02-13 12:47:24 +01: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);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_transform", "xform"), &Node2D::set_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_global_transform", "xform"), &Node2D::set_global_transform);
|
|
|
|
|
|
|
|
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-07-23 13:37:26 +02: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-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_relative_transform_to_parent", "parent"), &Node2D::get_relative_transform_to_parent);
|
2017-01-04 05:16:14 +01:00
|
|
|
|
|
|
|
ADD_GROUP("Transform", "");
|
2022-10-03 18:59:33 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_RANGE, "-99999,99999,0.001,or_less,or_greater,hide_slider,suffix:px"), "set_position", "get_position");
|
2023-09-23 16:27:01 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
|
2022-12-18 16:58:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
|
2022-04-05 02:58:21 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale", PROPERTY_HINT_LINK), "set_scale", "get_scale");
|
2023-09-23 16:27:01 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "skew", PROPERTY_HINT_RANGE, "-89.9,89.9,0.1,radians_as_degrees"), "set_skew", "get_skew");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_transform", "get_transform");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_global_position", "get_global_position");
|
2023-09-23 16:27:01 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rotation", PROPERTY_HINT_NONE, "radians_as_degrees", PROPERTY_USAGE_NONE), "set_global_rotation", "get_global_rotation");
|
2022-12-18 16:58:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_rotation_degrees", "get_global_rotation_degrees");
|
2021-06-18 01:10:18 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_scale", "get_global_scale");
|
2023-09-23 16:27:01 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_skew", PROPERTY_HINT_NONE, "radians_as_degrees", PROPERTY_USAGE_NONE), "set_global_skew", "get_global_skew");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_transform", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_global_transform", "get_global_transform");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|