BsEditorApplication.cpp 11 KB

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