AEEditorCommon.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. editorWindow["x"] = 0;
  130. editorWindow["y"] = 0;
  131. editorWindow["width"] = 0;
  132. editorWindow["height"] = 0;
  133. editorWindow["monitor"] = 0;
  134. editorWindow["maximized"] = true;
  135. JSONValue playerWindow;
  136. playerWindow["x"] = 0;
  137. playerWindow["y"] = 0;
  138. playerWindow["width"] = 0;
  139. playerWindow["height"] = 0;
  140. playerWindow["monitor"] = 0;
  141. playerWindow["maximized"] = false;
  142. root["editorWindow"] = editorWindow;
  143. root["playerWindow"] = playerWindow;
  144. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  145. if (!file->IsOpen())
  146. {
  147. LOGERRORF("Unable to open Atomic Editor preferences for writing: %s", path.CString());
  148. return false;
  149. }
  150. jsonFile->Save(*file, " ");
  151. prefs = root;
  152. return true;
  153. }
  154. bool AEEditorCommon::ReadPreferences()
  155. {
  156. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  157. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  158. path += "prefs.json";
  159. JSONValue prefs;
  160. if (!fileSystem->FileExists(path))
  161. {
  162. if (!CreateDefaultPreferences(path, prefs))
  163. return false;
  164. }
  165. else
  166. {
  167. SharedPtr<File> file(new File(context_, path, FILE_READ));
  168. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  169. if (!jsonFile->BeginLoad(*file))
  170. {
  171. file->Close();
  172. if (!CreateDefaultPreferences(path, prefs))
  173. return false;
  174. }
  175. else
  176. {
  177. prefs = jsonFile->GetRoot();
  178. }
  179. }
  180. if (!prefs.IsObject() || !prefs["editorWindow"].IsObject())
  181. {
  182. if (!CreateDefaultPreferences(path, prefs))
  183. return false;
  184. }
  185. JSONValue& editorWindow = prefs["editorWindow"];
  186. engineParameters_["WindowPositionX"] = editorWindow["x"].GetUInt();
  187. engineParameters_["WindowPositionY"] = editorWindow["y"].GetUInt();
  188. engineParameters_["WindowWidth"] = editorWindow["width"].GetUInt();
  189. engineParameters_["WindowHeight"] = editorWindow["height"].GetUInt();
  190. engineParameters_["WindowMaximized"] = editorWindow["maximized"].GetBool();
  191. return true;
  192. }
  193. void AEEditorCommon::ValidateWindow()
  194. {
  195. Graphics* graphics = GetSubsystem<Graphics>();
  196. IntVector2 windowPosition = graphics->GetWindowPosition();
  197. int monitors = graphics->GetMonitorsNumber();
  198. IntVector2 maxResolution;
  199. for (int i = 0; i < monitors; i++)
  200. {
  201. IntVector2 monitorResolution = graphics->GetMonitorResolution(i);
  202. maxResolution += monitorResolution;
  203. }
  204. if (windowPosition.x_ >= maxResolution.x_ || windowPosition.y_ >= maxResolution.y_ || windowPosition.x_ < 0 || windowPosition.y_ < 0)
  205. {
  206. bool editor = this->GetTypeName() == "AEEditorApp";
  207. JSONValue window;
  208. window["x"] = 0;
  209. window["y"] = 0;
  210. window["width"] = 0;
  211. window["height"] = 0;
  212. window["monitor"] = 0;
  213. window["maximized"] = editor ? true : false;
  214. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  215. String path = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  216. path += "prefs.json";
  217. JSONValue prefs;
  218. SharedPtr<File> file(new File(context_, path, FILE_READWRITE));
  219. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  220. jsonFile->BeginLoad(*file);
  221. prefs = jsonFile->GetRoot();
  222. prefs[editor ? "editorWindow" : "playerWindow"] = window;
  223. jsonFile->Save(*file, " ");
  224. file->Close();
  225. graphics->SetMode(0, 0);
  226. if (editor)
  227. {
  228. graphics->Maximize();
  229. }
  230. }
  231. }
  232. }