2
0

BsDockManager.cpp 35 KB

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