Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_skew to RemoteTransform2D #103445

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/math/transform_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ Size2 Transform2D::get_scale() const {
}

void Transform2D::set_scale(const Size2 &p_scale) {
real_t det_sign = SIGN(determinant());
columns[0].normalize();
columns[1].normalize();
columns[0] *= p_scale.x;
columns[1] *= p_scale.y;
columns[1] *= det_sign * p_scale.y;
}

void Transform2D::scale(const Size2 &p_scale) {
Expand Down
6 changes: 5 additions & 1 deletion doc/classes/RemoteTransform2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</brief_description>
<description>
RemoteTransform2D pushes its own [Transform2D] to another [Node2D] derived node (called the remote node) in the scene.
It can be set to update another node's position, rotation and/or scale. It can use either global or local coordinates.
It can be set to update another node's position, rotation, scale, and/or skew. It can use either global or local coordinates.
</description>
<tutorials>
</tutorials>
Expand All @@ -29,6 +29,10 @@
</member>
<member name="update_scale" type="bool" setter="set_update_scale" getter="get_update_scale" default="true">
If [code]true[/code], the remote node's scale is updated.
[b]Note:[/b] Negative X scales in 2D are not decomposable from the transformation matrix. Due to the way scale is represented with transformation matrices in Godot, negative scales on the X axis will be changed to negative scales on the Y axis and a rotation of 180 degrees when decomposed.
</member>
<member name="update_skew" type="bool" setter="set_update_skew" getter="get_update_skew" default="true">
If [code]true[/code], the remote node's skew is updated.
</member>
<member name="use_global_coordinates" type="bool" setter="set_use_global_coordinates" getter="get_use_global_coordinates" default="true">
If [code]true[/code], global coordinates are used. If [code]false[/code], local coordinates are used.
Expand Down
75 changes: 50 additions & 25 deletions scene/2d/remote_transform_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ void RemoteTransform2D::_update_cache() {
}
}

Transform2D RemoteTransform2D::_create_updated_transform(Transform2D our_trans, Transform2D n_trans) const {
if (update_remote_rotation) {
if (!update_remote_position) {
our_trans.set_origin(n_trans.get_origin());
}
if (!update_remote_scale) {
our_trans.set_scale(n_trans.get_scale());
}
if (!update_remote_skew) {
our_trans.set_skew(n_trans.get_skew());
}
Comment on lines +50 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the elapsed time of these methods: set_scale() << set_rotation_scale_and_skew() < set_skew()

That is, if you don't just call set_scale(), it is better to call set_rotation_scale_and_skew() directly. You can single out the case where only set_scale() is called.

return our_trans;
} else {
if (update_remote_position) {
n_trans.set_origin(our_trans.get_origin());
}
if (update_remote_scale) {
n_trans.set_scale(our_trans.get_scale());
}
if (update_remote_skew) {
n_trans.set_skew(our_trans.get_skew());
}
return n_trans;
}
}

