Keep property values when extending script

(cherry picked from commit 44cf3c22ac)
This commit is contained in:
Tomasz Chabora 2020-10-25 22:32:32 +01:00 committed by Haoyu Qiu
parent daa4be06b0
commit 4951f50b0f
4 changed files with 69 additions and 13 deletions

View File

@ -2608,7 +2608,10 @@ void EditorPropertyResource::_resource_selected(const RES &p_resource, bool p_ed
void EditorPropertyResource::_resource_changed(const RES &p_resource) {
// Make visual script the correct type.
Ref<Script> s = p_resource;
bool is_script = false;
if (get_edited_object() && s.is_valid()) {
is_script = true;
EditorNode::get_singleton()->get_inspector_dock()->store_script_properties(get_edited_object());
s->call("set_instance_base_type", get_edited_object()->get_class());
}
@ -2634,6 +2637,11 @@ void EditorPropertyResource::_resource_changed(const RES &p_resource) {
emit_changed(get_edited_property(), p_resource);
update_property();
if (is_script) {
// Restore properties if script was changed.
EditorNode::get_singleton()->get_inspector_dock()->apply_script_properties(get_edited_object());
}
// Automatically suggest setting up the path for a ViewportTexture.
if (vpt.is_valid() && vpt->get_viewport_path_in_scene().is_empty()) {
if (!scene_tree) {

View File

@ -400,6 +400,9 @@ void InspectorDock::_bind_methods() {
ClassDB::bind_method("_edit_forward", &InspectorDock::_edit_forward);
ClassDB::bind_method("_edit_back", &InspectorDock::_edit_back);
ClassDB::bind_method("store_script_properties", &InspectorDock::store_script_properties);
ClassDB::bind_method("apply_script_properties", &InspectorDock::apply_script_properties);
ADD_SIGNAL(MethodInfo("request_help"));
}
@ -527,6 +530,32 @@ EditorPropertyNameProcessor::Style InspectorDock::get_property_name_style() cons
return property_name_style;
}
void InspectorDock::store_script_properties(Object *p_object) {
ERR_FAIL_NULL(p_object);
ScriptInstance *si = p_object->get_script_instance();
if (!si) {
return;
}
si->get_property_state(stored_properties);
}
void InspectorDock::apply_script_properties(Object *p_object) {
ERR_FAIL_NULL(p_object);
ScriptInstance *si = p_object->get_script_instance();
if (!si) {
return;
}
for (List<Pair<StringName, Variant>>::Element *E = stored_properties.front(); E; E = E->next()) {
const Pair<StringName, Variant> &p = E->get();
Variant current;
if (si->get(p.first, current) && current.get_type() == p.second.get_type()) {
si->set(p.first, p.second);
}
}
stored_properties.clear();
}
InspectorDock::InspectorDock(EditorNode *p_editor, EditorData &p_editor_data) {
set_name("Inspector");
set_theme(p_editor->get_gui_base()->get_theme());

View File

@ -100,6 +100,7 @@ class InspectorDock : public VBoxContainer {
AcceptDialog *warning_dialog;
EditorPropertyNameProcessor::Style property_name_style;
List<Pair<StringName, Variant>> stored_properties;
void _prepare_menu();
void _menu_option(int p_option);
@ -144,6 +145,9 @@ public:
EditorPropertyNameProcessor::Style get_property_name_style() const;
void store_script_properties(Object *p_object);
void apply_script_properties(Object *p_object);
InspectorDock(EditorNode *p_editor, EditorData &p_editor_data);
~InspectorDock();
};

View File

@ -2050,16 +2050,24 @@ void SceneTreeDock::_script_created(Ref<Script> p_script) {
return;
}
editor_data->get_undo_redo().create_action(TTR("Attach Script"));
InspectorDock *inspector_dock = EditorNode::get_singleton()->get_inspector_dock();
UndoRedo &undo_redo = editor_data->get_undo_redo();
undo_redo.create_action(TTR("Attach Script"));
for (List<Node *>::Element *E = selected.front(); E; E = E->next()) {
Ref<Script> existing = E->get()->get_script();
editor_data->get_undo_redo().add_do_method(E->get(), "set_script", p_script.get_ref_ptr());
editor_data->get_undo_redo().add_undo_method(E->get(), "set_script", existing);
editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
Node *node = E->get();
Ref<Script> existing = node->get_script();
undo_redo.add_do_method(inspector_dock, "store_script_properties", node);
undo_redo.add_undo_method(inspector_dock, "store_script_properties", node);
undo_redo.add_do_method(node, "set_script", p_script.get_ref_ptr());
undo_redo.add_undo_method(node, "set_script", existing);
undo_redo.add_do_method(inspector_dock, "apply_script_properties", node);
undo_redo.add_undo_method(inspector_dock, "apply_script_properties", node);
undo_redo.add_do_method(this, "_update_script_button");
undo_redo.add_undo_method(this, "_update_script_button");
}
editor_data->get_undo_redo().commit_action();
undo_redo.commit_action();
_push_item(p_script.operator->());
_update_script_button();
@ -2706,12 +2714,19 @@ void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
ERR_FAIL_COND(!scr.is_valid());
Node *n = get_node(p_to);
if (n) {
editor_data->get_undo_redo().create_action(TTR("Attach Script"));
editor_data->get_undo_redo().add_do_method(n, "set_script", scr);
editor_data->get_undo_redo().add_undo_method(n, "set_script", n->get_script());
editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
editor_data->get_undo_redo().commit_action();
InspectorDock *inspector_dock = EditorNode::get_singleton()->get_inspector_dock();
UndoRedo &undo_redo = editor_data->get_undo_redo();
undo_redo.create_action(TTR("Attach Script"));
undo_redo.add_do_method(inspector_dock, "store_script_properties", n);
undo_redo.add_undo_method(inspector_dock, "store_script_properties", n);
undo_redo.add_do_method(n, "set_script", scr);
undo_redo.add_undo_method(n, "set_script", n->get_script());
undo_redo.add_do_method(inspector_dock, "apply_script_properties", n);
undo_redo.add_undo_method(inspector_dock, "apply_script_properties", n);
undo_redo.add_do_method(this, "_update_script_button");
undo_redo.add_undo_method(this, "_update_script_button");
undo_redo.commit_action();
}
}