Preferences.ts 3.8 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(): boolean {
  74. var filePath = this.getPreferencesFullPath();
  75. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  76. if (!jsonFile.isOpen()) return false;
  77. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  78. }
  79. saveEditorWindowData(windowData:WindowData) {
  80. this._prefs.editorWindow = windowData;
  81. this.write();
  82. }
  83. savePlayerWindowData(windowData:WindowData) {
  84. this._prefs.playerWindow = windowData;
  85. this.write();
  86. }
  87. useDefaultConfig():void {
  88. this._prefs = new PreferencesFormat();
  89. if (!this._prefs.recentProjects) this._prefs.recentProjects = [""];
  90. }
  91. get editorWindow():WindowData {
  92. return this._prefs.editorWindow;
  93. }
  94. get playerWindow():WindowData {
  95. return this._prefs.playerWindow;
  96. }
  97. get recentProjects(): [string] {
  98. return this._prefs.recentProjects;
  99. }
  100. static getInstance(): Preferences {
  101. return Preferences.instance;
  102. }
  103. }
  104. interface WindowData {
  105. x: number;
  106. y: number;
  107. width: number;
  108. height: number;
  109. monitor: number;
  110. maximized: boolean;
  111. }
  112. class PreferencesFormat {
  113. recentProjects: [string];
  114. editorWindow: WindowData;
  115. playerWindow: WindowData;
  116. }
  117. export = Preferences;