BsDockManager.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. #include "BsDockManager.h"
  2. #include "BsEditorWidgetContainer.h"
  3. #include "BsEditorWidget.h"
  4. #include "BsEditorWidgetManager.h"
  5. #include "BsMath.h"
  6. #include "BsException.h"
  7. #include "BsMesh.h"
  8. #include "BsMaterial.h"
  9. #include "BsVector2.h"
  10. #include "BsCoreApplication.h"
  11. #include "BsRendererManager.h"
  12. #include "BsCoreRenderer.h"
  13. #include "BsSceneObject.h"
  14. #include "BsGUIManager.h"
  15. #include "BsBuiltinEditorResources.h"
  16. #include "BsCGUIWidget.h"
  17. #include "BsCCamera.h"
  18. #include "BsDragAndDropManager.h"
  19. #include "BsGUIDockSlider.h"
  20. #include "BsVertexDataDesc.h"
  21. #include "BsDockManagerLayout.h"
  22. #include "BsEditorWindow.h"
  23. #include "BsGUIPanel.h"
  24. #include "BsCoreThread.h"
  25. #include "BsRendererUtility.h"
  26. using namespace std::placeholders;
  27. namespace BansheeEngine
  28. {
  29. const UINT32 DockManager::DockContainer::SLIDER_SIZE = 3;
  30. const UINT32 DockManager::DockContainer::MIN_CHILD_SIZE = 20;
  31. DockManager::DockContainer::DockContainer(DockManager* manager)
  32. :mIsLeaf(true), mWidgets(nullptr), mSplitPosition(0.5f),
  33. mIsHorizontal(false), mParent(nullptr), mSlider(nullptr), mManager(manager)
  34. {
  35. mChildren[0] = nullptr;
  36. mChildren[1] = nullptr;
  37. }
  38. DockManager::DockContainer::DockContainer(DockManager* manager, DockContainer* parent)
  39. :mIsLeaf(false), mWidgets(nullptr), mSplitPosition(0.5f),
  40. mIsHorizontal(false), mParent(parent), mSlider(nullptr), mManager(manager)
  41. {
  42. mChildren[0] = nullptr;
  43. mChildren[1] = nullptr;
  44. }
  45. DockManager::DockContainer::~DockContainer()
  46. {
  47. if (mIsLeaf)
  48. {
  49. if (mWidgets != nullptr)
  50. bs_delete(mWidgets);
  51. if (mGUIWidgetSO != nullptr)
  52. mGUIWidgetSO->destroy();
  53. }
  54. if(!mIsLeaf)
  55. {
  56. if(mChildren[0] != nullptr)
  57. bs_delete(mChildren[0]);
  58. if(mChildren[1] != nullptr)
  59. bs_delete(mChildren[1]);
  60. }
  61. if(mSlider != nullptr)
  62. {
  63. GUIElement::destroy(mSlider);
  64. mSlider = nullptr;
  65. }
  66. }
  67. void DockManager::DockContainer::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  68. {
  69. if(mIsLeaf)
  70. {
  71. if(mWidgets != nullptr)
  72. {
  73. mWidgets->setPosition(x, y);
  74. mWidgets->setSize(width, height);
  75. }
  76. }
  77. mArea.x = x;
  78. mArea.y = y;
  79. mArea.width = width;
  80. mArea.height = height;
  81. updateChildAreas();
  82. }
  83. void DockManager::DockContainer::updateChildAreas()
  84. {
  85. if(!mIsLeaf && mChildren[0] != nullptr && mChildren[1] != nullptr)
  86. {
  87. if(mIsHorizontal)
  88. {
  89. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  90. UINT32 sizeTop = Math::floorToInt(remainingSize * mSplitPosition);
  91. UINT32 sizeBottom = remainingSize - sizeTop;
  92. mChildren[0]->setArea(mArea.x, mArea.y, mArea.width, sizeTop);
  93. mChildren[1]->setArea(mArea.x, mArea.y + sizeTop + SLIDER_SIZE, mArea.width, sizeBottom);
  94. mSlider->setWidth(mArea.width);
  95. mSlider->setHeight(SLIDER_SIZE);
  96. mSlider->setPosition(mArea.x, mArea.y + sizeTop);
  97. }
  98. else
  99. {
  100. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  101. UINT32 sizeLeft = Math::floorToInt(remainingSize * mSplitPosition);
  102. UINT32 sizeRight = remainingSize - sizeLeft;
  103. mChildren[0]->setArea(mArea.x, mArea.y, sizeLeft, mArea.height);
  104. mChildren[1]->setArea(mArea.x + sizeLeft + SLIDER_SIZE, mArea.y, sizeRight, mArea.height);
  105. mSlider->setWidth(SLIDER_SIZE);
  106. mSlider->setHeight(mArea.height);
  107. mSlider->setPosition(mArea.x + sizeLeft, mArea.y);
  108. }
  109. }
  110. }
  111. void DockManager::DockContainer::makeLeaf(EditorWindowBase* parentWindow)
  112. {
  113. mGUIWidgetSO = SceneObject::create("DockContainer", SOF_Internal | SOF_Persistent | SOF_DontSave);
  114. HGUIWidget guiWidget = mGUIWidgetSO->addComponent<CGUIWidget>(parentWindow->getGUICamera());
  115. guiWidget->setDepth(128);
  116. guiWidget->setSkin(BuiltinEditorResources::instance().getSkin());
  117. mIsLeaf = true;
  118. mWidgets = bs_new<EditorWidgetContainer>(guiWidget->_getInternal(), parentWindow);
  119. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  120. mWidgets->onMaximized.connect(std::bind(&DockManager::DockContainer::maximizeClicked, this));
  121. if(mSlider != nullptr)
  122. {
  123. GUIElement::destroy(mSlider);
  124. mSlider = nullptr;
  125. }
  126. mWidgets->setPosition(mArea.x, mArea.y);
  127. mWidgets->setSize(mArea.width, mArea.height);
  128. }
  129. void DockManager::DockContainer::makeLeaf(const HSceneObject& guiWidgetSO, EditorWidgetContainer* existingContainer)
  130. {
  131. mIsLeaf = true;
  132. mWidgets = existingContainer;
  133. mGUIWidgetSO = guiWidgetSO;
  134. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  135. mWidgets->onMaximized.connect(std::bind(&DockManager::DockContainer::maximizeClicked, this));
  136. if(mSlider != nullptr)
  137. {
  138. GUIElement::destroy(mSlider);
  139. mSlider = nullptr;
  140. }
  141. mWidgets->setPosition(mArea.x, mArea.y);
  142. mWidgets->setSize(mArea.width, mArea.height);
  143. }
  144. void DockManager::DockContainer::addLeft(EditorWidgetBase* widget)
  145. {
  146. if(mIsLeaf)
  147. splitContainer(false, true);
  148. mChildren[0]->addWidget(widget);
  149. }
  150. void DockManager::DockContainer::addRight(EditorWidgetBase* widget)
  151. {
  152. if(mIsLeaf)
  153. splitContainer(false, false);
  154. mChildren[1]->addWidget(widget);
  155. }
  156. void DockManager::DockContainer::addTop(EditorWidgetBase* widget)
  157. {
  158. if(mIsLeaf)
  159. splitContainer(true, true);
  160. mChildren[0]->addWidget(widget);
  161. }
  162. void DockManager::DockContainer::addBottom(EditorWidgetBase* widget)
  163. {
  164. if(mIsLeaf)
  165. splitContainer(true, false);
  166. mChildren[1]->addWidget(widget);
  167. }
  168. void DockManager::DockContainer::splitContainer(bool horizontal, bool newChildIsFirst, float splitPosition)
  169. {
  170. DockContainer* children[2];
  171. UINT32 idxA = newChildIsFirst ? 0 : 1;
  172. UINT32 idxB = (idxA + 1) % 2;
  173. children[idxA] = bs_new<DockContainer>(mManager, this);
  174. children[idxB] = bs_new<DockContainer>(mManager, this);
  175. mWidgets->onWidgetClosed.clear();
  176. mWidgets->onMaximized.clear();
  177. children[idxA]->makeLeaf(mManager->mParentWindow);
  178. children[idxB]->makeLeaf(mGUIWidgetSO, mWidgets);
  179. mWidgets = nullptr;
  180. mGUIWidgetSO = nullptr;
  181. makeSplit(children[0], children[1], horizontal, splitPosition);
  182. }
  183. void DockManager::DockContainer::makeSplit(DockManager::DockContainer* first, DockManager::DockContainer* second, bool horizontal, float splitPosition)
  184. {
  185. mChildren[0] = first;
  186. mChildren[1] = second;
  187. mIsLeaf = false;
  188. mIsHorizontal = horizontal;
  189. mSplitPosition = splitPosition;
  190. if (mWidgets != nullptr)
  191. {
  192. bs_delete(mWidgets);
  193. mWidgets = nullptr;
  194. }
  195. if (mGUIWidgetSO != nullptr)
  196. {
  197. mGUIWidgetSO->destroy();
  198. mGUIWidgetSO = nullptr;
  199. }
  200. if (mSlider != nullptr)
  201. {
  202. GUIElement::destroy(mSlider);
  203. mSlider = nullptr;
  204. }
  205. mSlider = GUIDockSlider::create(horizontal, "DockSliderBtn");
  206. mManager->_getParentWidget()->getPanel()->addElement(mSlider);
  207. mSlider->onDragged.connect(std::bind(&DockManager::DockContainer::sliderDragged, this, _1));
  208. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  209. }
  210. void DockManager::DockContainer::addWidget(EditorWidgetBase* widget)
  211. {
  212. if(!mIsLeaf)
  213. return;
  214. mWidgets->add(*widget);
  215. }
  216. void DockManager::DockContainer::addWidget(const String& name)
  217. {
  218. if(!mIsLeaf)
  219. return;
  220. EditorWidgetManager::instance().create(name, *mWidgets);
  221. }
  222. void DockManager::DockContainer::sliderDragged(const Vector2I& delta)
  223. {
  224. if(mIsHorizontal && delta.y != 0)
  225. {
  226. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.height - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  227. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  228. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.y, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  229. updateChildAreas();
  230. }
  231. else if(!mIsHorizontal && delta.x != 0)
  232. {
  233. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.width - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  234. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  235. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.x, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  236. updateChildAreas();
  237. }
  238. }
  239. void DockManager::DockContainer::widgetRemoved()
  240. {
  241. assert(mIsLeaf);
  242. if(mWidgets->getNumWidgets() == 0)
  243. {
  244. if(mParent == nullptr) // We're root so we just reset ourselves, can't delete root
  245. {
  246. if (mManager->mIsMaximized)
  247. {
  248. mManager->mMaximizedContainer = this;
  249. mManager->mIsMaximized = false;
  250. }
  251. bs_delete(mWidgets);
  252. mWidgets = nullptr;
  253. mGUIWidgetSO->destroy();
  254. mGUIWidgetSO = nullptr;
  255. mIsLeaf = true;
  256. mSplitPosition = 0.5f;
  257. mIsHorizontal = false;
  258. }
  259. else
  260. {
  261. // Replace our parent with our sibling
  262. DockContainer* sibling = nullptr;
  263. if(mParent->mChildren[0] == this)
  264. sibling = mParent->mChildren[1];
  265. else
  266. sibling = mParent->mChildren[0];
  267. if (sibling->mIsLeaf)
  268. {
  269. sibling->mWidgets->onWidgetClosed.clear();
  270. sibling->mWidgets->onMaximized.clear();
  271. mParent->makeLeaf(sibling->mGUIWidgetSO, sibling->mWidgets);
  272. sibling->mWidgets = nullptr;
  273. sibling->mGUIWidgetSO = nullptr;
  274. }
  275. else
  276. {
  277. mParent->makeSplit(sibling->mChildren[0], sibling->mChildren[1], sibling->mIsHorizontal, sibling->mSplitPosition);
  278. sibling->mChildren[0]->mParent = mParent;
  279. sibling->mChildren[1]->mParent = mParent;
  280. sibling->mChildren[0] = nullptr;
  281. sibling->mChildren[1] = nullptr;
  282. }
  283. bs_delete(sibling);
  284. bs_delete(this);
  285. }
  286. }
  287. }
  288. void DockManager::DockContainer::maximizeClicked()
  289. {
  290. mManager->toggleMaximize(this);
  291. }
  292. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  293. {
  294. if(mIsLeaf)
  295. {
  296. if(mWidgets == widgetContainer)
  297. return this;
  298. else
  299. return nullptr;
  300. }
  301. else
  302. {
  303. if(mChildren[0] != nullptr)
  304. {
  305. DockContainer* foundContainer = mChildren[0]->find(widgetContainer);
  306. if (foundContainer != nullptr)
  307. return foundContainer;
  308. }
  309. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  310. {
  311. DockContainer* foundContainer = mChildren[1]->find(widgetContainer);
  312. if (foundContainer != nullptr)
  313. return foundContainer;
  314. }
  315. }
  316. return nullptr;
  317. }
  318. DockManager::DockContainer* DockManager::DockContainer::findAtPos(const Vector2I& pos)
  319. {
  320. if(mIsLeaf)
  321. {
  322. if(mArea.contains(pos))
  323. {
  324. return this;
  325. }
  326. }
  327. else
  328. {
  329. if (mChildren[0] != nullptr)
  330. {
  331. DockContainer* foundContainer = mChildren[0]->findAtPos(pos);
  332. if (foundContainer != nullptr)
  333. return foundContainer;
  334. }
  335. if(mChildren[1] != nullptr)
  336. {
  337. DockContainer* foundContainer = mChildren[1]->findAtPos(pos);
  338. if (foundContainer != nullptr)
  339. return foundContainer;
  340. }
  341. }
  342. return nullptr;
  343. }
  344. Rect2I DockManager::DockContainer::getContentBounds() const
  345. {
  346. if(!mIsLeaf || mWidgets == nullptr)
  347. return mArea;
  348. return mWidgets->getContentBounds();
  349. }
  350. void DockManager::DockContainer::update()
  351. {
  352. if (mIsLeaf)
  353. {
  354. if (mWidgets != nullptr)
  355. mWidgets->update();
  356. }
  357. else
  358. {
  359. if (mChildren[0] != nullptr)
  360. mChildren[0]->update();
  361. if (mChildren[1] != nullptr)
  362. mChildren[1]->update();
  363. }
  364. }
  365. DockManager::DockManager(EditorWindowBase* parentWindow, const GUIDimensions& dimensions)
  366. :GUIElementContainer(dimensions), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None),
  367. mShowOverlay(false), mRootContainer(this), mParentWindow(parentWindow), mIsMaximized(false), mMaximizedContainer(nullptr)
  368. {
  369. mTopDropPolygon = bs_newN<Vector2>(4);
  370. mBotDropPolygon = bs_newN<Vector2>(4);
  371. mLeftDropPolygon = bs_newN<Vector2>(4);
  372. mRightDropPolygon = bs_newN<Vector2>(4);
  373. HMaterial dropOverlayMat = BuiltinEditorResources::instance().createDockDropOverlayMaterial();
  374. mCore.store(bs_new<DockOverlayRenderer>(), std::memory_order_release);
  375. gCoreAccessor().queueCommand(std::bind(&DockManager::initializeOverlayRenderer,
  376. this, dropOverlayMat->getCore()));
  377. }
  378. DockManager::~DockManager()
  379. {
  380. bs_deleteN(mTopDropPolygon, 4);
  381. bs_deleteN(mBotDropPolygon, 4);
  382. bs_deleteN(mLeftDropPolygon, 4);
  383. bs_deleteN(mRightDropPolygon, 4);
  384. gCoreAccessor().queueCommand(std::bind(&DockManager::destroyOverlayRenderer,
  385. this, mCore.load(std::memory_order_relaxed)));
  386. }
  387. void DockManager::initializeOverlayRenderer(const SPtr<MaterialCore>& initData)
  388. {
  389. mCore.load(std::memory_order_acquire)->initialize(initData);
  390. }
  391. void DockManager::destroyOverlayRenderer(DockOverlayRenderer* core)
  392. {
  393. bs_delete(core);
  394. }
  395. DockManager* DockManager::create(EditorWindowBase* parentWindow)
  396. {
  397. return new (bs_alloc<DockManager>()) DockManager(parentWindow, GUIDimensions::create());
  398. }
  399. void DockManager::update()
  400. {
  401. if(!DragAndDropManager::instance().isDragInProgress())
  402. {
  403. mHighlightedDropLoc = DockLocation::None;
  404. mShowOverlay = false;
  405. }
  406. mRootContainer.update();
  407. HCamera camera = mParentWindow->getGUICamera();
  408. DockOverlayRenderer* core = mCore.load(std::memory_order_relaxed);
  409. gCoreAccessor().queueCommand(std::bind(&DockOverlayRenderer::updateData, core, camera->_getCamera()->getCore(),
  410. mDropOverlayMesh->getCore(), mShowOverlay, mHighlightedDropLoc));
  411. }
  412. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  413. {
  414. if(relativeTo != nullptr)
  415. {
  416. DockContainer* container = mRootContainer.find(relativeTo);
  417. if(container == nullptr)
  418. BS_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  419. switch(location)
  420. {
  421. case DockLocation::Left:
  422. container->addLeft(widgetToInsert);
  423. break;
  424. case DockLocation::Right:
  425. container->addRight(widgetToInsert);
  426. break;
  427. case DockLocation::Top:
  428. container->addTop(widgetToInsert);
  429. break;
  430. case DockLocation::Bottom:
  431. container->addBottom(widgetToInsert);
  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. DockManagerLayoutPtr 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. DockManagerLayoutPtr 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. DockManagerLayoutPtr 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 DockManagerLayoutPtr& 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. // Destroy any widgets that are no longer docked anywhere
  648. for(auto& widget : undockedWidgets)
  649. {
  650. if(widget->_getParent() == nullptr)
  651. widget->close();
  652. }
  653. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  654. }
  655. void DockManager::toggleMaximize(DockContainer* container)
  656. {
  657. if (mIsMaximized)
  658. {
  659. if (mRestoredLayout != nullptr)
  660. setLayout(mRestoredLayout);
  661. mRestoredLayout = nullptr;
  662. mMaximizedContainer = nullptr;
  663. mIsMaximized = false;
  664. }
  665. else
  666. {
  667. mRestoredLayout = getLayout();
  668. mMaximizedContainer = container;
  669. Vector<String> maximizedWidgetNames;
  670. if (container->mWidgets != nullptr)
  671. {
  672. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  673. for (UINT32 i = 0; i < numWidgets; i++)
  674. {
  675. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  676. maximizedWidgetNames.push_back(widget->getName());
  677. }
  678. }
  679. DockManagerLayoutPtr maxLayout = bs_shared_ptr_new<DockManagerLayout>();
  680. DockManagerLayout::Entry& rootEntry = maxLayout->getRootEntry();
  681. rootEntry.isLeaf = true;
  682. rootEntry.widgetNames = maximizedWidgetNames;
  683. setLayout(maxLayout);
  684. mIsMaximized = true;
  685. }
  686. }
  687. void DockManager::updateClippedBounds()
  688. {
  689. // TODO - Clipping not actually accounted for but shouldn't matter as right now DockManager is only used in one specific situation
  690. mClippedBounds = mRootContainer.mArea;
  691. }
  692. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  693. {
  694. const static int spacing = 10;
  695. const static float innerScale = 0.15f;
  696. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  697. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  698. UINT32 innerOffset = outWidth > outHeight ? Math::floorToInt(innerScale * outHeight) : Math::floorToInt(innerScale * outWidth);
  699. UINT32 inWidth = outWidth - innerOffset;
  700. UINT32 inHeight = outHeight - innerOffset;
  701. INT32 inXOffset = Math::floorToInt((outWidth - inWidth) * 0.5f);
  702. INT32 inYOffset = Math::floorToInt((outHeight - inHeight) * 0.5f);
  703. Vector2 outTopLeft((float)x, (float)y);
  704. Vector2 outTopRight((float)(x + outWidth), (float)y);
  705. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  706. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  707. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  708. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  709. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  710. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  711. VertexDataDescPtr vertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  712. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  713. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  714. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(16, 24, vertexDesc);
  715. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  716. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  717. // Top
  718. Vector2 topOffset((float)spacing, 0.0f);
  719. mTopDropPolygon[0] = outTopLeft + topOffset;
  720. mTopDropPolygon[1] = outTopRight + topOffset;
  721. mTopDropPolygon[2] = inTopRight + topOffset;
  722. mTopDropPolygon[3] = inTopLeft + topOffset;
  723. vertIter.addValue(mTopDropPolygon[0]);
  724. vertIter.addValue(mTopDropPolygon[1]);
  725. vertIter.addValue(mTopDropPolygon[2]);
  726. vertIter.addValue(mTopDropPolygon[3]);
  727. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  728. UINT32 color32 = color.getAsRGBA();
  729. colIter.addValue(color32);
  730. colIter.addValue(color32);
  731. colIter.addValue(color32);
  732. colIter.addValue(color32);
  733. // Bottom
  734. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  735. mBotDropPolygon[0] = inBotLeft + botOffset;
  736. mBotDropPolygon[1] = inBotRight + botOffset;
  737. mBotDropPolygon[2] = outBotRight + botOffset;
  738. mBotDropPolygon[3] = outBotLeft + botOffset;
  739. vertIter.addValue(mBotDropPolygon[0]);
  740. vertIter.addValue(mBotDropPolygon[1]);
  741. vertIter.addValue(mBotDropPolygon[2]);
  742. vertIter.addValue(mBotDropPolygon[3]);
  743. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  744. color32 = color.getAsRGBA();
  745. colIter.addValue(color32);
  746. colIter.addValue(color32);
  747. colIter.addValue(color32);
  748. colIter.addValue(color32);
  749. // Left
  750. Vector2 leftOffset(0.0f, (float)spacing);
  751. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  752. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  753. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  754. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  755. vertIter.addValue(mLeftDropPolygon[0]);
  756. vertIter.addValue(mLeftDropPolygon[1]);
  757. vertIter.addValue(mLeftDropPolygon[2]);
  758. vertIter.addValue(mLeftDropPolygon[3]);
  759. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  760. color32 = color.getAsRGBA();
  761. colIter.addValue(color32);
  762. colIter.addValue(color32);
  763. colIter.addValue(color32);
  764. colIter.addValue(color32);
  765. // Right
  766. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  767. mRightDropPolygon[0] = inTopRight + rightOffset;
  768. mRightDropPolygon[1] = outTopRight + rightOffset;
  769. mRightDropPolygon[2] = outBotRight + rightOffset;
  770. mRightDropPolygon[3] = inBotRight + rightOffset;
  771. vertIter.addValue(mRightDropPolygon[0]);
  772. vertIter.addValue(mRightDropPolygon[1]);
  773. vertIter.addValue(mRightDropPolygon[2]);
  774. vertIter.addValue(mRightDropPolygon[3]);
  775. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  776. color32 = color.getAsRGBA();
  777. colIter.addValue(color32);
  778. colIter.addValue(color32);
  779. colIter.addValue(color32);
  780. colIter.addValue(color32);
  781. UINT32* indexData = meshData->getIndices32();
  782. // Top
  783. indexData[0] = 0;
  784. indexData[1] = 1;
  785. indexData[2] = 2;
  786. indexData[3] = 0;
  787. indexData[4] = 2;
  788. indexData[5] = 3;
  789. // Bottom
  790. indexData[6] = 4;
  791. indexData[7] = 5;
  792. indexData[8] = 6;
  793. indexData[9] = 4;
  794. indexData[10] = 6;
  795. indexData[11] = 7;
  796. // Left
  797. indexData[12] = 8;
  798. indexData[13] = 9;
  799. indexData[14] = 10;
  800. indexData[15] = 8;
  801. indexData[16] = 10;
  802. indexData[17] = 11;
  803. // Right
  804. indexData[18] = 12;
  805. indexData[19] = 13;
  806. indexData[20] = 14;
  807. indexData[21] = 12;
  808. indexData[22] = 14;
  809. indexData[23] = 15;
  810. mDropOverlayMesh = Mesh::create(meshData);
  811. }
  812. bool DockManager::_mouseEvent(const GUIMouseEvent& event)
  813. {
  814. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  815. {
  816. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  817. return false;
  818. const Vector2I& widgetRelPos = event.getPosition();
  819. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  820. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  821. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  822. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  823. mMouseOverContainer = mRootContainer.findAtPos(windowPos);
  824. if(mMouseOverContainer == nullptr)
  825. mMouseOverContainer = &mRootContainer;
  826. Rect2I overlayBounds;
  827. if(mMouseOverContainer != nullptr)
  828. overlayBounds = mMouseOverContainer->getContentBounds();
  829. // Update mesh if needed
  830. if(mLastOverlayBounds != overlayBounds)
  831. {
  832. if(overlayBounds.width <= 0 || overlayBounds.height <= 0)
  833. mDropOverlayMesh = HMesh();
  834. else
  835. updateDropOverlay(overlayBounds.x, overlayBounds.y, overlayBounds.width, overlayBounds.height);
  836. mLastOverlayBounds = overlayBounds;
  837. }
  838. // Check if we need to highlight any drop locations
  839. if(mMouseOverContainer != nullptr)
  840. {
  841. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  842. mHighlightedDropLoc = DockLocation::Top;
  843. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  844. mHighlightedDropLoc = DockLocation::Bottom;
  845. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  846. mHighlightedDropLoc = DockLocation::Left;
  847. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  848. mHighlightedDropLoc = DockLocation::Right;
  849. else
  850. mHighlightedDropLoc = DockLocation::None;
  851. if(overlayBounds.contains(windowPos))
  852. mShowOverlay = true;
  853. else
  854. mShowOverlay = false;
  855. }
  856. else
  857. mShowOverlay = false;
  858. return false;
  859. }
  860. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  861. {
  862. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  863. return false;
  864. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  865. const Vector2I& widgetRelPos = event.getPosition();
  866. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  867. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  868. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  869. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  870. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  871. if(mouseOverContainer == nullptr)
  872. {
  873. Rect2I overlayBounds = mRootContainer.getContentBounds();
  874. if(overlayBounds.contains(windowPos))
  875. {
  876. insert(nullptr, draggedWidget, DockLocation::None);
  877. return true;
  878. }
  879. }
  880. else
  881. {
  882. if (insidePolygon(mTopDropPolygon, 4, windowPosVec))
  883. {
  884. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  885. return true;
  886. }
  887. else if (insidePolygon(mBotDropPolygon, 4, windowPosVec))
  888. {
  889. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  890. return true;
  891. }
  892. else if (insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  893. {
  894. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  895. return true;
  896. }
  897. else if (insidePolygon(mRightDropPolygon, 4, windowPosVec))
  898. {
  899. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  900. return true;
  901. }
  902. }
  903. }
  904. return false;
  905. }
  906. // TODO - Move to a separate Polygon class?
  907. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  908. {
  909. bool isInside = false;
  910. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  911. {
  912. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  913. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  914. isInside = !isInside;
  915. }
  916. return isInside;
  917. }
  918. const Color DockOverlayRenderer::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  919. const Color DockOverlayRenderer::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  920. DockOverlayRenderer::DockOverlayRenderer()
  921. :mShowOverlay(false), mHighlightedDropLoc(DockManager::DockLocation::None)
  922. {
  923. }
  924. DockOverlayRenderer::~DockOverlayRenderer()
  925. {
  926. if (mCamera != nullptr)
  927. {
  928. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  929. activeRenderer->_unregisterRenderCallback(mCamera.get(), 40);
  930. }
  931. }
  932. void DockOverlayRenderer::initialize(const SPtr<MaterialCore>& material)
  933. {
  934. mMaterial = material;
  935. }
  936. void DockOverlayRenderer::updateData(const SPtr<CameraCore>& camera, const SPtr<MeshCore>& mesh, bool active,
  937. DockManager::DockLocation location)
  938. {
  939. if (mCamera != camera)
  940. {
  941. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  942. if (mCamera != nullptr)
  943. activeRenderer->_unregisterRenderCallback(mCamera.get(), 40);
  944. if (camera != nullptr)
  945. activeRenderer->_registerRenderCallback(camera.get(), 40, std::bind(&DockOverlayRenderer::render, this), true);
  946. }
  947. mCamera = camera;
  948. mMesh = mesh;
  949. mShowOverlay = active;
  950. mHighlightedDropLoc = location;
  951. }
  952. void DockOverlayRenderer::render()
  953. {
  954. THROW_IF_NOT_CORE_THREAD;
  955. if (!mShowOverlay)
  956. return;
  957. SPtr<ViewportCore> viewport = mCamera->getViewport();
  958. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  959. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  960. mMaterial->setFloat("invViewportWidth", invViewportWidth);
  961. mMaterial->setFloat("invViewportHeight", invViewportHeight);
  962. mMaterial->setColor("tintColor", TINT_COLOR);
  963. mMaterial->setColor("highlightColor", HIGHLIGHT_COLOR);
  964. Color highlightColor;
  965. switch (mHighlightedDropLoc)
  966. {
  967. case DockManager::DockLocation::Top:
  968. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  969. break;
  970. case DockManager::DockLocation::Bottom:
  971. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  972. break;
  973. case DockManager::DockLocation::Left:
  974. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  975. break;
  976. case DockManager::DockLocation::Right:
  977. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  978. break;
  979. case DockManager::DockLocation::None:
  980. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  981. break;
  982. }
  983. mMaterial->setColor("highlightActive", highlightColor);
  984. gRendererUtility().setPass(mMaterial, 0);
  985. gRendererUtility().draw(mMesh, mMesh->getProperties().getSubMesh(0));
  986. }
  987. }