BsDockManager.cpp 28 KB

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