AEEditorCommon.cpp 9.4 KB

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