Add unsaved status for script and shader editors

This commit is contained in:
kobewi 2022-10-16 19:30:38 +02:00
parent 6dc5dc3479
commit 000471ee56
4 changed files with 49 additions and 0 deletions

View File

@ -2435,6 +2435,18 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
return true;
}
PackedStringArray ScriptEditor::get_unsaved_scripts() const {
PackedStringArray unsaved_list;
for (int i = 0; i < tab_container->get_tab_count(); i++) {
ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
if (se->is_unsaved()) {
unsaved_list.append(se->get_name());
}
}
return unsaved_list;
}
void ScriptEditor::save_current_script() {
ScriptEditorBase *current = _get_current_editor();
if (!current || _test_script_times_on_disk()) {
@ -4207,6 +4219,24 @@ void ScriptEditorPlugin::selected_notify() {
_focus_another_editor();
}
String ScriptEditorPlugin::get_unsaved_status() const {
const PackedStringArray unsaved_scripts = script_editor->get_unsaved_scripts();
if (unsaved_scripts.is_empty()) {
return String();
}
PackedStringArray message;
message.resize(unsaved_scripts.size() + 1);
message.write[0] = "Save changes to the following script(s) before quitting?";
int i = 1;
for (const String &E : unsaved_scripts) {
message.write[i] = E.trim_suffix("(*)");
i++;
}
return String("\n").join(message);
}
void ScriptEditorPlugin::save_external_data() {
script_editor->save_all_scripts();
}

View File

@ -512,6 +512,7 @@ public:
void get_breakpoints(List<String> *p_breakpoints);
PackedStringArray get_unsaved_scripts() const;
void save_current_script();
void save_all_scripts();
@ -572,6 +573,7 @@ public:
virtual void make_visible(bool p_visible) override;
virtual void selected_notify() override;
virtual String get_unsaved_status() const override;
virtual void save_external_data() override;
virtual void apply_changes() override;

View File

@ -287,6 +287,22 @@ void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);
}
String ShaderEditorPlugin::get_unsaved_status() const {
// TODO: This should also include visual shaders and shader includes, but save_external_data() doesn't seem to save them...
PackedStringArray unsaved_shaders;
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
if (edited_shaders[i].shader_editor) {
if (edited_shaders[i].shader_editor->is_unsaved()) {
if (unsaved_shaders.is_empty()) {
unsaved_shaders.append("Save changes to the following shaders(s) before quitting?");
}
unsaved_shaders.append(edited_shaders[i].shader_editor->get_name());
}
}
}
return String("\n").join(unsaved_shaders);
}
void ShaderEditorPlugin::save_external_data() {
for (EditedShader &edited_shader : edited_shaders) {
if (edited_shader.shader_editor) {

View File

@ -115,6 +115,7 @@ public:
virtual void set_window_layout(Ref<ConfigFile> p_layout) override;
virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
virtual String get_unsaved_status() const override;
virtual void save_external_data() override;
virtual void apply_changes() override;