2
0

BsDockManager.cpp 30 KB

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