Preferences.ts 4.1 KB

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