BsDockManager.cpp 34 KB

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