BsDockManager.cpp 30 KB

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