Browse Source

Shortcut handling improvements, resurrecting Javascript formatter

Josh Engebretson 10 years ago
parent
commit
1aae2dfd29

+ 2 - 0
Script/AtomicEditor/editor/EditorEvents.ts

@@ -43,6 +43,8 @@ export interface LoadProjectEvent {
 
 }
 
+export const SaveAllResources = "EditorSaveAllResources";
+
 export const SaveResource = "EditorSaveResource";
 export interface SaveResourceEvent {
 

+ 1 - 0
Script/AtomicEditor/tsconfig.json

@@ -28,6 +28,7 @@
         "./ui/ProjectFrameMenu.ts",
         "./ui/ResourceFrame.ts",
         "./ui/ScriptWidget.ts",
+        "./ui/Shortcuts.ts",
         "./ui/UIEvents.ts",
         "./ui/WelcomeFrame.ts",
         "./ui/inspector/ArrayEditWidget.ts",

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

@@ -2,6 +2,7 @@
 import EditorEvents = require("../editor/EditorEvents");
 import MainFrame = require("../ui/MainFrame");
 import ModalOps = require("./modal/ModalOps");
+import Shortcuts = require("./Shortcuts");
 
 // this is designed with public get functions to solve
 // circular dependency issues in TS
@@ -20,6 +21,10 @@ export function getView():Atomic.UIView {
   return editorUI.view;
 }
 
+export function getShortcuts():Shortcuts {
+  return editorUI.shortcuts;
+}
+
 export function initialize() {
   editorUI = new EditorUI();
 }
@@ -56,6 +61,7 @@ class EditorUI extends Atomic.ScriptObject {
     this.mainframe.setSize(graphics.width, graphics.height);
 
     this.modalOps = new ModalOps();
+    this.shortcuts = new Shortcuts();
 
     this.subscribeToEvent(EditorEvents.ModalError, (event:EditorEvents.ModalErrorEvent) => {
       this.showModalError(event.title, event.message);
@@ -72,5 +78,6 @@ class EditorUI extends Atomic.ScriptObject {
   view: Atomic.UIView;
   mainframe: MainFrame;
   modalOps: ModalOps;
+  shortcuts: Shortcuts;
 
 }

+ 16 - 20
Script/AtomicEditor/ui/MainFrameMenu.ts

@@ -13,22 +13,6 @@ class MainFrameMenu extends Atomic.ScriptObject {
         MenuItemSources.createMenuItemSource("menu atomic editor", editorItems);
         MenuItemSources.createMenuItemSource("menu edit", editItems);
         MenuItemSources.createMenuItemSource("menu file", fileItems);
-
-        this.subscribeToEvent("WidgetEvent", (ev: Atomic.UIWidgetEvent) => {
-
-            if (ev.type == Atomic.UI_EVENT_TYPE_SHORTCUT) {
-
-                if (ev.refid == "save") {
-
-                    this.sendEvent(EditorEvents.SaveResource);
-                    return true;
-
-                }
-
-            }
-
-        })
-
     }
 
     handlePopupMenu(target: Atomic.UIWidget, refid: string): boolean {
@@ -45,10 +29,13 @@ class MainFrameMenu extends Atomic.ScriptObject {
         } else if (target.id == "menu edit popup") {
 
             if (refid == "edit play") {
-
-                new ToolCore.PlayCmd().run();
+                EditorUI.getShortcuts().invokePlay();
                 return true;
+            }
 
+            if (refid == "edit format code") {
+              EditorUI.getShortcuts().invokeFormatCode();
+              return true;
             }
 
             return false;
@@ -65,13 +52,21 @@ class MainFrameMenu extends Atomic.ScriptObject {
             }
 
             if (refid == "file save file") {
+                EditorUI.getShortcuts().invokeFileSave();
+                return true;
+            }
 
-                this.sendEvent(EditorEvents.SaveResource);
-
+            if (refid == "file close file") {
+                EditorUI.getShortcuts().invokeFileClose();
                 return true;
+            }
 
+            if (refid == "file save all") {
+                this.sendEvent(EditorEvents.SaveAllResources);
+                return true;
             }
 
+
             return false;
         }
 
@@ -124,5 +119,6 @@ var fileItems = {
     "Close Project": ["file close project"],
     "-2": null,
     "Save File": ["file save file"],
+    "Save All Files": ["file save all"],
     "Close File": ["file close file"]
 }

+ 7 - 7
Script/AtomicEditor/ui/MainToolbar.ts

@@ -1,4 +1,4 @@
-
+import EditorUI = require("./EditorUI");
 
 class MainToolbar extends Atomic.UIWidget {
 
@@ -21,19 +21,19 @@ class MainToolbar extends Atomic.UIWidget {
             if (ev.target.id == "3d_translate" || ev.target.id == "3d_rotate" || ev.target.id == "3d_scale") {
 
                 var mode = 1;
-                if (ev.target.id == "3d_rotate" )
-                  mode = 2;
+                if (ev.target.id == "3d_rotate")
+                    mode = 2;
                 else if (ev.target.id == "3d_scale")
-                  mode = 3;
+                    mode = 3;
 
-                  this.sendEvent("GizmoEditModeChanged", {mode:mode});
+                this.sendEvent("GizmoEditModeChanged", { mode: mode });
 
                 return true;
 
             } else if (ev.target.id == "maintoolbar_play") {
 
-              Atomic.editorMode.playProject();
-              return true;
+                EditorUI.getShortcuts().invokePlay();
+                return true;
 
             }
 

+ 12 - 13
Script/AtomicEditor/ui/ResourceFrame.ts

@@ -28,13 +28,21 @@ class ResourceFrame extends ScriptWidget {
 
     }
 
-    handleSaveResource(Ev: EditorEvents.SaveResourceEvent) {
+    handleSaveResource(ev: EditorEvents.SaveResourceEvent) {
 
         if (this.currentResourceEditor)
             this.currentResourceEditor.save();
 
     }
 
+    handleSaveAllResources(data) {
+
+        for (var i in this.editors) {
+            this.editors[i].save();
+        }
+
+    }
+
     handleEditResource(ev: EditorEvents.EditResourceEvent) {
 
         var path = ev.path;
@@ -162,17 +170,6 @@ class ResourceFrame extends ScriptWidget {
 
     handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
 
-        if (ev.type == Atomic.UI_EVENT_TYPE_SHORTCUT) {
-
-            if (ev.refid == "close") {
-                if (this.currentResourceEditor) {
-                    this.currentResourceEditor.close(true);
-                    return true;
-                }
-            }
-        }
-
-
         if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
             var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
 
@@ -182,7 +179,7 @@ class ResourceFrame extends ScriptWidget {
 
                     if (w.editor.typeName == "SceneEditor3D") {
 
-                        this.sendEvent(EditorEvents.ActiveSceneChange, { scene: (<Editor.SceneEditor3D> w.editor).scene});
+                        this.sendEvent(EditorEvents.ActiveSceneChange, { scene: (<Editor.SceneEditor3D> w.editor).scene });
 
                     }
 
@@ -226,7 +223,9 @@ class ResourceFrame extends ScriptWidget {
 
         this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
         this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
+        this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
         this.subscribeToEvent(EditorEvents.CloseResource, (ev: EditorEvents.CloseResourceEvent) => this.handleCloseResource(ev));
+
         this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
 
 

+ 73 - 0
Script/AtomicEditor/ui/Shortcuts.ts

@@ -0,0 +1,73 @@
+import EditorEvents = require("../editor/EditorEvents");
+import EditorUI = require("./EditorUI");
+
+class Shortcuts extends Atomic.ScriptObject {
+
+    constructor() {
+
+        super();
+
+        this.subscribeToEvent("UIShortcut", (ev: Atomic.UIShortcutEvent) => this.handleUIShortcut(ev));
+
+
+    }
+
+    invokePlay() {
+
+        this.sendEvent(EditorEvents.SaveAllResources);
+        Atomic.editorMode.playProject();
+
+    }
+
+    invokeFormatCode() {
+
+        var editor = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
+
+        if (editor && editor.typeName == "JSResourceEditor") {
+
+            (<Editor.JSResourceEditor>editor).formatCode();
+
+        }
+
+    }
+
+    invokeFileClose() {
+
+        // pretty gross
+        var editor = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
+        editor.close(true);
+
+    }
+
+    invokeFileSave() {
+        this.sendEvent(EditorEvents.SaveResource);
+    }
+
+
+    // global shortcut handler
+    handleUIShortcut(ev: Atomic.UIShortcutEvent) {
+
+        // global shortcuts without qualifiers
+        if (!ev.qualifiers) {
+
+            if (ev.key == Atomic.KEY_S) {
+                this.invokeFileSave();
+            }
+            else if (ev.key == Atomic.KEY_W) {
+                this.invokeFileClose();
+            }
+            else if (ev.key == Atomic.KEY_I) {
+                this.invokeFormatCode();
+            }
+
+            else if (ev.key == Atomic.KEY_P) {
+                this.invokePlay();
+            }
+
+        }
+
+    }
+
+}
+
+export = Shortcuts;

+ 2 - 0
Script/TypeScript/Atomic.d.ts

@@ -7396,6 +7396,7 @@ declare module Atomic {
       fontDescription: UIFontDescription;
       gravity: UI_GRAVITY;
       value: number;
+      focus: boolean;
       visibility: UI_WIDGET_VISIBILITY;
       stateRaw: number;
       dragObject: UIDragObject;
@@ -7428,6 +7429,7 @@ declare module Atomic {
       setValue(value: number): void;
       getValue(): number;
       setFocus(): void;
+      getFocus(): boolean;
       // Set focus to first widget which accepts it
       setFocusRecursive(): void;
       onFocusChanged(focused: boolean): void;

+ 10 - 0
Script/TypeScript/AtomicWork.d.ts

@@ -32,6 +32,16 @@ declare module Atomic {
 
     }
 
+
+    export interface UIShortcutEvent {
+
+        // keycode
+        key:number;
+        //  Atomic.QUAL_SHIFT, Atomic.QUAL_CTRL, Atomic.QUAL_ALT, Atomic.QUAL_ANY
+        qualifiers:number;
+
+    }
+
     export interface NodeAddedEvent {
 
         scene: Atomic.Scene;

+ 1 - 0
Script/TypeScript/Editor.d.ts

@@ -87,6 +87,7 @@ declare module Editor {
       findTextClose(): void;
       gotoTokenPos(tokenPos: number): void;
       gotoLineNumber(lineNumber: number): void;
+      formatCode(): void;
       setFocus(): void;
       hasUnsavedModifications(): boolean;
       save(): boolean;

+ 7 - 0
Source/Atomic/UI/UIEvents.h

@@ -96,4 +96,11 @@ EVENT(E_POPUPMENUSELECT, PopupMenuSelect)
     PARAM(P_REFID, RefID);             // string tbid
 }
 
+EVENT(E_UISHORTCUT, UIShortcut)
+{
+    PARAM(P_KEY, Key);                    // int
+    PARAM(P_QUALIFIERS, Qualifiers);        // int
+
+}
+
 }

+ 24 - 2
Source/Atomic/UI/UIInput.cpp

@@ -7,6 +7,7 @@ using namespace tb;
 #include "../Input/InputEvents.h"
 
 #include "UI.h"
+#include "UIEvents.h"
 
 namespace Atomic
 {
@@ -185,8 +186,6 @@ static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modif
 #endif
     else if (upper_key == 'P')
         id = TBIDC("play");
-    else if (upper_key == 'I')
-        id = TBIDC("beautify");
     else if (special_key == TB_KEY_PAGE_UP)
         id = TBIDC("prev_doc");
     else if (special_key == TB_KEY_PAGE_DOWN)
@@ -350,6 +349,29 @@ void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
 
     HandleKey(true, keycode, scancode);
 
+    // Send Global Shortcut
+    Input* input = GetSubsystem<Input>();
+
+#ifdef ATOMIC_PLATFORM_WINDOWS
+    bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
+    if (keycode == KEY_LCTRL || keycode == KEY_RCTRL)
+        superdown = false;
+#else
+    bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
+
+    if (keycode == KEY_LGUI || keycode == KEY_RGUI)
+        superdown = false;
+#endif
+
+    if (!superdown)
+        return;
+
+    VariantMap shortcutData;
+    shortcutData[UIShortcut::P_KEY] = keycode;
+    shortcutData[UIShortcut::P_QUALIFIERS] = eventData[P_QUALIFIERS].GetInt();
+
+    SendEvent(E_UISHORTCUT, shortcutData);
+
 }
 
 void UI::HandleKeyUp(StringHash eventType, VariantMap& eventData)

+ 9 - 0
Source/Atomic/UI/UIWidget.cpp

@@ -433,6 +433,15 @@ void UIWidget::SetFocus()
         return;
 
     widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
+
+}
+
+bool UIWidget::GetFocus()
+{
+    if (!widget_)
+        return false;
+
+    return widget_->GetIsFocused();
 }
 
 void UIWidget::SetFocusRecursive()

+ 3 - 0
Source/Atomic/UI/UIWidget.h

@@ -150,6 +150,9 @@ class UIWidget : public Object, public tb::TBWidgetDelegate
     double GetValue();
 
     void SetFocus();
+    bool GetFocus();
+
+
     /// Set focus to first widget which accepts it
     void SetFocusRecursive();
     void OnFocusChanged(bool focused);

+ 24 - 35
Source/AtomicEditorWork/Editors/JSResourceEditor.cpp

@@ -147,6 +147,28 @@ JSResourceEditor::~JSResourceEditor()
 {
 }
 
+void JSResourceEditor::FormatCode()
+{
+
+    TBStr text;
+    styleEdit_->GetText(text);
+
+    if (text.Length())
+    {
+        String output;
+        if (BeautifyJavascript(text.CStr(), output))
+        {
+            if (output.Length())
+            {
+                styleEdit_->selection.SelectAll();
+                styleEdit_->InsertText(output.CString(), output.Length());
+            }
+        }
+    }
+
+
+}
+
 void JSResourceEditor::UpdateLineNumbers()
 {
     if (!styleEdit_)
@@ -259,22 +281,7 @@ bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
 
         }
 
-        if (ev.ref_id == TBIDC("save"))
-        {
-            //TBStr text;
-            //styleEdit_->GetText(text);
-            //File file(context_, fullpath_, FILE_WRITE);
-            //file.Write((void*) text.CStr(), text.Length());
-            //file.Close();
-
-            //String filename = GetFileNameAndExtension(fullpath_);
-            //button_->SetText(filename.CString());
-            //modified_ = false;
-            //SendEvent(E_JAVASCRIPTSAVED);
-
-            return false;
-        }
-        else if (ev.ref_id == TBIDC("find"))
+        if (ev.ref_id == TBIDC("find"))
         {
             //using namespace FindTextOpen;
             //SendEvent(E_FINDTEXTOPEN);
@@ -300,24 +307,6 @@ bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
             finder->Find(text, flags);
             */
         }
-        else if (ev.ref_id == TBIDC("beautify"))
-        {
-            TBStr text;
-            styleEdit_->GetText(text);
-
-            if (text.Length())
-            {
-                String output;
-                if (BeautifyJavascript(text.CStr(), output))
-                {
-                    if (output.Length())
-                    {
-                        styleEdit_->selection.SelectAll();
-                        styleEdit_->InsertText(output.CString(), output.Length());
-                    }
-                }
-            }
-        }
         else if (ev.ref_id == TBIDC("cut") || ev.ref_id == TBIDC("copy") || ev.ref_id == TBIDC("paste")
                  || ev.ref_id == TBIDC("selectall") || ev.ref_id == TBIDC("undo") || ev.ref_id == TBIDC("redo") )
         {
@@ -586,7 +575,7 @@ bool JSResourceEditor::BeautifyJavascript(const char* source, String& output)
     output.Clear();
 
     duk_get_global_string(ctx, "require");
-    duk_push_string(ctx, "AtomicEditor/typescript/modules/jsutils");
+    duk_push_string(ctx, "AtomicEditor/modules/jsutils");
 
     if (duk_pcall(ctx, 1))
     {

+ 2 - 0
Source/AtomicEditorWork/Editors/JSResourceEditor.h

@@ -38,6 +38,8 @@ public:
     void GotoTokenPos(int tokenPos);
     void GotoLineNumber(int lineNumber);
 
+    void FormatCode();
+
     void SetFocus();
 
     bool HasUnsavedModifications();

+ 1 - 13
Source/AtomicEditorWork/Editors/SceneEditor3D/SceneEditor3D.cpp

@@ -104,19 +104,7 @@ bool SceneEditor3D::OnEvent(const TBWidgetEvent &ev)
 
     if (ev.type == EVENT_TYPE_SHORTCUT)
     {
-        /*
-        if (ev.ref_id == TBIDC("save"))
-        {
-            File file(context_);
-            if (file.Open(fullpath_, FILE_WRITE))
-            {
-                scene_->SaveXML(file);
-                file.Close();
-            }
-
-            return true;
-        }
-        else */if (ev.ref_id == TBIDC("copy"))
+        if (ev.ref_id == TBIDC("copy"))
         {
             if (selectedNode_.NotNull())
             {

+ 1 - 1
Source/AtomicEditorWork/Editors/SceneEditor3D/SceneView3D.cpp

@@ -137,7 +137,7 @@ void SceneView3D::Disable()
 
 void SceneView3D::MoveCamera(float timeStep)
 {
-    if (!enabled_)
+    if (!enabled_ && !GetFocus())
         return;
 
     Input* input = GetSubsystem<Input>();