2
0

BsEditorApplication.cpp 11 KB

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