Shortcuts.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorEvents = require("../editor/EditorEvents");
  23. import EditorUI = require("./EditorUI");
  24. import Preferences = require("editor/Preferences");
  25. class Shortcuts extends Atomic.ScriptObject {
  26. constructor() {
  27. super();
  28. this.subscribeToEvent("UIShortcut", (ev: Atomic.UIShortcutEvent) => this.handleUIShortcut(ev));
  29. this.subscribeToEvent("KeyDown", (ev: Atomic.KeyDownEvent) => this.handleKeyDown(ev));
  30. }
  31. //this should be moved somewhere else...
  32. invokePlayOrStopPlayer(debug:boolean = false) {
  33. this.sendEvent(EditorEvents.SaveAllResources);
  34. if (Atomic.editorMode.isPlayerEnabled()) {
  35. this.sendEvent("IPCPlayerExitRequest");
  36. } else {
  37. var playerWindow = Preferences.getInstance().playerWindow;
  38. if (playerWindow) {
  39. if ((playerWindow.monitor + 1) > Atomic.graphics.getNumMonitors()) {
  40. //will use default settings if monitor is not available
  41. var args = "--resizable";
  42. Atomic.editorMode.playProject(args, debug);
  43. } else {
  44. var args = "--windowposx " + playerWindow.x + " --windowposy " + playerWindow.y + " --windowwidth " + playerWindow.width + " --windowheight " + playerWindow.height + " --resizable" + " --fromEditorPlay";
  45. if (playerWindow.maximized) {
  46. args += " --maximize";
  47. }
  48. Atomic.editorMode.playProject(args, debug);
  49. }
  50. } else {
  51. Atomic.editorMode.playProject("", debug);
  52. }
  53. }
  54. }
  55. invokeFormatCode() {
  56. var editor = EditorUI.getCurrentResourceEditor();
  57. if (editor && editor.typeName == "JSResourceEditor") {
  58. (<Editor.JSResourceEditor>editor).formatCode();
  59. }
  60. }
  61. invokeFileClose() {
  62. this.invokeResourceFrameShortcut("close");
  63. }
  64. invokeFileSave() {
  65. this.sendEvent(EditorEvents.SaveResource);
  66. }
  67. invokeUndo() {
  68. this.invokeResourceFrameShortcut("undo");
  69. }
  70. invokeRedo() {
  71. this.invokeResourceFrameShortcut("redo");
  72. }
  73. invokeCut() {
  74. this.invokeResourceFrameShortcut("cut");
  75. }
  76. invokeCopy() {
  77. this.invokeResourceFrameShortcut("copy");
  78. }
  79. invokePaste() {
  80. this.invokeResourceFrameShortcut("paste");
  81. }
  82. invokeFrameSelected() {
  83. this.invokeResourceFrameShortcut("frameselected");
  84. }
  85. invokeSelectAll() {
  86. this.invokeResourceFrameShortcut("selectall");
  87. }
  88. invokeGizmoEditModeChanged(mode: Editor.EditMode) {
  89. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  90. }
  91. toggleGizmoAxisMode() {
  92. var editor = EditorUI.getCurrentResourceEditor();
  93. if (editor && editor instanceof Editor.SceneEditor3D) {
  94. var mode = editor.getGizmo().axisMode ? Editor.AXIS_WORLD : Editor.AXIS_LOCAL;
  95. this.sendEvent("GizmoAxisModeChanged", { mode: mode });
  96. }
  97. }
  98. invokeResourceFrameShortcut(shortcut: string) {
  99. if (!ToolCore.toolSystem.project) return;
  100. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  101. if (resourceFrame) {
  102. resourceFrame.invokeShortcut(shortcut);
  103. }
  104. }
  105. handleKeyDown(ev: Atomic.KeyDownEvent) {
  106. // if the right mouse buttons isn't down
  107. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  108. // TODO: Make these customizable
  109. if (!Atomic.ui.focusedWidget && !this.cmdKeyDown()) {
  110. if (ev.key == Atomic.KEY_W) {
  111. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  112. } else if (ev.key == Atomic.KEY_E) {
  113. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  114. } else if (ev.key == Atomic.KEY_R) {
  115. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  116. } else if (ev.key == Atomic.KEY_X) {
  117. this.toggleGizmoAxisMode();
  118. } else if (ev.key == Atomic.KEY_F) {
  119. this.invokeFrameSelected();
  120. }
  121. }
  122. }
  123. }
  124. cmdKeyDown(): boolean {
  125. var cmdKey;
  126. if (Atomic.platform == "MacOSX") {
  127. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  128. } else {
  129. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  130. }
  131. return cmdKey;
  132. }
  133. // global shortcut handler
  134. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  135. var cmdKey = this.cmdKeyDown();
  136. if (cmdKey) {
  137. if (ev.key == Atomic.KEY_S) {
  138. this.invokeFileSave();
  139. }
  140. else if (ev.key == Atomic.KEY_W) {
  141. this.invokeFileClose();
  142. }
  143. else if (ev.key == Atomic.KEY_I) {
  144. this.invokeFormatCode();
  145. }
  146. else if (ev.key == Atomic.KEY_P) {
  147. this.invokePlayOrStopPlayer();
  148. //if shift is pressed
  149. } else if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  150. if (ev.key == Atomic.KEY_B) {
  151. EditorUI.getModelOps().showBuildSettings();
  152. }
  153. } else if (ev.key == Atomic.KEY_B) {
  154. EditorUI.getModelOps().showBuild();
  155. }
  156. }
  157. }
  158. }
  159. export = Shortcuts;