BsEditorApplication.cpp 10 KB

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