BsDockManager.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsDockManager.h"
  4. #include "BsEditorWidgetContainer.h"
  5. #include "BsEditorWidget.h"
  6. #include "BsEditorWidgetManager.h"
  7. #include "BsMath.h"
  8. #include "BsException.h"
  9. #include "BsMesh.h"
  10. #include "BsMaterial.h"
  11. #include "BsVector2.h"
  12. #include "BsCoreApplication.h"
  13. #include "BsRendererManager.h"
  14. #include "BsCoreRenderer.h"
  15. #include "BsSceneObject.h"
  16. #include "BsGUIManager.h"
  17. #include "BsBuiltinEditorResources.h"
  18. #include "BsCGUIWidget.h"
  19. #include "BsCCamera.h"
  20. #include "BsDragAndDropManager.h"
  21. #include "BsGUIDockSlider.h"
  22. #include "BsVertexDataDesc.h"
  23. #include "BsDockManagerLayout.h"
  24. #include "BsEditorWindow.h"
  25. #include "BsGUIPanel.h"
  26. #include "BsCoreThread.h"
  27. #include "BsRendererUtility.h"
  28. using namespace std::placeholders;
  29. namespace BansheeEngine
  30. {
  31. const UINT32 DockManager::DockContainer::SLIDER_SIZE = 3;
  32. const UINT32 DockManager::DockContainer::MIN_CHILD_SIZE = 20;
  33. DockManager::DockContainer::DockContainer(DockManager* manager)
  34. :mIsLeaf(true), mWidgets(nullptr), mSplitPosition(0.5f),
  35. mIsHorizontal(false), mParent(nullptr), mSlider(nullptr), mManager(manager)
  36. {
  37. mChildren[0] = nullptr;
  38. mChildren[1] = nullptr;
  39. }
  40. DockManager::DockContainer::DockContainer(DockManager* manager, DockContainer* parent)
  41. :mIsLeaf(false), mWidgets(nullptr), mSplitPosition(0.5f),
  42. mIsHorizontal(false), mParent(parent), mSlider(nullptr), mManager(manager)
  43. {
  44. mChildren[0] = nullptr;
  45. mChildren[1] = nullptr;
  46. }
  47. DockManager::DockContainer::~DockContainer()
  48. {
  49. if (mIsLeaf)
  50. {
  51. if (mWidgets != nullptr)
  52. bs_delete(mWidgets);
  53. if (mGUIWidgetSO != nullptr)
  54. mGUIWidgetSO->destroy();
  55. }
  56. if(!mIsLeaf)
  57. {
  58. if(mChildren[0] != nullptr)
  59. bs_delete(mChildren[0]);
  60. if(mChildren[1] != nullptr)
  61. bs_delete(mChildren[1]);
  62. }
  63. if(mSlider != nullptr)
  64. {
  65. GUIElement::destroy(mSlider);
  66. mSlider = nullptr;
  67. }
  68. }
  69. void DockManager::DockContainer::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  70. {
  71. if(mIsLeaf)
  72. {
  73. if(mWidgets != nullptr)
  74. {
  75. mWidgets->setPosition(x, y);
  76. mWidgets->setSize(width, height);
  77. }
  78. }
  79. mArea.x = x;
  80. mArea.y = y;
  81. mArea.width = width;
  82. mArea.height = height;
  83. updateChildAreas();
  84. }
  85. void DockManager::DockContainer::updateChildAreas()
  86. {
  87. if(!mIsLeaf && mChildren[0] != nullptr && mChildren[1] != nullptr)
  88. {
  89. if(mIsHorizontal)
  90. {
  91. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  92. UINT32 sizeTop = Math::floorToInt(remainingSize * mSplitPosition);
  93. UINT32 sizeBottom = remainingSize - sizeTop;
  94. mChildren[0]->setArea(mArea.x, mArea.y, mArea.width, sizeTop);
  95. mChildren[1]->setArea(mArea.x, mArea.y + sizeTop + SLIDER_SIZE, mArea.width, sizeBottom);
  96. mSlider->setWidth(mArea.width);
  97. mSlider->setHeight(SLIDER_SIZE);
  98. mSlider->setPosition(mArea.x, mArea.y + sizeTop);
  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->setWidth(SLIDER_SIZE);
  108. mSlider->setHeight(mArea.height);
  109. mSlider->setPosition(mArea.x + sizeLeft, mArea.y);
  110. }
  111. }
  112. }
  113. void DockManager::DockContainer::makeLeaf(EditorWindowBase* parentWindow)
  114. {
  115. mGUIWidgetSO = SceneObject::create("DockContainer", SOF_Internal | SOF_Persistent | SOF_DontSave);
  116. HGUIWidget guiWidget = mGUIWidgetSO->addComponent<CGUIWidget>(parentWindow->getGUICamera());
  117. guiWidget->setDepth(128);
  118. guiWidget->setSkin(BuiltinEditorResources::instance().getSkin());
  119. mIsLeaf = true;
  120. mWidgets = bs_new<EditorWidgetContainer>(guiWidget->_getInternal(), parentWindow);
  121. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  122. mWidgets->onMaximized.connect(std::bind(&DockManager::DockContainer::maximizeClicked, this));
  123. if(mSlider != nullptr)
  124. {
  125. GUIElement::destroy(mSlider);
  126. mSlider = nullptr;
  127. }
  128. mWidgets->setPosition(mArea.x, mArea.y);
  129. mWidgets->setSize(mArea.width, mArea.height);
  130. }
  131. void DockManager::DockContainer::makeLeaf(const HSceneObject& guiWidgetSO, EditorWidgetContainer* existingContainer)
  132. {
  133. mIsLeaf = true;
  134. mWidgets = existingContainer;
  135. mGUIWidgetSO = guiWidgetSO;
  136. mWidgets->onWidgetClosed.connect(std::bind(&DockManager::DockContainer::widgetRemoved, this));
  137. mWidgets->onMaximized.connect(std::bind(&DockManager::DockContainer::maximizeClicked, this));
  138. if(mSlider != nullptr)
  139. {
  140. GUIElement::destroy(mSlider);
  141. mSlider = nullptr;
  142. }
  143. mWidgets->setPosition(mArea.x, mArea.y);
  144. mWidgets->setSize(mArea.width, mArea.height);
  145. }
  146. void DockManager::DockContainer::addLeft(EditorWidgetBase* widget)
  147. {
  148. if(mIsLeaf)
  149. splitContainer(false, true);
  150. mChildren[0]->addWidget(widget);
  151. }
  152. void DockManager::DockContainer::addRight(EditorWidgetBase* widget)
  153. {
  154. if(mIsLeaf)
  155. splitContainer(false, false);
  156. mChildren[1]->addWidget(widget);
  157. }
  158. void DockManager::DockContainer::addTop(EditorWidgetBase* widget)
  159. {
  160. if(mIsLeaf)
  161. splitContainer(true, true);
  162. mChildren[0]->addWidget(widget);
  163. }
  164. void DockManager::DockContainer::addBottom(EditorWidgetBase* widget)
  165. {
  166. if(mIsLeaf)
  167. splitContainer(true, false);
  168. mChildren[1]->addWidget(widget);
  169. }
  170. void DockManager::DockContainer::splitContainer(bool horizontal, bool newChildIsFirst, float splitPosition)
  171. {
  172. DockContainer* children[2];
  173. UINT32 idxA = newChildIsFirst ? 0 : 1;
  174. UINT32 idxB = (idxA + 1) % 2;
  175. children[idxA] = bs_new<DockContainer>(mManager, this);
  176. children[idxB] = bs_new<DockContainer>(mManager, this);
  177. mWidgets->onWidgetClosed.clear();
  178. mWidgets->onMaximized.clear();
  179. children[idxA]->makeLeaf(mManager->mParentWindow);
  180. children[idxB]->makeLeaf(mGUIWidgetSO, mWidgets);
  181. mWidgets = nullptr;
  182. mGUIWidgetSO = nullptr;
  183. makeSplit(children[0], children[1], horizontal, splitPosition);
  184. }
  185. void DockManager::DockContainer::makeSplit(DockManager::DockContainer* first, DockManager::DockContainer* second, bool horizontal, float splitPosition)
  186. {
  187. mChildren[0] = first;
  188. mChildren[1] = second;
  189. mIsLeaf = false;
  190. mIsHorizontal = horizontal;
  191. mSplitPosition = splitPosition;
  192. if (mWidgets != nullptr)
  193. {
  194. bs_delete(mWidgets);
  195. mWidgets = nullptr;
  196. }
  197. if (mGUIWidgetSO != nullptr)
  198. {
  199. mGUIWidgetSO->destroy(true);
  200. mGUIWidgetSO = nullptr;
  201. }
  202. if (mSlider != nullptr)
  203. {
  204. GUIElement::destroy(mSlider);
  205. mSlider = nullptr;
  206. }
  207. mSlider = GUIDockSlider::create(horizontal, "DockSliderBtn");
  208. mManager->_getParentWidget()->getPanel()->addElement(mSlider);
  209. mSlider->onDragged.connect(std::bind(&DockManager::DockContainer::sliderDragged, this, _1));
  210. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  211. }
  212. void DockManager::DockContainer::addWidget(EditorWidgetBase* widget)
  213. {
  214. if(!mIsLeaf)
  215. return;
  216. mWidgets->add(*widget);
  217. }
  218. void DockManager::DockContainer::addWidget(const String& name)
  219. {
  220. if(!mIsLeaf)
  221. return;
  222. EditorWidgetManager::instance().create(name, *mWidgets);
  223. }
  224. void DockManager::DockContainer::sliderDragged(const Vector2I& delta)
  225. {
  226. if(mIsHorizontal && delta.y != 0)
  227. {
  228. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.height - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  229. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.height - (INT32)SLIDER_SIZE);
  230. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.y, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  231. updateChildAreas();
  232. }
  233. else if(!mIsHorizontal && delta.x != 0)
  234. {
  235. UINT32 maxSize = (UINT32)std::max(MIN_CHILD_SIZE, (INT32)mArea.width - (INT32)SLIDER_SIZE - MIN_CHILD_SIZE);
  236. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mArea.width - (INT32)SLIDER_SIZE);
  237. mSplitPosition = Math::clamp((UINT32)Math::floorToInt(remainingSize * mSplitPosition) + delta.x, MIN_CHILD_SIZE, maxSize) / (float)remainingSize;
  238. updateChildAreas();
  239. }
  240. }
  241. void DockManager::DockContainer::widgetRemoved()
  242. {
  243. assert(mIsLeaf);
  244. if(mWidgets->getNumWidgets() == 0)
  245. {
  246. if(mParent == nullptr) // We're root so we just reset ourselves, can't delete root
  247. {
  248. if (mManager->mIsMaximized)
  249. {
  250. mManager->mMaximizedContainer = this;
  251. mManager->mIsMaximized = false;
  252. }
  253. bs_delete(mWidgets);
  254. mWidgets = nullptr;
  255. mGUIWidgetSO->destroy(true);
  256. mGUIWidgetSO = nullptr;
  257. mIsLeaf = true;
  258. mSplitPosition = 0.5f;
  259. mIsHorizontal = false;
  260. }
  261. else
  262. {
  263. // Replace our parent with our sibling
  264. DockContainer* sibling = nullptr;
  265. if(mParent->mChildren[0] == this)
  266. sibling = mParent->mChildren[1];
  267. else
  268. sibling = mParent->mChildren[0];
  269. if (sibling->mIsLeaf)
  270. {
  271. sibling->mWidgets->onWidgetClosed.clear();
  272. sibling->mWidgets->onMaximized.clear();
  273. mParent->makeLeaf(sibling->mGUIWidgetSO, sibling->mWidgets);
  274. sibling->mWidgets = nullptr;
  275. sibling->mGUIWidgetSO = nullptr;
  276. }
  277. else
  278. {
  279. mParent->makeSplit(sibling->mChildren[0], sibling->mChildren[1], sibling->mIsHorizontal, sibling->mSplitPosition);
  280. sibling->mChildren[0]->mParent = mParent;
  281. sibling->mChildren[1]->mParent = mParent;
  282. sibling->mChildren[0] = nullptr;
  283. sibling->mChildren[1] = nullptr;
  284. }
  285. bs_delete(sibling);
  286. bs_delete(this);
  287. }
  288. }
  289. }
  290. void DockManager::DockContainer::maximizeClicked()
  291. {
  292. mManager->toggleMaximize(this);
  293. }
  294. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  295. {
  296. if(mIsLeaf)
  297. {
  298. if(mWidgets == widgetContainer)
  299. return this;
  300. else
  301. return nullptr;
  302. }
  303. else
  304. {
  305. if(mChildren[0] != nullptr)
  306. {
  307. DockContainer* foundContainer = mChildren[0]->find(widgetContainer);
  308. if (foundContainer != nullptr)
  309. return foundContainer;
  310. }
  311. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  312. {
  313. DockContainer* foundContainer = mChildren[1]->find(widgetContainer);
  314. if (foundContainer != nullptr)
  315. return foundContainer;
  316. }
  317. }
  318. return nullptr;
  319. }
  320. DockManager::DockContainer* DockManager::DockContainer::findAtPos(const Vector2I& pos)
  321. {
  322. if(mIsLeaf)
  323. {
  324. if(mArea.contains(pos))
  325. {
  326. return this;
  327. }
  328. }
  329. else
  330. {
  331. if (mChildren[0] != nullptr)
  332. {
  333. DockContainer* foundContainer = mChildren[0]->findAtPos(pos);
  334. if (foundContainer != nullptr)
  335. return foundContainer;
  336. }
  337. if(mChildren[1] != nullptr)
  338. {
  339. DockContainer* foundContainer = mChildren[1]->findAtPos(pos);
  340. if (foundContainer != nullptr)
  341. return foundContainer;
  342. }
  343. }
  344. return nullptr;
  345. }
  346. Rect2I DockManager::DockContainer::getContentBounds() const
  347. {
  348. if(!mIsLeaf || mWidgets == nullptr)
  349. return mArea;
  350. return mWidgets->getContentBounds();
  351. }
  352. void DockManager::DockContainer::update()
  353. {
  354. if (mIsLeaf)
  355. {
  356. if (mWidgets != nullptr)
  357. mWidgets->update();
  358. }
  359. else
  360. {
  361. if (mChildren[0] != nullptr)
  362. mChildren[0]->update();
  363. if (mChildren[1] != nullptr)
  364. mChildren[1]->update();
  365. }
  366. }
  367. DockManager::DockManager(EditorWindowBase* parentWindow, const GUIDimensions& dimensions)
  368. :GUIElementContainer(dimensions), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None),
  369. mShowOverlay(false), mRootContainer(this), mParentWindow(parentWindow), mIsMaximized(false), mMaximizedContainer(nullptr)
  370. {
  371. mTopDropPolygon = bs_newN<Vector2>(4);
  372. mBotDropPolygon = bs_newN<Vector2>(4);
  373. mLeftDropPolygon = bs_newN<Vector2>(4);
  374. mRightDropPolygon = bs_newN<Vector2>(4);
  375. HMaterial dropOverlayMat = BuiltinEditorResources::instance().createDockDropOverlayMaterial();
  376. mCore.store(bs_new<DockOverlayRenderer>(), std::memory_order_release);
  377. gCoreAccessor().queueCommand(std::bind(&DockManager::initializeOverlayRenderer,
  378. this, dropOverlayMat->getCore()));
  379. }
  380. DockManager::~DockManager()
  381. {
  382. bs_deleteN(mTopDropPolygon, 4);
  383. bs_deleteN(mBotDropPolygon, 4);
  384. bs_deleteN(mLeftDropPolygon, 4);
  385. bs_deleteN(mRightDropPolygon, 4);
  386. gCoreAccessor().queueCommand(std::bind(&DockManager::destroyOverlayRenderer,
  387. this, mCore.load(std::memory_order_relaxed)));
  388. }
  389. void DockManager::initializeOverlayRenderer(const SPtr<MaterialCore>& initData)
  390. {
  391. mCore.load(std::memory_order_acquire)->initialize(initData);
  392. }
  393. void DockManager::destroyOverlayRenderer(DockOverlayRenderer* core)
  394. {
  395. bs_delete(core);
  396. }
  397. DockManager* DockManager::create(EditorWindowBase* parentWindow)
  398. {
  399. return new (bs_alloc<DockManager>()) DockManager(parentWindow, GUIDimensions::create());
  400. }
  401. void DockManager::update()
  402. {
  403. if(!DragAndDropManager::instance().isDragInProgress())
  404. {
  405. mHighlightedDropLoc = DockLocation::None;
  406. mShowOverlay = false;
  407. }
  408. mRootContainer.update();
  409. HCamera camera = mParentWindow->getGUICamera();
  410. DockOverlayRenderer* core = mCore.load(std::memory_order_relaxed);
  411. gCoreAccessor().queueCommand(std::bind(&DockOverlayRenderer::updateData, core, camera->_getCamera()->getCore(),
  412. mDropOverlayMesh->getCore(), mShowOverlay, mHighlightedDropLoc));
  413. }
  414. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  415. {
  416. if(relativeTo != nullptr)
  417. {
  418. DockContainer* container = mRootContainer.find(relativeTo);
  419. if(container == nullptr)
  420. BS_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  421. switch(location)
  422. {
  423. case DockLocation::Left:
  424. container->addLeft(widgetToInsert);
  425. break;
  426. case DockLocation::Right:
  427. container->addRight(widgetToInsert);
  428. break;
  429. case DockLocation::Top:
  430. container->addTop(widgetToInsert);
  431. break;
  432. case DockLocation::Bottom:
  433. container->addBottom(widgetToInsert);
  434. break;
  435. }
  436. }
  437. else
  438. {
  439. if(mRootContainer.mWidgets != nullptr)
  440. BS_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  441. mRootContainer.makeLeaf(mParentWindow);
  442. mRootContainer.addWidget(widgetToInsert);
  443. }
  444. }
  445. void DockManager::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  446. {
  447. mRootContainer.setArea(x, y, width, height);
  448. mArea = Rect2I(x, y, width, height);
  449. updateDropOverlay(x, y, width, height);
  450. }
  451. void DockManager::closeAll()
  452. {
  453. mRootContainer = DockContainer(this);
  454. mMouseOverContainer = nullptr;
  455. }
  456. DockManagerLayoutPtr DockManager::getLayout() const
  457. {
  458. struct StackElem
  459. {
  460. StackElem(DockManagerLayout::Entry* layoutEntry, const DockContainer* container)
  461. :layoutEntry(layoutEntry), container(container)
  462. { }
  463. DockManagerLayout::Entry* layoutEntry;
  464. const DockContainer* container;
  465. };
  466. auto GetWidgetNamesInContainer = [&] (const DockContainer* container)
  467. {
  468. Vector<String> widgetNames;
  469. if(container->mWidgets != nullptr)
  470. {
  471. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  472. for(UINT32 i = 0; i < numWidgets; i++)
  473. {
  474. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  475. widgetNames.push_back(widget->getName());
  476. }
  477. }
  478. return widgetNames;
  479. };
  480. if (mIsMaximized)
  481. {
  482. DockManagerLayoutPtr layout;
  483. if (mRestoredLayout != nullptr)
  484. layout = mRestoredLayout->clone();
  485. else
  486. layout = bs_shared_ptr_new<DockManagerLayout>();
  487. layout->setIsMaximized(true, GetWidgetNamesInContainer(mMaximizedContainer));
  488. return layout;
  489. }
  490. else
  491. {
  492. DockManagerLayoutPtr layout = bs_shared_ptr_new<DockManagerLayout>();
  493. DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  494. if (mRootContainer.mIsLeaf)
  495. {
  496. rootEntry->isLeaf = true;
  497. rootEntry->widgetNames = GetWidgetNamesInContainer(&mRootContainer);
  498. }
  499. else
  500. {
  501. rootEntry->isLeaf = false;
  502. rootEntry->horizontalSplit = mRootContainer.mIsHorizontal;
  503. rootEntry->splitPosition = mRootContainer.mSplitPosition;
  504. rootEntry->parent = nullptr;
  505. }
  506. Stack<StackElem> todo;
  507. todo.push(StackElem(rootEntry, &mRootContainer));
  508. while (!todo.empty())
  509. {
  510. StackElem currentElem = todo.top();
  511. todo.pop();
  512. if (!currentElem.container->mIsLeaf)
  513. {
  514. for (UINT32 i = 0; i < 2; i++)
  515. {
  516. if (currentElem.container->mChildren[i] == nullptr)
  517. continue;
  518. if (currentElem.container->mChildren[i]->mIsLeaf)
  519. {
  520. Vector<String> widgetNames = GetWidgetNamesInContainer(currentElem.container->mChildren[i]);
  521. currentElem.layoutEntry->children[i] =
  522. DockManagerLayout::Entry::createLeaf(currentElem.layoutEntry, i, widgetNames);
  523. }
  524. else
  525. {
  526. currentElem.layoutEntry->children[i] =
  527. DockManagerLayout::Entry::createContainer(currentElem.layoutEntry, i,
  528. currentElem.container->mChildren[i]->mSplitPosition,
  529. currentElem.container->mChildren[i]->mIsHorizontal);
  530. todo.push(StackElem(currentElem.layoutEntry->children[i], currentElem.container->mChildren[i]));
  531. }
  532. }
  533. }
  534. }
  535. return layout;
  536. }
  537. }
  538. void DockManager::setLayout(const DockManagerLayoutPtr& layout)
  539. {
  540. // Undock all currently docked widgets
  541. Vector<EditorWidgetBase*> undockedWidgets;
  542. std::function<void(DockContainer*)> undockWidgets = [&](DockContainer* container)
  543. {
  544. while (!container->mIsLeaf)
  545. {
  546. // Due to the way undocking works a container can be transfromed from non-leaf to leaf
  547. // if its child container is deleted, so we need to check to that specially
  548. undockWidgets(container->mChildren[0]);
  549. }
  550. if (container->mIsLeaf)
  551. {
  552. if (container->mWidgets != nullptr)
  553. {
  554. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  555. for (UINT32 i = 0; i < numWidgets; i++)
  556. {
  557. EditorWidgetBase* curWidget = container->mWidgets->getWidget(0);
  558. EditorWidgetManager::instance().close(curWidget);
  559. undockedWidgets.push_back(curWidget);
  560. }
  561. }
  562. }
  563. };
  564. undockWidgets(&mRootContainer);
  565. mRootContainer = DockContainer(this);
  566. // Load layout
  567. struct StackEntry
  568. {
  569. StackEntry(const DockManagerLayout::Entry* layoutEntry, DockContainer* container)
  570. :layoutEntry(layoutEntry), container(container)
  571. { }
  572. const DockManagerLayout::Entry* layoutEntry;
  573. DockContainer* container;
  574. };
  575. auto GetLeafEntry = [] (const DockManagerLayout::Entry* parentEntry, UINT32 childIdx) -> const DockManagerLayout::Entry*
  576. {
  577. while(true)
  578. {
  579. if(parentEntry->isLeaf)
  580. return parentEntry;
  581. parentEntry = parentEntry->children[childIdx];
  582. }
  583. return nullptr;
  584. };
  585. auto OpenWidgets = [&] (DockContainer* parent, const Vector<String>& widgetNames)
  586. {
  587. for(auto& widgetName : widgetNames)
  588. {
  589. parent->addWidget(widgetName);
  590. }
  591. };
  592. // Prune layout by removing invalid leafs (ones with no widgets, or widgets that no longer exist)
  593. layout->pruneInvalidLeaves();
  594. if (layout->isMaximized())
  595. {
  596. mRestoredLayout = layout->clone();
  597. mRestoredLayout->setIsMaximized(false, Vector<String>());
  598. const Vector<String>& maximizedWidgets = mRestoredLayout->getMaximizedWidgetNames();
  599. if (maximizedWidgets.size() > 0) // If zero, entire layout is empty
  600. {
  601. mRootContainer.makeLeaf(mParentWindow);
  602. OpenWidgets(&mRootContainer, maximizedWidgets);
  603. }
  604. }
  605. else
  606. {
  607. // Dock elements
  608. const DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  609. const DockManagerLayout::Entry* leafEntry = GetLeafEntry(rootEntry, 0);
  610. if (leafEntry->widgetNames.size() > 0) // If zero, entire layout is empty
  611. {
  612. mRootContainer.makeLeaf(mParentWindow);
  613. OpenWidgets(&mRootContainer, leafEntry->widgetNames);
  614. if (!rootEntry->isLeaf)
  615. {
  616. Stack<StackEntry> layoutTodo;
  617. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  618. while (!layoutTodo.empty())
  619. {
  620. StackEntry curEntry = layoutTodo.top();
  621. layoutTodo.pop();
  622. leafEntry = GetLeafEntry(curEntry.layoutEntry->children[1], 0);
  623. curEntry.container->splitContainer(curEntry.layoutEntry->horizontalSplit, false, curEntry.layoutEntry->splitPosition);
  624. DockContainer* otherChild = curEntry.container->mChildren[1];
  625. OpenWidgets(otherChild, leafEntry->widgetNames);
  626. if (!curEntry.layoutEntry->children[0]->isLeaf)
  627. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  628. if (!curEntry.layoutEntry->children[1]->isLeaf)
  629. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  630. }
  631. }
  632. }
  633. // Set container sizes
  634. {
  635. Stack<StackEntry> layoutTodo;
  636. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  637. while (!layoutTodo.empty())
  638. {
  639. StackEntry curEntry = layoutTodo.top();
  640. layoutTodo.pop();
  641. if (!curEntry.layoutEntry->isLeaf)
  642. {
  643. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  644. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  645. }
  646. }
  647. }
  648. }
  649. // Destroy any widgets that are no longer docked anywhere
  650. for(auto& widget : undockedWidgets)
  651. {
  652. if(widget->_getParent() == nullptr)
  653. widget->close();
  654. }
  655. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  656. }
  657. void DockManager::toggleMaximize(DockContainer* container)
  658. {
  659. if (mIsMaximized)
  660. {
  661. if (mRestoredLayout != nullptr)
  662. setLayout(mRestoredLayout);
  663. mRestoredLayout = nullptr;
  664. mMaximizedContainer = nullptr;
  665. mIsMaximized = false;
  666. }
  667. else
  668. {
  669. mRestoredLayout = getLayout();
  670. mMaximizedContainer = container;
  671. Vector<String> maximizedWidgetNames;
  672. if (container->mWidgets != nullptr)
  673. {
  674. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  675. for (UINT32 i = 0; i < numWidgets; i++)
  676. {
  677. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  678. maximizedWidgetNames.push_back(widget->getName());
  679. }
  680. }
  681. DockManagerLayoutPtr maxLayout = bs_shared_ptr_new<DockManagerLayout>();
  682. DockManagerLayout::Entry& rootEntry = maxLayout->getRootEntry();
  683. rootEntry.isLeaf = true;
  684. rootEntry.widgetNames = maximizedWidgetNames;
  685. setLayout(maxLayout);
  686. mIsMaximized = true;
  687. }
  688. }
  689. void DockManager::updateClippedBounds()
  690. {
  691. // TODO - Clipping not actually accounted for but shouldn't matter as right now DockManager is only used in one specific situation
  692. mClippedBounds = mRootContainer.mArea;
  693. }
  694. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  695. {
  696. const static int spacing = 10;
  697. const static float innerScale = 0.15f;
  698. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  699. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  700. UINT32 innerOffset = outWidth > outHeight ? Math::floorToInt(innerScale * outHeight) : Math::floorToInt(innerScale * outWidth);
  701. UINT32 inWidth = outWidth - innerOffset;
  702. UINT32 inHeight = outHeight - innerOffset;
  703. INT32 inXOffset = Math::floorToInt((outWidth - inWidth) * 0.5f);
  704. INT32 inYOffset = Math::floorToInt((outHeight - inHeight) * 0.5f);
  705. Vector2 outTopLeft((float)x, (float)y);
  706. Vector2 outTopRight((float)(x + outWidth), (float)y);
  707. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  708. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  709. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  710. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  711. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  712. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  713. VertexDataDescPtr vertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  714. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  715. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  716. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(16, 24, vertexDesc);
  717. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  718. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  719. // Top
  720. Vector2 topOffset((float)spacing, 0.0f);
  721. mTopDropPolygon[0] = outTopLeft + topOffset;
  722. mTopDropPolygon[1] = outTopRight + topOffset;
  723. mTopDropPolygon[2] = inTopRight + topOffset;
  724. mTopDropPolygon[3] = inTopLeft + topOffset;
  725. vertIter.addValue(mTopDropPolygon[0]);
  726. vertIter.addValue(mTopDropPolygon[1]);
  727. vertIter.addValue(mTopDropPolygon[2]);
  728. vertIter.addValue(mTopDropPolygon[3]);
  729. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  730. UINT32 color32 = color.getAsRGBA();
  731. colIter.addValue(color32);
  732. colIter.addValue(color32);
  733. colIter.addValue(color32);
  734. colIter.addValue(color32);
  735. // Bottom
  736. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  737. mBotDropPolygon[0] = inBotLeft + botOffset;
  738. mBotDropPolygon[1] = inBotRight + botOffset;
  739. mBotDropPolygon[2] = outBotRight + botOffset;
  740. mBotDropPolygon[3] = outBotLeft + botOffset;
  741. vertIter.addValue(mBotDropPolygon[0]);
  742. vertIter.addValue(mBotDropPolygon[1]);
  743. vertIter.addValue(mBotDropPolygon[2]);
  744. vertIter.addValue(mBotDropPolygon[3]);
  745. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  746. color32 = color.getAsRGBA();
  747. colIter.addValue(color32);
  748. colIter.addValue(color32);
  749. colIter.addValue(color32);
  750. colIter.addValue(color32);
  751. // Left
  752. Vector2 leftOffset(0.0f, (float)spacing);
  753. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  754. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  755. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  756. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  757. vertIter.addValue(mLeftDropPolygon[0]);
  758. vertIter.addValue(mLeftDropPolygon[1]);
  759. vertIter.addValue(mLeftDropPolygon[2]);
  760. vertIter.addValue(mLeftDropPolygon[3]);
  761. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  762. color32 = color.getAsRGBA();
  763. colIter.addValue(color32);
  764. colIter.addValue(color32);
  765. colIter.addValue(color32);
  766. colIter.addValue(color32);
  767. // Right
  768. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  769. mRightDropPolygon[0] = inTopRight + rightOffset;
  770. mRightDropPolygon[1] = outTopRight + rightOffset;
  771. mRightDropPolygon[2] = outBotRight + rightOffset;
  772. mRightDropPolygon[3] = inBotRight + rightOffset;
  773. vertIter.addValue(mRightDropPolygon[0]);
  774. vertIter.addValue(mRightDropPolygon[1]);
  775. vertIter.addValue(mRightDropPolygon[2]);
  776. vertIter.addValue(mRightDropPolygon[3]);
  777. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  778. color32 = color.getAsRGBA();
  779. colIter.addValue(color32);
  780. colIter.addValue(color32);
  781. colIter.addValue(color32);
  782. colIter.addValue(color32);
  783. UINT32* indexData = meshData->getIndices32();
  784. // Top
  785. indexData[0] = 0;
  786. indexData[1] = 1;
  787. indexData[2] = 2;
  788. indexData[3] = 0;
  789. indexData[4] = 2;
  790. indexData[5] = 3;
  791. // Bottom
  792. indexData[6] = 4;
  793. indexData[7] = 5;
  794. indexData[8] = 6;
  795. indexData[9] = 4;
  796. indexData[10] = 6;
  797. indexData[11] = 7;
  798. // Left
  799. indexData[12] = 8;
  800. indexData[13] = 9;
  801. indexData[14] = 10;
  802. indexData[15] = 8;
  803. indexData[16] = 10;
  804. indexData[17] = 11;
  805. // Right
  806. indexData[18] = 12;
  807. indexData[19] = 13;
  808. indexData[20] = 14;
  809. indexData[21] = 12;
  810. indexData[22] = 14;
  811. indexData[23] = 15;
  812. mDropOverlayMesh = Mesh::create(meshData);
  813. }
  814. bool DockManager::_mouseEvent(const GUIMouseEvent& event)
  815. {
  816. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  817. {
  818. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  819. return false;
  820. const Vector2I& widgetRelPos = event.getPosition();
  821. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  822. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  823. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  824. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  825. mMouseOverContainer = mRootContainer.findAtPos(windowPos);
  826. if(mMouseOverContainer == nullptr)
  827. mMouseOverContainer = &mRootContainer;
  828. Rect2I overlayBounds;
  829. if(mMouseOverContainer != nullptr)
  830. overlayBounds = mMouseOverContainer->getContentBounds();
  831. // Update mesh if needed
  832. if(mLastOverlayBounds != overlayBounds)
  833. {
  834. if(overlayBounds.width <= 0 || overlayBounds.height <= 0)
  835. mDropOverlayMesh = HMesh();
  836. else
  837. updateDropOverlay(overlayBounds.x, overlayBounds.y, overlayBounds.width, overlayBounds.height);
  838. mLastOverlayBounds = overlayBounds;
  839. }
  840. // Check if we need to highlight any drop locations
  841. if(mMouseOverContainer != nullptr)
  842. {
  843. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  844. mHighlightedDropLoc = DockLocation::Top;
  845. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  846. mHighlightedDropLoc = DockLocation::Bottom;
  847. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  848. mHighlightedDropLoc = DockLocation::Left;
  849. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  850. mHighlightedDropLoc = DockLocation::Right;
  851. else
  852. mHighlightedDropLoc = DockLocation::None;
  853. if(overlayBounds.contains(windowPos))
  854. mShowOverlay = true;
  855. else
  856. mShowOverlay = false;
  857. }
  858. else
  859. mShowOverlay = false;
  860. return false;
  861. }
  862. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  863. {
  864. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  865. return false;
  866. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  867. const Vector2I& widgetRelPos = event.getPosition();
  868. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  869. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  870. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  871. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  872. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  873. if(mouseOverContainer == nullptr)
  874. {
  875. Rect2I overlayBounds = mRootContainer.getContentBounds();
  876. if(overlayBounds.contains(windowPos))
  877. {
  878. insert(nullptr, draggedWidget, DockLocation::None);
  879. return true;
  880. }
  881. }
  882. else
  883. {
  884. if (insidePolygon(mTopDropPolygon, 4, windowPosVec))
  885. {
  886. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  887. return true;
  888. }
  889. else if (insidePolygon(mBotDropPolygon, 4, windowPosVec))
  890. {
  891. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  892. return true;
  893. }
  894. else if (insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  895. {
  896. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  897. return true;
  898. }
  899. else if (insidePolygon(mRightDropPolygon, 4, windowPosVec))
  900. {
  901. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  902. return true;
  903. }
  904. }
  905. }
  906. return false;
  907. }
  908. // TODO - Move to a separate Polygon class?
  909. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  910. {
  911. bool isInside = false;
  912. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  913. {
  914. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  915. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  916. isInside = !isInside;
  917. }
  918. return isInside;
  919. }
  920. const Color DockOverlayRenderer::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  921. const Color DockOverlayRenderer::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  922. DockOverlayRenderer::DockOverlayRenderer()
  923. :mShowOverlay(false), mHighlightedDropLoc(DockManager::DockLocation::None)
  924. {
  925. }
  926. DockOverlayRenderer::~DockOverlayRenderer()
  927. {
  928. if (mCamera != nullptr)
  929. {
  930. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  931. activeRenderer->_unregisterRenderCallback(mCamera.get(), 40);
  932. }
  933. }
  934. void DockOverlayRenderer::initialize(const SPtr<MaterialCore>& material)
  935. {
  936. mMaterial = material;
  937. }
  938. void DockOverlayRenderer::updateData(const SPtr<CameraCore>& camera, const SPtr<MeshCore>& mesh, bool active,
  939. DockManager::DockLocation location)
  940. {
  941. if (mCamera != camera)
  942. {
  943. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  944. if (mCamera != nullptr)
  945. activeRenderer->_unregisterRenderCallback(mCamera.get(), 40);
  946. if (camera != nullptr)
  947. activeRenderer->_registerRenderCallback(camera.get(), 40, std::bind(&DockOverlayRenderer::render, this), true);
  948. }
  949. mCamera = camera;
  950. mMesh = mesh;
  951. mShowOverlay = active;
  952. mHighlightedDropLoc = location;
  953. }
  954. void DockOverlayRenderer::render()
  955. {
  956. THROW_IF_NOT_CORE_THREAD;
  957. if (!mShowOverlay)
  958. return;
  959. SPtr<ViewportCore> viewport = mCamera->getViewport();
  960. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  961. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  962. mMaterial->setFloat("invViewportWidth", invViewportWidth);
  963. mMaterial->setFloat("invViewportHeight", invViewportHeight);
  964. mMaterial->setColor("tintColor", TINT_COLOR);
  965. mMaterial->setColor("highlightColor", HIGHLIGHT_COLOR);
  966. Color highlightColor;
  967. switch (mHighlightedDropLoc)
  968. {
  969. case DockManager::DockLocation::Top:
  970. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  971. break;
  972. case DockManager::DockLocation::Bottom:
  973. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  974. break;
  975. case DockManager::DockLocation::Left:
  976. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  977. break;
  978. case DockManager::DockLocation::Right:
  979. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  980. break;
  981. case DockManager::DockLocation::None:
  982. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  983. break;
  984. }
  985. mMaterial->setColor("highlightActive", highlightColor);
  986. gRendererUtility().setPass(mMaterial, 0);
  987. gRendererUtility().draw(mMesh, mMesh->getProperties().getSubMesh(0));
  988. }
  989. }