Shortcuts.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. invokePlay() {
  18. this.sendEvent(EditorEvents.SaveAllResources);
  19. var playerWindow = Preferences.getInstance().playerWindow;
  20. if (playerWindow) {
  21. var args = "--windowposx " + playerWindow.x + " --windowposy " + playerWindow.y + " --windowwidth " + playerWindow.width + " --windowheight " + playerWindow.height;
  22. Atomic.editorMode.playProject(args, false);
  23. } else {
  24. Atomic.editorMode.playProject("");
  25. }
  26. }
  27. invokePlayDebug() {
  28. this.sendEvent(EditorEvents.SaveAllResources);
  29. var playerWindow = Preferences.getInstance().playerWindow;
  30. if (playerWindow) {
  31. var args = "--windowposx " + playerWindow.x + " --windowposy " + playerWindow.y + " --windowwidth " + playerWindow.width + " --windowheight " + playerWindow.height;
  32. Atomic.editorMode.playProject(args, true);
  33. } else {
  34. Atomic.editorMode.playProjectDebug();
  35. }
  36. }
  37. invokeFormatCode() {
  38. var editor = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  39. if (editor && editor.typeName == "JSResourceEditor") {
  40. (<Editor.JSResourceEditor>editor).formatCode();
  41. }
  42. }
  43. invokeFileClose() {
  44. this.invokeResourceFrameShortcut("close");
  45. }
  46. invokeFileSave() {
  47. this.sendEvent(EditorEvents.SaveResource);
  48. }
  49. invokeUndo() {
  50. this.invokeResourceFrameShortcut("undo");
  51. }
  52. invokeRedo() {
  53. this.invokeResourceFrameShortcut("redo");
  54. }
  55. invokeCut() {
  56. this.invokeResourceFrameShortcut("cut");
  57. }
  58. invokeCopy() {
  59. this.invokeResourceFrameShortcut("copy");
  60. }
  61. invokePaste() {
  62. this.invokeResourceFrameShortcut("paste");
  63. }
  64. invokeSelectAll() {
  65. this.invokeResourceFrameShortcut("selectall");
  66. }
  67. invokeGizmoEditModeChanged(mode:Editor.EditMode) {
  68. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  69. }
  70. invokeGizmoAxisModeChanged(mode:Editor.AxisMode, toggle:boolean = false) {
  71. this.sendEvent("GizmoAxisModeChanged", { mode: mode, toggle: toggle });
  72. }
  73. invokeResourceFrameShortcut(shortcut: string) {
  74. if (!ToolCore.toolSystem.project) return;
  75. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  76. if (resourceFrame) {
  77. resourceFrame.invokeShortcut(shortcut);
  78. }
  79. }
  80. handleKeyDown(ev: Atomic.KeyDownEvent) {
  81. // if the right mouse buttons isn't down
  82. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  83. // TODO: Make these customizable
  84. if (ev.key == Atomic.KEY_W) {
  85. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  86. } else if (ev.key == Atomic.KEY_E) {
  87. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  88. } else if (ev.key == Atomic.KEY_R) {
  89. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  90. } else if (ev.key == Atomic.KEY_X) {
  91. this.invokeGizmoAxisModeChanged(Editor.AXIS_WORLD, true);
  92. }
  93. }
  94. }
  95. // global shortcut handler
  96. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  97. var cmdKey;
  98. if (Atomic.platform == "MacOSX") {
  99. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  100. } else {
  101. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  102. }
  103. if (cmdKey) {
  104. if (ev.key == Atomic.KEY_S) {
  105. this.invokeFileSave();
  106. }
  107. else if (ev.key == Atomic.KEY_W) {
  108. this.invokeFileClose();
  109. }
  110. else if (ev.key == Atomic.KEY_I) {
  111. this.invokeFormatCode();
  112. }
  113. else if (ev.key == Atomic.KEY_P) {
  114. this.invokePlay();
  115. //if shift is pressed
  116. } else if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  117. if (ev.key == Atomic.KEY_B) {
  118. EditorUI.getModelOps().showBuildSettings();
  119. }
  120. } else if (ev.key == Atomic.KEY_B) {
  121. EditorUI.getModelOps().showBuild();
  122. }
  123. }
  124. }
  125. }
  126. export = Shortcuts;