BsDockManager.cpp 34 KB

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