Skip to content

Commit

Permalink
Merge pull request #48918 from groud/fix_tilemap_y_sort
Browse files Browse the repository at this point in the history
Fixes TileSet Y-sort not working and TileSet not saving correctly
  • Loading branch information
akien-mga authored May 21, 2021
2 parents 2687ac2 + 7d80480 commit 3b3a55c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc/classes/TileData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
</member>
<member name="transpose" type="bool" setter="set_transpose" getter="get_transpose" default="false">
</member>
<member name="y_sort_origin" type="Vector2i" setter="set_y_sort_origin" getter="get_y_sort_origin" default="Vector2i( 0, 0 )">
<member name="y_sort_origin" type="int" setter="set_y_sort_origin" getter="get_y_sort_origin" default="0">
</member>
<member name="z_index" type="int" setter="set_z_index" getter="get_z_index" default="0">
</member>
Expand Down
17 changes: 16 additions & 1 deletion editor/plugins/tiles/tile_data_editors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,22 @@ void TileDataPositionEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform
ERR_FAIL_COND(value.get_type() != Variant::VECTOR2I && value.get_type() != Variant::VECTOR2);

Ref<Texture2D> position_icon = TileSetEditor::get_singleton()->get_theme_icon("EditorPosition", "EditorIcons");
p_canvas_item->draw_texture(position_icon, p_transform.get_origin() + Vector2(value) - position_icon->get_size() / 2);
p_canvas_item->draw_texture(position_icon, p_transform.xform(Vector2(value)) - position_icon->get_size() / 2);
}

void TileDataYSortEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileSet *p_tile_set, int p_atlas_source_id, Vector2i p_atlas_coords, int p_alternative_tile, String p_property) {
TileData *tile_data = _get_tile_data(p_tile_set, p_atlas_source_id, p_atlas_coords, p_alternative_tile);
ERR_FAIL_COND(!tile_data);

bool valid;
Variant value = tile_data->get(p_property, &valid);
if (!valid) {
return;
}
ERR_FAIL_COND(value.get_type() != Variant::INT);

Ref<Texture2D> position_icon = TileSetEditor::get_singleton()->get_theme_icon("EditorPosition", "EditorIcons");
p_canvas_item->draw_texture(position_icon, p_transform.xform(Vector2(0, value)) - position_icon->get_size() / 2);
}

void TileDataOcclusionShapeEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileSet *p_tile_set, int p_atlas_source_id, Vector2i p_atlas_coords, int p_alternative_tile, String p_property) {
Expand Down
7 changes: 7 additions & 0 deletions editor/plugins/tiles/tile_data_editors.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class TileDataPositionEditor : public TileDataEditor {
virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileSet *p_tile_set, int p_atlas_source_id, Vector2i p_atlas_coords, int p_alternative_tile, String p_property) override;
};

class TileDataYSortEditor : public TileDataEditor {
GDCLASS(TileDataYSortEditor, TileDataEditor);

public:
virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileSet *p_tile_set, int p_atlas_source_id, Vector2i p_atlas_coords, int p_alternative_tile, String p_property) override;
};

