Shortcuts.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.getMonitorsNumber()) {
  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";
  30. if (playerWindow.maximized) {
  31. args += " --maximize";
  32. }
  33. if (playerWindow.centered) {
  34. args += " --center";
  35. }
  36. Atomic.editorMode.playProject(args, debug);
  37. }
  38. } else {
  39. Atomic.editorMode.playProject("", debug);
  40. }
  41. }
  42. }
  43. invokeFormatCode() {
  44. var editor = EditorUI.getCurrentResourceEditor();
  45. if (editor && editor.typeName == "JSResourceEditor") {
  46. (<Editor.JSResourceEditor>editor).formatCode();
  47. }
  48. }
  49. invokeFileClose() {
  50. this.invokeResourceFrameShortcut("close");
  51. }
  52. invokeFileSave() {
  53. this.sendEvent(EditorEvents.SaveResource);
  54. }
  55. invokeUndo() {
  56. this.invokeResourceFrameShortcut("undo");
  57. }
  58. invokeRedo() {
  59. this.invokeResourceFrameShortcut("redo");
  60. }
  61. invokeCut() {
  62. this.invokeResourceFrameShortcut("cut");
  63. }
  64. invokeCopy() {
  65. this.invokeResourceFrameShortcut("copy");
  66. }
  67. invokePaste() {
  68. this.invokeResourceFrameShortcut("paste");
  69. }
  70. invokeFrameSelected() {
  71. this.invokeResourceFrameShortcut("frameselected");
  72. }
  73. invokeSelectAll() {
  74. this.invokeResourceFrameShortcut("selectall");
  75. }
  76. invokeGizmoEditModeChanged(mode: Editor.EditMode) {
  77. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  78. }
  79. toggleGizmoAxisMode() {
  80. var editor = EditorUI.getCurrentResourceEditor();
  81. if (editor && editor instanceof Editor.SceneEditor3D) {
  82. var mode = editor.getGizmo().axisMode ? Editor.AXIS_WORLD : Editor.AXIS_LOCAL;
  83. this.sendEvent("GizmoAxisModeChanged", { mode: mode });
  84. }
  85. }
  86. invokeResourceFrameShortcut(shortcut: string) {
  87. if (!ToolCore.toolSystem.project) return;
  88. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  89. if (resourceFrame) {
  90. resourceFrame.invokeShortcut(shortcut);
  91. }
  92. }
  93. handleKeyDown(ev: Atomic.KeyDownEvent) {
  94. // if the right mouse buttons isn't down
  95. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  96. // TODO: Make these customizable
  97. if (!Atomic.ui.focusedWidget && !this.cmdKeyDown()) {
  98. if (ev.key == Atomic.KEY_W) {
  99. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  100. } else if (ev.key == Atomic.KEY_E) {
  101. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  102. } else if (ev.key == Atomic.KEY_R) {
  103. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  104. } else if (ev.key == Atomic.KEY_X) {
  105. this.toggleGizmoAxisMode();
  106. } else if (ev.key == Atomic.KEY_F) {
  107. this.invokeFrameSelected();
  108. }
  109. }
  110. }
  111. }
  112. cmdKeyDown(): boolean {
  113. var cmdKey;
  114. if (Atomic.platform == "MacOSX") {
  115. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  116. } else {
  117. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  118. }
  119. return cmdKey;
  120. }
  121. // global shortcut handler
  122. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  123. var cmdKey = this.cmdKeyDown();
  124. if (cmdKey) {
  125. if (ev.key == Atomic.KEY_S) {
  126. this.invokeFileSave();
  127. }
  128. else if (ev.key == Atomic.KEY_W) {
  129. this.invokeFileClose();
  130. }
  131. else if (ev.key == Atomic.KEY_I) {
  132. this.invokeFormatCode();
  133. }
  134. else if (ev.key == Atomic.KEY_P) {
  135. this.invokePlayOrStopPlayer();
  136. //if shift is pressed
  137. } else if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  138. if (ev.key == Atomic.KEY_B) {
  139. EditorUI.getModelOps().showBuildSettings();
  140. }
  141. } else if (ev.key == Atomic.KEY_B) {
  142. EditorUI.getModelOps().showBuild();
  143. }
  144. }
  145. }
  146. }
  147. export = Shortcuts;