BsDockManager.cpp 29 KB

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