Preferences.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = <PreferencesFormat>JSON.parse(jsonFile.readText());
  61. if (prefs) {
  62. if (!prefs.recentProjects) prefs.recentProjects = [""];
  63. this._prefs = prefs;
  64. }
  65. }
  66. write(): void {
  67. var filePath = this.getPreferencesFullPath();
  68. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  69. if (!jsonFile.isOpen()) return;
  70. var graphics = Atomic.getGraphics();
  71. var pos, width, height;
  72. if (graphics && !graphics.getFullscreen()) {
  73. pos = graphics.getWindowPosition();
  74. width = graphics.getWidth();
  75. height = graphics.getHeight();
  76. }
  77. this._prefs.window = { x: pos[0], y: pos[1], width: width, height: height };
  78. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  79. }
  80. get recentProjects(): [string] {
  81. return this._prefs.recentProjects;
  82. }
  83. static getInstance(): Preferences {
  84. return Preferences.instance;
  85. }
  86. }
  87. class PreferencesFormat {
  88. recentProjects: [string];
  89. window: { x: number, y: number, width: number, height: number };
  90. }
  91. export = Preferences;