Preferences.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. class Preferences {
  8. private static Ctor = (() => {
  9. new Preferences();
  10. })();
  11. private fileSystem: Atomic.FileSystem;
  12. private static instance: Preferences;
  13. private _prefs: PreferencesFormat;
  14. constructor() {
  15. this.fileSystem = Atomic.getFileSystem();
  16. Preferences.instance = this;
  17. }
  18. registerRecentProject(path: string): void {
  19. var index = this._prefs.recentProjects.indexOf(path);
  20. if (index >= 0) {
  21. this._prefs.recentProjects.splice(index, 1);
  22. }
  23. this._prefs.recentProjects.unshift(path);
  24. this.updateRecentProjects();
  25. }
  26. unRegisterRecentProject(path: string): void {
  27. var index = this._prefs.recentProjects.indexOf(path);
  28. if (index >= 0) {
  29. this._prefs.recentProjects.splice(index, 1);
  30. }
  31. this.updateRecentProjects();
  32. }
  33. updateRecentProjects(): void {
  34. for (var i in this._prefs.recentProjects) {
  35. var path = this._prefs.recentProjects[i];
  36. if (!this.fileSystem.exists(path)) {
  37. this._prefs.recentProjects.splice(i, 1);
  38. }
  39. }
  40. }
  41. deleteRecentProjects(): void {
  42. this._prefs.recentProjects.length = 0;
  43. }
  44. getPreferencesFullPath(): string {
  45. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  46. filePath += "prefs.json";
  47. return filePath;
  48. }
  49. read(): void {
  50. var filePath = this.getPreferencesFullPath();
  51. var jsonFile;
  52. //check if file doesn't exists, create an empty JSON file
  53. if (!this.fileSystem.fileExists(filePath)) {
  54. jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  55. jsonFile.writeString("{}");
  56. jsonFile.close();
  57. }
  58. //Read file
  59. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  60. var prefs;
  61. try {
  62. prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
  63. } catch (e){
  64. console.log("Editor preference file invalid, regenerating default configuration");
  65. prefs = null;
  66. this.useDefaultConfig();
  67. }
  68. if (prefs) {
  69. if (!prefs.recentProjects) prefs.recentProjects = [""];
  70. this._prefs = prefs;
  71. }
  72. }
  73. write(): void {
  74. var filePath = this.getPreferencesFullPath();
  75. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  76. if (!jsonFile.isOpen()) return;
  77. var graphics = Atomic.getGraphics();
  78. var pos, width, height;
  79. if (graphics && !graphics.getFullscreen()) {
  80. pos = graphics.getWindowPosition();
  81. width = graphics.getWidth();
  82. height = graphics.getHeight();
  83. }
  84. this._prefs.editorWindow = { x: pos[0], y: pos[1], width: width, height: height, monitor: graphics.getCurrentMonitor() };
  85. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  86. }
  87. savePlayerWindowData(x, y, width, height, monitor) {
  88. this._prefs.playerWindow = { x: x, y: y, width: width, height: height, monitor: monitor };
  89. }
  90. useDefaultConfig():void {
  91. this._prefs = new PreferencesFormat();
  92. }
  93. get editorWindow():WindowData {
  94. return this._prefs.editorWindow;
  95. }
  96. get playerWindow():WindowData {
  97. return this._prefs.playerWindow;
  98. }
  99. get recentProjects(): [string] {
  100. return this._prefs.recentProjects;
  101. }
  102. static getInstance(): Preferences {
  103. return Preferences.instance;
  104. }
  105. }
  106. interface WindowData {
  107. x: number;
  108. y: number;
  109. width: number;
  110. height: number;
  111. monitor: number;
  112. }
  113. class PreferencesFormat {
  114. recentProjects: [string];
  115. editorWindow: WindowData;
  116. playerWindow: WindowData;
  117. }
  118. export = Preferences;