Shortcuts.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import EditorEvents = require("../editor/EditorEvents");
  8. import EditorUI = require("./EditorUI");
  9. import Preferences = require("editor/Preferences");
  10. class Shortcuts extends Atomic.ScriptObject {
  11. constructor() {
  12. super();
  13. this.subscribeToEvent("UIShortcut", (ev: Atomic.UIShortcutEvent) => this.handleUIShortcut(ev));
  14. this.subscribeToEvent("KeyDown", (ev: Atomic.KeyDownEvent) => this.handleKeyDown(ev));
  15. }
  16. //this should be moved somewhere else...
  17. invokePlayOrStopPlayer(debug:boolean = false) {
  18. this.sendEvent(EditorEvents.SaveAllResources);
  19. if (Atomic.editorMode.isPlayerEnabled()) {
  20. this.sendEvent("IPCPlayerExitRequest");
  21. } else {
  22. var playerWindow = Preferences.getInstance().playerWindow;
  23. if (playerWindow) {
  24. if ((playerWindow.monitor + 1) > Atomic.graphics.getNumMonitors()) {
  25. //will use default settings if monitor is not available
  26. var args = "--resizable";
  27. Atomic.editorMode.playProject(args, debug);
  28. } else {
  29. var args = "--windowposx " + playerWindow.x + " --windowposy " + playerWindow.y + " --windowwidth " + playerWindow.width + " --windowheight " + playerWindow.height + " --resizable" + " --fromEditorPlay";
  30. if (playerWindow.maximized) {
  31. args += " --maximize";
  32. }
  33. Atomic.editorMode.playProject(args, debug);
  34. }
  35. } else {
  36. Atomic.editorMode.playProject("", debug);
  37. }
  38. }
  39. }
  40. invokePauseOrResumePlayer() {
  41. if (Atomic.editorMode.isPlayerEnabled()) {
  42. this.sendEvent("IPCPlayerPauseResumeRequest");
  43. }
  44. }
  45. invokeStepPausedPlayer() {
  46. if (Atomic.editorMode.isPlayerEnabled()) {
  47. this.sendEvent("IPCPlayerPauseStepRequest");
  48. }
  49. }
  50. invokeFormatCode() {
  51. var editor = EditorUI.getCurrentResourceEditor();
  52. if (editor && editor.typeName == "JSResourceEditor") {
  53. (<Editor.JSResourceEditor>editor).formatCode();
  54. }
  55. }
  56. invokeFileClose() {
  57. this.invokeResourceFrameShortcut("close");
  58. }
  59. invokeFileSave() {
  60. this.sendEvent(EditorEvents.SaveResource);
  61. }
  62. invokeUndo() {
  63. this.invokeResourceFrameShortcut("undo");
  64. }
  65. invokeRedo() {
  66. this.invokeResourceFrameShortcut("redo");
  67. }
  68. invokeCut() {
  69. this.invokeResourceFrameShortcut("cut");
  70. }
  71. invokeCopy() {
  72. this.invokeResourceFrameShortcut("copy");
  73. }
  74. invokePaste() {
  75. this.invokeResourceFrameShortcut("paste");
  76. }
  77. invokeFrameSelected() {
  78. this.invokeResourceFrameShortcut("frameselected");
  79. }
  80. invokeSelectAll() {
  81. this.invokeResourceFrameShortcut("selectall");
  82. }
  83. invokeGizmoEditModeChanged(mode: Editor.EditMode) {
  84. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  85. }
  86. toggleGizmoAxisMode() {
  87. var editor = EditorUI.getCurrentResourceEditor();
  88. if (editor && editor instanceof Editor.SceneEditor3D) {
  89. var mode = editor.getGizmo().axisMode ? Editor.AXIS_WORLD : Editor.AXIS_LOCAL;
  90. this.sendEvent("GizmoAxisModeChanged", { mode: mode });
  91. }
  92. }
  93. invokeResourceFrameShortcut(shortcut: string) {
  94. if (!ToolCore.toolSystem.project) return;
  95. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  96. if (resourceFrame) {
  97. resourceFrame.invokeShortcut(shortcut);
  98. }
  99. }
  100. handleKeyDown(ev: Atomic.KeyDownEvent) {
  101. // if the right mouse buttons isn't down
  102. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  103. // TODO: Make these customizable
  104. if (!Atomic.ui.focusedWidget && !this.cmdKeyDown()) {
  105. if (ev.key == Atomic.KEY_W) {
  106. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  107. } else if (ev.key == Atomic.KEY_E) {
  108. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  109. } else if (ev.key == Atomic.KEY_R) {
  110. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  111. } else if (ev.key == Atomic.KEY_X) {
  112. this.toggleGizmoAxisMode();
  113. } else if (ev.key == Atomic.KEY_F) {
  114. this.invokeFrameSelected();
  115. }
  116. }
  117. }
  118. }
  119. cmdKeyDown(): boolean {
  120. var cmdKey;
  121. if (Atomic.platform == "MacOSX") {
  122. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  123. } else {
  124. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  125. }
  126. return cmdKey;
  127. }
  128. // global shortcut handler
  129. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  130. var cmdKey = this.cmdKeyDown();
  131. if (cmdKey) {
  132. if (ev.key == Atomic.KEY_S) {
  133. this.invokeFileSave();
  134. }
  135. else if (ev.key == Atomic.KEY_W) {
  136. this.invokeFileClose();
  137. }
  138. else if (ev.key == Atomic.KEY_I) {
  139. this.invokeFormatCode();
  140. }
  141. else if (ev.key == Atomic.KEY_P) {
  142. this.invokePlayOrStopPlayer();
  143. } else if (ev.key == Atomic.KEY_B) {
  144. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  145. EditorUI.getModelOps().showBuildSettings();
  146. } else {
  147. EditorUI.getModelOps().showBuild();
  148. }
  149. }
  150. else if (ev.key == Atomic.KEY_U) {
  151. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  152. this.invokeStepPausedPlayer();
  153. } else {
  154. this.invokePauseOrResumePlayer();
  155. }
  156. }
  157. }
  158. }
  159. }
  160. export = Shortcuts;