class TileDataOcclusionShapeEditor : public TileDataEditor {
GDCLASS(TileDataOcclusionShapeEditor, TileDataEditor);

Expand Down
7 changes: 5 additions & 2 deletions editor/plugins/tiles/tile_set_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ void TileSetEditor::_notification(int p_what) {
break;
case NOTIFICATION_INTERNAL_PROCESS:
if (tile_set_changed_needs_update) {
if (tile_set.is_valid()) {
tile_set->set_edited(true);
}
_update_atlas_sources_list();
tile_set_changed_needs_update = false;
}
Expand Down Expand Up @@ -445,7 +448,7 @@ TileDataEditor *TileSetEditor::get_tile_data_editor(String p_property) {
} else if (p_property == "probability") {
return tile_data_float_editor;
} else if (p_property == "y_sort_origin") {
return tile_data_position_editor;
return tile_data_y_sort_editor;
} else if (p_property == "texture_offset") {
return tile_data_texture_offset_editor;
} else if (components.size() >= 1 && components[0].begins_with("occlusion_layer_")) {
Expand Down Expand Up @@ -575,7 +578,7 @@ TileSetEditor::~TileSetEditor() {

// Delete tile data editors.
memdelete(tile_data_texture_offset_editor);
memdelete(tile_data_position_editor);
memdelete(tile_data_y_sort_editor);
memdelete(tile_data_integer_editor);
memdelete(tile_data_float_editor);
memdelete(tile_data_occlusion_shape_editor);
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/tiles/tile_set_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TileSetEditor : public VBoxContainer {

// List of tile data editors.
TileDataTextureOffsetEditor *tile_data_texture_offset_editor = memnew(TileDataTextureOffsetEditor);
TileDataPositionEditor *tile_data_position_editor = memnew(TileDataPositionEditor);
TileDataYSortEditor *tile_data_y_sort_editor = memnew(TileDataYSortEditor);
TileDataIntegerEditor *tile_data_integer_editor = memnew(TileDataIntegerEditor);
TileDataFloatEditor *tile_data_float_editor = memnew(TileDataFloatEditor);
TileDataOcclusionShapeEditor *tile_data_occlusion_shape_editor = memnew(TileDataOcclusionShapeEditor);
Expand Down
22 changes: 17 additions & 5 deletions scene/resources/tile_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2351,11 +2351,11 @@ int TileData::get_z_index() const {
return z_index;
}

void TileData::set_y_sort_origin(Vector2i p_y_sort_origin) {
void TileData::set_y_sort_origin(int p_y_sort_origin) {
y_sort_origin = p_y_sort_origin;
emit_signal("changed");
}
Vector2i TileData::get_y_sort_origin() const {
int TileData::get_y_sort_origin() const {
return y_sort_origin;
}

Expand Down Expand Up @@ -3020,7 +3020,7 @@ void TileData::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "texture_offset"), "set_texture_offset", "get_texture_offset");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index"), "set_z_index", "get_z_index");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");
ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");

ADD_GROUP("Terrains", "");
ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain_set"), "set_terrain_set", "get_terrain_set");
Expand Down Expand Up @@ -3866,6 +3866,12 @@ void TileSetPluginAtlasRendering::tilemap_notification(TileMap *p_tile_map, int
}
}
} break;
case CanvasItem::NOTIFICATION_DRAW: {
Ref<TileSet> tile_set = p_tile_map->get_tileset();
if (tile_set.is_valid()) {
RenderingServer::get_singleton()->canvas_item_set_sort_children_by_y(p_tile_map->get_canvas_item(), tile_set->is_y_sorting());
}
} break;
}
}

Expand Down Expand Up @@ -3978,24 +3984,30 @@ void TileSetPluginAtlasRendering::update_dirty_quadrants(TileMap *p_tile_map, Se
int z_index = tile_data->get_z_index();

// Quandrant pos.
Vector2 position = p_tile_map->map_to_world(q.coords * p_tile_map->get_effective_quadrant_size()) - tile_set->get_tile_size() / 2;
Vector2 position = p_tile_map->map_to_world(q.coords * p_tile_map->get_effective_quadrant_size());
if (tile_set->is_y_sorting()) {
// When Y-sorting, the quandrant size is sure to be 1, we can thus offset the CanvasItem.
position.y += tile_data->get_y_sort_origin();
}

// --- CanvasItems ---
// Create two canvas items, for rendering and debug.
RID canvas_item;

// Check if the material or the z_index changed.
if (prev_canvas_item == RID() || prev_material != mat || prev_z_index != z_index) {
// If so, create a new CanvasItem.
canvas_item = rs->canvas_item_create();
if (mat.is_valid()) {
rs->canvas_item_set_material(canvas_item, mat->get_rid());
}
rs->canvas_item_set_parent(canvas_item, p_tile_map->get_canvas_item());
rs->canvas_item_set_use_parent_material(canvas_item, p_tile_map->get_use_parent_material() || p_tile_map->get_material().is_valid());

Transform2D xform;
xform.set_origin(position);

rs->canvas_item_set_transform(canvas_item, xform);

rs->canvas_item_set_light_mask(canvas_item, p_tile_map->get_light_mask());
rs->canvas_item_set_z_index(canvas_item, z_index);

Expand Down
6 changes: 3 additions & 3 deletions scene/resources/tile_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class TileData : public Object {
Ref<ShaderMaterial> material = Ref<ShaderMaterial>();
Color modulate = Color(1.0, 1.0, 1.0, 1.0);
int z_index = 0;
Vector2i y_sort_origin = Vector2i();
int y_sort_origin = 0;
Vector<Ref<OccluderPolygon2D>> occluders;

// Physics
Expand Down Expand Up @@ -563,8 +563,8 @@ class TileData : public Object {
Color get_modulate() const;
void set_z_index(int p_z_index);
int get_z_index() const;
void set_y_sort_origin(Vector2i p_y_sort_origin);
Vector2i get_y_sort_origin() const;
void set_y_sort_origin(int p_y_sort_origin);
int get_y_sort_origin() const;

void set_occluder(int p_layer_id, Ref<OccluderPolygon2D> p_occluder_polygon);
Ref<OccluderPolygon2D> get_occluder(int p_layer_id) const;
Expand Down

0 comments on commit 3b3a55c

Please sign in to comment.