AEEditorPrefs.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include <Atomic/Core/Context.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include "AEEditorPrefs.h"
  28. namespace AtomicEditor
  29. {
  30. AEEditorPrefs::AEEditorPrefs(Context* context) :
  31. Object(context)
  32. {
  33. }
  34. AEEditorPrefs::~AEEditorPrefs()
  35. {
  36. }
  37. bool AEEditorPrefs::CreateDefaultPreferences(String& path, JSONValue& prefs)
  38. {
  39. // Note there is some duplication here with the editor's
  40. // TypeScript preference code, this is due to the preferences for
  41. // the editor window needing to be available at window creation time
  42. // It could be better to split this all out to a native, scriptable
  43. // preferences object
  44. LOGINFOF("Creating default Atomic Editor preferences: %s", path.CString());
  45. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  46. JSONValue& root = jsonFile->GetRoot();
  47. root.Clear();
  48. root["recentProjects"] = JSONArray();
  49. JSONValue editorWindow;
  50. GetDefaultWindowPreferences(editorWindow, true);
  51. JSONValue playerWindow;
  52. GetDefaultWindowPreferences(playerWindow, false);
  53. root["editorWindow"] = editorWindow;
  54. root["playerWindow"] = playerWindow;
  55. prefs = root;
  56. SavePreferences(prefs);
  57. return true;
  58. }
  59. bool AEEditorPrefs::ReadPreferences(VariantMap& engineParameters)
  60. {
  61. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  62. String path = GetPreferencesPath();
  63. JSONValue prefs;
  64. LoadPreferences(prefs);
  65. if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
  66. {
  67. if (!CreateDefaultPreferences(path, prefs))
  68. return false;
  69. }
  70. JSONValue& editorWindow = prefs["editorWindow"];
  71. engineParameters["WindowPositionX"] = editorWindow["x"].GetUInt();
  72. engineParameters["WindowPositionY"] = editorWindow["y"].GetUInt();
  73. engineParameters["WindowWidth"] = editorWindow["width"].GetUInt();
  74. engineParameters["WindowHeight"] = editorWindow["height"].GetUInt();
  75. engineParameters["WindowMaximized"] = editorWindow["maximized"].GetBool();
  76. return true;
  77. }
  78. void AEEditorPrefs::ValidateWindow()
  79. {
  80. Graphics* graphics = GetSubsystem<Graphics>();
  81. IntVector2 windowPosition = graphics->GetWindowPosition();
  82. int monitors = graphics->GetNumMonitors();
  83. IntVector2 maxResolution;
  84. for (int i = 0; i < monitors; i++)
  85. {
  86. IntVector2 monitorResolution = graphics->GetMonitorResolution(i);
  87. maxResolution += monitorResolution;
  88. }
  89. if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || (windowPosition.x_ + graphics->GetWidth()) < 0 || (windowPosition.y_ + graphics->GetHeight()) < 0)
  90. {
  91. JSONValue prefs;
  92. if (!LoadPreferences(prefs))
  93. return;
  94. bool editor = context_->GetEditorContext();
  95. JSONValue window;
  96. GetDefaultWindowPreferences(window, editor);
  97. prefs[editor ? "editorWindow" : "playerWindow"] = window;
  98. //Setting the mode to 0 width/height will use engine defaults for window size and layout
  99. graphics->SetMode(0, 0, graphics->GetFullscreen(), graphics->GetBorderless(), graphics->GetResizable(), graphics->GetVSync(), graphics->GetTripleBuffer(), graphics->GetMultiSample(), editor);
  100. SavePreferences(prefs);
  101. }
  102. }
  103. void AEEditorPrefs::GetDefaultWindowPreferences(JSONValue& windowPrefs, bool maximized)
  104. {
  105. windowPrefs["x"] = 0;
  106. windowPrefs["y"] = 0;
  107. windowPrefs["width"] = 0;
  108. windowPrefs["height"] = 0;
  109. windowPrefs["monitor"] = 0;
  110. windowPrefs["maximized"] = maximized;
  111. }
  112. String AEEditorPrefs::GetPreferencesPath()
  113. {
  114. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  115. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  116. path += "prefs.json";
  117. return path;
  118. }
  119. bool AEEditorPrefs::LoadPreferences(JSONValue& prefs)
  120. {
  121. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  122. String path = GetPreferencesPath();
  123. if (!fileSystem->FileExists(path))
  124. {
  125. if (!CreateDefaultPreferences(path, prefs))
  126. return false;
  127. }
  128. else
  129. {
  130. SharedPtr<File> file(new File(context_, path, FILE_READ));
  131. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  132. if (!jsonFile->BeginLoad(*file))
  133. {
  134. file->Close();
  135. if (!CreateDefaultPreferences(path, prefs))
  136. return false;
  137. }
  138. else
  139. {
  140. prefs = jsonFile->GetRoot();
  141. }
  142. file->Close();
  143. }
  144. return true;
  145. }
  146. bool AEEditorPrefs::SavePreferences(JSONValue& prefs)
  147. {
  148. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  149. String path = GetPreferencesPath();
  150. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  151. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  152. jsonFile->GetRoot() = prefs;
  153. if (!file->IsOpen())
  154. {
  155. LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
  156. return false;
  157. }
  158. jsonFile->Save(*file, " ");
  159. file->Close();
  160. return true;
  161. }
  162. }