BsDockManager.cpp 29 KB

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