Shortcuts.ts 4.6 KB

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