AEEditorPrefs.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. ATOMIC_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. String path = GetPreferencesPath();
  62. JSONValue prefs;
  63. LoadPreferences(prefs);
  64. if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
  65. {
  66. if (!CreateDefaultPreferences(path, prefs))
  67. return false;
  68. }
  69. JSONValue& editorWindow = prefs["editorWindow"];
  70. engineParameters["WindowPositionX"] = editorWindow["x"].GetUInt();
  71. engineParameters["WindowPositionY"] = editorWindow["y"].GetUInt();
  72. engineParameters["WindowWidth"] = editorWindow["width"].GetUInt();
  73. engineParameters["WindowHeight"] = editorWindow["height"].GetUInt();
  74. engineParameters["WindowMaximized"] = editorWindow["maximized"].GetBool();
  75. return true;
  76. }
  77. void AEEditorPrefs::ValidateWindow()
  78. {
  79. Graphics* graphics = GetSubsystem<Graphics>();
  80. IntVector2 windowPosition = graphics->GetWindowPosition();
  81. int monitors = graphics->GetNumMonitors();
  82. IntVector2 maxResolution;
  83. for (int i = 0; i < monitors; i++)
  84. {
  85. IntVector2 monitorResolution = graphics->GetMonitorResolution(i);
  86. maxResolution += monitorResolution;
  87. }
  88. if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || (windowPosition.x_ + graphics->GetWidth()) < 0 || (windowPosition.y_ + graphics->GetHeight()) < 0)
  89. {
  90. JSONValue prefs;
  91. if (!LoadPreferences(prefs))
  92. return;
  93. bool editor = context_->GetEditorContext();
  94. JSONValue window;
  95. GetDefaultWindowPreferences(window, editor);
  96. prefs[editor ? "editorWindow" : "playerWindow"] = window;
  97. // TODO: add highDPI support
  98. bool highDPI = false;
  99. //Setting the mode to 0 width/height will use engine defaults for window size and layout
  100. graphics->SetMode(0, 0, graphics->GetFullscreen(), graphics->GetBorderless(), graphics->GetResizable(), highDPI, graphics->GetVSync(), graphics->GetTripleBuffer(), graphics->GetMultiSample());
  101. SavePreferences(prefs);
  102. }
  103. }
  104. void AEEditorPrefs::GetDefaultWindowPreferences(JSONValue& windowPrefs, bool maximized)
  105. {
  106. windowPrefs["x"] = 0;
  107. windowPrefs["y"] = 0;
  108. windowPrefs["width"] = 0;
  109. windowPrefs["height"] = 0;
  110. windowPrefs["monitor"] = 0;
  111. windowPrefs["maximized"] = maximized;
  112. }
  113. String AEEditorPrefs::GetPreferencesPath()
  114. {
  115. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  116. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  117. path += "prefs.json";
  118. return path;
  119. }
  120. bool AEEditorPrefs::LoadPreferences(JSONValue& prefs)
  121. {
  122. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  123. String path = GetPreferencesPath();
  124. if (!fileSystem->FileExists(path))
  125. {
  126. if (!CreateDefaultPreferences(path, prefs))
  127. return false;
  128. }
  129. else
  130. {
  131. SharedPtr<File> file(new File(context_, path, FILE_READ));
  132. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  133. if (!jsonFile->BeginLoad(*file))
  134. {
  135. file->Close();
  136. if (!CreateDefaultPreferences(path, prefs))
  137. return false;
  138. }
  139. else
  140. {
  141. prefs = jsonFile->GetRoot();
  142. }
  143. file->Close();
  144. }
  145. return true;
  146. }
  147. bool AEEditorPrefs::SavePreferences(JSONValue& prefs)
  148. {
  149. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  150. String path = GetPreferencesPath();
  151. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  152. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  153. jsonFile->GetRoot() = prefs;
  154. if (!file->IsOpen())
  155. {
  156. ATOMIC_LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
  157. return false;
  158. }
  159. jsonFile->Save(*file, " ");
  160. file->Close();
  161. return true;
  162. }
  163. }