BsDockManager.cpp 30 KB

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