BsDockManager.cpp 29 KB

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