Merge pull request #66557 from MisterMX/fix/tileset-custom-data-type-reload-editor

fix(tileset): Recreate custom data editor if type has changed
This commit is contained in:
Rémi Verschelde 2022-11-17 13:49:02 +01:00
commit 2ca972ad01
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 24 additions and 6 deletions

View File

@ -1138,6 +1138,7 @@ void TileDataDefaultEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2
void TileDataDefaultEditor::setup_property_editor(Variant::Type p_type, String p_property, String p_label, Variant p_default_value) {
ERR_FAIL_COND_MSG(!property.is_empty(), "Cannot setup TileDataDefaultEditor twice");
property = p_property;
property_type = p_type;
// Update everything.
if (property_editor) {
@ -1182,6 +1183,10 @@ void TileDataDefaultEditor::_notification(int p_what) {
}
}
Variant::Type TileDataDefaultEditor::get_property_type() {
return property_type;
}
TileDataDefaultEditor::TileDataDefaultEditor() {
undo_redo = EditorNode::get_undo_redo();

View File

@ -220,6 +220,7 @@ protected:
StringName type;
String property;
Variant::Type property_type;
void _notification(int p_what);
virtual Variant _get_painted_value();
@ -237,6 +238,7 @@ public:
virtual void draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected = false) override;
void setup_property_editor(Variant::Type p_type, String p_property, String p_label = "", Variant p_default_value = Variant());
Variant::Type get_property_type();
TileDataDefaultEditor();
~TileDataDefaultEditor();

View File

@ -737,18 +737,29 @@ void TileSetAtlasSourceEditor::_update_tile_data_editors() {
// --- Custom Data ---
ADD_TILE_DATA_EDITOR_GROUP("Custom Data");
for (int i = 0; i < tile_set->get_custom_data_layers_count(); i++) {
if (tile_set->get_custom_data_layer_name(i).is_empty()) {
ADD_TILE_DATA_EDITOR(group, vformat("Custom Data %d", i), vformat("custom_data_%d", i));
String editor_name = vformat("custom_data_%d", i);
String prop_name = tile_set->get_custom_data_layer_name(i);
Variant::Type prop_type = tile_set->get_custom_data_layer_type(i);
if (prop_name.is_empty()) {
ADD_TILE_DATA_EDITOR(group, vformat("Custom Data %d", i), editor_name);
} else {
ADD_TILE_DATA_EDITOR(group, tile_set->get_custom_data_layer_name(i), vformat("custom_data_%d", i));
ADD_TILE_DATA_EDITOR(group, prop_name, editor_name);
}
if (!tile_data_editors.has(vformat("custom_data_%d", i))) {
// If the type of the edited property has been changed, delete the
// editor and create a new one.
if (tile_data_editors.has(editor_name) && ((TileDataDefaultEditor *)tile_data_editors[editor_name])->get_property_type() != prop_type) {
tile_data_editors[vformat("custom_data_%d", i)]->queue_delete();
tile_data_editors.erase(vformat("custom_data_%d", i));
}
if (!tile_data_editors.has(editor_name)) {
TileDataDefaultEditor *tile_data_custom_data_editor = memnew(TileDataDefaultEditor());
tile_data_custom_data_editor->hide();
tile_data_custom_data_editor->setup_property_editor(tile_set->get_custom_data_layer_type(i), vformat("custom_data_%d", i), tile_set->get_custom_data_layer_name(i));
tile_data_custom_data_editor->setup_property_editor(prop_type, editor_name, prop_name);
tile_data_custom_data_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
tile_data_custom_data_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
tile_data_editors[vformat("custom_data_%d", i)] = tile_data_custom_data_editor;
tile_data_editors[editor_name] = tile_data_custom_data_editor;
}
}
for (int i = tile_set->get_custom_data_layers_count(); tile_data_editors.has(vformat("custom_data_%d", i)); i++) {