BsEditorApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "BsEditorShaderIncludeHandler.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(1920, 1080);
  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. Application::onStartUp();
  73. SplashScreen::show();
  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. setFPSLimit(mEditorSettings->getFPSLimit());
  144. }
  145. void EditorApplication::quitRequested()
  146. {
  147. typedef void(*QuitRequestedFunc)();
  148. QuitRequestedFunc quitRequestedCall = (QuitRequestedFunc)mSBansheeEditorPlugin->getSymbol("quitRequested");
  149. if (quitRequestedCall != nullptr)
  150. quitRequestedCall();
  151. }
  152. Path EditorApplication::getEditorAssemblyPath() const
  153. {
  154. Path assemblyPath = getBuiltinAssemblyFolder();
  155. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  156. return assemblyPath;
  157. }
  158. Path EditorApplication::getEditorScriptAssemblyPath() const
  159. {
  160. Path assemblyPath = getScriptAssemblyFolder();
  161. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  162. return assemblyPath;
  163. }
  164. Path EditorApplication::getScriptAssemblyFolder() const
  165. {
  166. if (!isProjectLoaded())
  167. return Path::BLANK;
  168. Path assemblyFolder = getProjectPath();
  169. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  170. return assemblyFolder;
  171. }
  172. void EditorApplication::saveProject()
  173. {
  174. if (!isProjectLoaded())
  175. return;
  176. Path buildDataPath = getProjectPath();
  177. buildDataPath.append(BUILD_DATA_PATH);
  178. BuildManager::instance().save(buildDataPath);
  179. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  180. saveEditorSettings();
  181. saveProjectSettings();
  182. gProjectLibrary().saveLibrary();
  183. }
  184. void EditorApplication::unloadProject()
  185. {
  186. if (!isProjectLoaded())
  187. return;
  188. saveProject();
  189. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  190. BuildManager::instance().clear();
  191. UndoRedo::instance().clear();
  192. EditorWidgetManager::instance().closeAll();
  193. gProjectLibrary().unloadLibrary();
  194. Resources::instance().unloadAllUnused();
  195. gCoreSceneManager().clearScene();
  196. mProjectPath = Path::BLANK;
  197. mProjectName = StringUtil::WBLANK;
  198. mIsProjectLoaded = false;
  199. }
  200. void EditorApplication::loadProject(const Path& projectPath)
  201. {
  202. unloadProject();
  203. mProjectPath = projectPath;
  204. mProjectName = projectPath.getWTail();
  205. mIsProjectLoaded = true;
  206. loadProjectSettings();
  207. Path buildDataPath = getProjectPath();
  208. buildDataPath.append(BUILD_DATA_PATH);
  209. BuildManager::instance().load(buildDataPath);
  210. gProjectLibrary().loadLibrary();
  211. // Do this before restoring windows to ensure types are loaded
  212. ScriptManager::instance().reload();
  213. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  214. if (layout != nullptr)
  215. EditorWidgetManager::instance().setLayout(layout);
  216. }
  217. void EditorApplication::createProject(const Path& path)
  218. {
  219. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  220. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  221. if (!FileSystem::exists(resourceDir))
  222. FileSystem::createDir(resourceDir);
  223. if (!FileSystem::exists(internalResourcesDir))
  224. FileSystem::createDir(internalResourcesDir);
  225. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  226. defaultLayoutPath.append(BuiltinEditorResources::getDefaultWidgetLayoutPath());
  227. if (FileSystem::exists(defaultLayoutPath))
  228. {
  229. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  230. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  231. }
  232. }
  233. bool EditorApplication::isValidProjectPath(const Path& path)
  234. {
  235. if (!path.isAbsolute())
  236. return false;
  237. if (!FileSystem::isDirectory(path))
  238. return false;
  239. return true;
  240. }
  241. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  242. {
  243. Path layoutPath = getProjectPath();
  244. layoutPath.append(WIDGET_LAYOUT_PATH);
  245. if(FileSystem::exists(layoutPath))
  246. {
  247. FileDecoder fs(layoutPath);
  248. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  249. }
  250. return nullptr;
  251. }
  252. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  253. {
  254. Path layoutPath = getProjectPath();
  255. layoutPath.append(WIDGET_LAYOUT_PATH);
  256. FileEncoder fs(layoutPath);
  257. fs.encode(layout.get());
  258. }
  259. void EditorApplication::loadEditorSettings()
  260. {
  261. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  262. absoluteDataPath.append(getEditorSettingsPath());
  263. if (FileSystem::exists(absoluteDataPath))
  264. {
  265. FileDecoder fs(absoluteDataPath);
  266. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  267. }
  268. if (mEditorSettings == nullptr)
  269. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  270. }
  271. void EditorApplication::saveEditorSettings()
  272. {
  273. if (mEditorSettings == nullptr)
  274. return;
  275. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  276. absoluteDataPath.append(getEditorSettingsPath());
  277. FileEncoder fs(absoluteDataPath);
  278. fs.encode(mEditorSettings.get());
  279. }
  280. void EditorApplication::loadProjectSettings()
  281. {
  282. if (isProjectLoaded())
  283. {
  284. Path absoluteDataPath = getProjectPath();
  285. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  286. if (FileSystem::exists(absoluteDataPath))
  287. {
  288. FileDecoder fs(absoluteDataPath);
  289. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  290. }
  291. }
  292. if (mProjectSettings == nullptr)
  293. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  294. }
  295. void EditorApplication::saveProjectSettings()
  296. {
  297. if (mProjectSettings == nullptr || !isProjectLoaded())
  298. return;
  299. Path absoluteDataPath = getProjectPath();
  300. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  301. FileEncoder fs(absoluteDataPath);
  302. fs.encode(mProjectSettings.get());
  303. }
  304. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  305. {
  306. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  307. }
  308. RenderAPIPlugin EditorApplication::toEngineRenderAPI(EditorRenderAPI renderAPI)
  309. {
  310. if (renderAPI == EditorRenderAPI::DX11)
  311. return RenderAPIPlugin::DX11;
  312. else if (renderAPI == EditorRenderAPI::OpenGL)
  313. return RenderAPIPlugin::OpenGL;
  314. BS_EXCEPT(InvalidStateException, "Unsupported render API.");
  315. return RenderAPIPlugin::DX11;
  316. }
  317. EditorApplication& gEditorApplication()
  318. {
  319. return static_cast<EditorApplication&>(EditorApplication::instance());
  320. }
  321. }