AEEditor.cpp 7.1 KB

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