Skip to content

Commit e3350cf

Browse files
committed
Merge pull request #112790 from kleonc/tile_map_layer_fix_scene_tile_transformations
Fix `TileMapLayer` transformations for `Node2D` scene tiles
2 parents 9e6fefd + 944bd82 commit e3350cf

File tree

2 files changed

+14
-41
lines changed

2 files changed

+14
-41
lines changed

scene/2d/tile_map_layer.cpp

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,10 +1635,7 @@ void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
16351635
if (scene_as_control) {
16361636
scene_as_control->set_position(tile_set->map_to_local(r_cell_data.coords) + scene_as_control->get_position());
16371637
} else if (scene_as_node2d) {
1638-
Transform2D xform;
1639-
xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
1640-
scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
1641-
_set_scene_transformed_alternative(c.alternative_tile, scene_as_node2d);
1638+
_set_scene_transform_with_alternative(scene_as_node2d, tile_set->map_to_local(r_cell_data.coords), c.alternative_tile);
16421639
}
16431640
#ifdef TOOLS_ENABLED
16441641
RenderingServer *rs = RenderingServer::get_singleton();
@@ -1706,47 +1703,23 @@ void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vecto
17061703
}
17071704
#endif // DEBUG_ENABLED
17081705

1709-
void TileMapLayer::_set_scene_transformed_alternative(const int p_alternative_id, Node2D *p_scene) {
1706+
void TileMapLayer::_set_scene_transform_with_alternative(Node2D *p_scene, const Vector2 &p_cell_position, const int p_alternative_id) {
17101707
// Determine the transformations based on the alternative ID.
17111708
bool transform_flip_h = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H;
17121709
bool transform_flip_v = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V;
17131710
bool transform_transpose = p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
17141711

1715-
double scene_rotation = 0.0;
1716-
Vector2 scene_scale = p_scene->get_scale();
1717-
1718-
// Determine the scene rotation and scale based on the transformation flags.
1719-
if (!transform_flip_h && !transform_flip_v && !transform_transpose) {
1720-
scene_rotation = 0.0;
1721-
scene_scale.x = 1;
1722-
} else if (transform_flip_h && !transform_flip_v && transform_transpose) {
1723-
scene_rotation = 90.0;
1724-
scene_scale.x = 1;
1725-
} else if (transform_flip_h && transform_flip_v && !transform_transpose) {
1726-
scene_rotation = 180.0;
1727-
scene_scale.x = 1;
1728-
} else if (!transform_flip_h && transform_flip_v && transform_transpose) {
1729-
scene_rotation = 270.0;
1730-
scene_scale.x = 1;
1731-
} else if (transform_flip_h && !transform_flip_v && !transform_transpose) {
1732-
scene_rotation = 0.0;
1733-
scene_scale.x = -1;
1734-
} else if (transform_flip_h && transform_flip_v && transform_transpose) {
1735-
scene_rotation = 90.0;
1736-
scene_scale.x = -1;
1737-
} else if (!transform_flip_h && transform_flip_v && !transform_transpose) {
1738-
scene_rotation = 180.0;
1739-
scene_scale.x = -1;
1740-
} else if (!transform_flip_h && !transform_flip_v && transform_transpose) {
1741-
scene_rotation = 270.0;
1742-
scene_scale.x = -1;
1743-
}
1744-
1745-
// Apply the transformations to the scene.
1746-
double current_rotation = p_scene->get_rotation_degrees() + scene_rotation;
1747-
Vector2 current_scale = p_scene->get_scale() + scene_scale;
1748-
p_scene->set_rotation_degrees(current_rotation);
1749-
p_scene->set_scale(current_scale);
1712+
int axis_h = transform_transpose ? 1 : 0;
1713+
int axis_v = 1 - axis_h;
1714+
1715+
Transform2D xform;
1716+
xform[axis_h].x = transform_flip_h ? -1.0f : 1.0f;
1717+
xform[axis_h].y = 0.0f;
1718+
xform[axis_v].x = 0.0f;
1719+
xform[axis_v].y = transform_flip_v ? -1.0f : 1.0f;
1720+
xform.set_origin(p_cell_position);
1721+
1722+
p_scene->set_transform(xform * p_scene->get_transform());
17501723
}
17511724

17521725
/////////////////////////////////////////////////////////////////////

scene/2d/tile_map_layer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ class TileMapLayer : public Node2D {
492492
#ifdef DEBUG_ENABLED
493493
void _scenes_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data);
494494
#endif // DEBUG_ENABLED
495-
void _set_scene_transformed_alternative(const int p_alternative_id, Node2D *p_scene);
495+
void _set_scene_transform_with_alternative(Node2D *p_scene, const Vector2 &p_cell_position, const int p_alternative_id);
496496

497497
// Terrains.
498498
TileSet::TerrainsPattern _get_best_terrain_pattern_for_constraints(int p_terrain_set, const Vector2i &p_position, const RBSet<TerrainConstraint> &p_constraints, TileSet::TerrainsPattern p_current_pattern) const;

0 commit comments

Comments
 (0)