AEEditorCommon.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/Engine/Engine.h>
  23. #include <Atomic/Input/Input.h>
  24. #include <Atomic/Graphics/Graphics.h>
  25. #include <Atomic/IPC/IPC.h>
  26. // Move me to Engine
  27. #include <Atomic/Environment/Environment.h>
  28. #include <Atomic/Script/ScriptSystem.h>
  29. #include <AtomicJS/Javascript/Javascript.h>
  30. #include <ToolCore/ToolSystem.h>
  31. #include <ToolCore/ToolEnvironment.h>
  32. #include <Atomic/IO/File.h>
  33. #ifdef ATOMIC_WEBVIEW
  34. #include <AtomicWebView/WebBrowserHost.h>
  35. #endif
  36. #include "../Components/EditorComponents.h"
  37. #include "AEEditorCommon.h"
  38. namespace Atomic
  39. {
  40. void jsapi_init_webview(JSVM* vm, const VariantMap& engineParameters);
  41. }
  42. using namespace ToolCore;
  43. namespace ToolCore
  44. {
  45. extern void jsapi_init_toolcore(JSVM* vm);
  46. }
  47. namespace AtomicEditor
  48. {
  49. AEEditorCommon::AEEditorCommon(Context* context) :
  50. Application(context)
  51. {
  52. }
  53. void AEEditorCommon::Start()
  54. {
  55. ValidateWindow();
  56. Input* input = GetSubsystem<Input>();
  57. input->SetMouseVisible(true);
  58. Javascript* javascript = GetSubsystem<Javascript>();
  59. vm_ = javascript->InstantiateVM("MainVM");
  60. vm_->InitJSContext();
  61. jsapi_init_toolcore(vm_);
  62. #ifdef ATOMIC_WEBVIEW
  63. // Initialize in Start so window already exists
  64. jsapi_init_webview(vm_, engineParameters_);
  65. #endif
  66. }
  67. void AEEditorCommon::Setup()
  68. {
  69. #ifdef ATOMIC_3D
  70. RegisterEnvironmentLibrary(context_);
  71. #endif
  72. RegisterEditorComponentLibrary(context_);
  73. // Register IPC system
  74. context_->RegisterSubsystem(new IPC(context_));
  75. context_->RegisterSubsystem(new ScriptSystem(context_));
  76. // Instantiate and register the Javascript subsystem
  77. Javascript* javascript = new Javascript(context_);
  78. context_->RegisterSubsystem(javascript);
  79. ToolEnvironment* env = new ToolEnvironment(context_);
  80. context_->RegisterSubsystem(env);
  81. #ifdef ATOMIC_DEV_BUILD
  82. if (!env->InitFromJSON())
  83. {
  84. ErrorExit(ToString("Unable to initialize tool environment from %s", env->GetDevConfigFilename().CString()));
  85. return;
  86. }
  87. #else
  88. env->InitFromPackage();
  89. #endif
  90. ToolSystem* system = new ToolSystem(context_);
  91. context_->RegisterSubsystem(system);
  92. }
  93. void AEEditorCommon::Stop()
  94. {
  95. context_->RemoveSubsystem<IPC>();
  96. vm_ = 0;
  97. context_->RemoveSubsystem<Javascript>();
  98. // make sure JSVM is really down and no outstanding refs
  99. // as if not, will hold on engine subsystems, which is bad
  100. assert(!JSVM::GetJSVM(0));
  101. }
  102. bool AEEditorCommon::CreateDefaultPreferences(String& path, JSONValue& prefs)
  103. {
  104. // Note there is some duplication here with the editor's
  105. // TypeScript preference code, this is due to the preferences for
  106. // the editor window needing to be available at window creation time
  107. // It could be better to split this all out to a native, scriptable
  108. // preferences object
  109. LOGINFOF("Creating default Atomic Editor preferences: %s", path.CString());
  110. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  111. JSONValue& root = jsonFile->GetRoot();
  112. root.Clear();
  113. root["recentProjects"] = JSONArray();
  114. JSONValue editorWindow;
  115. GetDefaultWindowPreferences(editorWindow, true);
  116. JSONValue playerWindow;
  117. GetDefaultWindowPreferences(playerWindow, false);
  118. root["editorWindow"] = editorWindow;
  119. root["playerWindow"] = playerWindow;
  120. prefs = root;
  121. SavePreferences(prefs);
  122. return true;
  123. }
  124. bool AEEditorCommon::ReadPreferences()
  125. {
  126. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  127. String path = GetPreferencesPath();
  128. JSONValue prefs;
  129. LoadPreferences(prefs);
  130. if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
  131. {
  132. if (!CreateDefaultPreferences(path, prefs))
  133. return false;
  134. }
  135. JSONValue& editorWindow = prefs["editorWindow"];
  136. engineParameters_["WindowPositionX"] = editorWindow["x"].GetUInt();
  137. engineParameters_["WindowPositionY"] = editorWindow["y"].GetUInt();
  138. engineParameters_["WindowWidth"] = editorWindow["width"].GetUInt();
  139. engineParameters_["WindowHeight"] = editorWindow["height"].GetUInt();
  140. engineParameters_["WindowMaximized"] = editorWindow["maximized"].GetBool();
  141. return true;
  142. }
  143. void AEEditorCommon::ValidateWindow()
  144. {
  145. Graphics* graphics = GetSubsystem<Graphics>();
  146. IntVector2 windowPosition = graphics->GetWindowPosition();
  147. int monitors = graphics->GetNumMonitors();
  148. IntVector2 maxResolution;
  149. for (int i = 0; i < monitors; i++)
  150. {
  151. IntVector2 monitorResolution = graphics->GetMonitorResolution(i);
  152. maxResolution += monitorResolution;
  153. }
  154. if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || (windowPosition.x_ + graphics->GetWidth()) < 0 || (windowPosition.y_ + graphics->GetHeight()) < 0)
  155. {
  156. JSONValue prefs;
  157. if (!LoadPreferences(prefs))
  158. return;
  159. bool editor = context_->GetEditorContext();
  160. JSONValue window;
  161. GetDefaultWindowPreferences(window, editor);
  162. prefs[editor ? "editorWindow" : "playerWindow"] = window;
  163. //Setting the mode to 0 width/height will use engine defaults for window size and layout
  164. graphics->SetMode(0, 0, graphics->GetFullscreen(), graphics->GetBorderless(), graphics->GetResizable(), graphics->GetVSync(), graphics->GetTripleBuffer(), graphics->GetMultiSample(), editor);
  165. SavePreferences(prefs);
  166. }
  167. }
  168. void AEEditorCommon::GetDefaultWindowPreferences(JSONValue& windowPrefs, bool maximized)
  169. {
  170. windowPrefs["x"] = 0;
  171. windowPrefs["y"] = 0;
  172. windowPrefs["width"] = 0;
  173. windowPrefs["height"] = 0;
  174. windowPrefs["monitor"] = 0;
  175. windowPrefs["maximized"] = maximized;
  176. }
  177. String AEEditorCommon::GetPreferencesPath()
  178. {
  179. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  180. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  181. path += "prefs.json";
  182. return path;
  183. }
  184. bool AEEditorCommon::LoadPreferences(JSONValue& prefs)
  185. {
  186. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  187. String path = GetPreferencesPath();
  188. if (!fileSystem->FileExists(path))
  189. {
  190. if (!CreateDefaultPreferences(path, prefs))
  191. return false;
  192. }
  193. else
  194. {
  195. SharedPtr<File> file(new File(context_, path, FILE_READ));
  196. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  197. if (!jsonFile->BeginLoad(*file))
  198. {
  199. file->Close();
  200. if (!CreateDefaultPreferences(path, prefs))
  201. return false;
  202. }
  203. else
  204. {
  205. prefs = jsonFile->GetRoot();
  206. }
  207. file->Close();
  208. }
  209. return true;
  210. }
  211. bool AEEditorCommon::SavePreferences(JSONValue& prefs)
  212. {
  213. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  214. String path = GetPreferencesPath();
  215. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  216. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  217. jsonFile->GetRoot() = prefs;
  218. if (!file->IsOpen())
  219. {
  220. LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
  221. return false;
  222. }
  223. jsonFile->Save(*file, " ");
  224. file->Close();
  225. return true;
  226. }
  227. }