Skip to content

Commit d1a9010

Browse files
committed
Merge pull request #108010 from DexterFstone/add-scene-tile-rotation
Add support for rotating scene tiles in TileMapLayer
2 parents d602742 + 6a39930 commit d1a9010

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
@@ -134,18 +134,7 @@ void TileMapLayerEditorTilesPlugin::_update_transform_buttons() {
134134
return;
135135
}
136136

137-
bool has_scene_tile = false;
138-
for (const KeyValue<Vector2i, TileMapCell> &E : selection_pattern->get_pattern()) {
139-
if (Object::cast_to<TileSetScenesCollectionSource>(tile_set->get_source(E.value.source_id).ptr())) {
140-
has_scene_tile = true;
141-
break;
142-
}
143-
}
144-
145-
if (has_scene_tile) {
146-
_set_transform_buttons_state({}, { transform_button_rotate_left, transform_button_rotate_right, transform_button_flip_h, transform_button_flip_v },
147-
TTR("Can't transform scene tiles."));
148-
} else if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE && selection_pattern->get_size() != Vector2i(1, 1)) {
137+
if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE && selection_pattern->get_size() != Vector2i(1, 1)) {
149138
_set_transform_buttons_state({ transform_button_flip_h, transform_button_flip_v }, { transform_button_rotate_left, transform_button_rotate_right },
150139
TTR("Can't rotate patterns when using non-square tile grid."));
151140
} else {

scene/2d/tile_map_layer.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
16381638
Transform2D xform;
16391639
xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
16401640
scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
1641+
_set_scene_transformed_alternative(c.alternative_tile, scene_as_node2d);
16411642
}
16421643
#ifdef TOOLS_ENABLED
16431644
RenderingServer *rs = RenderingServer::get_singleton();
@@ -1705,6 +1706,49 @@ void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vecto
17051706
}
17061707
#endif // DEBUG_ENABLED
17071708

1709+
void TileMapLayer::_set_scene_transformed_alternative(const int p_alternative_id, Node2D *p_scene) {
1710+
// Determine the transformations based on the alternative ID.
1711+
bool transform_flip_h = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H;
1712+
bool transform_flip_v = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V;
1713+
bool transform_transpose = p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
1714+
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);
1750+
}
1751+
17081752
/////////////////////////////////////////////////////////////////////
17091753

17101754
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
@@ -492,6 +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);
495496

496497
// Terrains.
497498
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
@@ -5774,7 +5774,7 @@ int TileSetScenesCollectionSource::get_alternative_tile_id(const Vector2i p_atla
57745774

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

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

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

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

0 commit comments

Comments
 (0)