BsDockManager.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. #include "BsDockManager.h"
  2. #include "BsEditorWidgetContainer.h"
  3. #include "BsEditorWidget.h"
  4. #include "BsEditorWidgetManager.h"
  5. #include "CmMath.h"
  6. #include "CmException.h"
  7. #include "CmMesh.h"
  8. #include "CmMaterial.h"
  9. #include "CmVector2.h"
  10. #include "CmRenderQueue.h"
  11. #include "CmApplication.h"
  12. #include "CmRendererManager.h"
  13. #include "CmRenderer.h"
  14. #include "CmSceneObject.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 "CmVertexDataDesc.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. cm_delete(mWidgets);
  52. if(!mIsLeaf)
  53. {
  54. if(mChildren[0] != nullptr)
  55. cm_delete(mChildren[0]);
  56. if(mChildren[1] != nullptr)
  57. cm_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 = cm_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] = cm_new<DockContainer>(this);
  169. mChildren[idxB] = cm_new<DockContainer>(this);
  170. mWidgets->onWidgetClosed.disconnect_all_slots();
  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. cm_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.disconnect_all_slots();
  242. mParent->makeLeaf(sibling->mWidgets);
  243. sibling->mWidgets = nullptr;
  244. cm_delete(sibling);
  245. cm_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. DockManager::DockManager(RenderWindow* parentWindow, const GUILayoutOptions& layoutOptions)
  292. :GUIElementContainer(layoutOptions), mParentWindow(parentWindow), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None),
  293. mShowOverlay(false), mAddedRenderCallback(false)
  294. {
  295. mTopDropPolygon = cm_newN<Vector2>(4);
  296. mBotDropPolygon = cm_newN<Vector2>(4);
  297. mLeftDropPolygon = cm_newN<Vector2>(4);
  298. mRightDropPolygon = cm_newN<Vector2>(4);
  299. mDropOverlayMat = BuiltinMaterialManager::instance().createDockDropOverlayMaterial();
  300. }
  301. DockManager::~DockManager()
  302. {
  303. cm_deleteN(mTopDropPolygon, 4);
  304. cm_deleteN(mBotDropPolygon, 4);
  305. cm_deleteN(mLeftDropPolygon, 4);
  306. cm_deleteN(mRightDropPolygon, 4);
  307. }
  308. DockManager* DockManager::create(RenderWindow* parentWindow)
  309. {
  310. return new (cm_alloc<DockManager, PoolAlloc>()) DockManager(parentWindow, GUILayoutOptions::create());
  311. }
  312. void DockManager::update()
  313. {
  314. if(!DragAndDropManager::instance().isDragInProgress())
  315. {
  316. mHighlightedDropLoc = DockLocation::None;
  317. mShowOverlay = false;
  318. }
  319. }
  320. void DockManager::render(const Viewport* viewport, RenderQueue& renderQueue)
  321. {
  322. if(!mShowOverlay)
  323. return;
  324. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  325. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  326. if(mDropOverlayMesh == nullptr || !mDropOverlayMesh.isLoaded() || !mDropOverlayMesh->isInitialized())
  327. return;
  328. if(mDropOverlayMat == nullptr || !mDropOverlayMat.isLoaded() || !mDropOverlayMat->isInitialized())
  329. return;
  330. mDropOverlayMat->setFloat("invViewportWidth", invViewportWidth);
  331. mDropOverlayMat->setFloat("invViewportHeight", invViewportHeight);
  332. mDropOverlayMat->setColor("tintColor", TINT_COLOR);
  333. mDropOverlayMat->setColor("highlightColor", HIGHLIGHT_COLOR);
  334. Color highlightColor;
  335. switch(mHighlightedDropLoc)
  336. {
  337. case DockLocation::Top:
  338. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  339. break;
  340. case DockLocation::Bottom:
  341. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  342. break;
  343. case DockLocation::Left:
  344. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  345. break;
  346. case DockLocation::Right:
  347. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  348. break;
  349. case DockLocation::None:
  350. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  351. break;
  352. }
  353. mDropOverlayMat->setColor("highlightActive", highlightColor);
  354. renderQueue.add(mDropOverlayMat.getInternalPtr(), mDropOverlayMesh.getInternalPtr(), 0, Vector3::ZERO);
  355. }
  356. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  357. {
  358. if(relativeTo != nullptr)
  359. {
  360. DockContainer* container = mRootContainer.find(relativeTo);
  361. if(container == nullptr)
  362. CM_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  363. switch(location)
  364. {
  365. case DockLocation::Left:
  366. container->addLeft(_getParentWidget(), mParentWindow, widgetToInsert);
  367. break;
  368. case DockLocation::Right:
  369. container->addRight(_getParentWidget(), mParentWindow, widgetToInsert);
  370. break;
  371. case DockLocation::Top:
  372. container->addTop(_getParentWidget(), mParentWindow, widgetToInsert);
  373. break;
  374. case DockLocation::Bottom:
  375. container->addBottom(_getParentWidget(), mParentWindow, widgetToInsert);
  376. break;
  377. }
  378. }
  379. else
  380. {
  381. if(mRootContainer.mWidgets != nullptr)
  382. CM_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  383. mRootContainer.makeLeaf(_getParentWidget(), mParentWindow);
  384. mRootContainer.addWidget(widgetToInsert);
  385. }
  386. }
  387. void DockManager::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  388. {
  389. mRootContainer.setArea(x, y, width, height);
  390. mArea = RectI(x, y, width, height);
  391. updateDropOverlay(x, y, width, height);
  392. }
  393. DockManagerLayoutPtr DockManager::getLayout() const
  394. {
  395. struct StackElem
  396. {
  397. StackElem(DockManagerLayout::Entry* layoutEntry, const DockContainer* container)
  398. :layoutEntry(layoutEntry), container(container)
  399. { }
  400. DockManagerLayout::Entry* layoutEntry;
  401. const DockContainer* container;
  402. };
  403. auto GetWidgetNamesInContainer = [&] (const DockContainer* container)
  404. {
  405. Vector<String>::type widgetNames;
  406. if(container->mWidgets != nullptr)
  407. {
  408. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  409. for(UINT32 i = 0; i < numWidgets; i++)
  410. {
  411. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  412. widgetNames.push_back(widget->getName());
  413. }
  414. }
  415. return widgetNames;
  416. };
  417. DockManagerLayoutPtr layout = cm_shared_ptr<DockManagerLayout>();
  418. DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  419. if(mRootContainer.mIsLeaf)
  420. {
  421. rootEntry->isLeaf = true;
  422. rootEntry->widgetNames = GetWidgetNamesInContainer(&mRootContainer);
  423. }
  424. else
  425. {
  426. rootEntry->isLeaf = false;
  427. rootEntry->horizontalSplit = mRootContainer.mIsHorizontal;
  428. rootEntry->splitPosition = mRootContainer.mSplitPosition;
  429. rootEntry->parent = nullptr;
  430. }
  431. Stack<StackElem>::type todo;
  432. todo.push(StackElem(rootEntry, &mRootContainer));
  433. while(!todo.empty())
  434. {
  435. StackElem currentElem = todo.top();
  436. todo.pop();
  437. if(!currentElem.container->mIsLeaf)
  438. {
  439. for(UINT32 i = 0; i < 2; i++)
  440. {
  441. if(currentElem.container->mChildren[i] == nullptr)
  442. continue;
  443. if(currentElem.container->mChildren[i]->mIsLeaf)
  444. {
  445. Vector<String>::type widgetNames = GetWidgetNamesInContainer(currentElem.container->mChildren[i]);
  446. currentElem.layoutEntry->children[i] =
  447. DockManagerLayout::Entry::createLeaf(currentElem.layoutEntry, i, widgetNames);
  448. }
  449. else
  450. {
  451. currentElem.layoutEntry->children[i] =
  452. DockManagerLayout::Entry::createContainer(currentElem.layoutEntry, i,
  453. currentElem.container->mChildren[i]->mSplitPosition,
  454. currentElem.container->mChildren[i]->mIsHorizontal);
  455. todo.push(StackElem(currentElem.layoutEntry->children[i], currentElem.container->mChildren[i]));
  456. }
  457. }
  458. }
  459. }
  460. return layout;
  461. }
  462. void DockManager::setLayout(const DockManagerLayoutPtr& layout)
  463. {
  464. // Undock all currently docked widgets
  465. Vector<EditorWidgetBase*>::type undockedWidgets;
  466. Stack<DockContainer*>::type todo;
  467. todo.push(&mRootContainer);
  468. while(!todo.empty())
  469. {
  470. DockContainer* current = todo.top();
  471. todo.pop();
  472. if(current->mIsLeaf)
  473. {
  474. if(current->mWidgets != nullptr)
  475. {
  476. while(current->mWidgets->getNumWidgets() > 0)
  477. {
  478. EditorWidgetBase* curWidget = current->mWidgets->getWidget(0);
  479. current->mWidgets->remove(*curWidget);
  480. undockedWidgets.push_back(curWidget);
  481. }
  482. }
  483. }
  484. else
  485. {
  486. todo.push(current->mChildren[0]);
  487. todo.push(current->mChildren[1]);
  488. }
  489. }
  490. mRootContainer = DockContainer();
  491. // Load layout
  492. struct StackEntry
  493. {
  494. StackEntry(const DockManagerLayout::Entry* layoutEntry, DockContainer* container)
  495. :layoutEntry(layoutEntry), container(container)
  496. { }
  497. const DockManagerLayout::Entry* layoutEntry;
  498. DockContainer* container;
  499. };
  500. auto GetLeafEntry = [] (const DockManagerLayout::Entry* parentEntry, UINT32 childIdx) -> const DockManagerLayout::Entry*
  501. {
  502. while(true)
  503. {
  504. if(parentEntry->isLeaf)
  505. return parentEntry;
  506. parentEntry = parentEntry->children[childIdx];
  507. }
  508. return nullptr;
  509. };
  510. auto OpenWidgets = [&] (DockContainer* parent, const Vector<String>::type& widgetNames)
  511. {
  512. for(auto& widgetName : widgetNames)
  513. {
  514. parent->addWidget(widgetName);
  515. }
  516. };
  517. // Dock elements
  518. const DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  519. const DockManagerLayout::Entry* leafEntry = GetLeafEntry(rootEntry, 0);
  520. if(leafEntry->widgetNames.size() > 0) // If zero, entire layout is empty
  521. {
  522. mRootContainer.makeLeaf(_getParentWidget(), mParentWindow);
  523. OpenWidgets(&mRootContainer, leafEntry->widgetNames);
  524. if(!rootEntry->isLeaf)
  525. {
  526. Stack<StackEntry>::type layoutTodo;
  527. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  528. while(!layoutTodo.empty())
  529. {
  530. StackEntry curEntry = layoutTodo.top();
  531. layoutTodo.pop();
  532. leafEntry = GetLeafEntry(curEntry.layoutEntry->children[1], 0);
  533. curEntry.container->splitContainer(_getParentWidget(), mParentWindow, curEntry.layoutEntry->horizontalSplit, false, curEntry.layoutEntry->splitPosition);
  534. DockContainer* otherChild = curEntry.container->mChildren[1];
  535. OpenWidgets(otherChild, leafEntry->widgetNames);
  536. if(!curEntry.layoutEntry->children[0]->isLeaf)
  537. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  538. if(!curEntry.layoutEntry->children[1]->isLeaf)
  539. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  540. }
  541. }
  542. }
  543. // Set container sizes
  544. {
  545. Stack<StackEntry>::type layoutTodo;
  546. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  547. while(!layoutTodo.empty())
  548. {
  549. StackEntry curEntry = layoutTodo.top();
  550. layoutTodo.pop();
  551. if(!curEntry.layoutEntry->isLeaf)
  552. {
  553. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  554. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  555. }
  556. }
  557. }
  558. // Destroy any widgets that are no longer docked anywhere
  559. for(auto& widget : undockedWidgets)
  560. {
  561. if(widget->_getParent() == nullptr)
  562. widget->close();
  563. }
  564. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  565. }
  566. void DockManager::updateClippedBounds()
  567. {
  568. // TODO - Clipping not actually accounted for but shouldn't matter as right now DockManager is only used in one specific situation
  569. mClippedBounds = mRootContainer.mArea;
  570. }
  571. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  572. {
  573. const static int spacing = 10;
  574. const static float innerScale = 0.75f;
  575. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  576. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  577. UINT32 inWidth = (UINT32)Math::floorToInt(innerScale * outWidth);
  578. UINT32 inHeight = (UINT32)Math::floorToInt(innerScale * outHeight);
  579. INT32 inXOffset = Math::floorToInt((outWidth - inWidth) * 0.5f);
  580. INT32 inYOffset = Math::floorToInt((outHeight - inHeight) * 0.5f);
  581. Vector2 outTopLeft((float)x, (float)y);
  582. Vector2 outTopRight((float)(x + outWidth), (float)y);
  583. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  584. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  585. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  586. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  587. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  588. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  589. VertexDataDescPtr vertexDesc = cm_shared_ptr<VertexDataDesc>();
  590. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  591. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  592. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(16, 24, vertexDesc);
  593. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  594. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  595. // Top
  596. Vector2 topOffset((float)spacing, 0.0f);
  597. mTopDropPolygon[0] = outTopLeft + topOffset;
  598. mTopDropPolygon[1] = outTopRight + topOffset;
  599. mTopDropPolygon[2] = inTopRight + topOffset;
  600. mTopDropPolygon[3] = inTopLeft + topOffset;
  601. vertIter.addValue(mTopDropPolygon[0]);
  602. vertIter.addValue(mTopDropPolygon[1]);
  603. vertIter.addValue(mTopDropPolygon[2]);
  604. vertIter.addValue(mTopDropPolygon[3]);
  605. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  606. UINT32 color32 = color.getAsRGBA();
  607. colIter.addValue(color32);
  608. colIter.addValue(color32);
  609. colIter.addValue(color32);
  610. colIter.addValue(color32);
  611. // Bottom
  612. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  613. mBotDropPolygon[0] = inBotLeft + botOffset;
  614. mBotDropPolygon[1] = inBotRight + botOffset;
  615. mBotDropPolygon[2] = outBotRight + botOffset;
  616. mBotDropPolygon[3] = outBotLeft + botOffset;
  617. vertIter.addValue(mBotDropPolygon[0]);
  618. vertIter.addValue(mBotDropPolygon[1]);
  619. vertIter.addValue(mBotDropPolygon[2]);
  620. vertIter.addValue(mBotDropPolygon[3]);
  621. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  622. color32 = color.getAsRGBA();
  623. colIter.addValue(color32);
  624. colIter.addValue(color32);
  625. colIter.addValue(color32);
  626. colIter.addValue(color32);
  627. // Left
  628. Vector2 leftOffset(0.0f, (float)spacing);
  629. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  630. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  631. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  632. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  633. vertIter.addValue(mLeftDropPolygon[0]);
  634. vertIter.addValue(mLeftDropPolygon[1]);
  635. vertIter.addValue(mLeftDropPolygon[2]);
  636. vertIter.addValue(mLeftDropPolygon[3]);
  637. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  638. color32 = color.getAsRGBA();
  639. colIter.addValue(color32);
  640. colIter.addValue(color32);
  641. colIter.addValue(color32);
  642. colIter.addValue(color32);
  643. // Right
  644. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  645. mRightDropPolygon[0] = inTopRight + rightOffset;
  646. mRightDropPolygon[1] = outTopRight + rightOffset;
  647. mRightDropPolygon[2] = outBotRight + rightOffset;
  648. mRightDropPolygon[3] = inBotRight + rightOffset;
  649. vertIter.addValue(mRightDropPolygon[0]);
  650. vertIter.addValue(mRightDropPolygon[1]);
  651. vertIter.addValue(mRightDropPolygon[2]);
  652. vertIter.addValue(mRightDropPolygon[3]);
  653. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  654. color32 = color.getAsRGBA();
  655. colIter.addValue(color32);
  656. colIter.addValue(color32);
  657. colIter.addValue(color32);
  658. colIter.addValue(color32);
  659. UINT32* indexData = meshData->getIndices32();
  660. // Top
  661. indexData[0] = 0;
  662. indexData[1] = 1;
  663. indexData[2] = 2;
  664. indexData[3] = 0;
  665. indexData[4] = 2;
  666. indexData[5] = 3;
  667. // Bottom
  668. indexData[6] = 4;
  669. indexData[7] = 5;
  670. indexData[8] = 6;
  671. indexData[9] = 4;
  672. indexData[10] = 6;
  673. indexData[11] = 7;
  674. // Left
  675. indexData[12] = 8;
  676. indexData[13] = 9;
  677. indexData[14] = 10;
  678. indexData[15] = 8;
  679. indexData[16] = 10;
  680. indexData[17] = 11;
  681. // Right
  682. indexData[18] = 12;
  683. indexData[19] = 13;
  684. indexData[20] = 14;
  685. indexData[21] = 12;
  686. indexData[22] = 14;
  687. indexData[23] = 15;
  688. mDropOverlayMesh = Mesh::create(meshData);
  689. }
  690. bool DockManager::mouseEvent(const GUIMouseEvent& event)
  691. {
  692. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  693. {
  694. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  695. return false;
  696. const Vector2I& widgetRelPos = event.getPosition();
  697. const Matrix4& worldTfrm = _getParentWidget()->SO()->getWorldTfrm();
  698. Vector4 tfrmdPos = worldTfrm.multiply3x4(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  699. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  700. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  701. mMouseOverContainer = mRootContainer.findAtPos(windowPos);
  702. if(mMouseOverContainer == nullptr)
  703. mMouseOverContainer = &mRootContainer;
  704. RectI overlayBounds;
  705. if(mMouseOverContainer != nullptr)
  706. overlayBounds = mMouseOverContainer->getContentBounds();
  707. // Update mesh if needed
  708. if(mLastOverlayBounds != overlayBounds)
  709. {
  710. if(overlayBounds.width <= 0 || overlayBounds.height <= 0)
  711. mDropOverlayMesh = HMesh();
  712. else
  713. updateDropOverlay(overlayBounds.x, overlayBounds.y, overlayBounds.width, overlayBounds.height);
  714. mLastOverlayBounds = overlayBounds;
  715. }
  716. // Check if we need to highlight any drop locations
  717. if(mMouseOverContainer != nullptr)
  718. {
  719. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  720. mHighlightedDropLoc = DockLocation::Top;
  721. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  722. mHighlightedDropLoc = DockLocation::Bottom;
  723. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  724. mHighlightedDropLoc = DockLocation::Left;
  725. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  726. mHighlightedDropLoc = DockLocation::Right;
  727. else
  728. mHighlightedDropLoc = DockLocation::None;
  729. if(overlayBounds.contains(windowPos))
  730. mShowOverlay = true;
  731. else
  732. mShowOverlay = false;
  733. }
  734. else
  735. mShowOverlay = false;
  736. return true;
  737. }
  738. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  739. {
  740. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  741. return false;
  742. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  743. const Vector2I& widgetRelPos = event.getPosition();
  744. const Matrix4& worldTfrm = _getParentWidget()->SO()->getWorldTfrm();
  745. Vector4 tfrmdPos = worldTfrm.multiply3x4(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  746. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  747. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  748. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  749. if(mouseOverContainer == nullptr)
  750. {
  751. RectI overlayBounds = mRootContainer.getContentBounds();
  752. if(overlayBounds.contains(windowPos))
  753. {
  754. insert(nullptr, draggedWidget, DockLocation::None);
  755. }
  756. }
  757. else
  758. {
  759. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  760. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  761. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  762. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  763. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  764. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  765. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  766. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  767. }
  768. return true;
  769. }
  770. return false;
  771. }
  772. void DockManager::_changeParentWidget(GUIWidget* widget)
  773. {
  774. GUIElement::_changeParentWidget(widget);
  775. if(widget != nullptr)
  776. {
  777. if(mAddedRenderCallback)
  778. {
  779. // Note: Adding support for this should be fairly simple though, I just didn't bother with it. You could
  780. // remove the current render callback and register a new one. Will likely need to make other minor fixes.
  781. CM_EXCEPT(InvalidStateException, "Attempting to change parent widget of a DockManager. This is not supported");
  782. }
  783. RendererManager::instance().getActive()->addRenderCallback(widget->getTarget(), std::bind(&DockManager::render, this, _1, _2));
  784. mAddedRenderCallback = true;
  785. }
  786. }
  787. // TODO - Move to a separate Polygon class?
  788. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  789. {
  790. bool isInside = false;
  791. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  792. {
  793. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  794. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  795. isInside = !isInside;
  796. }
  797. return isInside;
  798. }
  799. }