Shortcuts.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if (playerWindow.width == 0 || playerWindow.height == 0) {
  45. playerWindow.width = Atomic.graphics.width * .75;
  46. // 16:9
  47. playerWindow.height = playerWindow.width * .56;
  48. let pos = Atomic.graphics.windowPosition;
  49. playerWindow.x = pos[0] + (Atomic.graphics.width / 2 - playerWindow.width / 2);
  50. playerWindow.y = pos[1] + (Atomic.graphics.height / 2 - playerWindow.height / 2);
  51. // if too small a window, use default (which maximizes)
  52. if (playerWindow.width < 480) {
  53. playerWindow.width = playerWindow.height = playerWindow.x = playerWindow.y = 0;
  54. }
  55. }
  56. var args = "--windowposx " + playerWindow.x + " --windowposy " + playerWindow.y + " --windowwidth " + playerWindow.width + " --windowheight " + playerWindow.height + " --resizable" + " --fromEditorPlay";
  57. if (playerWindow.maximized) {
  58. args += " --maximize";
  59. }
  60. Atomic.editorMode.playProject(args, debug);
  61. }
  62. } else {
  63. Atomic.editorMode.playProject("", debug);
  64. }
  65. }
  66. }
  67. invokePauseOrResumePlayer() {
  68. if (Atomic.editorMode.isPlayerEnabled()) {
  69. this.sendEvent("IPCPlayerPauseResumeRequest");
  70. }
  71. }
  72. invokeStepPausedPlayer() {
  73. if (Atomic.editorMode.isPlayerEnabled()) {
  74. this.sendEvent("IPCPlayerPauseStepRequest");
  75. }
  76. }
  77. invokeFormatCode() {
  78. var editor = EditorUI.getCurrentResourceEditor();
  79. if (editor && editor.typeName == "JSResourceEditor") {
  80. (<Editor.JSResourceEditor>editor).formatCode();
  81. }
  82. }
  83. invokeFileClose() {
  84. this.invokeResourceFrameShortcut("close");
  85. }
  86. invokeFileSave() {
  87. this.sendEvent(EditorEvents.SaveResource);
  88. }
  89. invokeUndo() {
  90. this.invokeResourceFrameShortcut("undo");
  91. }
  92. invokeRedo() {
  93. this.invokeResourceFrameShortcut("redo");
  94. }
  95. invokeCut() {
  96. this.invokeResourceFrameShortcut("cut");
  97. }
  98. invokeCopy() {
  99. this.invokeResourceFrameShortcut("copy");
  100. }
  101. invokePaste() {
  102. this.invokeResourceFrameShortcut("paste");
  103. }
  104. invokeFrameSelected() {
  105. this.invokeResourceFrameShortcut("frameselected");
  106. }
  107. invokeSelectAll() {
  108. this.invokeResourceFrameShortcut("selectall");
  109. }
  110. invokeGizmoEditModeChanged(mode: Editor.EditMode) {
  111. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  112. }
  113. toggleGizmoAxisMode() {
  114. var editor = EditorUI.getCurrentResourceEditor();
  115. if (editor && editor instanceof Editor.SceneEditor3D) {
  116. var mode = editor.getGizmo().axisMode ? Editor.AXIS_WORLD : Editor.AXIS_LOCAL;
  117. this.sendEvent("GizmoAxisModeChanged", { mode: mode });
  118. }
  119. }
  120. invokeResourceFrameShortcut(shortcut: Editor.EditorShortcutType) {
  121. if (!ToolCore.toolSystem.project) return;
  122. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  123. if (resourceFrame) {
  124. resourceFrame.invokeShortcut(shortcut);
  125. }
  126. }
  127. handleKeyDown(ev: Atomic.KeyDownEvent) {
  128. // if the right mouse buttons isn't down
  129. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  130. // TODO: Make these customizable
  131. if (!Atomic.ui.focusedWidget && !this.cmdKeyDown()) {
  132. if (ev.key == Atomic.KEY_W) {
  133. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  134. } else if (ev.key == Atomic.KEY_E) {
  135. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  136. } else if (ev.key == Atomic.KEY_R) {
  137. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  138. } else if (ev.key == Atomic.KEY_X) {
  139. this.toggleGizmoAxisMode();
  140. } else if (ev.key == Atomic.KEY_F) {
  141. this.invokeFrameSelected();
  142. }
  143. }
  144. }
  145. }
  146. cmdKeyDown(): boolean {
  147. var cmdKey;
  148. if (Atomic.platform == "MacOSX") {
  149. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  150. } else {
  151. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  152. }
  153. return cmdKey;
  154. }
  155. // global shortcut handler
  156. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  157. var cmdKey = this.cmdKeyDown();
  158. if (cmdKey) {
  159. if (ev.key == Atomic.KEY_S) {
  160. this.invokeFileSave();
  161. }
  162. else if (ev.key == Atomic.KEY_W) {
  163. this.invokeFileClose();
  164. }
  165. else if (ev.key == Atomic.KEY_I) {
  166. this.invokeFormatCode();
  167. }
  168. else if (ev.key == Atomic.KEY_P) {
  169. this.invokePlayOrStopPlayer();
  170. } else if (ev.key == Atomic.KEY_B) {
  171. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  172. EditorUI.getModelOps().showBuildSettings();
  173. } else {
  174. EditorUI.getModelOps().showBuild();
  175. }
  176. }
  177. else if (ev.key == Atomic.KEY_U) {
  178. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  179. this.invokeStepPausedPlayer();
  180. } else {
  181. this.invokePauseOrResumePlayer();
  182. }
  183. }
  184. }
  185. }
  186. }
  187. export = Shortcuts;