BsEditorApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsEditorWidgetManager.h"
  4. #include "BsMainEditorWindow.h"
  5. #include "BsRenderWindow.h"
  6. #include "BsBuiltinEditorResources.h"
  7. #include "BsUndoRedo.h"
  8. #include "BsFileSerializer.h"
  9. #include "BsFileSystem.h"
  10. #include "BsEditorWidgetLayout.h"
  11. #include "BsScenePicking.h"
  12. #include "BsSelection.h"
  13. #include "BsGizmoManager.h"
  14. #include "BsCodeEditor.h"
  15. #include "BsBuildManager.h"
  16. #include "BsScriptCodeImporter.h"
  17. #include "BsShaderIncludeHandler.h"
  18. #include "BsDropDownWindowManager.h"
  19. #include "BsProjectLibrary.h"
  20. #include "BsProjectSettings.h"
  21. #include "BsEditorSettings.h"
  22. #include "BsScriptManager.h"
  23. #include "BsImporter.h"
  24. #include "BsVirtualInput.h"
  25. #include "BsResources.h"
  26. #include "BsCoreSceneManager.h"
  27. #include "BsSplashScreen.h"
  28. #include "BsDynLib.h"
  29. namespace BansheeEngine
  30. {
  31. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + L"Layout.asset";
  32. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  33. const Path EditorApplication::EDITOR_SETTINGS_PATH = RUNTIME_DATA_PATH + L"Settings.asset";
  34. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"Settings.asset";
  35. RENDER_WINDOW_DESC createRenderWindowDesc()
  36. {
  37. RENDER_WINDOW_DESC renderWindowDesc;
  38. renderWindowDesc.videoMode = VideoMode(1280, 720);
  39. renderWindowDesc.title = "BansheeEditor";
  40. renderWindowDesc.fullscreen = false;
  41. renderWindowDesc.border = WindowBorder::None;
  42. renderWindowDesc.hideUntilSwap = true;
  43. return renderWindowDesc;
  44. }
  45. EditorApplication::EditorApplication(EditorRenderAPI renderAPIPlugin)
  46. :Application(createRenderWindowDesc(), toEngineRenderAPI(renderAPIPlugin), RendererPlugin::Default),
  47. mActiveRAPIPlugin(toEngineRenderAPI(renderAPIPlugin)), mSBansheeEditorPlugin(nullptr), mIsProjectLoaded(false)
  48. {
  49. }
  50. EditorApplication::~EditorApplication()
  51. {
  52. ProjectLibrary::shutDown();
  53. BuiltinEditorResources::shutDown();
  54. }
  55. void EditorApplication::onStartUp()
  56. {
  57. SplashScreen::show();
  58. Application::onStartUp();
  59. loadEditorSettings();
  60. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  61. BuiltinEditorResources::startUp();
  62. {
  63. auto inputConfig = VirtualInput::instance().getConfiguration();
  64. inputConfig->registerButton("Rename", BC_F2);
  65. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  66. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  67. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  68. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  69. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  70. inputConfig->registerButton("Duplicate", BC_D, ButtonModifier::Ctrl);
  71. inputConfig->registerButton("Delete", BC_DELETE);
  72. }
  73. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  74. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  75. ProjectLibrary::startUp();
  76. UndoRedo::startUp();
  77. EditorWindowManager::startUp();
  78. EditorWidgetManager::startUp();
  79. DropDownWindowManager::startUp();
  80. ScenePicking::startUp();
  81. Selection::startUp();
  82. GizmoManager::startUp();
  83. BuildManager::startUp();
  84. CodeEditorManager::startUp();
  85. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  86. ScriptManager::instance().initialize();
  87. }
  88. void EditorApplication::onShutDown()
  89. {
  90. unloadProject();
  91. CodeEditorManager::shutDown();
  92. BuildManager::shutDown();
  93. GizmoManager::shutDown();
  94. Selection::shutDown();
  95. ScenePicking::shutDown();
  96. saveEditorSettings();
  97. DropDownWindowManager::shutDown();
  98. EditorWidgetManager::shutDown();
  99. EditorWindowManager::shutDown();
  100. UndoRedo::shutDown();
  101. Application::onShutDown();
  102. }
  103. void EditorApplication::loadScriptSystem()
  104. {
  105. loadPlugin("BansheeMono", &mMonoPlugin);
  106. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  107. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  108. }
  109. void EditorApplication::startUp(EditorRenderAPI renderAPI)
  110. {
  111. CoreApplication::startUp<EditorApplication>(renderAPI);
  112. }
  113. void EditorApplication::preUpdate()
  114. {
  115. Application::preUpdate();
  116. EditorWidgetManager::instance().update();
  117. DropDownWindowManager::instance().update();
  118. }
  119. void EditorApplication::postUpdate()
  120. {
  121. // Call update on editor widgets before parent's postUpdate because the parent will render the GUI and we need
  122. // to ensure editor widget's GUI is updated.
  123. EditorWindowManager::instance().update();
  124. Application::postUpdate();
  125. SplashScreen::hide();
  126. }
  127. void EditorApplication::quitRequested()
  128. {
  129. typedef void(*QuitRequestedFunc)();
  130. QuitRequestedFunc quitRequestedCall = (QuitRequestedFunc)mSBansheeEditorPlugin->getSymbol("quitRequested");
  131. if (quitRequestedCall != nullptr)
  132. quitRequestedCall();
  133. }
  134. Path EditorApplication::getEditorAssemblyPath() const
  135. {
  136. Path assemblyPath = getBuiltinAssemblyFolder();
  137. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  138. return assemblyPath;
  139. }
  140. Path EditorApplication::getEditorScriptAssemblyPath() const
  141. {
  142. Path assemblyPath = getScriptAssemblyFolder();
  143. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  144. return assemblyPath;
  145. }
  146. Path EditorApplication::getScriptAssemblyFolder() const
  147. {
  148. if (!isProjectLoaded())
  149. return Path::BLANK;
  150. Path assemblyFolder = getProjectPath();
  151. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  152. return assemblyFolder;
  153. }
  154. void EditorApplication::saveProject()
  155. {
  156. if (!isProjectLoaded())
  157. return;
  158. Path buildDataPath = getProjectPath();
  159. buildDataPath.append(BUILD_DATA_PATH);
  160. BuildManager::instance().save(buildDataPath);
  161. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  162. saveEditorSettings();
  163. saveProjectSettings();
  164. gProjectLibrary().saveLibrary();
  165. }
  166. void EditorApplication::unloadProject()
  167. {
  168. if (!isProjectLoaded())
  169. return;
  170. saveProject();
  171. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  172. BuildManager::instance().clear();
  173. UndoRedo::instance().clear();
  174. EditorWidgetManager::instance().closeAll();
  175. gProjectLibrary().unloadLibrary();
  176. Resources::instance().unloadAllUnused();
  177. gCoreSceneManager().clearScene();
  178. mProjectPath = Path::BLANK;
  179. mProjectName = StringUtil::WBLANK;
  180. mIsProjectLoaded = false;
  181. }
  182. void EditorApplication::loadProject(const Path& projectPath)
  183. {
  184. unloadProject();
  185. mProjectPath = projectPath;
  186. mProjectName = projectPath.getWTail();
  187. mIsProjectLoaded = true;
  188. loadProjectSettings();
  189. Path buildDataPath = getProjectPath();
  190. buildDataPath.append(BUILD_DATA_PATH);
  191. BuildManager::instance().load(buildDataPath);
  192. gProjectLibrary().loadLibrary();
  193. // Do this before restoring windows to ensure types are loaded
  194. ScriptManager::instance().reload();
  195. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  196. if (layout != nullptr)
  197. EditorWidgetManager::instance().setLayout(layout);
  198. }
  199. void EditorApplication::createProject(const Path& path)
  200. {
  201. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  202. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  203. if (!FileSystem::exists(resourceDir))
  204. FileSystem::createDir(resourceDir);
  205. if (!FileSystem::exists(internalResourcesDir))
  206. FileSystem::createDir(internalResourcesDir);
  207. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  208. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  209. if (FileSystem::exists(defaultLayoutPath))
  210. {
  211. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  212. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  213. }
  214. }
  215. bool EditorApplication::isValidProjectPath(const Path& path)
  216. {
  217. if (!path.isAbsolute())
  218. return false;
  219. if (!FileSystem::isDirectory(path))
  220. return false;
  221. return true;
  222. }
  223. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  224. {
  225. Path layoutPath = getProjectPath();
  226. layoutPath.append(WIDGET_LAYOUT_PATH);
  227. if(FileSystem::exists(layoutPath))
  228. {
  229. FileDecoder fs(layoutPath);
  230. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  231. }
  232. return nullptr;
  233. }
  234. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  235. {
  236. Path layoutPath = getProjectPath();
  237. layoutPath.append(WIDGET_LAYOUT_PATH);
  238. FileEncoder fs(layoutPath);
  239. fs.encode(layout.get());
  240. }
  241. void EditorApplication::loadEditorSettings()
  242. {
  243. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  244. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  245. if (FileSystem::exists(absoluteDataPath))
  246. {
  247. FileDecoder fs(absoluteDataPath);
  248. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  249. }
  250. if (mEditorSettings == nullptr)
  251. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  252. }
  253. void EditorApplication::saveEditorSettings()
  254. {
  255. if (mEditorSettings == nullptr)
  256. return;
  257. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  258. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  259. FileEncoder fs(absoluteDataPath);
  260. fs.encode(mEditorSettings.get());
  261. }
  262. void EditorApplication::loadProjectSettings()
  263. {
  264. if (isProjectLoaded())
  265. {
  266. Path absoluteDataPath = getProjectPath();
  267. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  268. if (FileSystem::exists(absoluteDataPath))
  269. {
  270. FileDecoder fs(absoluteDataPath);
  271. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  272. }
  273. }
  274. if (mProjectSettings == nullptr)
  275. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  276. }
  277. void EditorApplication::saveProjectSettings()
  278. {
  279. if (mProjectSettings == nullptr || !isProjectLoaded())
  280. return;
  281. Path absoluteDataPath = getProjectPath();
  282. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  283. FileEncoder fs(absoluteDataPath);
  284. fs.encode(mProjectSettings.get());
  285. }
  286. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  287. {
  288. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  289. }
  290. RenderAPIPlugin EditorApplication::toEngineRenderAPI(EditorRenderAPI renderAPI)
  291. {
  292. if (renderAPI == EditorRenderAPI::DX11)
  293. return RenderAPIPlugin::DX11;
  294. else if (renderAPI == EditorRenderAPI::OpenGL)
  295. return RenderAPIPlugin::OpenGL;
  296. BS_EXCEPT(InvalidStateException, "Unsupported render API.");
  297. }
  298. EditorApplication& gEditorApplication()
  299. {
  300. return static_cast<EditorApplication&>(EditorApplication::instance());
  301. }
  302. }