Shortcuts.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. invokePauseOrResumePlayer() {
  56. if (Atomic.editorMode.isPlayerEnabled()) {
  57. this.sendEvent("IPCPlayerPauseResumeRequest");
  58. }
  59. }
  60. invokeStepPausedPlayer() {
  61. if (Atomic.editorMode.isPlayerEnabled()) {
  62. this.sendEvent("IPCPlayerPauseStepRequest");
  63. }
  64. }
  65. invokeFormatCode() {
  66. var editor = EditorUI.getCurrentResourceEditor();
  67. if (editor && editor.typeName == "JSResourceEditor") {
  68. (<Editor.JSResourceEditor>editor).formatCode();
  69. }
  70. }
  71. invokeFileClose() {
  72. this.invokeResourceFrameShortcut("close");
  73. }
  74. invokeFileSave() {
  75. this.sendEvent(EditorEvents.SaveResource);
  76. }
  77. invokeUndo() {
  78. this.invokeResourceFrameShortcut("undo");
  79. }
  80. invokeRedo() {
  81. this.invokeResourceFrameShortcut("redo");
  82. }
  83. invokeCut() {
  84. this.invokeResourceFrameShortcut("cut");
  85. }
  86. invokeCopy() {
  87. this.invokeResourceFrameShortcut("copy");
  88. }
  89. invokePaste() {
  90. this.invokeResourceFrameShortcut("paste");
  91. }
  92. invokeFrameSelected() {
  93. this.invokeResourceFrameShortcut("frameselected");
  94. }
  95. invokeSelectAll() {
  96. this.invokeResourceFrameShortcut("selectall");
  97. }
  98. invokeGizmoEditModeChanged(mode: Editor.EditMode) {
  99. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  100. }
  101. toggleGizmoAxisMode() {
  102. var editor = EditorUI.getCurrentResourceEditor();
  103. if (editor && editor instanceof Editor.SceneEditor3D) {
  104. var mode = editor.getGizmo().axisMode ? Editor.AXIS_WORLD : Editor.AXIS_LOCAL;
  105. this.sendEvent("GizmoAxisModeChanged", { mode: mode });
  106. }
  107. }
  108. invokeResourceFrameShortcut(shortcut: string) {
  109. if (!ToolCore.toolSystem.project) return;
  110. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  111. if (resourceFrame) {
  112. resourceFrame.invokeShortcut(shortcut);
  113. }
  114. }
  115. handleKeyDown(ev: Atomic.KeyDownEvent) {
  116. // if the right mouse buttons isn't down
  117. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  118. // TODO: Make these customizable
  119. if (!Atomic.ui.focusedWidget && !this.cmdKeyDown()) {
  120. if (ev.key == Atomic.KEY_W) {
  121. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  122. } else if (ev.key == Atomic.KEY_E) {
  123. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  124. } else if (ev.key == Atomic.KEY_R) {
  125. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  126. } else if (ev.key == Atomic.KEY_X) {
  127. this.toggleGizmoAxisMode();
  128. } else if (ev.key == Atomic.KEY_F) {
  129. this.invokeFrameSelected();
  130. }
  131. }
  132. }
  133. }
  134. cmdKeyDown(): boolean {
  135. var cmdKey;
  136. if (Atomic.platform == "MacOSX") {
  137. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  138. } else {
  139. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  140. }
  141. return cmdKey;
  142. }
  143. // global shortcut handler
  144. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  145. var cmdKey = this.cmdKeyDown();
  146. if (cmdKey) {
  147. if (ev.key == Atomic.KEY_S) {
  148. this.invokeFileSave();
  149. }
  150. else if (ev.key == Atomic.KEY_W) {
  151. this.invokeFileClose();
  152. }
  153. else if (ev.key == Atomic.KEY_I) {
  154. this.invokeFormatCode();
  155. }
  156. else if (ev.key == Atomic.KEY_P) {
  157. this.invokePlayOrStopPlayer();
  158. } else if (ev.key == Atomic.KEY_B) {
  159. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  160. EditorUI.getModelOps().showBuildSettings();
  161. } else {
  162. EditorUI.getModelOps().showBuild();
  163. }
  164. }
  165. else if (ev.key == Atomic.KEY_U) {
  166. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  167. this.invokeStepPausedPlayer();
  168. } else {
  169. this.invokePauseOrResumePlayer();
  170. }
  171. }
  172. }
  173. }
  174. }
  175. export = Shortcuts;