BsDockManager.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 "BsDrawList.h"
  11. #include "BsCoreApplication.h"
  12. #include "BsRendererManager.h"
  13. #include "BsCoreRenderer.h"
  14. #include "BsSceneObject.h"
  15. #include "BsGUIManager.h"
  16. #include "BsBuiltinEditorResources.h"
  17. #include "BsGUIWidget.h"
  18. #include "BsCamera.h"
  19. #include "BsDragAndDropManager.h"
  20. #include "BsGUIDockSlider.h"
  21. #include "BsVertexDataDesc.h"
  22. #include "BsGUISkin.h"
  23. #include "BsBuiltinResources.h"
  24. #include "BsDockManagerLayout.h"
  25. #include "BsEditorWindow.h"
  26. #include "BsGUISkin.h"
  27. #include "BsGUIButton.h"
  28. using namespace std::placeholders;
  29. namespace BansheeEngine
  30. {
  31. const UINT32 DockManager::DockContainer::SLIDER_SIZE = 4;
  32. const UINT32 DockManager::DockContainer::MIN_CHILD_SIZE = 20;
  33. const Color DockManager::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  34. const Color DockManager::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  35. DockManager::DockContainer::DockContainer(DockManager* manager)
  36. :mIsLeaf(true), mWidgets(nullptr), mSplitPosition(0.5f),
  37. mIsHorizontal(false), mParent(nullptr), mSlider(nullptr), mManager(manager)
  38. {
  39. mChildren[0] = nullptr;
  40. mChildren[1] = nullptr;
  41. }
  42. DockManager::DockContainer::DockContainer(DockManager* manager, DockContainer* parent)
  43. :mIsLeaf(false), mWidgets(nullptr), mSplitPosition(0.5f),
  44. mIsHorizontal(false), mParent(parent), mSlider(nullptr), mManager(manager)
  45. {
  46. mChildren[0] = nullptr;
  47. mChildren[1] = nullptr;
  48. }
  49. DockManager::DockContainer::~DockContainer()
  50. {
  51. if(mIsLeaf && mWidgets != nullptr)
  52. bs_delete(mWidgets);
  53. if(!mIsLeaf)
  54. {
  55. if(mChildren[0] != nullptr)
  56. bs_delete(mChildren[0]);
  57. if(mChildren[1] != nullptr)
  58. bs_delete(mChildren[1]);
  59. }
  60. if(mSlider != nullptr)
  61. {
  62. GUIElement::destroy(mSlider);
  63. mSlider = nullptr;
  64. }
  65. }
  66. void DockManager::DockContainer::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  67. {
  68. if(mIsLeaf)
  69. {
  70. if(mWidgets != nullptr)
  71. {
  72. mWidgets->setPosition(x, y);
  73. mWidgets->setSize(width, height);
  74. }
  75. }
  76. mArea.x = x;
  77. mArea.y = y;
  78. mArea.width = width;
  79. mArea.height = height;
  80. updateChildAreas();
  81. }
  82. void DockManager::DockContainer::updateChildAreas()
  83. {
  84. if(!mIsLeaf && mChildren[0] != nullptr && mChildren[1] != nullptr)
  85. {
  86. Rect2I clipRect = mArea;
  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->_setOffset(Vector2I(mArea.x, mArea.y + sizeTop));
  95. mSlider->_setWidth(mArea.width);
  96. mSlider->_setHeight(SLIDER_SIZE);
  97. Rect2I elemClipRect(clipRect.x - mArea.x, clipRect.y - mArea.y, clipRect.width, clipRect.height);
  98. mSlider->_setClipRect(elemClipRect);
  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->_setOffset(Vector2I(mArea.x + sizeLeft, mArea.y));
  108. mSlider->_setWidth(SLIDER_SIZE);
  109. mSlider->_setHeight(mArea.height);
  110. Rect2I elemClipRect(clipRect.x - mArea.x, clipRect.y - mArea.y, clipRect.width, clipRect.height);
  111. mSlider->_setClipRect(elemClipRect);
  112. }
  113. }
  114. }
  115. void DockManager::DockContainer::makeLeaf(GUIWidget* widgetParent, EditorWindowBase* parentWindow)
  116. {
  117. mIsLeaf = true;
  118. mWidgets = bs_new<EditorWidgetContainer>(widgetParent, parentWindow);
  119. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  120. if(mSlider != nullptr)
  121. {
  122. GUIElement::destroy(mSlider);
  123. mSlider = nullptr;
  124. }
  125. mWidgets->setPosition(mArea.x, mArea.y);
  126. mWidgets->setSize(mArea.width, mArea.height);
  127. }
  128. void DockManager::DockContainer::makeLeaf(EditorWidgetContainer* existingContainer)
  129. {
  130. mIsLeaf = true;
  131. mWidgets = existingContainer;
  132. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  133. if(mSlider != nullptr)
  134. {
  135. GUIElement::destroy(mSlider);
  136. mSlider = nullptr;
  137. }
  138. mWidgets->setPosition(mArea.x, mArea.y);
  139. mWidgets->setSize(mArea.width, mArea.height);
  140. }
  141. void DockManager::DockContainer::addLeft(EditorWidgetBase* widget)
  142. {
  143. if(mIsLeaf)
  144. splitContainer(false, true);
  145. mChildren[0]->addWidget(widget);
  146. }
  147. void DockManager::DockContainer::addRight(EditorWidgetBase* widget)
  148. {
  149. if(mIsLeaf)
  150. splitContainer(false, false);
  151. mChildren[1]->addWidget(widget);
  152. }
  153. void DockManager::DockContainer::addTop(EditorWidgetBase* widget)
  154. {
  155. if(mIsLeaf)
  156. splitContainer(true, true);
  157. mChildren[0]->addWidget(widget);
  158. }
  159. void DockManager::DockContainer::addBottom(EditorWidgetBase* widget)
  160. {
  161. if(mIsLeaf)
  162. splitContainer(true, false);
  163. mChildren[1]->addWidget(widget);
  164. }
  165. void DockManager::DockContainer::splitContainer(bool horizontal, bool newChildIsFirst, float splitPosition)
  166. {
  167. DockContainer* children[2];
  168. UINT32 idxA = newChildIsFirst ? 0 : 1;
  169. UINT32 idxB = (idxA + 1) % 2;
  170. children[idxA] = bs_new<DockContainer>(mManager, this);
  171. children[idxB] = bs_new<DockContainer>(mManager, this);
  172. mWidgets->onWidgetClosed.clear();
  173. children[idxA]->makeLeaf(mManager->_getParentWidget(), mManager->mParentWindow);
  174. children[idxB]->makeLeaf(mWidgets);
  175. mWidgets = nullptr;
  176. makeSplit(mManager->_getParentWidget(), children[0], children[1], horizontal, splitPosition);
  177. }
  178. void DockManager::DockContainer::makeSplit(GUIWidget* widgetParent, DockManager::DockContainer* first, DockManager::DockContainer* second, bool horizontal, float splitPosition)
  179. {
  180. mChildren[0] = first;
  181. mChildren[1] = second;
  182. mIsLeaf = false;
  183. mIsHorizontal = horizontal;
  184. mSplitPosition = splitPosition;
  185. if (mWidgets != nullptr)
  186. {
  187. bs_delete(mWidgets);
  188. mWidgets = nullptr;
  189. }
  190. if (mSlider != nullptr)
  191. {
  192. GUIElement::destroy(mSlider);
  193. mSlider = nullptr;
  194. }
  195. if (horizontal)
  196. {
  197. mSlider = GUIDockSlider::create(true, "DockSliderBtn");
  198. mSlider->_setWidgetDepth(widgetParent->getDepth());
  199. }
  200. else
  201. {
  202. mSlider = GUIDockSlider::create(false, "DockSliderBtn");
  203. mSlider->_setWidgetDepth(widgetParent->getDepth());
  204. }
  205. mSlider->_changeParentWidget(widgetParent);
  206. mSlider->onDragged.connect(std::bind(&DockManager::DockContainer::sliderDragged, this, _1));
  207. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  208. }
  209. void DockManager::DockContainer::addWidget(EditorWidgetBase* widget)
  210. {
  211. if(!mIsLeaf)
  212. return;
  213. mWidgets->add(*widget);
  214. }
  215. void DockManager::DockContainer::addWidget(const String& name)
  216. {
  217. if(!mIsLeaf)
  218. return;
  219. EditorWidgetManager::instance().create(name, *mWidgets);
  220. }
  221. void DockManager::DockContainer::sliderDragged(const Vector2I& delta)
  222. {
  223. if(mIsHorizontal && delta.y != 0)
  224. {
  225. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.height - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  226. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  227. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.y, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  228. updateChildAreas();
  229. }
  230. else if(!mIsHorizontal && delta.x != 0)
  231. {
  232. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.width - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  233. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  234. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.x, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  235. updateChildAreas();
  236. }
  237. }
  238. void DockManager::DockContainer::widgetRemoved()
  239. {
  240. assert(mIsLeaf);
  241. if(mWidgets->getNumWidgets() == 0)
  242. {
  243. if(mParent == nullptr) // We're root so we just reset ourselves, can't delete root
  244. {
  245. bs_delete(mWidgets);
  246. mWidgets = nullptr;
  247. mIsLeaf = false;
  248. mSplitPosition = 0.5f;
  249. mIsHorizontal = false;
  250. }
  251. else
  252. {
  253. // Replace our parent with our sibling
  254. DockContainer* sibling = nullptr;
  255. if(mParent->mChildren[0] == this)
  256. sibling = mParent->mChildren[1];
  257. else
  258. sibling = mParent->mChildren[0];
  259. if (sibling->mIsLeaf)
  260. {
  261. sibling->mWidgets->onWidgetClosed.clear();
  262. mParent->makeLeaf(sibling->mWidgets);
  263. sibling->mWidgets = nullptr;
  264. }
  265. else
  266. {
  267. mParent->makeSplit(mManager->_getParentWidget(), sibling->mChildren[0], sibling->mChildren[1], sibling->mIsHorizontal, sibling->mSplitPosition);
  268. sibling->mChildren[0]->mParent = mParent;
  269. sibling->mChildren[1]->mParent = mParent;
  270. sibling->mChildren[0] = nullptr;
  271. sibling->mChildren[1] = nullptr;
  272. }
  273. bs_delete(sibling);
  274. bs_delete(this);
  275. }
  276. }
  277. }
  278. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  279. {
  280. if(mIsLeaf)
  281. {
  282. if(mWidgets == widgetContainer)
  283. return this;
  284. else
  285. return nullptr;
  286. }
  287. else
  288. {
  289. if(mChildren[0] != nullptr && mChildren[0]->find(widgetContainer) != nullptr)
  290. return mChildren[0];
  291. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  292. return mChildren[1];
  293. }
  294. return nullptr;
  295. }
  296. DockManager::DockContainer* DockManager::DockContainer::findAtPos(const Vector2I& pos)
  297. {
  298. if(mIsLeaf)
  299. {
  300. if(mArea.contains(pos))
  301. {
  302. return this;
  303. }
  304. }
  305. else
  306. {
  307. if(mChildren[0] != nullptr && mChildren[0]->findAtPos(pos) != nullptr)
  308. return mChildren[0];
  309. if(mChildren[1] != nullptr && mChildren[1]->findAtPos(pos) != nullptr)
  310. return mChildren[1];
  311. }
  312. return nullptr;
  313. }
  314. Rect2I DockManager::DockContainer::getContentBounds() const
  315. {
  316. if(!mIsLeaf || mWidgets == nullptr)
  317. return mArea;
  318. return mWidgets->getContentBounds();
  319. }
  320. void DockManager::DockContainer::update()
  321. {
  322. if (mIsLeaf)
  323. {
  324. if (mWidgets != nullptr)
  325. mWidgets->_update();
  326. }
  327. else
  328. {
  329. if (mChildren[0] != nullptr)
  330. mChildren[0]->update();
  331. if (mChildren[1] != nullptr)
  332. mChildren[1]->update();
  333. }
  334. }
  335. DockManager::DockManager(EditorWindowBase* parentWindow, const GUILayoutOptions& layoutOptions)
  336. :GUIElementContainer(layoutOptions), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None),
  337. mShowOverlay(false), mRootContainer(this), mParentWindow(parentWindow)
  338. {
  339. mTopDropPolygon = bs_newN<Vector2>(4);
  340. mBotDropPolygon = bs_newN<Vector2>(4);
  341. mLeftDropPolygon = bs_newN<Vector2>(4);
  342. mRightDropPolygon = bs_newN<Vector2>(4);
  343. mDropOverlayMat = BuiltinEditorResources::instance().createDockDropOverlayMaterial();
  344. mRenderCallback = RendererManager::instance().getActive()->onRenderViewport.connect(std::bind(&DockManager::render, this, _1, _2));
  345. }
  346. DockManager::~DockManager()
  347. {
  348. mRenderCallback.disconnect();
  349. bs_deleteN(mTopDropPolygon, 4);
  350. bs_deleteN(mBotDropPolygon, 4);
  351. bs_deleteN(mLeftDropPolygon, 4);
  352. bs_deleteN(mRightDropPolygon, 4);
  353. }
  354. DockManager* DockManager::create(EditorWindowBase* parentWindow)
  355. {
  356. return new (bs_alloc<DockManager, PoolAlloc>()) DockManager(parentWindow, GUILayoutOptions::create());
  357. }
  358. void DockManager::update()
  359. {
  360. if(!DragAndDropManager::instance().isDragInProgress())
  361. {
  362. mHighlightedDropLoc = DockLocation::None;
  363. mShowOverlay = false;
  364. }
  365. mRootContainer.update();
  366. }
  367. void DockManager::render(const Viewport* viewport, DrawList& drawList)
  368. {
  369. if (_getParentWidget() == nullptr || _getParentWidget()->getTarget() != viewport)
  370. return;
  371. if(!mShowOverlay)
  372. return;
  373. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  374. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  375. if(!mDropOverlayMesh.isLoaded())
  376. return;
  377. if(!mDropOverlayMat.isLoaded())
  378. return;
  379. mDropOverlayMat->setFloat("invViewportWidth", invViewportWidth);
  380. mDropOverlayMat->setFloat("invViewportHeight", invViewportHeight);
  381. mDropOverlayMat->setColor("tintColor", TINT_COLOR);
  382. mDropOverlayMat->setColor("highlightColor", HIGHLIGHT_COLOR);
  383. Color highlightColor;
  384. switch(mHighlightedDropLoc)
  385. {
  386. case DockLocation::Top:
  387. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  388. break;
  389. case DockLocation::Bottom:
  390. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  391. break;
  392. case DockLocation::Left:
  393. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  394. break;
  395. case DockLocation::Right:
  396. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  397. break;
  398. case DockLocation::None:
  399. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  400. break;
  401. }
  402. mDropOverlayMat->setColor("highlightActive", highlightColor);
  403. drawList.add(mDropOverlayMat.getInternalPtr(), mDropOverlayMesh.getInternalPtr(), 0, Vector3::ZERO);
  404. }
  405. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  406. {
  407. if(relativeTo != nullptr)
  408. {
  409. DockContainer* container = mRootContainer.find(relativeTo);
  410. if(container == nullptr)
  411. BS_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  412. switch(location)
  413. {
  414. case DockLocation::Left:
  415. container->addLeft(widgetToInsert);
  416. break;
  417. case DockLocation::Right:
  418. container->addRight(widgetToInsert);
  419. break;
  420. case DockLocation::Top:
  421. container->addTop(widgetToInsert);
  422. break;
  423. case DockLocation::Bottom:
  424. container->addBottom(widgetToInsert);
  425. break;
  426. }
  427. }
  428. else
  429. {
  430. if(mRootContainer.mWidgets != nullptr)
  431. BS_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  432. mRootContainer.makeLeaf(_getParentWidget(), mParentWindow);
  433. mRootContainer.addWidget(widgetToInsert);
  434. }
  435. }
  436. void DockManager::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  437. {
  438. mRootContainer.setArea(x, y, width, height);
  439. mArea = Rect2I(x, y, width, height);
  440. updateDropOverlay(x, y, width, height);
  441. }
  442. void DockManager::closeAll()
  443. {
  444. mRootContainer = DockContainer(this);
  445. mMouseOverContainer = nullptr;
  446. }
  447. DockManagerLayoutPtr DockManager::getLayout() const
  448. {
  449. struct StackElem
  450. {
  451. StackElem(DockManagerLayout::Entry* layoutEntry, const DockContainer* container)
  452. :layoutEntry(layoutEntry), container(container)
  453. { }
  454. DockManagerLayout::Entry* layoutEntry;
  455. const DockContainer* container;
  456. };
  457. auto GetWidgetNamesInContainer = [&] (const DockContainer* container)
  458. {
  459. Vector<String> widgetNames;
  460. if(container->mWidgets != nullptr)
  461. {
  462. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  463. for(UINT32 i = 0; i < numWidgets; i++)
  464. {
  465. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  466. widgetNames.push_back(widget->getName());
  467. }
  468. }
  469. return widgetNames;
  470. };
  471. DockManagerLayoutPtr layout = bs_shared_ptr<DockManagerLayout>();
  472. DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  473. if(mRootContainer.mIsLeaf)
  474. {
  475. rootEntry->isLeaf = true;
  476. rootEntry->widgetNames = GetWidgetNamesInContainer(&mRootContainer);
  477. }
  478. else
  479. {
  480. rootEntry->isLeaf = false;
  481. rootEntry->horizontalSplit = mRootContainer.mIsHorizontal;
  482. rootEntry->splitPosition = mRootContainer.mSplitPosition;
  483. rootEntry->parent = nullptr;
  484. }
  485. Stack<StackElem> todo;
  486. todo.push(StackElem(rootEntry, &mRootContainer));
  487. while(!todo.empty())
  488. {
  489. StackElem currentElem = todo.top();
  490. todo.pop();
  491. if(!currentElem.container->mIsLeaf)
  492. {
  493. for(UINT32 i = 0; i < 2; i++)
  494. {
  495. if(currentElem.container->mChildren[i] == nullptr)
  496. continue;
  497. if(currentElem.container->mChildren[i]->mIsLeaf)
  498. {
  499. Vector<String> widgetNames = GetWidgetNamesInContainer(currentElem.container->mChildren[i]);
  500. currentElem.layoutEntry->children[i] =
  501. DockManagerLayout::Entry::createLeaf(currentElem.layoutEntry, i, widgetNames);
  502. }
  503. else
  504. {
  505. currentElem.layoutEntry->children[i] =
  506. DockManagerLayout::Entry::createContainer(currentElem.layoutEntry, i,
  507. currentElem.container->mChildren[i]->mSplitPosition,
  508. currentElem.container->mChildren[i]->mIsHorizontal);
  509. todo.push(StackElem(currentElem.layoutEntry->children[i], currentElem.container->mChildren[i]));
  510. }
  511. }
  512. }
  513. }
  514. return layout;
  515. }
  516. void DockManager::setLayout(const DockManagerLayoutPtr& layout)
  517. {
  518. // Undock all currently docked widgets
  519. Vector<EditorWidgetBase*> undockedWidgets;
  520. Stack<DockContainer*> todo;
  521. todo.push(&mRootContainer);
  522. while(!todo.empty())
  523. {
  524. DockContainer* current = todo.top();
  525. todo.pop();
  526. if(current->mIsLeaf)
  527. {
  528. if(current->mWidgets != nullptr)
  529. {
  530. while(current->mWidgets->getNumWidgets() > 0)
  531. {
  532. EditorWidgetBase* curWidget = current->mWidgets->getWidget(0);
  533. current->mWidgets->remove(*curWidget);
  534. undockedWidgets.push_back(curWidget);
  535. }
  536. }
  537. }
  538. else
  539. {
  540. todo.push(current->mChildren[0]);
  541. todo.push(current->mChildren[1]);
  542. }
  543. }
  544. mRootContainer = DockContainer(this);
  545. // Load layout
  546. struct StackEntry
  547. {
  548. StackEntry(const DockManagerLayout::Entry* layoutEntry, DockContainer* container)
  549. :layoutEntry(layoutEntry), container(container)
  550. { }
  551. const DockManagerLayout::Entry* layoutEntry;
  552. DockContainer* container;
  553. };
  554. auto GetLeafEntry = [] (const DockManagerLayout::Entry* parentEntry, UINT32 childIdx) -> const DockManagerLayout::Entry*
  555. {
  556. while(true)
  557. {
  558. if(parentEntry->isLeaf)
  559. return parentEntry;
  560. parentEntry = parentEntry->children[childIdx];
  561. }
  562. return nullptr;
  563. };
  564. auto OpenWidgets = [&] (DockContainer* parent, const Vector<String>& widgetNames)
  565. {
  566. for(auto& widgetName : widgetNames)
  567. {
  568. parent->addWidget(widgetName);
  569. }
  570. };
  571. // Dock elements
  572. const DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  573. const DockManagerLayout::Entry* leafEntry = GetLeafEntry(rootEntry, 0);
  574. if(leafEntry->widgetNames.size() > 0) // If zero, entire layout is empty
  575. {
  576. mRootContainer.makeLeaf(_getParentWidget(), mParentWindow);
  577. OpenWidgets(&mRootContainer, leafEntry->widgetNames);
  578. if(!rootEntry->isLeaf)
  579. {
  580. Stack<StackEntry> layoutTodo;
  581. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  582. while(!layoutTodo.empty())
  583. {
  584. StackEntry curEntry = layoutTodo.top();
  585. layoutTodo.pop();
  586. leafEntry = GetLeafEntry(curEntry.layoutEntry->children[1], 0);
  587. curEntry.container->splitContainer(curEntry.layoutEntry->horizontalSplit, false, curEntry.layoutEntry->splitPosition);
  588. DockContainer* otherChild = curEntry.container->mChildren[1];
  589. OpenWidgets(otherChild, leafEntry->widgetNames);
  590. if(!curEntry.layoutEntry->children[0]->isLeaf)
  591. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  592. if(!curEntry.layoutEntry->children[1]->isLeaf)
  593. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  594. }
  595. }
  596. }
  597. // Set container sizes
  598. {
  599. Stack<StackEntry> layoutTodo;
  600. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  601. while(!layoutTodo.empty())
  602. {
  603. StackEntry curEntry = layoutTodo.top();
  604. layoutTodo.pop();
  605. if(!curEntry.layoutEntry->isLeaf)
  606. {
  607. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  608. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  609. }
  610. }
  611. }
  612. // Destroy any widgets that are no longer docked anywhere
  613. for(auto& widget : undockedWidgets)
  614. {
  615. if(widget->_getParent() == nullptr)
  616. widget->close();
  617. }
  618. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  619. }
  620. void DockManager::updateClippedBounds()
  621. {
  622. // TODO - Clipping not actually accounted for but shouldn't matter as right now DockManager is only used in one specific situation
  623. mClippedBounds = mRootContainer.mArea;
  624. }
  625. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  626. {
  627. const static int spacing = 10;
  628. const static float innerScale = 0.75f;
  629. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  630. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  631. UINT32 inWidth = (UINT32)Math::floorToInt(innerScale * outWidth);
  632. UINT32 inHeight = (UINT32)Math::floorToInt(innerScale * outHeight);
  633. INT32 inXOffset = Math::floorToInt((outWidth - inWidth) * 0.5f);
  634. INT32 inYOffset = Math::floorToInt((outHeight - inHeight) * 0.5f);
  635. Vector2 outTopLeft((float)x, (float)y);
  636. Vector2 outTopRight((float)(x + outWidth), (float)y);
  637. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  638. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  639. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  640. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  641. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  642. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  643. VertexDataDescPtr vertexDesc = bs_shared_ptr<VertexDataDesc>();
  644. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  645. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  646. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(16, 24, vertexDesc);
  647. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  648. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  649. // Top
  650. Vector2 topOffset((float)spacing, 0.0f);
  651. mTopDropPolygon[0] = outTopLeft + topOffset;
  652. mTopDropPolygon[1] = outTopRight + topOffset;
  653. mTopDropPolygon[2] = inTopRight + topOffset;
  654. mTopDropPolygon[3] = inTopLeft + topOffset;
  655. vertIter.addValue(mTopDropPolygon[0]);
  656. vertIter.addValue(mTopDropPolygon[1]);
  657. vertIter.addValue(mTopDropPolygon[2]);
  658. vertIter.addValue(mTopDropPolygon[3]);
  659. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  660. UINT32 color32 = color.getAsRGBA();
  661. colIter.addValue(color32);
  662. colIter.addValue(color32);
  663. colIter.addValue(color32);
  664. colIter.addValue(color32);
  665. // Bottom
  666. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  667. mBotDropPolygon[0] = inBotLeft + botOffset;
  668. mBotDropPolygon[1] = inBotRight + botOffset;
  669. mBotDropPolygon[2] = outBotRight + botOffset;
  670. mBotDropPolygon[3] = outBotLeft + botOffset;
  671. vertIter.addValue(mBotDropPolygon[0]);
  672. vertIter.addValue(mBotDropPolygon[1]);
  673. vertIter.addValue(mBotDropPolygon[2]);
  674. vertIter.addValue(mBotDropPolygon[3]);
  675. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  676. color32 = color.getAsRGBA();
  677. colIter.addValue(color32);
  678. colIter.addValue(color32);
  679. colIter.addValue(color32);
  680. colIter.addValue(color32);
  681. // Left
  682. Vector2 leftOffset(0.0f, (float)spacing);
  683. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  684. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  685. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  686. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  687. vertIter.addValue(mLeftDropPolygon[0]);
  688. vertIter.addValue(mLeftDropPolygon[1]);
  689. vertIter.addValue(mLeftDropPolygon[2]);
  690. vertIter.addValue(mLeftDropPolygon[3]);
  691. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  692. color32 = color.getAsRGBA();
  693. colIter.addValue(color32);
  694. colIter.addValue(color32);
  695. colIter.addValue(color32);
  696. colIter.addValue(color32);
  697. // Right
  698. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  699. mRightDropPolygon[0] = inTopRight + rightOffset;
  700. mRightDropPolygon[1] = outTopRight + rightOffset;
  701. mRightDropPolygon[2] = outBotRight + rightOffset;
  702. mRightDropPolygon[3] = inBotRight + rightOffset;
  703. vertIter.addValue(mRightDropPolygon[0]);
  704. vertIter.addValue(mRightDropPolygon[1]);
  705. vertIter.addValue(mRightDropPolygon[2]);
  706. vertIter.addValue(mRightDropPolygon[3]);
  707. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  708. color32 = color.getAsRGBA();
  709. colIter.addValue(color32);
  710. colIter.addValue(color32);
  711. colIter.addValue(color32);
  712. colIter.addValue(color32);
  713. UINT32* indexData = meshData->getIndices32();
  714. // Top
  715. indexData[0] = 0;
  716. indexData[1] = 1;
  717. indexData[2] = 2;
  718. indexData[3] = 0;
  719. indexData[4] = 2;
  720. indexData[5] = 3;
  721. // Bottom
  722. indexData[6] = 4;
  723. indexData[7] = 5;
  724. indexData[8] = 6;
  725. indexData[9] = 4;
  726. indexData[10] = 6;
  727. indexData[11] = 7;
  728. // Left
  729. indexData[12] = 8;
  730. indexData[13] = 9;
  731. indexData[14] = 10;
  732. indexData[15] = 8;
  733. indexData[16] = 10;
  734. indexData[17] = 11;
  735. // Right
  736. indexData[18] = 12;
  737. indexData[19] = 13;
  738. indexData[20] = 14;
  739. indexData[21] = 12;
  740. indexData[22] = 14;
  741. indexData[23] = 15;
  742. mDropOverlayMesh = Mesh::create(meshData);
  743. }
  744. bool DockManager::mouseEvent(const GUIMouseEvent& event)
  745. {
  746. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  747. {
  748. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  749. return false;
  750. const Vector2I& widgetRelPos = event.getPosition();
  751. const Matrix4& worldTfrm = _getParentWidget()->SO()->getWorldTfrm();
  752. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  753. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  754. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  755. mMouseOverContainer = mRootContainer.findAtPos(windowPos);
  756. if(mMouseOverContainer == nullptr)
  757. mMouseOverContainer = &mRootContainer;
  758. Rect2I overlayBounds;
  759. if(mMouseOverContainer != nullptr)
  760. overlayBounds = mMouseOverContainer->getContentBounds();
  761. // Update mesh if needed
  762. if(mLastOverlayBounds != overlayBounds)
  763. {
  764. if(overlayBounds.width <= 0 || overlayBounds.height <= 0)
  765. mDropOverlayMesh = HMesh();
  766. else
  767. updateDropOverlay(overlayBounds.x, overlayBounds.y, overlayBounds.width, overlayBounds.height);
  768. mLastOverlayBounds = overlayBounds;
  769. }
  770. // Check if we need to highlight any drop locations
  771. if(mMouseOverContainer != nullptr)
  772. {
  773. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  774. mHighlightedDropLoc = DockLocation::Top;
  775. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  776. mHighlightedDropLoc = DockLocation::Bottom;
  777. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  778. mHighlightedDropLoc = DockLocation::Left;
  779. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  780. mHighlightedDropLoc = DockLocation::Right;
  781. else
  782. mHighlightedDropLoc = DockLocation::None;
  783. if(overlayBounds.contains(windowPos))
  784. mShowOverlay = true;
  785. else
  786. mShowOverlay = false;
  787. }
  788. else
  789. mShowOverlay = false;
  790. return true;
  791. }
  792. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  793. {
  794. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  795. return false;
  796. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  797. const Vector2I& widgetRelPos = event.getPosition();
  798. const Matrix4& worldTfrm = _getParentWidget()->SO()->getWorldTfrm();
  799. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  800. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  801. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  802. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  803. if(mouseOverContainer == nullptr)
  804. {
  805. Rect2I overlayBounds = mRootContainer.getContentBounds();
  806. if(overlayBounds.contains(windowPos))
  807. {
  808. insert(nullptr, draggedWidget, DockLocation::None);
  809. }
  810. }
  811. else
  812. {
  813. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  814. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  815. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  816. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  817. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  818. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  819. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  820. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  821. }
  822. return true;
  823. }
  824. return false;
  825. }
  826. // TODO - Move to a separate Polygon class?
  827. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  828. {
  829. bool isInside = false;
  830. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  831. {
  832. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  833. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  834. isInside = !isInside;
  835. }
  836. return isInside;
  837. }
  838. }