BsDockManager.cpp 28 KB

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