AEEditorCommon.cpp 8.3 KB

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