AEEditorCommon.cpp 8.0 KB

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