Shortcuts.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. invokeSelectAll() {
  51. this.invokeResourceFrameShortcut("selectall");
  52. }
  53. invokeGizmoEditModeChanged(mode:Editor.EditMode) {
  54. this.sendEvent("GizmoEditModeChanged", { mode: mode });
  55. }
  56. invokeGizmoAxisModeChanged(mode:Editor.AxisMode, toggle:boolean = false) {
  57. this.sendEvent("GizmoAxisModeChanged", { mode: mode, toggle: toggle });
  58. }
  59. invokeResourceFrameShortcut(shortcut: string) {
  60. if (!ToolCore.toolSystem.project) return;
  61. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  62. if (resourceFrame) {
  63. resourceFrame.invokeShortcut(shortcut);
  64. }
  65. }
  66. handleKeyDown(ev: Atomic.KeyDownEvent) {
  67. // if the right mouse buttons isn't down
  68. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  69. // TODO: Make these customizable
  70. if (ev.key == Atomic.KEY_W) {
  71. this.invokeGizmoEditModeChanged(Editor.EDIT_MOVE);
  72. } else if (ev.key == Atomic.KEY_E) {
  73. this.invokeGizmoEditModeChanged(Editor.EDIT_ROTATE);
  74. } else if (ev.key == Atomic.KEY_R) {
  75. this.invokeGizmoEditModeChanged(Editor.EDIT_SCALE);
  76. } else if (ev.key == Atomic.KEY_X) {
  77. this.invokeGizmoAxisModeChanged(Editor.AXIS_WORLD, true);
  78. }
  79. }
  80. }
  81. // global shortcut handler
  82. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  83. var cmdKey;
  84. if (Atomic.platform == "MacOSX") {
  85. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  86. } else {
  87. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  88. }
  89. if (cmdKey) {
  90. if (ev.key == Atomic.KEY_S) {
  91. this.invokeFileSave();
  92. }
  93. else if (ev.key == Atomic.KEY_W) {
  94. this.invokeFileClose();
  95. }
  96. else if (ev.key == Atomic.KEY_I) {
  97. this.invokeFormatCode();
  98. }
  99. else if (ev.key == Atomic.KEY_P) {
  100. this.invokePlay();
  101. //if shift is pressed
  102. } else if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  103. if (ev.key == Atomic.KEY_B) {
  104. EditorUI.getModelOps().showBuildSettings();
  105. }
  106. } else if (ev.key == Atomic.KEY_B) {
  107. EditorUI.getModelOps().showBuild();
  108. }
  109. }
  110. }
  111. }
  112. export = Shortcuts;