AEEditor.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/Engine/Engine.h>
  6. #include <Atomic/IO/FileSystem.h>
  7. #include <Atomic/Resource/ResourceCache.h>
  8. #include <Atomic/Input/Input.h>
  9. #include <Atomic/UI/TBUI.h>
  10. #include <Atomic/Javascript/Javascript.h>
  11. #include <Atomic/Core/CoreEvents.h>
  12. #include "AEEditor.h"
  13. #include "AEPreferences.h"
  14. #include "Project/AEProject.h"
  15. #include "AEJavascript.h"
  16. #include "AEEvents.h"
  17. #include "Player/AEPlayer.h"
  18. #ifdef USE_SPIDERMONKEY
  19. #include "Javascript/JSSpiderMonkeyVM.h"
  20. #endif
  21. #include "UI/UIMainFrame.h"
  22. #include "UI/UIProjectFrame.h"
  23. #include "UI/UIResourceFrame.h"
  24. #include "UI/Modal/UIMessageModal.h"
  25. #include "License/AELicenseSystem.h"
  26. #include "Resources/AEResourceOps.h"
  27. #include <TurboBadger/tb_message_window.h>
  28. namespace AtomicEditor
  29. {
  30. Editor::Editor(Context* context) :
  31. Object(context),
  32. currentPlatform_(AE_PLATFORM_UNDEFINED),
  33. requestExit_(false)
  34. {
  35. RegisterEditorLibrary(context_);
  36. aejavascript_ = new AEJavascript(context_);
  37. aepreferences_ = new AEPreferences(context_);
  38. #ifdef USE_SPIDERMONKEY
  39. spidermonkey_ = new JSSpiderMonkeyVM(context_);
  40. #endif
  41. resourceCreator_ = new ResourceOps(context_);
  42. // Create the Main Editor Frame
  43. mainframe_ = new MainFrame(context_);
  44. SubscribeToEvent(E_EXITREQUESTED, HANDLER(Editor, HandleExitRequested));
  45. SubscribeToEvent(E_PLAYERERROR, HANDLER(Editor, HandlePlayerError));
  46. SubscribeToEvent(E_POSTUPDATE, HANDLER(Editor, HandlePostUpdate));
  47. // the player handling might move
  48. SubscribeToEvent(E_EDITORPLAYREQUEST, HANDLER(Editor, HandlePlayRequest));
  49. SubscribeToEvent(E_EDITORPLAYSTOP, HANDLER(Editor, HandlePlayStop));
  50. SubscribeToEvent(E_EDITORPLAYSTARTED, HANDLER(Editor, HandlePlayStarted));
  51. SubscribeToEvent(E_EDITORPLAYSTOPPED, HANDLER(Editor, HandlePlayStopped));
  52. // BEGIN LICENSE MANAGEMENT
  53. GetSubsystem<LicenseSystem>()->Initialize();
  54. // END LICENSE MANAGEMENT
  55. }
  56. Editor::~Editor()
  57. {
  58. }
  59. MainFrame* Editor::GetMainFrame()
  60. {
  61. return mainframe_;
  62. }
  63. Project* Editor::GetProject()
  64. {
  65. return project_;
  66. }
  67. AEPreferences* Editor::GetPreferences()
  68. {
  69. return aepreferences_;
  70. }
  71. void Editor::EditResource(const String& fullpath)
  72. {
  73. mainframe_->GetResourceFrame()->EditResource(fullpath);
  74. }
  75. void Editor::LoadProject(const String& fullpath)
  76. {
  77. aepreferences_->RegisterRecentProject(fullpath);
  78. String path = GetPath(fullpath);
  79. ResourceCache* cache = GetSubsystem<ResourceCache>();
  80. cache->AddResourceDir(path, 0);
  81. String resourcePath = path;
  82. resourcePath += "Resources";
  83. cache->AddResourceDir(resourcePath, 0);
  84. project_ = new Project(context_);
  85. project_->SetResourcePath(resourcePath);
  86. project_->Load(fullpath);
  87. mainframe_->ShowResourceFrame();
  88. mainframe_->GetProjectFrame()->RefreshFolders();
  89. mainframe_->UpdateJavascriptErrors();
  90. }
  91. void Editor::CloseProject()
  92. {
  93. if (project_.Null())
  94. return;
  95. ResourceCache* cache = GetSubsystem<ResourceCache>();
  96. String projectPath = project_->GetProjectFilePath();
  97. String resourcePath = project_->GetResourcePath();
  98. mainframe_->GetProjectFrame()->Clear();
  99. mainframe_->GetResourceFrame()->CloseAllResourceEditors();
  100. project_ = 0;
  101. cache->RemoveResourceDir(resourcePath);
  102. cache->RemoveResourceDir(projectPath);
  103. cache->ReleaseAllResources(true);
  104. mainframe_->ShowWelcomeFrame();
  105. }
  106. void Editor::SaveProject()
  107. {
  108. if (project_.Null())
  109. return;
  110. project_->Save(project_->GetProjectFilePath());
  111. }
  112. // Project Playing
  113. void Editor::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
  114. {
  115. }
  116. void Editor::HandlePlayStop(StringHash eventType, VariantMap& eventData)
  117. {
  118. if (!player_)
  119. return;
  120. TBUI* tbui = GetSubsystem<TBUI>();
  121. tbui->SetKeyboardDisabled(false);
  122. if (player_->GetMode() != AE_PLAYERMODE_WIDGET)
  123. {
  124. tbui->SetInputDisabled(false);
  125. tbui->FadeIn(.5f);
  126. }
  127. Input* input = GetSubsystem<Input>();
  128. input->SetMouseVisible(true);
  129. mainframe_->UpdateJavascriptErrors();
  130. player_->Invalidate();
  131. player_ = NULL;
  132. SendEvent(E_EDITORPLAYSTOPPED);
  133. }
  134. void Editor::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
  135. {
  136. }
  137. void Editor::HandlePlayerError(StringHash eventType, VariantMap& eventData)
  138. {
  139. }
  140. void Editor::PostModalError(const String& title, const String& message)
  141. {
  142. using namespace EditorModal;
  143. VariantMap eventData;
  144. eventData[P_TYPE] = EDITOR_MODALERROR;
  145. eventData[P_TITLE] = title;
  146. eventData[P_MESSAGE] = message;
  147. SendEvent(E_EDITORMODAL, eventData);
  148. }
  149. void Editor::PostModalInfo(const String& title, const String& message)
  150. {
  151. using namespace EditorModal;
  152. VariantMap eventData;
  153. eventData[P_TYPE] = EDITOR_MODALINFO;
  154. eventData[P_TITLE] = title;
  155. eventData[P_MESSAGE] = message;
  156. SendEvent(E_EDITORMODAL, eventData);
  157. }
  158. void Editor::RequestPlatformChange(AEEditorPlatform platform)
  159. {
  160. // BEGIN LICENSE MANAGEMENT
  161. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  162. if (!licenseSystem->RequestPlatformChange(platform))
  163. {
  164. PostModalInfo("Platform License Required", "Platform License is required to switch to this deployment");
  165. return;
  166. }
  167. if (currentPlatform_ == platform)
  168. return;
  169. // if we can switch platforms via some other event, may want to do this in a handler
  170. currentPlatform_ = platform;
  171. if (!project_.Null())
  172. project_->Save(project_->GetProjectFilePath());
  173. using namespace PlatformChange;
  174. VariantMap eventData;
  175. eventData[P_PLATFORM] = (unsigned) platform;
  176. SendEvent(E_PLATFORMCHANGE, eventData);
  177. // END LICENSE MANAGEMENT
  178. }
  179. void Editor::HandlePlayRequest(StringHash eventType, VariantMap& eventData)
  180. {
  181. if (player_ || project_.Null())
  182. return;
  183. ResourceFrame* resourceFrame = mainframe_->GetResourceFrame();
  184. if (resourceFrame->HasUnsavedModifications())
  185. {
  186. PostModalError("Unsaved Modifications", "There are unsaved modications.\nPlease save before entering play mode");
  187. return;
  188. }
  189. else if (mainframe_->UpdateJavascriptErrors())
  190. {
  191. return;
  192. }
  193. assert(!player_);
  194. AEPlayerMode mode = (AEPlayerMode) eventData[EditorPlayStarted::P_MODE].GetUInt();
  195. TBUI* tbui = GetSubsystem<TBUI>();
  196. tbui->SetKeyboardDisabled(true);
  197. if (mode != AE_PLAYERMODE_WIDGET)
  198. {
  199. tbui->SetInputDisabled(true);
  200. tbui->FadeOut(.5f);
  201. }
  202. player_ = new AEPlayer(context_);
  203. TBWidgetDelegate* tb = mainframe_->GetResourceFrame()->GetWidgetDelegate();
  204. TBRect rect = tb->GetRect();
  205. tb->ConvertToRoot(rect.x, rect.y);
  206. player_->Play(mode, IntRect(rect.x, rect.y, rect.x + rect.w, rect.y + rect.h));
  207. SendEvent(E_EDITORPLAYSTARTED, eventData);
  208. }
  209. void Editor::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  210. {
  211. if (player_ && player_->HasErrors())
  212. {
  213. SendEvent(E_EDITORPLAYSTOP);
  214. }
  215. if (requestExit_)
  216. {
  217. requestExit_ = false;
  218. SendEvent(E_EXITREQUESTED);
  219. }
  220. }
  221. void Editor::HandleExitRequested(StringHash eventType, VariantMap& eventData)
  222. {
  223. if (aepreferences_.NotNull())
  224. {
  225. aepreferences_->Write();
  226. }
  227. TBUI* tbui = GetSubsystem<TBUI>();
  228. tbui->Shutdown();
  229. mainframe_ = 0;
  230. player_ = 0;
  231. project_ = 0;
  232. javascript_ = 0;
  233. aejavascript_ = 0;
  234. aepreferences_ = 0;
  235. context_->RemoveSubsystem(Javascript::GetBaseTypeStatic());
  236. SendEvent(E_EDITORSHUTDOWN);
  237. SharedPtr<Editor> keepAlive(this);
  238. context_->RemoveSubsystem(GetType());
  239. Engine* engine = GetSubsystem<Engine>();
  240. engine->Exit();
  241. }
  242. void RegisterEditorLibrary(Context* context)
  243. {
  244. }
  245. }