BsDockManager.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "EditorWindow/BsDockManager.h"
  4. #include "EditorWindow/BsEditorWidgetContainer.h"
  5. #include "EditorWindow/BsEditorWidget.h"
  6. #include "EditorWindow/BsEditorWidgetManager.h"
  7. #include "Math/BsMath.h"
  8. #include "Error/BsException.h"
  9. #include "Mesh/BsMesh.h"
  10. #include "Material/BsMaterial.h"
  11. #include "Math/BsVector2.h"
  12. #include "BsCoreApplication.h"
  13. #include "Renderer/BsRendererManager.h"
  14. #include "Renderer/BsRenderer.h"
  15. #include "Scene/BsSceneObject.h"
  16. #include "GUI/BsGUIManager.h"
  17. #include "Utility/BsBuiltinEditorResources.h"
  18. #include "GUI/BsCGUIWidget.h"
  19. #include "Components/BsCCamera.h"
  20. #include "GUI/BsDragAndDropManager.h"
  21. #include "GUI/BsGUIDockSlider.h"
  22. #include "RenderAPI/BsVertexDataDesc.h"
  23. #include "EditorWindow/BsDockManagerLayout.h"
  24. #include "EditorWindow/BsEditorWindow.h"
  25. #include "GUI/BsGUIPanel.h"
  26. #include "CoreThread/BsCoreThread.h"
  27. #include "Renderer/BsRendererUtility.h"
  28. using namespace std::placeholders;
  29. namespace bs
  30. {
  31. const UINT32 DockManager::DockContainer::SLIDER_SIZE = 3;
  32. const UINT32 DockManager::DockContainer::MIN_CHILD_SIZE = 20;
  33. DockManager::DockContainer::DockContainer(DockManager* manager)
  34. : mIsLeaf(true), mParent(nullptr), mManager(manager), mWidgets(nullptr), mSlider(nullptr), mSplitPosition(0.5f)
  35. , mIsHorizontal(false)
  36. {
  37. mChildren[0] = nullptr;
  38. mChildren[1] = nullptr;
  39. }
  40. DockManager::DockContainer::DockContainer(DockManager* manager, DockContainer* parent)
  41. : mIsLeaf(false), mParent(parent), mManager(manager), mWidgets(nullptr), mSlider(nullptr), mSplitPosition(0.5f)
  42. , mIsHorizontal(false)
  43. {
  44. mChildren[0] = nullptr;
  45. mChildren[1] = nullptr;
  46. }
  47. DockManager::DockContainer::~DockContainer()
  48. {
  49. if (mIsLeaf)
  50. {
  51. if (mWidgets != nullptr)
  52. bs_delete(mWidgets);
  53. if (mGUIWidgetSO != nullptr)
  54. mGUIWidgetSO->destroy();
  55. }
  56. if(!mIsLeaf)
  57. {
  58. if(mChildren[0] != nullptr)
  59. bs_delete(mChildren[0]);
  60. if(mChildren[1] != nullptr)
  61. bs_delete(mChildren[1]);
  62. }
  63. if(mSlider != nullptr)
  64. {
  65. GUIElement::destroy(mSlider);
  66. mSlider = nullptr;
  67. }
  68. }
  69. void DockManager::DockContainer::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  70. {
  71. if(mIsLeaf)
  72. {
  73. if(mWidgets != nullptr)
  74. {
  75. mWidgets->setPosition(x, y);
  76. mWidgets->setSize(width, height);
  77. }
  78. }
  79. mArea.x = x;
  80. mArea.y = y;
  81. mArea.width = width;
  82. mArea.height = height;
  83. updateChildAreas();
  84. }
  85. void DockManager::DockContainer::updateChildAreas()
  86. {
  87. if(!mIsLeaf && mChildren[0] != nullptr && mChildren[1] != nullptr)
  88. {
  89. if(mIsHorizontal)
  90. {
  91. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  92. UINT32 sizeTop = Math::floorToInt(remainingSize * mSplitPosition);
  93. UINT32 sizeBottom = remainingSize - sizeTop;
  94. mChildren[0]->setArea(mArea.x, mArea.y, mArea.width, sizeTop);
  95. mChildren[1]->setArea(mArea.x, mArea.y + sizeTop + SLIDER_SIZE, mArea.width, sizeBottom);
  96. mSlider->setWidth(mArea.width);
  97. mSlider->setHeight(SLIDER_SIZE);
  98. mSlider->setPosition(mArea.x, mArea.y + sizeTop);
  99. }
  100. else
  101. {
  102. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  103. UINT32 sizeLeft = Math::floorToInt(remainingSize * mSplitPosition);
  104. UINT32 sizeRight = remainingSize - sizeLeft;
  105. mChildren[0]->setArea(mArea.x, mArea.y, sizeLeft, mArea.height);
  106. mChildren[1]->setArea(mArea.x + sizeLeft + SLIDER_SIZE, mArea.y, sizeRight, mArea.height);
  107. mSlider->setWidth(SLIDER_SIZE);
  108. mSlider->setHeight(mArea.height);
  109. mSlider->setPosition(mArea.x + sizeLeft, mArea.y);
  110. }
  111. }
  112. }
  113. void DockManager::DockContainer::makeLeaf(EditorWindowBase* parentWindow)
  114. {
  115. mGUIWidgetSO = SceneObject::create("DockContainer", SOF_Internal | SOF_Persistent | SOF_DontSave);
  116. HGUIWidget guiWidget = mGUIWidgetSO->addComponent<CGUIWidget>(parentWindow->getGUICamera());
  117. guiWidget->setDepth(128);
  118. guiWidget->setSkin(BuiltinEditorResources::instance().getSkin());
  119. mIsLeaf = true;
  120. mWidgets = bs_new<EditorWidgetContainer>(guiWidget->_getInternal(), parentWindow);
  121. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  122. mWidgets->onMaximized.connect(std::bind(&DockManager::DockContainer::maximizeClicked, this));
  123. if(mSlider != nullptr)
  124. {
  125. GUIElement::destroy(mSlider);
  126. mSlider = nullptr;
  127. }
  128. mWidgets->setPosition(mArea.x, mArea.y);
  129. mWidgets->setSize(mArea.width, mArea.height);
  130. }
  131. void DockManager::DockContainer::makeLeaf(const HSceneObject& guiWidgetSO, EditorWidgetContainer* existingContainer)
  132. {
  133. mIsLeaf = true;
  134. mWidgets = existingContainer;
  135. mGUIWidgetSO = guiWidgetSO;
  136. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  137. mWidgets->onMaximized.connect(std::bind(&DockManager::DockContainer::maximizeClicked, this));
  138. if(mSlider != nullptr)
  139. {
  140. GUIElement::destroy(mSlider);
  141. mSlider = nullptr;
  142. }
  143. mWidgets->setPosition(mArea.x, mArea.y);
  144. mWidgets->setSize(mArea.width, mArea.height);
  145. }
  146. void DockManager::DockContainer::addLeft(EditorWidgetBase* widget)
  147. {
  148. if(mIsLeaf)
  149. splitContainer(false, true);
  150. mChildren[0]->addWidget(widget);
  151. }
  152. void DockManager::DockContainer::addRight(EditorWidgetBase* widget)
  153. {
  154. if(mIsLeaf)
  155. splitContainer(false, false);
  156. mChildren[1]->addWidget(widget);
  157. }
  158. void DockManager::DockContainer::addTop(EditorWidgetBase* widget)
  159. {
  160. if(mIsLeaf)
  161. splitContainer(true, true);
  162. mChildren[0]->addWidget(widget);
  163. }
  164. void DockManager::DockContainer::addBottom(EditorWidgetBase* widget)
  165. {
  166. if(mIsLeaf)
  167. splitContainer(true, false);
  168. mChildren[1]->addWidget(widget);
  169. }
  170. void DockManager::DockContainer::splitContainer(bool horizontal, bool newChildIsFirst, float splitPosition)
  171. {
  172. DockContainer* children[2];
  173. UINT32 idxA = newChildIsFirst ? 0 : 1;
  174. UINT32 idxB = (idxA + 1) % 2;
  175. children[idxA] = bs_new<DockContainer>(mManager, this);
  176. children[idxB] = bs_new<DockContainer>(mManager, this);
  177. mWidgets->onWidgetClosed.clear();
  178. mWidgets->onMaximized.clear();
  179. children[idxA]->makeLeaf(mManager->mParentWindow);
  180. children[idxB]->makeLeaf(mGUIWidgetSO, mWidgets);
  181. mWidgets = nullptr;
  182. mGUIWidgetSO = nullptr;
  183. makeSplit(children[0], children[1], horizontal, splitPosition);
  184. }
  185. void DockManager::DockContainer::makeSplit(DockManager::DockContainer* first, DockManager::DockContainer* second, bool horizontal, float splitPosition)
  186. {
  187. mChildren[0] = first;
  188. mChildren[1] = second;
  189. mIsLeaf = false;
  190. mIsHorizontal = horizontal;
  191. mSplitPosition = splitPosition;
  192. if (mWidgets != nullptr)
  193. {
  194. bs_delete(mWidgets);
  195. mWidgets = nullptr;
  196. }
  197. if (mGUIWidgetSO != nullptr)
  198. {
  199. mGUIWidgetSO->destroy(true);
  200. mGUIWidgetSO = nullptr;
  201. }
  202. if (mSlider != nullptr)
  203. {
  204. GUIElement::destroy(mSlider);
  205. mSlider = nullptr;
  206. }
  207. mSlider = GUIDockSlider::create(horizontal, "Separator");
  208. mManager->_getParentWidget()->getPanel()->addElement(mSlider);
  209. mSlider->onDragged.connect(std::bind(&DockManager::DockContainer::sliderDragged, this, _1));
  210. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  211. }
  212. void DockManager::DockContainer::addWidget(EditorWidgetBase* widget)
  213. {
  214. if(!mIsLeaf)
  215. return;
  216. mWidgets->add(*widget);
  217. }
  218. void DockManager::DockContainer::addWidget(const String& name)
  219. {
  220. if(!mIsLeaf)
  221. return;
  222. EditorWidgetManager::instance().create(name, *mWidgets);
  223. }
  224. void DockManager::DockContainer::sliderDragged(const Vector2I& delta)
  225. {
  226. if(mIsHorizontal && delta.y != 0)
  227. {
  228. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.height - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  229. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  230. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.y, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  231. updateChildAreas();
  232. }
  233. else if(!mIsHorizontal && delta.x != 0)
  234. {
  235. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.width - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  236. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  237. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.x, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  238. updateChildAreas();
  239. }
  240. }
  241. void DockManager::DockContainer::widgetRemoved()
  242. {
  243. assert(mIsLeaf);
  244. if(mWidgets->getNumWidgets() == 0)
  245. {
  246. if(mParent == nullptr) // We're root so we just reset ourselves, can't delete root
  247. {
  248. if (mManager->mIsMaximized)
  249. {
  250. mManager->mMaximizedContainer = this;
  251. mManager->mIsMaximized = false;
  252. }
  253. bs_delete(mWidgets);
  254. mWidgets = nullptr;
  255. mGUIWidgetSO->destroy(true);
  256. mGUIWidgetSO = nullptr;
  257. mIsLeaf = true;
  258. mSplitPosition = 0.5f;
  259. mIsHorizontal = false;
  260. }
  261. else
  262. {
  263. // Replace our parent with our sibling
  264. DockContainer* sibling = nullptr;
  265. if(mParent->mChildren[0] == this)
  266. sibling = mParent->mChildren[1];
  267. else
  268. sibling = mParent->mChildren[0];
  269. if (sibling->mIsLeaf)
  270. {
  271. sibling->mWidgets->onWidgetClosed.clear();
  272. sibling->mWidgets->onMaximized.clear();
  273. mParent->makeLeaf(sibling->mGUIWidgetSO, sibling->mWidgets);
  274. sibling->mWidgets = nullptr;
  275. sibling->mGUIWidgetSO = nullptr;
  276. }
  277. else
  278. {
  279. mParent->makeSplit(sibling->mChildren[0], sibling->mChildren[1], sibling->mIsHorizontal, sibling->mSplitPosition);
  280. sibling->mChildren[0]->mParent = mParent;
  281. sibling->mChildren[1]->mParent = mParent;
  282. sibling->mChildren[0] = nullptr;
  283. sibling->mChildren[1] = nullptr;
  284. }
  285. bs_delete(sibling);
  286. bs_delete(this);
  287. }
  288. }
  289. }
  290. void DockManager::DockContainer::maximizeClicked()
  291. {
  292. mManager->toggleMaximize(this);
  293. }
  294. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  295. {
  296. if(mIsLeaf)
  297. {
  298. if(mWidgets == widgetContainer)
  299. return this;
  300. else
  301. return nullptr;
  302. }
  303. else
  304. {
  305. if(mChildren[0] != nullptr)
  306. {
  307. DockContainer* foundContainer = mChildren[0]->find(widgetContainer);
  308. if (foundContainer != nullptr)
  309. return foundContainer;
  310. }
  311. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  312. {
  313. DockContainer* foundContainer = mChildren[1]->find(widgetContainer);
  314. if (foundContainer != nullptr)
  315. return foundContainer;
  316. }
  317. }
  318. return nullptr;
  319. }
  320. DockManager::DockContainer* DockManager::DockContainer::findAtPos(const Vector2I& pos)
  321. {
  322. if(mIsLeaf)
  323. {
  324. if(mArea.contains(pos))
  325. {
  326. return this;
  327. }
  328. }
  329. else
  330. {
  331. if (mChildren[0] != nullptr)
  332. {
  333. DockContainer* foundContainer = mChildren[0]->findAtPos(pos);
  334. if (foundContainer != nullptr)
  335. return foundContainer;
  336. }
  337. if(mChildren[1] != nullptr)
  338. {
  339. DockContainer* foundContainer = mChildren[1]->findAtPos(pos);
  340. if (foundContainer != nullptr)
  341. return foundContainer;
  342. }
  343. }
  344. return nullptr;
  345. }
  346. Rect2I DockManager::DockContainer::getContentBounds() const
  347. {
  348. if(!mIsLeaf || mWidgets == nullptr)
  349. return mArea;
  350. return mWidgets->getContentBounds();
  351. }
  352. void DockManager::DockContainer::update()
  353. {
  354. if (mIsLeaf)
  355. {
  356. if (mWidgets != nullptr)
  357. mWidgets->update();
  358. }
  359. else
  360. {
  361. if (mChildren[0] != nullptr)
  362. mChildren[0]->update();
  363. if (mChildren[1] != nullptr)
  364. mChildren[1]->update();
  365. }
  366. }
  367. DockManager::DockManager(EditorWindowBase* parentWindow, const GUIDimensions& dimensions)
  368. : GUIElementContainer(dimensions), mParentWindow(parentWindow), mRootContainer(this), mIsMaximized(false)
  369. , mMaximizedContainer(nullptr), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None)
  370. , mShowOverlay(false)
  371. {
  372. mTopDropPolygon = bs_newN<Vector2>(4);
  373. mBotDropPolygon = bs_newN<Vector2>(4);
  374. mLeftDropPolygon = bs_newN<Vector2>(4);
  375. mRightDropPolygon = bs_newN<Vector2>(4);
  376. for(UINT32 i = 0; i < 4; i++)
  377. {
  378. mTopDropPolygon[i] = Vector2::ZERO;
  379. mBotDropPolygon[i] = Vector2::ZERO;
  380. mLeftDropPolygon[i] = Vector2::ZERO;
  381. mRightDropPolygon[i] = Vector2::ZERO;
  382. }
  383. HMaterial dropOverlayMat = BuiltinEditorResources::instance().createDockDropOverlayMaterial();
  384. mRenderer = RendererExtension::create<ct::DockOverlayRenderer>(dropOverlayMat->getCore());
  385. }
  386. DockManager::~DockManager()
  387. {
  388. bs_deleteN(mTopDropPolygon, 4);
  389. bs_deleteN(mBotDropPolygon, 4);
  390. bs_deleteN(mLeftDropPolygon, 4);
  391. bs_deleteN(mRightDropPolygon, 4);
  392. }
  393. DockManager* DockManager::create(EditorWindowBase* parentWindow)
  394. {
  395. return new (bs_alloc<DockManager>()) DockManager(parentWindow, GUIDimensions::create());
  396. }
  397. void DockManager::update()
  398. {
  399. if(!DragAndDropManager::instance().isDragInProgress())
  400. {
  401. mHighlightedDropLoc = DockLocation::None;
  402. mShowOverlay = false;
  403. }
  404. mRootContainer.update();
  405. HCamera camera = mParentWindow->getGUICamera();
  406. ct::DockOverlayRenderer* renderer = mRenderer.get();
  407. gCoreThread().queueCommand(std::bind(&ct::DockOverlayRenderer::updateData, renderer, camera->_getCamera()->getCore(),
  408. mDropOverlayMesh->getCore(), mShowOverlay, mHighlightedDropLoc));
  409. }
  410. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  411. {
  412. if(relativeTo != nullptr)
  413. {
  414. DockContainer* container = mRootContainer.find(relativeTo);
  415. if(container == nullptr)
  416. BS_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  417. switch(location)
  418. {
  419. case DockLocation::Left:
  420. container->addLeft(widgetToInsert);
  421. break;
  422. case DockLocation::Right:
  423. container->addRight(widgetToInsert);
  424. break;
  425. case DockLocation::Top:
  426. container->addTop(widgetToInsert);
  427. break;
  428. case DockLocation::Bottom:
  429. container->addBottom(widgetToInsert);
  430. break;
  431. case DockLocation::None:
  432. break;
  433. }
  434. }
  435. else
  436. {
  437. if(mRootContainer.mWidgets != nullptr)
  438. BS_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  439. mRootContainer.makeLeaf(mParentWindow);
  440. mRootContainer.addWidget(widgetToInsert);
  441. }
  442. }
  443. void DockManager::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  444. {
  445. mRootContainer.setArea(x, y, width, height);
  446. mArea = Rect2I(x, y, width, height);
  447. updateDropOverlay(x, y, width, height);
  448. }
  449. void DockManager::closeAll()
  450. {
  451. mRootContainer = DockContainer(this);
  452. mMouseOverContainer = nullptr;
  453. }
  454. SPtr<DockManagerLayout> DockManager::getLayout() const
  455. {
  456. struct StackElem
  457. {
  458. StackElem(DockManagerLayout::Entry* layoutEntry, const DockContainer* container)
  459. :layoutEntry(layoutEntry), container(container)
  460. { }
  461. DockManagerLayout::Entry* layoutEntry;
  462. const DockContainer* container;
  463. };
  464. auto GetWidgetNamesInContainer = [&] (const DockContainer* container)
  465. {
  466. Vector<String> widgetNames;
  467. if(container->mWidgets != nullptr)
  468. {
  469. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  470. for(UINT32 i = 0; i < numWidgets; i++)
  471. {
  472. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  473. widgetNames.push_back(widget->getName());
  474. }
  475. }
  476. return widgetNames;
  477. };
  478. if (mIsMaximized)
  479. {
  480. SPtr<DockManagerLayout> layout;
  481. if (mRestoredLayout != nullptr)
  482. layout = mRestoredLayout->clone();
  483. else
  484. layout = bs_shared_ptr_new<DockManagerLayout>();
  485. layout->setIsMaximized(true, GetWidgetNamesInContainer(mMaximizedContainer));
  486. return layout;
  487. }
  488. else
  489. {
  490. SPtr<DockManagerLayout> layout = bs_shared_ptr_new<DockManagerLayout>();
  491. DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  492. if (mRootContainer.mIsLeaf)
  493. {
  494. rootEntry->isLeaf = true;
  495. rootEntry->widgetNames = GetWidgetNamesInContainer(&mRootContainer);
  496. }
  497. else
  498. {
  499. rootEntry->isLeaf = false;
  500. rootEntry->horizontalSplit = mRootContainer.mIsHorizontal;
  501. rootEntry->splitPosition = mRootContainer.mSplitPosition;
  502. rootEntry->parent = nullptr;
  503. }
  504. Stack<StackElem> todo;
  505. todo.push(StackElem(rootEntry, &mRootContainer));
  506. while (!todo.empty())
  507. {
  508. StackElem currentElem = todo.top();
  509. todo.pop();
  510. if (!currentElem.container->mIsLeaf)
  511. {
  512. for (UINT32 i = 0; i < 2; i++)
  513. {
  514. if (currentElem.container->mChildren[i] == nullptr)
  515. continue;
  516. if (currentElem.container->mChildren[i]->mIsLeaf)
  517. {
  518. Vector<String> widgetNames = GetWidgetNamesInContainer(currentElem.container->mChildren[i]);
  519. currentElem.layoutEntry->children[i] =
  520. DockManagerLayout::Entry::createLeaf(currentElem.layoutEntry, i, widgetNames);
  521. }
  522. else
  523. {
  524. currentElem.layoutEntry->children[i] =
  525. DockManagerLayout::Entry::createContainer(currentElem.layoutEntry, i,
  526. currentElem.container->mChildren[i]->mSplitPosition,
  527. currentElem.container->mChildren[i]->mIsHorizontal);
  528. todo.push(StackElem(currentElem.layoutEntry->children[i], currentElem.container->mChildren[i]));
  529. }
  530. }
  531. }
  532. }
  533. return layout;
  534. }
  535. }
  536. void DockManager::setLayout(const SPtr<DockManagerLayout>& layout)
  537. {
  538. // Undock all currently docked widgets
  539. Vector<EditorWidgetBase*> undockedWidgets;
  540. std::function<void(DockContainer*)> undockWidgets = [&](DockContainer* container)
  541. {
  542. while (!container->mIsLeaf)
  543. {
  544. // Due to the way undocking works a container can be transfromed from non-leaf to leaf
  545. // if its child container is deleted, so we need to check to that specially
  546. undockWidgets(container->mChildren[0]);
  547. }
  548. if (container->mIsLeaf)
  549. {
  550. if (container->mWidgets != nullptr)
  551. {
  552. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  553. for (UINT32 i = 0; i < numWidgets; i++)
  554. {
  555. EditorWidgetBase* curWidget = container->mWidgets->getWidget(0);
  556. EditorWidgetManager::instance().close(curWidget);
  557. undockedWidgets.push_back(curWidget);
  558. }
  559. }
  560. }
  561. };
  562. undockWidgets(&mRootContainer);
  563. mRootContainer = DockContainer(this);
  564. // Load layout
  565. struct StackEntry
  566. {
  567. StackEntry(const DockManagerLayout::Entry* layoutEntry, DockContainer* container)
  568. :layoutEntry(layoutEntry), container(container)
  569. { }
  570. const DockManagerLayout::Entry* layoutEntry;
  571. DockContainer* container;
  572. };
  573. auto GetLeafEntry = [] (const DockManagerLayout::Entry* parentEntry, UINT32 childIdx) -> const DockManagerLayout::Entry*
  574. {
  575. while(true)
  576. {
  577. if(parentEntry->isLeaf)
  578. return parentEntry;
  579. parentEntry = parentEntry->children[childIdx];
  580. }
  581. return nullptr;
  582. };
  583. auto OpenWidgets = [&] (DockContainer* parent, const Vector<String>& widgetNames)
  584. {
  585. for(auto& widgetName : widgetNames)
  586. {
  587. parent->addWidget(widgetName);
  588. }
  589. };
  590. // Prune layout by removing invalid leafs (ones with no widgets, or widgets that no longer exist)
  591. layout->pruneInvalidLeaves();
  592. if (layout->isMaximized())
  593. {
  594. mRestoredLayout = layout->clone();
  595. mRestoredLayout->setIsMaximized(false, Vector<String>());
  596. const Vector<String>& maximizedWidgets = mRestoredLayout->getMaximizedWidgetNames();
  597. if (maximizedWidgets.size() > 0) // If zero, entire layout is empty
  598. {
  599. mRootContainer.makeLeaf(mParentWindow);
  600. OpenWidgets(&mRootContainer, maximizedWidgets);
  601. }
  602. }
  603. else
  604. {
  605. // Dock elements
  606. const DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  607. const DockManagerLayout::Entry* leafEntry = GetLeafEntry(rootEntry, 0);
  608. if (leafEntry->widgetNames.size() > 0) // If zero, entire layout is empty
  609. {
  610. mRootContainer.makeLeaf(mParentWindow);
  611. OpenWidgets(&mRootContainer, leafEntry->widgetNames);
  612. if (!rootEntry->isLeaf)
  613. {
  614. Stack<StackEntry> layoutTodo;
  615. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  616. while (!layoutTodo.empty())
  617. {
  618. StackEntry curEntry = layoutTodo.top();
  619. layoutTodo.pop();
  620. leafEntry = GetLeafEntry(curEntry.layoutEntry->children[1], 0);
  621. curEntry.container->splitContainer(curEntry.layoutEntry->horizontalSplit, false, curEntry.layoutEntry->splitPosition);
  622. DockContainer* otherChild = curEntry.container->mChildren[1];
  623. OpenWidgets(otherChild, leafEntry->widgetNames);
  624. if (!curEntry.layoutEntry->children[0]->isLeaf)
  625. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  626. if (!curEntry.layoutEntry->children[1]->isLeaf)
  627. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  628. }
  629. }
  630. }
  631. // Set container sizes
  632. {
  633. Stack<StackEntry> layoutTodo;
  634. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  635. while (!layoutTodo.empty())
  636. {
  637. StackEntry curEntry = layoutTodo.top();
  638. layoutTodo.pop();
  639. if (!curEntry.layoutEntry->isLeaf)
  640. {
  641. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  642. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  643. }
  644. }
  645. }
  646. }
  647. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  648. }
  649. void DockManager::toggleMaximize(DockContainer* container)
  650. {
  651. if (mIsMaximized)
  652. {
  653. if (mRestoredLayout != nullptr)
  654. setLayout(mRestoredLayout);
  655. mRestoredLayout = nullptr;
  656. mMaximizedContainer = nullptr;
  657. mIsMaximized = false;
  658. }
  659. else
  660. {
  661. mRestoredLayout = getLayout();
  662. mMaximizedContainer = container;
  663. Vector<String> maximizedWidgetNames;
  664. if (container->mWidgets != nullptr)
  665. {
  666. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  667. for (UINT32 i = 0; i < numWidgets; i++)
  668. {
  669. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  670. maximizedWidgetNames.push_back(widget->getName());
  671. }
  672. }
  673. SPtr<DockManagerLayout> maxLayout = bs_shared_ptr_new<DockManagerLayout>();
  674. DockManagerLayout::Entry& rootEntry = maxLayout->getRootEntry();
  675. rootEntry.isLeaf = true;
  676. rootEntry.widgetNames = maximizedWidgetNames;
  677. setLayout(maxLayout);
  678. mIsMaximized = true;
  679. }
  680. }
  681. void DockManager::updateClippedBounds()
  682. {
  683. // TODO - Clipping not actually accounted for but shouldn't matter as right now DockManager is only used in one specific situation
  684. mClippedBounds = mRootContainer.mArea;
  685. }
  686. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  687. {
  688. const static int spacing = 10;
  689. const static float innerScale = 0.15f;
  690. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  691. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  692. UINT32 innerOffset = outWidth > outHeight ? Math::floorToInt(innerScale * outHeight) : Math::floorToInt(innerScale * outWidth);
  693. UINT32 inWidth = outWidth - innerOffset;
  694. UINT32 inHeight = outHeight - innerOffset;
  695. INT32 inXOffset = Math::floorToInt((outWidth - inWidth) * 0.5f);
  696. INT32 inYOffset = Math::floorToInt((outHeight - inHeight) * 0.5f);
  697. Vector2 outTopLeft((float)x, (float)y);
  698. Vector2 outTopRight((float)(x + outWidth), (float)y);
  699. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  700. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  701. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  702. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  703. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  704. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  705. SPtr<VertexDataDesc> vertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  706. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  707. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  708. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(16, 24, vertexDesc);
  709. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  710. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  711. // Top
  712. Vector2 topOffset((float)spacing, 0.0f);
  713. mTopDropPolygon[0] = outTopLeft + topOffset;
  714. mTopDropPolygon[1] = outTopRight + topOffset;
  715. mTopDropPolygon[2] = inTopRight + topOffset;
  716. mTopDropPolygon[3] = inTopLeft + topOffset;
  717. vertIter.addValue(mTopDropPolygon[0]);
  718. vertIter.addValue(mTopDropPolygon[1]);
  719. vertIter.addValue(mTopDropPolygon[2]);
  720. vertIter.addValue(mTopDropPolygon[3]);
  721. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  722. UINT32 color32 = color.getAsRGBA();
  723. colIter.addValue(color32);
  724. colIter.addValue(color32);
  725. colIter.addValue(color32);
  726. colIter.addValue(color32);
  727. // Bottom
  728. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  729. mBotDropPolygon[0] = inBotLeft + botOffset;
  730. mBotDropPolygon[1] = inBotRight + botOffset;
  731. mBotDropPolygon[2] = outBotRight + botOffset;
  732. mBotDropPolygon[3] = outBotLeft + botOffset;
  733. vertIter.addValue(mBotDropPolygon[0]);
  734. vertIter.addValue(mBotDropPolygon[1]);
  735. vertIter.addValue(mBotDropPolygon[2]);
  736. vertIter.addValue(mBotDropPolygon[3]);
  737. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  738. color32 = color.getAsRGBA();
  739. colIter.addValue(color32);
  740. colIter.addValue(color32);
  741. colIter.addValue(color32);
  742. colIter.addValue(color32);
  743. // Left
  744. Vector2 leftOffset(0.0f, (float)spacing);
  745. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  746. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  747. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  748. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  749. vertIter.addValue(mLeftDropPolygon[0]);
  750. vertIter.addValue(mLeftDropPolygon[1]);
  751. vertIter.addValue(mLeftDropPolygon[2]);
  752. vertIter.addValue(mLeftDropPolygon[3]);
  753. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  754. color32 = color.getAsRGBA();
  755. colIter.addValue(color32);
  756. colIter.addValue(color32);
  757. colIter.addValue(color32);
  758. colIter.addValue(color32);
  759. // Right
  760. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  761. mRightDropPolygon[0] = inTopRight + rightOffset;
  762. mRightDropPolygon[1] = outTopRight + rightOffset;
  763. mRightDropPolygon[2] = outBotRight + rightOffset;
  764. mRightDropPolygon[3] = inBotRight + rightOffset;
  765. vertIter.addValue(mRightDropPolygon[0]);
  766. vertIter.addValue(mRightDropPolygon[1]);
  767. vertIter.addValue(mRightDropPolygon[2]);
  768. vertIter.addValue(mRightDropPolygon[3]);
  769. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  770. color32 = color.getAsRGBA();
  771. colIter.addValue(color32);
  772. colIter.addValue(color32);
  773. colIter.addValue(color32);
  774. colIter.addValue(color32);
  775. UINT32* indexData = meshData->getIndices32();
  776. // Top
  777. indexData[0] = 0;
  778. indexData[1] = 1;
  779. indexData[2] = 2;
  780. indexData[3] = 0;
  781. indexData[4] = 2;
  782. indexData[5] = 3;
  783. // Bottom
  784. indexData[6] = 4;
  785. indexData[7] = 5;
  786. indexData[8] = 6;
  787. indexData[9] = 4;
  788. indexData[10] = 6;
  789. indexData[11] = 7;
  790. // Left
  791. indexData[12] = 8;
  792. indexData[13] = 9;
  793. indexData[14] = 10;
  794. indexData[15] = 8;
  795. indexData[16] = 10;
  796. indexData[17] = 11;
  797. // Right
  798. indexData[18] = 12;
  799. indexData[19] = 13;
  800. indexData[20] = 14;
  801. indexData[21] = 12;
  802. indexData[22] = 14;
  803. indexData[23] = 15;
  804. mDropOverlayMesh = Mesh::create(meshData);
  805. }
  806. bool DockManager::_mouseEvent(const GUIMouseEvent& event)
  807. {
  808. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  809. {
  810. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  811. return false;
  812. const Vector2I& widgetRelPos = event.getPosition();
  813. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  814. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  815. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  816. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  817. mMouseOverContainer = mRootContainer.findAtPos(windowPos);
  818. if(mMouseOverContainer == nullptr)
  819. mMouseOverContainer = &mRootContainer;
  820. Rect2I overlayBounds;
  821. if(mMouseOverContainer != nullptr)
  822. overlayBounds = mMouseOverContainer->getContentBounds();
  823. // Update mesh if needed
  824. if(mLastOverlayBounds != overlayBounds)
  825. {
  826. if(overlayBounds.width <= 0 || overlayBounds.height <= 0)
  827. mDropOverlayMesh = HMesh();
  828. else
  829. updateDropOverlay(overlayBounds.x, overlayBounds.y, overlayBounds.width, overlayBounds.height);
  830. mLastOverlayBounds = overlayBounds;
  831. }
  832. // Check if we need to highlight any drop locations
  833. if(mMouseOverContainer != nullptr)
  834. {
  835. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  836. mHighlightedDropLoc = DockLocation::Top;
  837. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  838. mHighlightedDropLoc = DockLocation::Bottom;
  839. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  840. mHighlightedDropLoc = DockLocation::Left;
  841. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  842. mHighlightedDropLoc = DockLocation::Right;
  843. else
  844. mHighlightedDropLoc = DockLocation::None;
  845. if(overlayBounds.contains(windowPos))
  846. mShowOverlay = true;
  847. else
  848. mShowOverlay = false;
  849. }
  850. else
  851. mShowOverlay = false;
  852. return false;
  853. }
  854. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  855. {
  856. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  857. return false;
  858. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  859. const Vector2I& widgetRelPos = event.getPosition();
  860. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  861. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  862. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  863. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  864. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  865. if (mouseOverContainer == nullptr)
  866. return false;
  867. else if(mouseOverContainer == &mRootContainer && mRootContainer.mWidgets == nullptr)
  868. {
  869. Rect2I overlayBounds = mRootContainer.getContentBounds();
  870. if (overlayBounds.contains(windowPos))
  871. {
  872. insert(nullptr, draggedWidget, DockLocation::None);
  873. return true;
  874. }
  875. }
  876. else
  877. {
  878. if (insidePolygon(mTopDropPolygon, 4, windowPosVec))
  879. {
  880. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  881. return true;
  882. }
  883. else if (insidePolygon(mBotDropPolygon, 4, windowPosVec))
  884. {
  885. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  886. return true;
  887. }
  888. else if (insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  889. {
  890. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  891. return true;
  892. }
  893. else if (insidePolygon(mRightDropPolygon, 4, windowPosVec))
  894. {
  895. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  896. return true;
  897. }
  898. }
  899. }
  900. return false;
  901. }
  902. // TODO - Move to a separate Polygon class?
  903. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  904. {
  905. bool isInside = false;
  906. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  907. {
  908. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  909. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  910. isInside = !isInside;
  911. }
  912. return isInside;
  913. }
  914. namespace ct
  915. {
  916. const Color DockOverlayRenderer::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  917. const Color DockOverlayRenderer::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  918. DockOverlayRenderer::DockOverlayRenderer()
  919. : RendererExtension(RenderLocation::Overlay, 0), mHighlightedDropLoc(DockManager::DockLocation::None)
  920. , mShowOverlay(false)
  921. {
  922. }
  923. void DockOverlayRenderer::initialize(const Any& data)
  924. {
  925. mMaterial = any_cast<SPtr<Material>>(data);
  926. mMaterial->getTechnique(0)->compile();
  927. mParams = mMaterial->createParamsSet();
  928. }
  929. void DockOverlayRenderer::updateData(const SPtr<Camera>& camera, const SPtr<Mesh>& mesh, bool active,
  930. DockManager::DockLocation location)
  931. {
  932. mCamera = camera;
  933. mMesh = mesh;
  934. mShowOverlay = active;
  935. mHighlightedDropLoc = location;
  936. }
  937. RendererExtensionRequest DockOverlayRenderer::check(const Camera& camera)
  938. {
  939. if (mCamera.get() != &camera || !mShowOverlay)
  940. return RendererExtensionRequest::DontRender;
  941. return RendererExtensionRequest::ForceRender;
  942. }
  943. void DockOverlayRenderer::render(const Camera& camera, const RendererViewContext& viewContext)
  944. {
  945. THROW_IF_NOT_CORE_THREAD;
  946. if (!mShowOverlay)
  947. return;
  948. SPtr<Viewport> viewport = mCamera->getViewport();
  949. float invViewportWidth = 1.0f / (viewport->getPixelArea().width * 0.5f);
  950. float invViewportHeight = 1.0f / (viewport->getPixelArea().height * 0.5f);
  951. mMaterial->setFloat("invViewportWidth", invViewportWidth);
  952. mMaterial->setFloat("invViewportHeight", invViewportHeight);
  953. mMaterial->setColor("tintColor", TINT_COLOR);
  954. mMaterial->setColor("highlightColor", HIGHLIGHT_COLOR);
  955. Color highlightColor;
  956. switch (mHighlightedDropLoc)
  957. {
  958. case DockManager::DockLocation::Top:
  959. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  960. break;
  961. case DockManager::DockLocation::Bottom:
  962. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  963. break;
  964. case DockManager::DockLocation::Left:
  965. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  966. break;
  967. case DockManager::DockLocation::Right:
  968. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  969. break;
  970. case DockManager::DockLocation::None:
  971. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  972. break;
  973. }
  974. mMaterial->setColor("highlightActive", highlightColor);
  975. mMaterial->updateParamsSet(mParams);
  976. gRendererUtility().setPass(mMaterial);
  977. gRendererUtility().setPassParams(mParams);
  978. gRendererUtility().draw(mMesh, mMesh->getProperties().getSubMesh(0));
  979. }
  980. }
  981. }