Shortcuts.ts 4.7 KB

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