Shortcuts.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorUI = require("./EditorUI");
  23. import Preferences = require("editor/Preferences");
  24. import * as EditorEvents from "../editor/EditorEvents";
  25. class Shortcuts extends Atomic.ScriptObject {
  26. constructor() {
  27. super();
  28. this.subscribeToEvent(Atomic.UIShortcutEvent((ev) => this.handleUIShortcut(ev)));
  29. this.subscribeToEvent(Atomic.KeyDownEvent((ev) => this.handleKeyDown(ev)));
  30. }
  31. //this should be moved somewhere else...
  32. invokePlayOrStopPlayer(debug: boolean = false) {
  33. this.sendEvent(Editor.EditorSaveAllResourcesEventType);
  34. if (Atomic.editorMode.isPlayerEnabled()) {
  35. this.sendEvent(EditorEvents.IPCPlayerExitRequestEventType);
  36. } else {
  37. var playerWindow = Preferences.getInstance().playerWindow;
  38. if (playerWindow) {
  39. if ((playerWindow.monitor + 1) > Atomic.graphics.getNumMonitors()) {
  40. //will use default settings if monitor is not available
  41. var args = "--resizable";
  42. Atomic.editorMode.playProject(args, debug);
  43. } else {
  44. if (playerWindow.width == 0 || playerWindow.height == 0) {
  45. playerWindow.width = Atomic.graphics.width * .75;
  46. // 16:9
  47. playerWindow.height = playerWindow.width * .56;
  48. let pos = Atomic.graphics.windowPosition;
  49. playerWindow.x = pos[0] + (Atomic.graphics.width / 2 - playerWindow.width / 2);
  50. playerWindow.y = pos[1] + (Atomic.graphics.height / 2 - playerWindow.height / 2);
  51. // if too small a window, use default (which maximizes)
  52. if (playerWindow.width < 480) {
  53. playerWindow.width = playerWindow.height = playerWindow.x = playerWindow.y = 0;
  54. }
  55. }
  56. var args = "--windowposx " + playerWindow.x + " --windowposy " + playerWindow.y + " --windowwidth " + playerWindow.width + " --windowheight " + playerWindow.height + " --resizable" + " --fromEditorPlay";
  57. if (playerWindow.maximized) {
  58. args += " --maximize";
  59. }
  60. Atomic.editorMode.playProject(args, debug);
  61. }
  62. } else {
  63. Atomic.editorMode.playProject("", debug);
  64. }
  65. }
  66. }
  67. invokePauseOrResumePlayer() {
  68. if (Atomic.editorMode.isPlayerEnabled()) {
  69. this.sendEvent(EditorEvents.IPCPlayerPauseResumeRequestEventType);
  70. }
  71. }
  72. invokeStepPausedPlayer() {
  73. if (Atomic.editorMode.isPlayerEnabled()) {
  74. this.sendEvent(EditorEvents.IPCPlayerPauseStepRequestEventType);
  75. }
  76. }
  77. invokeFormatCode() {
  78. var editor = EditorUI.getCurrentResourceEditor();
  79. if (editor && editor.typeName == "JSResourceEditor") {
  80. (<Editor.JSResourceEditor>editor).formatCode();
  81. }
  82. }
  83. invokeFileClose() {
  84. this.invokeResourceFrameShortcut("close");
  85. }
  86. invokeFileSave() {
  87. this.sendEvent(Editor.EditorSaveResourceEventType);
  88. }
  89. invokeUndo() {
  90. this.invokeResourceFrameShortcut("undo");
  91. }
  92. invokeRedo() {
  93. this.invokeResourceFrameShortcut("redo");
  94. }
  95. invokeCut() {
  96. this.invokeResourceFrameShortcut("cut");
  97. }
  98. invokeCopy() {
  99. this.invokeResourceFrameShortcut("copy");
  100. }
  101. invokePaste() {
  102. this.invokeResourceFrameShortcut("paste");
  103. }
  104. invokeFrameSelected() {
  105. this.invokeResourceFrameShortcut("frameselected");
  106. }
  107. invokeSelectAll() {
  108. this.invokeResourceFrameShortcut("selectall");
  109. }
  110. invokeGizmoEditModeChanged(mode: Editor.EditMode) {
  111. this.sendEvent(Editor.GizmoEditModeChangedEventData({ mode: mode }));
  112. }
  113. toggleGizmoAxisMode() {
  114. var editor = EditorUI.getCurrentResourceEditor();
  115. if (editor && editor instanceof Editor.SceneEditor3D) {
  116. var mode = editor.getGizmo().axisMode ? Editor.AxisMode.AXIS_WORLD : Editor.AxisMode.AXIS_LOCAL;
  117. this.sendEvent(Editor.GizmoAxisModeChangedEventData({ mode: mode }));
  118. }
  119. }
  120. invokeResourceFrameShortcut(shortcut: Editor.EditorShortcutType) {
  121. if (!ToolCore.toolSystem.project) return;
  122. var resourceFrame = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
  123. if (resourceFrame) {
  124. resourceFrame.invokeShortcut(shortcut);
  125. }
  126. }
  127. invokeScreenshot() {
  128. var features = Preferences.getInstance().editorFeatures; // get prefs
  129. var pic_ext = features.screenshotFormat;
  130. var pic_path = features.screenshotPath;
  131. var dx = new Date(); // get the date NOW
  132. var datestring = dx.getFullYear() + "_" + ("0" + (dx.getMonth() + 1 )).slice(-2) + "_" + ("0" + dx.getDate()).slice(-2)
  133. + "_" + ("0" + dx.getHours()).slice(-2) + "_" + ("0" + dx.getMinutes()).slice(-2) + "_" + ("0" + dx.getSeconds()).slice(-2);
  134. pic_path += "/Screenshot_" + datestring + "." + pic_ext; // form filename
  135. var myimage = new Atomic.Image; // make an image to save
  136. if (Atomic.graphics.takeScreenShot(myimage)) { // take the screenshot
  137. var saved_pic = false;
  138. var jpgquality = 92; // very good quality jpeg
  139. if ( pic_ext == "png" ) saved_pic = myimage.savePNG(pic_path);
  140. else if ( pic_ext == "jpg" ) saved_pic = myimage.saveJPG(pic_path, jpgquality);
  141. else if ( pic_ext == "tga" ) saved_pic = myimage.saveTGA(pic_path);
  142. else if ( pic_ext == "bmp" ) saved_pic = myimage.saveBMP(pic_path);
  143. else if ( pic_ext == "dds" ) saved_pic = myimage.saveDDS(pic_path);
  144. if (saved_pic) EditorUI.showEditorStatus ( "Saved screenshot " + pic_path );
  145. else EditorUI.showEditorStatus ( "Error - could not save screenshot " + pic_path );
  146. }
  147. else EditorUI.showEditorStatus ( "Error - could not take screenshot.");
  148. }
  149. handleKeyDown(ev: Atomic.KeyDownEvent) {
  150. // if the right mouse buttons isn't down
  151. if (!(ev.buttons & Atomic.MOUSEB_RIGHT)) {
  152. // TODO: Make these customizable
  153. if (!Atomic.ui.focusedWidget && !this.cmdKeyDown()) {
  154. if (ev.key == Atomic.KEY_ESCAPE) {
  155. if (Atomic.ui.consoleIsVisible) {
  156. Atomic.ui.showConsole(false);
  157. }
  158. }
  159. if (ev.key == Atomic.KEY_W) {
  160. this.invokeGizmoEditModeChanged(Editor.EditMode.EDIT_MOVE);
  161. } else if (ev.key == Atomic.KEY_E) {
  162. this.invokeGizmoEditModeChanged(Editor.EditMode.EDIT_ROTATE);
  163. } else if (ev.key == Atomic.KEY_R) {
  164. this.invokeGizmoEditModeChanged(Editor.EditMode.EDIT_SCALE);
  165. } else if (ev.key == Atomic.KEY_X) {
  166. this.toggleGizmoAxisMode();
  167. } else if (ev.key == Atomic.KEY_F) {
  168. this.invokeFrameSelected();
  169. }
  170. }
  171. }
  172. }
  173. cmdKeyDown(): boolean {
  174. var cmdKey;
  175. if (Atomic.platform == "MacOSX") {
  176. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LGUI) || Atomic.input.getKeyDown(Atomic.KEY_RGUI));
  177. } else {
  178. cmdKey = (Atomic.input.getKeyDown(Atomic.KEY_LCTRL) || Atomic.input.getKeyDown(Atomic.KEY_RCTRL));
  179. }
  180. return cmdKey;
  181. }
  182. // global shortcut handler
  183. handleUIShortcut(ev: Atomic.UIShortcutEvent) {
  184. var cmdKey = this.cmdKeyDown();
  185. if ( !cmdKey && ev.qualifiers > 0 ) { // check the event, the qualifier may have been programmitically set
  186. cmdKey = ( ev.qualifiers == Atomic.QUAL_CTRL );
  187. }
  188. if (cmdKey) {
  189. if (ev.key == Atomic.KEY_S) {
  190. this.invokeFileSave();
  191. }
  192. else if (ev.key == Atomic.KEY_W) {
  193. this.invokeFileClose();
  194. }
  195. else if (ev.key == Atomic.KEY_I) {
  196. this.invokeFormatCode();
  197. }
  198. else if (ev.key == Atomic.KEY_P) {
  199. this.invokePlayOrStopPlayer();
  200. } else if (ev.key == Atomic.KEY_B) {
  201. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  202. EditorUI.getModelOps().showBuildSettings();
  203. } else {
  204. EditorUI.getModelOps().showBuild();
  205. }
  206. }
  207. else if (ev.key == Atomic.KEY_U) {
  208. if (ev.qualifiers & Atomic.QUAL_SHIFT) {
  209. this.invokeStepPausedPlayer();
  210. } else {
  211. this.invokePauseOrResumePlayer();
  212. }
  213. }
  214. else if (ev.key == Atomic.KEY_9) {
  215. this.invokeScreenshot();
  216. }
  217. }
  218. }
  219. }
  220. export = Shortcuts;