Skip to content

Commit 6a39930

Browse files
committed
Add support for rotating scene tiles in TileMapLayer
1 parent 71a9948 commit 6a39930

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

editor/scene/2d/tiles/tile_map_layer_editor.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,7 @@ void TileMapLayerEditorTilesPlugin::_update_transform_buttons() {
127127
return;
128128
}
129129

130-
bool has_scene_tile = false;
131-
for (const KeyValue<Vector2i, TileMapCell> &E : selection_pattern->get_pattern()) {
132-
if (Object::cast_to<TileSetScenesCollectionSource>(tile_set->get_source(E.value.source_id).ptr())) {
133-
has_scene_tile = true;
134-
break;
135-
}
136-
}
137-
138-
if (has_scene_tile) {
139-
_set_transform_buttons_state({}, { transform_button_rotate_left, transform_button_rotate_right, transform_button_flip_h, transform_button_flip_v },
140-
TTR("Can't transform scene tiles."));
141-
} else if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE && selection_pattern->get_size() != Vector2i(1, 1)) {
130+
if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE && selection_pattern->get_size() != Vector2i(1, 1)) {
142131
_set_transform_buttons_state({ transform_button_flip_h, transform_button_flip_v }, { transform_button_rotate_left, transform_button_rotate_right },
143132
TTR("Can't rotate patterns when using non-square tile grid."));
144133
} else {

scene/2d/tile_map_layer.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
15711571
Transform2D xform;
15721572
xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
15731573
scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
1574+
_set_scene_transformed_alternative(c.alternative_tile, scene_as_node2d);
15741575
}
15751576
if (tile_map_node) {
15761577
// Compatibility with TileMap.
@@ -1631,6 +1632,49 @@ void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vecto
16311632
}
16321633
#endif // DEBUG_ENABLED
16331634

1635+
void TileMapLayer::_set_scene_transformed_alternative(const int p_alternative_id, Node2D *p_scene) {
1636+
// Determine the transformations based on the alternative ID.
1637+
bool transform_flip_h = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H;
1638+
bool transform_flip_v = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V;
1639+
bool transform_transpose = p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
1640+
1641+
double scene_rotation = 0.0;
1642+
Vector2 scene_scale = p_scene->get_scale();
1643+
1644+
// Determine the scene rotation and scale based on the transformation flags.
1645+
if (!transform_flip_h && !transform_flip_v && !transform_transpose) {
1646+
scene_rotation = 0.0;
1647+
scene_scale.x = 1;
1648+
} else if (transform_flip_h && !transform_flip_v && transform_transpose) {
1649+
scene_rotation = 90.0;
1650+
scene_scale.x = 1;
1651+
} else if (transform_flip_h && transform_flip_v && !transform_transpose) {
1652+
scene_rotation = 180.0;
1653+
scene_scale.x = 1;
1654+
} else if (!transform_flip_h && transform_flip_v && transform_transpose) {
1655+
scene_rotation = 270.0;
1656+
scene_scale.x = 1;
1657+
} else if (transform_flip_h && !transform_flip_v && !transform_transpose) {
1658+
scene_rotation = 0.0;
1659+
scene_scale.x = -1;
1660+
} else if (transform_flip_h && transform_flip_v && transform_transpose) {
1661+
scene_rotation = 90.0;
1662+
scene_scale.x = -1;
1663+
} else if (!transform_flip_h && transform_flip_v && !transform_transpose) {
1664+
scene_rotation = 180.0;
1665+
scene_scale.x = -1;
1666+
} else if (!transform_flip_h && !transform_flip_v && transform_transpose) {
1667+
scene_rotation = 270.0;
1668+
scene_scale.x = -1;
1669+
}
1670+
1671+
// Apply the transformations to the scene.
1672+
double current_rotation = p_scene->get_rotation_degrees() + scene_rotation;
1673+
Vector2 current_scale = p_scene->get_scale() + scene_scale;
1674+
p_scene->set_rotation_degrees(current_rotation);
1675+
p_scene->set_scale(current_scale);
1676+
}
1677+
16341678
/////////////////////////////////////////////////////////////////////
16351679

16361680
void TileMapLayer::_build_runtime_update_tile_data(bool p_force_cleanup) {

scene/2d/tile_map_layer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ class TileMapLayer : public Node2D {
474474
#ifdef DEBUG_ENABLED
475475
void _scenes_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data);
476476
#endif // DEBUG_ENABLED
477+
void _set_scene_transformed_alternative(const int p_alternative_id, Node2D *p_scene);
477478

478479
// Terrains.
479480
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;

scene/resources/2d/tile_set.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5775,7 +5775,7 @@ int TileSetScenesCollectionSource::get_alternative_tile_id(const Vector2i p_atla
57755775

57765776
bool TileSetScenesCollectionSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
57775777
ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), false);
5778-
return scenes.has(p_alternative_tile);
5778+
return scenes.has(TileSetAtlasSource::alternative_no_transform(p_alternative_tile));
57795779
}
57805780

57815781
int TileSetScenesCollectionSource::create_scene_tile(Ref<PackedScene> p_packed_scene, int p_id_override) {
@@ -5837,8 +5837,9 @@ void TileSetScenesCollectionSource::set_scene_tile_scene(int p_id, Ref<PackedSce
58375837
}
58385838

58395839
Ref<PackedScene> TileSetScenesCollectionSource::get_scene_tile_scene(int p_id) const {
5840-
ERR_FAIL_COND_V(!scenes.has(p_id), Ref<PackedScene>());
5841-
return scenes[p_id].scene;
5840+
int scene_tile = TileSetAtlasSource::alternative_no_transform(p_id);
5841+
ERR_FAIL_COND_V(!scenes.has(scene_tile), Ref<PackedScene>());
5842+
return scenes[scene_tile].scene;
58425843
}
58435844

58445845
void TileSetScenesCollectionSource::set_scene_tile_display_placeholder(int p_id, bool p_display_placeholder) {

0 commit comments

Comments
 (0)