BsEditorApplication.cpp 11 KB

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