AEEditorCommon.cpp 8.4 KB

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