Przeglądaj źródła

Merge pull request #410 from rsredsq/RED-EDITOR-325

Added check for unsaved changes before closing a project
JoshEngebretson 10 lat temu
rodzic
commit
3f197a152f

+ 4 - 0
Script/AtomicEditor/ui/EditorUI.ts

@@ -48,6 +48,10 @@ export function showModalError(windowText:string, message:string) {
   editorUI.showModalError(windowText, message);
 }
 
+export function getCurrentResourceEditor():Editor.ResourceEditor {
+    return getMainFrame().resourceframe.currentResourceEditor;
+}
+
 
 class EditorUI extends Atomic.ScriptObject {
 

+ 1 - 6
Script/AtomicEditor/ui/Shortcuts.ts

@@ -49,12 +49,7 @@ class Shortcuts extends Atomic.ScriptObject {
     }
 
     invokeFileClose() {
-
-        // pretty gross
-        var editor = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
-        if (!editor) return;
-        editor.close(true);
-
+        this.invokeResourceFrameShortcut("close");
     }
 
     invokeFileSave() {

+ 15 - 0
Script/AtomicEditor/ui/frames/HierarchyFrame.ts

@@ -8,6 +8,7 @@
 import HierarchyFrameMenu = require("./menus/HierarchyFrameMenu");
 import MenuItemSources = require("./menus/MenuItemSources");
 import EditorEvents = require("editor/EditorEvents");
+import EditorUI = require("ui/EditorUI");
 
 var IconTemporary = "ComponentBitmap";
 
@@ -67,6 +68,11 @@ class HierarchyFrame extends Atomic.UIWidget {
 
         this.subscribeToEvent("ComponentAdded", (ev: Atomic.ComponentAddedEvent) => {
 
+            //var resourceEditor = EditorUI.getCurrentResourceEditor();
+            //if (resourceEditor) {
+            //    resourceEditor.setModified(true);
+            //}
+
             if (!ev.component || ev.component.typeName != "PrefabComponent") return;
 
             var node = ev.node;
@@ -83,6 +89,11 @@ class HierarchyFrame extends Atomic.UIWidget {
 
         this.subscribeToEvent("ComponentRemoved", (ev: Atomic.ComponentRemovedEvent) => {
 
+            //var resourceEditor = EditorUI.getCurrentResourceEditor();
+            //if (resourceEditor) {
+            //    resourceEditor.setModified(true);
+            //}
+
             if (!ev.component || ev.component.typeName != "PrefabComponent") return;
 
             var node = ev.node;
@@ -131,6 +142,8 @@ class HierarchyFrame extends Atomic.UIWidget {
         if (!node.parent || node.scene != this.scene)
             return;
 
+        //EditorUI.getCurrentResourceEditor().setModified(true);
+
         var parentID = this.nodeIDToItemID[node.parent.id];
 
         var childItemID = this.recursiveAddNode(parentID, node);
@@ -145,6 +158,8 @@ class HierarchyFrame extends Atomic.UIWidget {
         if (!this.scene)
             return;
 
+        //EditorUI.getCurrentResourceEditor().setModified(true);
+
         var node = ev.node;
 
         if (this.filterNode(node))

+ 2 - 1
Script/AtomicEditor/ui/frames/ResourceFrame.ts

@@ -38,8 +38,9 @@ class ResourceFrame extends ScriptWidget {
 
     handleSaveResource(ev: EditorEvents.SaveResourceEvent) {
 
-        if (this.currentResourceEditor)
+        if (this.currentResourceEditor){
             this.currentResourceEditor.save();
+        }
 
     }
 

+ 1 - 1
Script/AtomicEditor/ui/frames/inspector/ComponentInspector.ts

@@ -211,7 +211,7 @@ class ComponentInspector extends Atomic.UISection {
 
             // refresh entire inspector, fix this...
             this.sendEvent("EditorActiveNodeChange", { node: node });
-
+            
             return true;
 
         }

+ 1 - 0
Script/AtomicEditor/ui/frames/inspector/DataBinding.ts

@@ -479,6 +479,7 @@ class DataBinding {
 
         if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
             if (this.widget == ev.target || this.widget.isAncestorOf(ev.target)) {
+                //EditorUI.getCurrentResourceEditor().setModified(true);
                 this.setObjectValueFromWidget(ev.target);
                 return true;
             }

+ 3 - 44
Source/AtomicEditor/Editors/JSResourceEditor.cpp

@@ -38,7 +38,6 @@ JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UI
     autocomplete_(0),
     textDirty_(true),
     textDelta_(0.0f),
-    modified_(false),
     currentFindPos_(-1)
 {
 
@@ -223,9 +222,7 @@ void JSResourceEditor::OnChange(TBStyleEdit* styleEdit)
     textDirty_ = true;
     modified_ = true;
 
-    String filename = GetFileNameAndExtension(fullpath_);
-    filename += "*";
-    button_->SetText(filename.CString());
+    SetModified(true);
 
     autocomplete_->Hide();
 
@@ -270,19 +267,7 @@ bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
     {
         if (ev.ref_id == TBIDC("close"))
         {
-            if (modified_)
-            {
-                TBMessageWindow *msg_win = new TBMessageWindow(container_->GetInternalWidget(), TBIDC("unsaved_jsmodifications_dialog"));
-                TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
-                settings.dimmer = true;
-                settings.styling = true;
-                msg_win->Show("Unsaved Modifications", "There are unsaved modications.\nDo you wish to discard them and close?", &settings, 640, 360);
-            }
-            else
-            {
-                Close();
-            }
-
+            RequestClose();
         }
 
         if (ev.ref_id == TBIDC("find"))
@@ -318,24 +303,6 @@ bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
         }
     }
 
-    if (ev.type == EVENT_TYPE_CLICK)
-    {
-        if (ev.target->GetID() == TBIDC("unsaved_jsmodifications_dialog"))
-        {
-            if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
-            {
-                Close();
-            }
-            else
-            {
-                SetFocus();
-            }
-
-            return true;
-        }
-
-    }
-
     return false;
 }
 
@@ -525,12 +492,6 @@ void JSResourceEditor::GotoLineNumber(int lineNumber)
 
 }
 
-
-bool JSResourceEditor::HasUnsavedModifications()
-{
-    return modified_;
-}
-
 bool JSResourceEditor::ParseJavascriptToJSON(const char* source, String& json, bool loose)
 {
 
@@ -621,9 +582,7 @@ bool JSResourceEditor::Save()
     file.Write((void*) text.CStr(), text.Length());
     file.Close();
 
-    String filename = GetFileNameAndExtension(fullpath_);
-    button_->SetText(filename.CString());
-    modified_ = false;
+    SetModified(false);
 
     return true;
 

+ 1 - 5
Source/AtomicEditor/Editors/JSResourceEditor.h

@@ -18,7 +18,7 @@ using namespace tb;
 namespace AtomicEditor
 {
 
-class JSAutocomplete;
+class JSAutocomplete; 
 
 class JSResourceEditor: public ResourceEditor, public TBStyleEditTextChangeListener
 {
@@ -45,8 +45,6 @@ public:
 
     void SetFocus();
 
-    bool HasUnsavedModifications();
-
     bool Save();
 
 private:
@@ -64,8 +62,6 @@ private:
     float textDelta_;
     bool textDirty_;
 
-    bool modified_;
-
     int currentFindPos_;
 
 };

+ 60 - 5
Source/AtomicEditor/Editors/ResourceEditor.cpp

@@ -10,6 +10,8 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceEvents.h>
 
+#include <TurboBadger/tb_message_window.h>
+
 #include "ResourceEditor.h"
 
 //#include "../UI/UIMainFrame.h"
@@ -32,17 +34,50 @@ public:
         button_->SetValue(value);
     }
 
+    bool RequestClose()
+    {
+        if (editor_->HasUnsavedModifications())
+        {
+            TBMessageWindow *msg_win = new TBMessageWindow(this, TBIDC("unsaved_modifications_dialog"));
+            TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
+            settings.dimmer = true;
+            settings.styling = true;
+            msg_win->Show("Unsaved Modifications", "There are unsaved modifications.\nDo you wish to discard them and close?", &settings, 640, 360);
+            return false;
+        }
+        else
+        {
+            editor_->Close();
+            return true;
+        }
+    }
+
     bool OnEvent(const TBWidgetEvent &ev)
     {
         if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
         {
-            if (ev.target->GetID() == TBIDC("tabclose"))
+            if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
             {
-                container_->OnEvent(ev);
-                editor_->Close();
+                if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
+                {
+                    container_->OnEvent(ev);
+                    editor_->Close();
+                }
+                else
+                {
+                    SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
+                }
                 return true;
             }
-            else
+            if (ev.target->GetID() == TBIDC("tabclose"))
+            {
+                if (RequestClose())
+                {
+                    container_->OnEvent(ev);
+                    return true;
+                }
+            }
+            else 
             {
                 TBWidgetEvent nevent = ev;
                 nevent.target = this;
@@ -56,7 +91,7 @@ public:
 
 ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
     Object(context), fullpath_(fullpath), container_(container),
-    editorTabLayout_(0), rootContentWidget_(0), button_(0)
+    editorTabLayout_(0), rootContentWidget_(0), button_(0), modified_(false)
 {
 
     String filename = GetFileNameAndExtension(fullpath_);
@@ -112,6 +147,11 @@ void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventDa
     */
 }
 
+void ResourceEditor::RequestClose()
+{
+    editorTabLayout_->RequestClose();
+}
+
 void ResourceEditor::Close(bool navigateToAvailableResource)
 {
     // keep us alive through the close
@@ -132,4 +172,19 @@ void ResourceEditor::InvokeShortcut(const String& shortcut)
     OnEvent(ev);
 }
 
+void ResourceEditor::SetModified(bool modified)
+{
+    if (modified)
+    {
+        String filename = GetFileNameAndExtension(fullpath_);
+        filename += "*";
+        button_->SetText(filename.CString());
+    }
+    else
+    {
+        String filename = GetFileNameAndExtension(fullpath_);
+        button_->SetText(filename.CString());
+    }
+}
+
 }

+ 7 - 1
Source/AtomicEditor/Editors/ResourceEditor.h

@@ -32,7 +32,7 @@ public:
 
     UIButton* GetButton() { return button_; }
 
-    virtual bool HasUnsavedModifications() { return false; }
+    virtual bool HasUnsavedModifications() { return modified_; }
 
     virtual void SetFocus() {}
     virtual void Close(bool navigateToAvailableResource = true);
@@ -52,10 +52,16 @@ public:
 
     void InvokeShortcut(const String& shortcut);
 
+    void RequestClose();
+
+    void SetModified(bool modified);
+
 protected:
 
     String fullpath_;
 
+    bool modified_;
+
     EditorTabLayout* editorTabLayout_;
 
     SharedPtr<UITabContainer> container_;

+ 8 - 0
Source/AtomicEditor/Editors/SceneEditor3D/SceneEditor3D.cpp

@@ -139,6 +139,12 @@ bool SceneEditor3D::OnEvent(const TBWidgetEvent &ev)
                 eventData[EditorActiveNodeChange::P_NODE] = pasteNode;
                 SendEvent(E_EDITORACTIVENODECHANGE, eventData);
             }
+        } 
+        else if (ev.ref_id == TBIDC("close"))
+        {
+            //Don't check for unsaved changes yet
+            Close();
+            //RequestClose();
         }
     }
 
@@ -251,6 +257,8 @@ bool SceneEditor3D::Save()
         file.Close();
     }
 
+    SetModified(false);
+
     return true;
 
 }