AEEditor.cpp 7.9 KB

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