BsEditorApplication.cpp 10 KB

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