BsEditorApplication.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. // DEBUG ONLY
  19. #include "DbgEditorWidget1.h"
  20. #include "DbgEditorWidget2.h"
  21. #include "BsResources.h"
  22. #include "BsSceneObject.h"
  23. #include "BsImporter.h"
  24. #include "BsGpuProgram.h"
  25. #include "BsShader.h"
  26. #include "BsTexture.h"
  27. #include "BsMaterial.h"
  28. #include "BsTechnique.h"
  29. #include "BsPass.h"
  30. #include "BsRenderable.h"
  31. #include "BsDbgTestGameObjectRef.h"
  32. #include "BsVirtualInput.h"
  33. #include "BsFolderMonitor.h"
  34. #include "BsProjectLibrary.h"
  35. #include "BsCamera.h"
  36. #include "BsGUIWidget.h"
  37. #include "BsGUIArea.h"
  38. #include "BsGUIButton.h"
  39. #include "BsGUILayout.h"
  40. #include "BsEvent.h"
  41. #include "BsCoreRenderer.h"
  42. #include "BsEditorSettings.h"
  43. #include "BsMesh.h"
  44. #include "BsMath.h"
  45. namespace BansheeEngine
  46. {
  47. const Path EditorApplication::WIDGET_LAYOUT_PATH = L"Internal\\Layout.asset";
  48. const Path EditorApplication::BUILD_DATA_PATH = L"Internal\\BuildData.asset";
  49. RENDER_WINDOW_DESC createRenderWindowDesc()
  50. {
  51. RENDER_WINDOW_DESC renderWindowDesc;
  52. renderWindowDesc.videoMode = VideoMode(1280, 720);
  53. renderWindowDesc.title = "BansheeEditor";
  54. renderWindowDesc.fullscreen = false;
  55. renderWindowDesc.border = WindowBorder::None;
  56. return renderWindowDesc;
  57. }
  58. EditorApplication::EditorApplication(RenderSystemPlugin renderSystemPlugin)
  59. :Application(createRenderWindowDesc(), renderSystemPlugin, RendererPlugin::Default),
  60. mActiveRSPlugin(renderSystemPlugin), mSBansheeEditorPlugin(nullptr)
  61. {
  62. // TODO - Load project settings
  63. mEditorSettings = bs_shared_ptr<EditorSettings>();
  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("Delete", BC_DELETE);
  74. }
  75. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  76. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  77. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  78. Importer::instance()._registerAssetImporter(resourceImporter);
  79. ProjectLibrary::startUp(getProjectPath());
  80. UndoRedo::startUp();
  81. EditorWindowManager::startUp();
  82. EditorWidgetManager::startUp();
  83. ScenePicking::startUp();
  84. Selection::startUp();
  85. GizmoManager::startUp();
  86. BuildManager::startUp();
  87. CodeEditorManager::startUp();
  88. }
  89. EditorApplication::~EditorApplication()
  90. {
  91. BuildManager::instance().save(BUILD_DATA_PATH);
  92. CodeEditorManager::shutDown();
  93. BuildManager::shutDown();
  94. GizmoManager::shutDown();
  95. Selection::shutDown();
  96. ScenePicking::shutDown();
  97. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  98. // TODO - Save project settings
  99. EditorWidgetManager::shutDown();
  100. EditorWindowManager::shutDown();
  101. UndoRedo::shutDown();
  102. // We purposely don't unload this plugin, it needs to be unloaded after
  103. // all mono assemblies have been unloaded (since their finalizers will call
  104. // into the plugin). So we leave it to be unloaded automatically on app exit
  105. shutdownPlugin(mSBansheeEditorPlugin);
  106. /************************************************************************/
  107. /* DEBUG CODE */
  108. /************************************************************************/
  109. gResources().unload(mTestTexRef);
  110. gResources().unload(mDbgMeshRef);
  111. gResources().unload(mTestShader);
  112. gResources().unload(mTestMaterial);
  113. mTestMaterial = nullptr;
  114. mTestTexRef = nullptr;
  115. mDbgMeshRef = nullptr;
  116. mTestShader = nullptr;
  117. /************************************************************************/
  118. /* END DEBUG CODE */
  119. /************************************************************************/
  120. ProjectLibrary::shutDown();
  121. BuiltinEditorResources::shutDown();
  122. }
  123. void EditorApplication::onStartUp()
  124. {
  125. Application::onStartUp();
  126. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  127. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin); // Managed part of the editor
  128. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  129. if (layout != nullptr)
  130. EditorWidgetManager::instance().setLayout(layout);
  131. BuildManager::instance().load(BUILD_DATA_PATH);
  132. /************************************************************************/
  133. /* DEBUG CODE */
  134. /************************************************************************/
  135. HShader dummyParsedShader = Importer::instance().import<Shader>("..\\..\\..\\..\\Data\\Raw\\Engine\\Shaders\\TestFX.bsl");
  136. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  137. RenderAPICore* renderSystem = RenderAPICore::instancePtr();
  138. HSceneObject testModelGO = SceneObject::create("TestMesh");
  139. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  140. WString testShaderLoc = L"..\\..\\..\\..\\Data\\Test.bsl";;
  141. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  142. gResources().save(mTestShader, L"C:\\testShader.asset", true);
  143. gResources().unload(mTestShader);
  144. mTestShader = gResources().load<Shader>(L"C:\\testShader.asset");
  145. mTestMaterial = Material::create(mTestShader);
  146. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.tga"));
  147. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.fbx"));
  148. gResources().save(mTestTexRef, L"C:\\ExportTest.tex", true);
  149. gResources().save(mDbgMeshRef, L"C:\\ExportMesh.mesh", true);
  150. gResources().unload(mTestTexRef);
  151. gResources().unload(mDbgMeshRef);
  152. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  153. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  154. mDbgMeshRef.blockUntilLoaded();
  155. mDbgMeshRef->blockUntilCoreInitialized();
  156. mTestTexRef.blockUntilLoaded();
  157. mTestTexRef->blockUntilCoreInitialized();
  158. mTestMaterial->setTexture("tex", mTestTexRef);
  159. gResources().save(mTestShader, L"C:\\ExportShader.asset", true);
  160. gResources().save(mTestMaterial, L"C:\\ExportMaterial.mat", true);
  161. gResources().unload(mTestMaterial);
  162. gResources().unload(mTestShader);
  163. mTestShader = gResources().load<Shader>(L"C:\\ExportShader.asset");
  164. mTestMaterial = gResources().load<Material>(L"C:\\ExportMaterial.mat");
  165. testRenderable->setMesh(mDbgMeshRef);
  166. testRenderable->setMaterial(0, mTestMaterial);
  167. GameObjectHandle<DbgTestGameObjectRef> dbgTestGameObjectRef = testModelGO->addComponent<DbgTestGameObjectRef>();
  168. dbgTestGameObjectRef->mRenderable = testRenderable;
  169. HSceneObject clone = testModelGO->clone();
  170. GameObjectHandle<DbgTestGameObjectRef> clonedDbgTestGameObjectRef = clone->getComponent<DbgTestGameObjectRef>();
  171. clone->setScale(Vector3::ONE * 0.01f);
  172. testModelGO->destroy();
  173. //Win32FolderMonitor* folderMonitor = bs_new<Win32FolderMonitor>();
  174. //FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  175. // (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  176. //folderMonitor->startMonitor(L"D:\\TestDetect", true, folderChanges);
  177. //HTexture dbgCursor = static_resource_cast<Texture>(Importer::instance().import(L"C:\\CursorDbg.psd"));
  178. //PixelDataPtr cursorPixelData = dbgCursor->allocateSubresourceBuffer(0);
  179. //gMainSyncedCA().readSubresource(dbgCursor.getInternalPtr(), 0, cursorPixelData);
  180. //gMainSyncedCA().submitToCoreThread(true);
  181. /************************************************************************/
  182. /* MODAL WINDOW */
  183. /************************************************************************/
  184. //RENDER_WINDOW_DESC modalWindowDesc;
  185. //modalWindowDesc.width = 200;
  186. //modalWindowDesc.height = 200;
  187. //modalWindowDesc.left = 0;
  188. //modalWindowDesc.top = 0;
  189. //modalWindowDesc.title = "ModalWindow";
  190. //modalWindowDesc.fullscreen = false;
  191. //modalWindowDesc.border = WindowBorder::None;
  192. //modalWindowDesc.toolWindow = true;
  193. //modalWindowDesc.modal = true;
  194. //RenderWindowPtr modalWindow = RenderWindow::create(modalWindowDesc, gApplication().getPrimaryWindow());
  195. //HSceneObject modalSceneObject = SceneObject::create("ModalWindow");
  196. //HCamera modalCamera = modalSceneObject->addComponent<Camera>();
  197. //modalCamera->initialize(modalWindow, 0.0f, 0.0f, 1.0f, 1.0f);
  198. //modalCamera->setNearClipDistance(5);
  199. //modalCamera->setAspectRatio(1.0f);
  200. //modalCamera->setIgnoreSceneRenderables(true);
  201. //HGUIWidget modalGUI = modalSceneObject->addComponent<GUIWidget>(modalCamera->getViewport().get());
  202. //modalGUI->setDepth(128);
  203. //modalGUI->setSkin(EditorGUI::instance().getSkin());
  204. //GUIArea* modalArea = GUIArea::createStretchedXY(*modalGUI, 0, 0, 0, 0, 500);
  205. //GUIButton* modalButton = GUIButton::create(*modalGUI, HString(L"Close"));
  206. //modalButton->onClick.connect(std::bind(&EditorApplication::closeModalWindow, modalWindow, modalSceneObject));
  207. //modalArea->getLayout().addElement(modalButton);
  208. /************************************************************************/
  209. /* END DEBUG CODE */
  210. /************************************************************************/
  211. DbgEditorWidget1::open(); // DEBUG ONLY
  212. DbgEditorWidget2::open(); // DEBUG ONLY
  213. }
  214. void EditorApplication::onShutDown()
  215. {
  216. Application::onShutDown();
  217. }
  218. void EditorApplication::startUp(RenderSystemPlugin renderSystemPlugin)
  219. {
  220. CoreApplication::startUp<EditorApplication>(renderSystemPlugin);
  221. }
  222. void EditorApplication::closeModalWindow(RenderWindowPtr window, HSceneObject sceneObject)
  223. {
  224. //sceneObject->destroy();
  225. window->destroy();
  226. }
  227. void EditorApplication::preUpdate()
  228. {
  229. Application::preUpdate();
  230. EditorWidgetManager::instance().update();
  231. }
  232. void EditorApplication::postUpdate()
  233. {
  234. Application::postUpdate();
  235. ProjectLibrary::instance().update();
  236. EditorWindowManager::instance().update();
  237. }
  238. bool EditorApplication::isProjectLoaded() const
  239. {
  240. return true; // TODO - DEBUG ONLY
  241. }
  242. bool EditorApplication::isGameViewFocused() const
  243. {
  244. return false; // TODO
  245. }
  246. bool EditorApplication::isSceneViewFocused() const
  247. {
  248. return true; // TODO
  249. }
  250. const Path& EditorApplication::getProjectPath() const
  251. {
  252. static Path dummyProjectPath = L"D:\\DummyBansheeProject\\";
  253. return dummyProjectPath;
  254. }
  255. const WString& EditorApplication::getProjectName() const
  256. {
  257. static WString dummyProjectName = L"DummyBansheeProject";
  258. return dummyProjectName;
  259. }
  260. Path EditorApplication::getEditorAssemblyPath() const
  261. {
  262. Path assemblyPath = getBuiltinAssemblyFolder();
  263. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  264. return assemblyPath;
  265. }
  266. Path EditorApplication::getEditorScriptAssemblyPath() const
  267. {
  268. Path assemblyPath = getScriptAssemblyFolder();
  269. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  270. return assemblyPath;
  271. }
  272. Path EditorApplication::getScriptAssemblyFolder() const
  273. {
  274. Path assemblyFolder = getProjectPath();
  275. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  276. return assemblyFolder;
  277. }
  278. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  279. {
  280. Path layoutPath = getProjectPath();
  281. layoutPath.append(WIDGET_LAYOUT_PATH);
  282. if(FileSystem::exists(layoutPath))
  283. {
  284. FileDecoder fs(layoutPath);
  285. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  286. }
  287. return nullptr;
  288. }
  289. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  290. {
  291. Path layoutPath = getProjectPath();
  292. layoutPath.append(WIDGET_LAYOUT_PATH);
  293. FileEncoder fs(layoutPath);
  294. fs.encode(layout.get());
  295. }
  296. EditorApplication& gEditorApplication()
  297. {
  298. return static_cast<EditorApplication&>(EditorApplication::instance());
  299. }
  300. }