BsDockManager.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 = 4;
  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. if(mSlider != nullptr)
  125. {
  126. GUIElement::destroy(mSlider);
  127. mSlider = nullptr;
  128. }
  129. mWidgets->setPosition(mArea.x, mArea.y);
  130. mWidgets->setSize(mArea.width, mArea.height);
  131. }
  132. void DockManager::DockContainer::makeLeaf(const HSceneObject& guiWidgetSO, EditorWidgetContainer* existingContainer)
  133. {
  134. mIsLeaf = true;
  135. mWidgets = existingContainer;
  136. mGUIWidgetSO = guiWidgetSO;
  137. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  138. if(mSlider != nullptr)
  139. {
  140. GUIElement::destroy(mSlider);
  141. mSlider = nullptr;
  142. }
  143. mWidgets->setPosition(mArea.x, mArea.y);
  144. mWidgets->setSize(mArea.width, mArea.height);
  145. }
  146. void DockManager::DockContainer::addLeft(EditorWidgetBase* widget)
  147. {
  148. if(mIsLeaf)
  149. splitContainer(false, true);
  150. mChildren[0]->addWidget(widget);
  151. }
  152. void DockManager::DockContainer::addRight(EditorWidgetBase* widget)
  153. {
  154. if(mIsLeaf)
  155. splitContainer(false, false);
  156. mChildren[1]->addWidget(widget);
  157. }
  158. void DockManager::DockContainer::addTop(EditorWidgetBase* widget)
  159. {
  160. if(mIsLeaf)
  161. splitContainer(true, true);
  162. mChildren[0]->addWidget(widget);
  163. }
  164. void DockManager::DockContainer::addBottom(EditorWidgetBase* widget)
  165. {
  166. if(mIsLeaf)
  167. splitContainer(true, false);
  168. mChildren[1]->addWidget(widget);
  169. }
  170. void DockManager::DockContainer::splitContainer(bool horizontal, bool newChildIsFirst, float splitPosition)
  171. {
  172. DockContainer* children[2];
  173. UINT32 idxA = newChildIsFirst ? 0 : 1;
  174. UINT32 idxB = (idxA + 1) % 2;
  175. children[idxA] = bs_new<DockContainer>(mManager, this);
  176. children[idxB] = bs_new<DockContainer>(mManager, this);
  177. mWidgets->onWidgetClosed.clear();
  178. children[idxA]->makeLeaf(mManager->mParentWindow);
  179. children[idxB]->makeLeaf(mGUIWidgetSO, mWidgets);
  180. mWidgets = nullptr;
  181. mGUIWidgetSO = nullptr;
  182. makeSplit(children[0], children[1], horizontal, splitPosition);
  183. }
  184. void DockManager::DockContainer::makeSplit(DockManager::DockContainer* first, DockManager::DockContainer* second, bool horizontal, float splitPosition)
  185. {
  186. mChildren[0] = first;
  187. mChildren[1] = second;
  188. mIsLeaf = false;
  189. mIsHorizontal = horizontal;
  190. mSplitPosition = splitPosition;
  191. if (mWidgets != nullptr)
  192. {
  193. bs_delete(mWidgets);
  194. mWidgets = nullptr;
  195. }
  196. if (mGUIWidgetSO != nullptr)
  197. {
  198. mGUIWidgetSO->destroy();
  199. mGUIWidgetSO = nullptr;
  200. }
  201. if (mSlider != nullptr)
  202. {
  203. GUIElement::destroy(mSlider);
  204. mSlider = nullptr;
  205. }
  206. mSlider = GUIDockSlider::create(horizontal, "DockSliderBtn");
  207. mManager->_getParentWidget()->getPanel()->addElement(mSlider);
  208. mSlider->onDragged.connect(std::bind(&DockManager::DockContainer::sliderDragged, this, _1));
  209. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  210. }
  211. void DockManager::DockContainer::addWidget(EditorWidgetBase* widget)
  212. {
  213. if(!mIsLeaf)
  214. return;
  215. mWidgets->add(*widget);
  216. }
  217. void DockManager::DockContainer::addWidget(const String& name)
  218. {
  219. if(!mIsLeaf)
  220. return;
  221. EditorWidgetManager::instance().create(name, *mWidgets);
  222. }
  223. void DockManager::DockContainer::sliderDragged(const Vector2I& delta)
  224. {
  225. if(mIsHorizontal && delta.y != 0)
  226. {
  227. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.height - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  228. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  229. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.y, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  230. updateChildAreas();
  231. }
  232. else if(!mIsHorizontal && delta.x != 0)
  233. {
  234. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.width - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  235. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  236. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.x, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  237. updateChildAreas();
  238. }
  239. }
  240. void DockManager::DockContainer::widgetRemoved()
  241. {
  242. assert(mIsLeaf);
  243. if(mWidgets->getNumWidgets() == 0)
  244. {
  245. if(mParent == nullptr) // We're root so we just reset ourselves, can't delete root
  246. {
  247. bs_delete(mWidgets);
  248. mWidgets = nullptr;
  249. mGUIWidgetSO->destroy();
  250. mGUIWidgetSO = nullptr;
  251. mIsLeaf = true;
  252. mSplitPosition = 0.5f;
  253. mIsHorizontal = false;
  254. }
  255. else
  256. {
  257. // Replace our parent with our sibling
  258. DockContainer* sibling = nullptr;
  259. if(mParent->mChildren[0] == this)
  260. sibling = mParent->mChildren[1];
  261. else
  262. sibling = mParent->mChildren[0];
  263. if (sibling->mIsLeaf)
  264. {
  265. sibling->mWidgets->onWidgetClosed.clear();
  266. mParent->makeLeaf(sibling->mGUIWidgetSO, sibling->mWidgets);
  267. sibling->mWidgets = nullptr;
  268. sibling->mGUIWidgetSO = nullptr;
  269. }
  270. else
  271. {
  272. mParent->makeSplit(sibling->mChildren[0], sibling->mChildren[1], sibling->mIsHorizontal, sibling->mSplitPosition);
  273. sibling->mChildren[0]->mParent = mParent;
  274. sibling->mChildren[1]->mParent = mParent;
  275. sibling->mChildren[0] = nullptr;
  276. sibling->mChildren[1] = nullptr;
  277. }
  278. bs_delete(sibling);
  279. bs_delete(this);
  280. }
  281. }
  282. }
  283. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  284. {
  285. if(mIsLeaf)
  286. {
  287. if(mWidgets == widgetContainer)
  288. return this;
  289. else
  290. return nullptr;
  291. }
  292. else
  293. {
  294. if(mChildren[0] != nullptr)
  295. {
  296. DockContainer* foundContainer = mChildren[0]->find(widgetContainer);
  297. if (foundContainer != nullptr)
  298. return foundContainer;
  299. }
  300. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  301. {
  302. DockContainer* foundContainer = mChildren[1]->find(widgetContainer);
  303. if (foundContainer != nullptr)
  304. return foundContainer;
  305. }
  306. }
  307. return nullptr;
  308. }
  309. DockManager::DockContainer* DockManager::DockContainer::findAtPos(const Vector2I& pos)
  310. {
  311. if(mIsLeaf)
  312. {
  313. if(mArea.contains(pos))
  314. {
  315. return this;
  316. }
  317. }
  318. else
  319. {
  320. if (mChildren[0] != nullptr)
  321. {
  322. DockContainer* foundContainer = mChildren[0]->findAtPos(pos);
  323. if (foundContainer != nullptr)
  324. return foundContainer;
  325. }
  326. if(mChildren[1] != nullptr)
  327. {
  328. DockContainer* foundContainer = mChildren[1]->findAtPos(pos);
  329. if (foundContainer != nullptr)
  330. return foundContainer;
  331. }
  332. }
  333. return nullptr;
  334. }
  335. Rect2I DockManager::DockContainer::getContentBounds() const
  336. {
  337. if(!mIsLeaf || mWidgets == nullptr)
  338. return mArea;
  339. return mWidgets->getContentBounds();
  340. }
  341. void DockManager::DockContainer::update()
  342. {
  343. if (mIsLeaf)
  344. {
  345. if (mWidgets != nullptr)
  346. mWidgets->update();
  347. }
  348. else
  349. {
  350. if (mChildren[0] != nullptr)
  351. mChildren[0]->update();
  352. if (mChildren[1] != nullptr)
  353. mChildren[1]->update();
  354. }
  355. }
  356. DockManager::DockManager(EditorWindowBase* parentWindow, const GUIDimensions& dimensions)
  357. :GUIElementContainer(dimensions), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None),
  358. mShowOverlay(false), mRootContainer(this), mParentWindow(parentWindow)
  359. {
  360. mTopDropPolygon = bs_newN<Vector2>(4);
  361. mBotDropPolygon = bs_newN<Vector2>(4);
  362. mLeftDropPolygon = bs_newN<Vector2>(4);
  363. mRightDropPolygon = bs_newN<Vector2>(4);
  364. HMaterial dropOverlayMat = BuiltinEditorResources::instance().createDockDropOverlayMaterial();
  365. mCore.store(bs_new<DockOverlayRenderer>(), std::memory_order_release);
  366. gCoreAccessor().queueCommand(std::bind(&DockManager::initializeOverlayRenderer,
  367. this, dropOverlayMat->getCore()));
  368. }
  369. DockManager::~DockManager()
  370. {
  371. bs_deleteN(mTopDropPolygon, 4);
  372. bs_deleteN(mBotDropPolygon, 4);
  373. bs_deleteN(mLeftDropPolygon, 4);
  374. bs_deleteN(mRightDropPolygon, 4);
  375. gCoreAccessor().queueCommand(std::bind(&DockManager::destroyOverlayRenderer,
  376. this, mCore.load(std::memory_order_relaxed)));
  377. }
  378. void DockManager::initializeOverlayRenderer(const SPtr<MaterialCore>& initData)
  379. {
  380. mCore.load(std::memory_order_acquire)->initialize(initData);
  381. }
  382. void DockManager::destroyOverlayRenderer(DockOverlayRenderer* core)
  383. {
  384. bs_delete(core);
  385. }
  386. DockManager* DockManager::create(EditorWindowBase* parentWindow)
  387. {
  388. return new (bs_alloc<DockManager>()) DockManager(parentWindow, GUIDimensions::create());
  389. }
  390. void DockManager::update()
  391. {
  392. if(!DragAndDropManager::instance().isDragInProgress())
  393. {
  394. mHighlightedDropLoc = DockLocation::None;
  395. mShowOverlay = false;
  396. }
  397. mRootContainer.update();
  398. HCamera camera = mParentWindow->getGUICamera();
  399. DockOverlayRenderer* core = mCore.load(std::memory_order_relaxed);
  400. gCoreAccessor().queueCommand(std::bind(&DockOverlayRenderer::updateData, core, camera->_getCamera()->getCore(),
  401. mDropOverlayMesh->getCore(), mShowOverlay, mHighlightedDropLoc));
  402. }
  403. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  404. {
  405. if(relativeTo != nullptr)
  406. {
  407. DockContainer* container = mRootContainer.find(relativeTo);
  408. if(container == nullptr)
  409. BS_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  410. switch(location)
  411. {
  412. case DockLocation::Left:
  413. container->addLeft(widgetToInsert);
  414. break;
  415. case DockLocation::Right:
  416. container->addRight(widgetToInsert);
  417. break;
  418. case DockLocation::Top:
  419. container->addTop(widgetToInsert);
  420. break;
  421. case DockLocation::Bottom:
  422. container->addBottom(widgetToInsert);
  423. break;
  424. }
  425. }
  426. else
  427. {
  428. if(mRootContainer.mWidgets != nullptr)
  429. BS_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  430. mRootContainer.makeLeaf(mParentWindow);
  431. mRootContainer.addWidget(widgetToInsert);
  432. }
  433. }
  434. void DockManager::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  435. {
  436. mRootContainer.setArea(x, y, width, height);
  437. mArea = Rect2I(x, y, width, height);
  438. updateDropOverlay(x, y, width, height);
  439. }
  440. void DockManager::closeAll()
  441. {
  442. mRootContainer = DockContainer(this);
  443. mMouseOverContainer = nullptr;
  444. }
  445. DockManagerLayoutPtr DockManager::getLayout() const
  446. {
  447. struct StackElem
  448. {
  449. StackElem(DockManagerLayout::Entry* layoutEntry, const DockContainer* container)
  450. :layoutEntry(layoutEntry), container(container)
  451. { }
  452. DockManagerLayout::Entry* layoutEntry;
  453. const DockContainer* container;
  454. };
  455. auto GetWidgetNamesInContainer = [&] (const DockContainer* container)
  456. {
  457. Vector<String> widgetNames;
  458. if(container->mWidgets != nullptr)
  459. {
  460. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  461. for(UINT32 i = 0; i < numWidgets; i++)
  462. {
  463. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  464. widgetNames.push_back(widget->getName());
  465. }
  466. }
  467. return widgetNames;
  468. };
  469. DockManagerLayoutPtr layout = bs_shared_ptr_new<DockManagerLayout>();
  470. DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  471. if(mRootContainer.mIsLeaf)
  472. {
  473. rootEntry->isLeaf = true;
  474. rootEntry->widgetNames = GetWidgetNamesInContainer(&mRootContainer);
  475. }
  476. else
  477. {
  478. rootEntry->isLeaf = false;
  479. rootEntry->horizontalSplit = mRootContainer.mIsHorizontal;
  480. rootEntry->splitPosition = mRootContainer.mSplitPosition;
  481. rootEntry->parent = nullptr;
  482. }
  483. Stack<StackElem> todo;
  484. todo.push(StackElem(rootEntry, &mRootContainer));
  485. while(!todo.empty())
  486. {
  487. StackElem currentElem = todo.top();
  488. todo.pop();
  489. if(!currentElem.container->mIsLeaf)
  490. {
  491. for(UINT32 i = 0; i < 2; i++)
  492. {
  493. if(currentElem.container->mChildren[i] == nullptr)
  494. continue;
  495. if(currentElem.container->mChildren[i]->mIsLeaf)
  496. {
  497. Vector<String> widgetNames = GetWidgetNamesInContainer(currentElem.container->mChildren[i]);
  498. currentElem.layoutEntry->children[i] =
  499. DockManagerLayout::Entry::createLeaf(currentElem.layoutEntry, i, widgetNames);
  500. }
  501. else
  502. {
  503. currentElem.layoutEntry->children[i] =
  504. DockManagerLayout::Entry::createContainer(currentElem.layoutEntry, i,
  505. currentElem.container->mChildren[i]->mSplitPosition,
  506. currentElem.container->mChildren[i]->mIsHorizontal);
  507. todo.push(StackElem(currentElem.layoutEntry->children[i], currentElem.container->mChildren[i]));
  508. }
  509. }
  510. }
  511. }
  512. return layout;
  513. }
  514. void DockManager::setLayout(const DockManagerLayoutPtr& layout)
  515. {
  516. // Undock all currently docked widgets
  517. Vector<EditorWidgetBase*> undockedWidgets;
  518. Stack<DockContainer*> todo;
  519. todo.push(&mRootContainer);
  520. while(!todo.empty())
  521. {
  522. DockContainer* current = todo.top();
  523. todo.pop();
  524. if(current->mIsLeaf)
  525. {
  526. if(current->mWidgets != nullptr)
  527. {
  528. while(current->mWidgets->getNumWidgets() > 0)
  529. {
  530. EditorWidgetBase* curWidget = current->mWidgets->getWidget(0);
  531. current->mWidgets->remove(*curWidget);
  532. undockedWidgets.push_back(curWidget);
  533. }
  534. }
  535. }
  536. else
  537. {
  538. todo.push(current->mChildren[0]);
  539. todo.push(current->mChildren[1]);
  540. }
  541. }
  542. mRootContainer = DockContainer(this);
  543. // Load layout
  544. struct StackEntry
  545. {
  546. StackEntry(const DockManagerLayout::Entry* layoutEntry, DockContainer* container)
  547. :layoutEntry(layoutEntry), container(container)
  548. { }
  549. const DockManagerLayout::Entry* layoutEntry;
  550. DockContainer* container;
  551. };
  552. auto GetLeafEntry = [] (const DockManagerLayout::Entry* parentEntry, UINT32 childIdx) -> const DockManagerLayout::Entry*
  553. {
  554. while(true)
  555. {
  556. if(parentEntry->isLeaf)
  557. return parentEntry;
  558. parentEntry = parentEntry->children[childIdx];
  559. }
  560. return nullptr;
  561. };
  562. auto OpenWidgets = [&] (DockContainer* parent, const Vector<String>& widgetNames)
  563. {
  564. for(auto& widgetName : widgetNames)
  565. {
  566. parent->addWidget(widgetName);
  567. }
  568. };
  569. // Prune layout by removing invalid leafs (ones with no widgets, or widgets that no longer exist)
  570. layout->pruneInvalidLeaves();
  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(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_new<VertexDataDesc>();
  644. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  645. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  646. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(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 false;
  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. return true;
  810. }
  811. }
  812. else
  813. {
  814. if (insidePolygon(mTopDropPolygon, 4, windowPosVec))
  815. {
  816. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  817. return true;
  818. }
  819. else if (insidePolygon(mBotDropPolygon, 4, windowPosVec))
  820. {
  821. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  822. return true;
  823. }
  824. else if (insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  825. {
  826. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  827. return true;
  828. }
  829. else if (insidePolygon(mRightDropPolygon, 4, windowPosVec))
  830. {
  831. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  832. return true;
  833. }
  834. }
  835. }
  836. return false;
  837. }
  838. // TODO - Move to a separate Polygon class?
  839. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  840. {
  841. bool isInside = false;
  842. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  843. {
  844. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  845. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  846. isInside = !isInside;
  847. }
  848. return isInside;
  849. }
  850. const Color DockOverlayRenderer::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  851. const Color DockOverlayRenderer::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  852. DockOverlayRenderer::DockOverlayRenderer()
  853. :mShowOverlay(false), mHighlightedDropLoc(DockManager::DockLocation::None)
  854. {
  855. }
  856. DockOverlayRenderer::~DockOverlayRenderer()
  857. {
  858. if (mCamera != nullptr)
  859. {
  860. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  861. activeRenderer->_unregisterRenderCallback(mCamera.get(), -20);
  862. }
  863. }
  864. void DockOverlayRenderer::initialize(const SPtr<MaterialCore>& material)
  865. {
  866. mMaterial = material;
  867. }
  868. void DockOverlayRenderer::updateData(const SPtr<CameraCore>& camera, const SPtr<MeshCore>& mesh, bool active,
  869. DockManager::DockLocation location)
  870. {
  871. if (mCamera != camera)
  872. {
  873. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  874. if (mCamera != nullptr)
  875. activeRenderer->_unregisterRenderCallback(mCamera.get(), -20);
  876. if (camera != nullptr)
  877. activeRenderer->_registerRenderCallback(camera.get(), -20, std::bind(&DockOverlayRenderer::render, this));
  878. }
  879. mCamera = camera;
  880. mMesh = mesh;
  881. mShowOverlay = active;
  882. mHighlightedDropLoc = location;
  883. }
  884. void DockOverlayRenderer::render()
  885. {
  886. THROW_IF_NOT_CORE_THREAD;
  887. if (!mShowOverlay)
  888. return;
  889. SPtr<ViewportCore> viewport = mCamera->getViewport();
  890. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  891. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  892. mMaterial->setFloat("invViewportWidth", invViewportWidth);
  893. mMaterial->setFloat("invViewportHeight", invViewportHeight);
  894. mMaterial->setColor("tintColor", TINT_COLOR);
  895. mMaterial->setColor("highlightColor", HIGHLIGHT_COLOR);
  896. Color highlightColor;
  897. switch (mHighlightedDropLoc)
  898. {
  899. case DockManager::DockLocation::Top:
  900. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  901. break;
  902. case DockManager::DockLocation::Bottom:
  903. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  904. break;
  905. case DockManager::DockLocation::Left:
  906. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  907. break;
  908. case DockManager::DockLocation::Right:
  909. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  910. break;
  911. case DockManager::DockLocation::None:
  912. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  913. break;
  914. }
  915. mMaterial->setColor("highlightActive", highlightColor);
  916. CoreRenderer::setPass(mMaterial, 0);
  917. CoreRenderer::draw(mMesh, mMesh->getProperties().getSubMesh(0));
  918. }
  919. }