AEEditorCommon.cpp 9.2 KB

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