Shortcuts.ts 8.3 KB

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