BsDockManager.cpp 29 KB

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