Shortcuts.ts 5.6 KB

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