AEEditorCommon.cpp 9.2 KB

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