Shortcuts.ts 8.2 KB

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