BsDockManager.cpp 29 KB

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