2
0

BsDockManager.cpp 30 KB

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