void RemoteTransform2D::_update_remote() {
if (!is_inside_tree()) {
return;
Expand All @@ -60,49 +86,33 @@ void RemoteTransform2D::_update_remote() {
return;
}

if (!(update_remote_position || update_remote_rotation || update_remote_scale)) {
if (!(update_remote_position || update_remote_rotation || update_remote_scale || update_remote_skew)) {
return; // The transform data of the RemoteTransform2D is not used at all.
}

//todo make faster
if (use_global_coordinates) {
if (update_remote_position && update_remote_rotation && update_remote_scale) {
if (update_remote_position && update_remote_rotation && update_remote_scale && update_remote_skew) {
n->set_global_transform(get_global_transform());
return;
}

Transform2D n_trans = n->get_global_transform();
Transform2D our_trans = get_global_transform();

// There are more steps in the operation of set_rotation, so avoid calling it.
Transform2D trans = update_remote_rotation ? our_trans : n_trans;

if (update_remote_rotation ^ update_remote_position) {
trans.set_origin(update_remote_position ? our_trans.get_origin() : n_trans.get_origin());
}
if (update_remote_rotation ^ update_remote_scale) {
trans.set_scale(update_remote_scale ? our_trans.get_scale() : n_trans.get_scale());
}
Transform2D trans = _create_updated_transform(our_trans, n_trans);

n->set_global_transform(trans);
} else {
if (update_remote_position && update_remote_rotation && update_remote_scale) {
if (update_remote_position && update_remote_rotation && update_remote_scale && update_remote_skew) {
n->set_transform(get_transform());
return;
}

Transform2D n_trans = n->get_transform();
Transform2D our_trans = get_transform();

// There are more steps in the operation of set_rotation, so avoid calling it.
Transform2D trans = update_remote_rotation ? our_trans : n_trans;

if (update_remote_rotation ^ update_remote_position) {
trans.set_origin(update_remote_position ? our_trans.get_origin() : n_trans.get_origin());
}
if (update_remote_rotation ^ update_remote_scale) {
trans.set_scale(update_remote_scale ? our_trans.get_scale() : n_trans.get_scale());
}
Transform2D trans = _create_updated_transform(our_trans, n_trans);

n->set_transform(trans);
}
Expand Down Expand Up @@ -155,7 +165,7 @@ NodePath RemoteTransform2D::get_remote_node() const {
return remote_node;
}

void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
void RemoteTransform2D::set_use_global_coordinates(bool p_enable) {
if (use_global_coordinates == p_enable) {
return;
}
Expand All @@ -170,7 +180,7 @@ bool RemoteTransform2D::get_use_global_coordinates() const {
return use_global_coordinates;
}

void RemoteTransform2D::set_update_position(const bool p_update) {
void RemoteTransform2D::set_update_position(bool p_update) {
if (update_remote_position == p_update) {
return;
}
Expand All @@ -182,7 +192,7 @@ bool RemoteTransform2D::get_update_position() const {
return update_remote_position;
}

void RemoteTransform2D::set_update_rotation(const bool p_update) {
void RemoteTransform2D::set_update_rotation(bool p_update) {
if (update_remote_rotation == p_update) {
return;
}
Expand All @@ -194,7 +204,7 @@ bool RemoteTransform2D::get_update_rotation() const {
return update_remote_rotation;
}

void RemoteTransform2D::set_update_scale(const bool p_update) {
void RemoteTransform2D::set_update_scale(bool p_update) {
if (update_remote_scale == p_update) {
return;
}
Expand All @@ -206,6 +216,18 @@ bool RemoteTransform2D::get_update_scale() const {
return update_remote_scale;
}

void RemoteTransform2D::set_update_skew(bool p_update) {
if (update_remote_skew == p_update) {
return;
}
update_remote_skew = p_update;
_update_remote();
}

bool RemoteTransform2D::get_update_skew() const {
return update_remote_skew;
}

void RemoteTransform2D::force_update_cache() {
_update_cache();
}
Expand Down Expand Up @@ -234,6 +256,8 @@ void RemoteTransform2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_update_rotation"), &RemoteTransform2D::get_update_rotation);
ClassDB::bind_method(D_METHOD("set_update_scale", "update_remote_scale"), &RemoteTransform2D::set_update_scale);
ClassDB::bind_method(D_METHOD("get_update_scale"), &RemoteTransform2D::get_update_scale);
ClassDB::bind_method(D_METHOD("set_update_skew", "update_remote_skew"), &RemoteTransform2D::set_update_skew);
ClassDB::bind_method(D_METHOD("get_update_skew"), &RemoteTransform2D::get_update_skew);

ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "remote_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_remote_node", "get_remote_node");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_coordinates"), "set_use_global_coordinates", "get_use_global_coordinates");
Expand All @@ -242,6 +266,7 @@ void RemoteTransform2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_position"), "set_update_position", "get_update_position");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_rotation"), "set_update_rotation", "get_update_rotation");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_scale"), "set_update_scale", "get_update_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_skew"), "set_update_skew", "get_update_skew");
}

RemoteTransform2D::RemoteTransform2D() {
Expand Down
13 changes: 9 additions & 4 deletions scene/2d/remote_transform_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ class RemoteTransform2D : public Node2D {
bool update_remote_position = true;
bool update_remote_rotation = true;
bool update_remote_scale = true;
bool update_remote_skew = true;

void _update_remote();
void _update_cache();
Transform2D _create_updated_transform(Transform2D our_trans, Transform2D n_trans) const;
//void _node_exited_scene();
protected:
static void _bind_methods();
Expand All @@ -56,18 +58,21 @@ class RemoteTransform2D : public Node2D {
void set_remote_node(const NodePath &p_remote_node);
NodePath get_remote_node() const;

void set_use_global_coordinates(const bool p_enable);
void set_use_global_coordinates(bool p_enable);
bool get_use_global_coordinates() const;

void set_update_position(const bool p_update);
void set_update_position(bool p_update);
bool get_update_position() const;

void set_update_rotation(const bool p_update);
void set_update_rotation(bool p_update);
bool get_update_rotation() const;

void set_update_scale(const bool p_update);
void set_update_scale(bool p_update);
bool get_update_scale() const;

void set_update_skew(bool p_update);
bool get_update_skew() const;

void force_update_cache();

PackedStringArray get_configuration_warnings() const override;
Expand Down