BsDockManager.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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), mParent(nullptr), mManager(manager), mWidgets(nullptr), mSlider(nullptr), mSplitPosition(0.5f)
  35. , mIsHorizontal(false)
  36. {
  37. mChildren[0] = nullptr;
  38. mChildren[1] = nullptr;
  39. }
  40. DockManager::DockContainer::DockContainer(DockManager* manager, DockContainer* parent)
  41. : mIsLeaf(false), mParent(parent), mManager(manager), mWidgets(nullptr), mSlider(nullptr), mSplitPosition(0.5f)
  42. , mIsHorizontal(false)
  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), mParentWindow(parentWindow), mRootContainer(this), mIsMaximized(false)
  369. , mMaximizedContainer(nullptr), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None)
  370. , mShowOverlay(false)
  371. {
  372. mTopDropPolygon = bs_newN<Vector2>(4);
  373. mBotDropPolygon = bs_newN<Vector2>(4);
  374. mLeftDropPolygon = bs_newN<Vector2>(4);
  375. mRightDropPolygon = bs_newN<Vector2>(4);
  376. HMaterial dropOverlayMat = BuiltinEditorResources::instance().createDockDropOverlayMaterial();
  377. mCore.store(bs_new<DockOverlayRenderer>(), std::memory_order_release);
  378. gCoreAccessor().queueCommand(std::bind(&DockManager::initializeOverlayRenderer,
  379. this, dropOverlayMat->getCore()));
  380. }
  381. DockManager::~DockManager()
  382. {
  383. bs_deleteN(mTopDropPolygon, 4);
  384. bs_deleteN(mBotDropPolygon, 4);
  385. bs_deleteN(mLeftDropPolygon, 4);
  386. bs_deleteN(mRightDropPolygon, 4);
  387. gCoreAccessor().queueCommand(std::bind(&DockManager::destroyOverlayRenderer,
  388. this, mCore.load(std::memory_order_relaxed)));
  389. }
  390. void DockManager::initializeOverlayRenderer(const SPtr<MaterialCore>& initData)
  391. {
  392. mCore.load(std::memory_order_acquire)->initialize(initData);
  393. }
  394. void DockManager::destroyOverlayRenderer(DockOverlayRenderer* core)
  395. {
  396. bs_delete(core);
  397. }
  398. DockManager* DockManager::create(EditorWindowBase* parentWindow)
  399. {
  400. return new (bs_alloc<DockManager>()) DockManager(parentWindow, GUIDimensions::create());
  401. }
  402. void DockManager::update()
  403. {
  404. if(!DragAndDropManager::instance().isDragInProgress())
  405. {
  406. mHighlightedDropLoc = DockLocation::None;
  407. mShowOverlay = false;
  408. }
  409. mRootContainer.update();
  410. HCamera camera = mParentWindow->getGUICamera();
  411. DockOverlayRenderer* core = mCore.load(std::memory_order_relaxed);
  412. gCoreAccessor().queueCommand(std::bind(&DockOverlayRenderer::updateData, core, camera->_getCamera()->getCore(),
  413. mDropOverlayMesh->getCore(), mShowOverlay, mHighlightedDropLoc));
  414. }
  415. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidgetBase* widgetToInsert, DockLocation location)
  416. {
  417. if(relativeTo != nullptr)
  418. {
  419. DockContainer* container = mRootContainer.find(relativeTo);
  420. if(container == nullptr)
  421. BS_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  422. switch(location)
  423. {
  424. case DockLocation::Left:
  425. container->addLeft(widgetToInsert);
  426. break;
  427. case DockLocation::Right:
  428. container->addRight(widgetToInsert);
  429. break;
  430. case DockLocation::Top:
  431. container->addTop(widgetToInsert);
  432. break;
  433. case DockLocation::Bottom:
  434. container->addBottom(widgetToInsert);
  435. break;
  436. case DockLocation::None:
  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. SPtr<DockManagerLayout> 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. SPtr<DockManagerLayout> 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. SPtr<DockManagerLayout> 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 SPtr<DockManagerLayout>& layout)
  542. {
  543. // Undock all currently docked widgets
  544. Vector<EditorWidgetBase*> undockedWidgets;
  545. std::function<void(DockContainer*)> undockWidgets = [&](DockContainer* container)
  546. {
  547. while (!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. }
  553. if (container->mIsLeaf)
  554. {
  555. if (container->mWidgets != nullptr)
  556. {
  557. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  558. for (UINT32 i = 0; i < numWidgets; i++)
  559. {
  560. EditorWidgetBase* curWidget = container->mWidgets->getWidget(0);
  561. EditorWidgetManager::instance().close(curWidget);
  562. undockedWidgets.push_back(curWidget);
  563. }
  564. }
  565. }
  566. };
  567. undockWidgets(&mRootContainer);
  568. mRootContainer = DockContainer(this);
  569. // Load layout
  570. struct StackEntry
  571. {
  572. StackEntry(const DockManagerLayout::Entry* layoutEntry, DockContainer* container)
  573. :layoutEntry(layoutEntry), container(container)
  574. { }
  575. const DockManagerLayout::Entry* layoutEntry;
  576. DockContainer* container;
  577. };
  578. auto GetLeafEntry = [] (const DockManagerLayout::Entry* parentEntry, UINT32 childIdx) -> const DockManagerLayout::Entry*
  579. {
  580. while(true)
  581. {
  582. if(parentEntry->isLeaf)
  583. return parentEntry;
  584. parentEntry = parentEntry->children[childIdx];
  585. }
  586. return nullptr;
  587. };
  588. auto OpenWidgets = [&] (DockContainer* parent, const Vector<String>& widgetNames)
  589. {
  590. for(auto& widgetName : widgetNames)
  591. {
  592. parent->addWidget(widgetName);
  593. }
  594. };
  595. // Prune layout by removing invalid leafs (ones with no widgets, or widgets that no longer exist)
  596. layout->pruneInvalidLeaves();
  597. if (layout->isMaximized())
  598. {
  599. mRestoredLayout = layout->clone();
  600. mRestoredLayout->setIsMaximized(false, Vector<String>());
  601. const Vector<String>& maximizedWidgets = mRestoredLayout->getMaximizedWidgetNames();
  602. if (maximizedWidgets.size() > 0) // If zero, entire layout is empty
  603. {
  604. mRootContainer.makeLeaf(mParentWindow);
  605. OpenWidgets(&mRootContainer, maximizedWidgets);
  606. }
  607. }
  608. else
  609. {
  610. // Dock elements
  611. const DockManagerLayout::Entry* rootEntry = &layout->getRootEntry();
  612. const DockManagerLayout::Entry* leafEntry = GetLeafEntry(rootEntry, 0);
  613. if (leafEntry->widgetNames.size() > 0) // If zero, entire layout is empty
  614. {
  615. mRootContainer.makeLeaf(mParentWindow);
  616. OpenWidgets(&mRootContainer, leafEntry->widgetNames);
  617. if (!rootEntry->isLeaf)
  618. {
  619. Stack<StackEntry> layoutTodo;
  620. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  621. while (!layoutTodo.empty())
  622. {
  623. StackEntry curEntry = layoutTodo.top();
  624. layoutTodo.pop();
  625. leafEntry = GetLeafEntry(curEntry.layoutEntry->children[1], 0);
  626. curEntry.container->splitContainer(curEntry.layoutEntry->horizontalSplit, false, curEntry.layoutEntry->splitPosition);
  627. DockContainer* otherChild = curEntry.container->mChildren[1];
  628. OpenWidgets(otherChild, leafEntry->widgetNames);
  629. if (!curEntry.layoutEntry->children[0]->isLeaf)
  630. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  631. if (!curEntry.layoutEntry->children[1]->isLeaf)
  632. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  633. }
  634. }
  635. }
  636. // Set container sizes
  637. {
  638. Stack<StackEntry> layoutTodo;
  639. layoutTodo.push(StackEntry(rootEntry, &mRootContainer));
  640. while (!layoutTodo.empty())
  641. {
  642. StackEntry curEntry = layoutTodo.top();
  643. layoutTodo.pop();
  644. if (!curEntry.layoutEntry->isLeaf)
  645. {
  646. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[0], curEntry.container->mChildren[0]));
  647. layoutTodo.push(StackEntry(curEntry.layoutEntry->children[1], curEntry.container->mChildren[1]));
  648. }
  649. }
  650. }
  651. }
  652. // Destroy any widgets that are no longer docked anywhere
  653. for(auto& widget : undockedWidgets)
  654. {
  655. if(widget->_getParent() == nullptr)
  656. widget->close();
  657. }
  658. setArea(mArea.x, mArea.y, mArea.width, mArea.height);
  659. }
  660. void DockManager::toggleMaximize(DockContainer* container)
  661. {
  662. if (mIsMaximized)
  663. {
  664. if (mRestoredLayout != nullptr)
  665. setLayout(mRestoredLayout);
  666. mRestoredLayout = nullptr;
  667. mMaximizedContainer = nullptr;
  668. mIsMaximized = false;
  669. }
  670. else
  671. {
  672. mRestoredLayout = getLayout();
  673. mMaximizedContainer = container;
  674. Vector<String> maximizedWidgetNames;
  675. if (container->mWidgets != nullptr)
  676. {
  677. UINT32 numWidgets = container->mWidgets->getNumWidgets();
  678. for (UINT32 i = 0; i < numWidgets; i++)
  679. {
  680. EditorWidgetBase* widget = container->mWidgets->getWidget(i);
  681. maximizedWidgetNames.push_back(widget->getName());
  682. }
  683. }
  684. SPtr<DockManagerLayout> maxLayout = bs_shared_ptr_new<DockManagerLayout>();
  685. DockManagerLayout::Entry& rootEntry = maxLayout->getRootEntry();
  686. rootEntry.isLeaf = true;
  687. rootEntry.widgetNames = maximizedWidgetNames;
  688. setLayout(maxLayout);
  689. mIsMaximized = true;
  690. }
  691. }
  692. void DockManager::updateClippedBounds()
  693. {
  694. // TODO - Clipping not actually accounted for but shouldn't matter as right now DockManager is only used in one specific situation
  695. mClippedBounds = mRootContainer.mArea;
  696. }
  697. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  698. {
  699. const static int spacing = 10;
  700. const static float innerScale = 0.15f;
  701. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  702. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  703. UINT32 innerOffset = outWidth > outHeight ? Math::floorToInt(innerScale * outHeight) : Math::floorToInt(innerScale * outWidth);
  704. UINT32 inWidth = outWidth - innerOffset;
  705. UINT32 inHeight = outHeight - innerOffset;
  706. INT32 inXOffset = Math::floorToInt((outWidth - inWidth) * 0.5f);
  707. INT32 inYOffset = Math::floorToInt((outHeight - inHeight) * 0.5f);
  708. Vector2 outTopLeft((float)x, (float)y);
  709. Vector2 outTopRight((float)(x + outWidth), (float)y);
  710. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  711. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  712. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  713. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  714. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  715. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  716. SPtr<VertexDataDesc> vertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  717. vertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  718. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  719. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(16, 24, vertexDesc);
  720. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  721. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  722. // Top
  723. Vector2 topOffset((float)spacing, 0.0f);
  724. mTopDropPolygon[0] = outTopLeft + topOffset;
  725. mTopDropPolygon[1] = outTopRight + topOffset;
  726. mTopDropPolygon[2] = inTopRight + topOffset;
  727. mTopDropPolygon[3] = inTopLeft + topOffset;
  728. vertIter.addValue(mTopDropPolygon[0]);
  729. vertIter.addValue(mTopDropPolygon[1]);
  730. vertIter.addValue(mTopDropPolygon[2]);
  731. vertIter.addValue(mTopDropPolygon[3]);
  732. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  733. UINT32 color32 = color.getAsRGBA();
  734. colIter.addValue(color32);
  735. colIter.addValue(color32);
  736. colIter.addValue(color32);
  737. colIter.addValue(color32);
  738. // Bottom
  739. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  740. mBotDropPolygon[0] = inBotLeft + botOffset;
  741. mBotDropPolygon[1] = inBotRight + botOffset;
  742. mBotDropPolygon[2] = outBotRight + botOffset;
  743. mBotDropPolygon[3] = outBotLeft + botOffset;
  744. vertIter.addValue(mBotDropPolygon[0]);
  745. vertIter.addValue(mBotDropPolygon[1]);
  746. vertIter.addValue(mBotDropPolygon[2]);
  747. vertIter.addValue(mBotDropPolygon[3]);
  748. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  749. color32 = color.getAsRGBA();
  750. colIter.addValue(color32);
  751. colIter.addValue(color32);
  752. colIter.addValue(color32);
  753. colIter.addValue(color32);
  754. // Left
  755. Vector2 leftOffset(0.0f, (float)spacing);
  756. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  757. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  758. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  759. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  760. vertIter.addValue(mLeftDropPolygon[0]);
  761. vertIter.addValue(mLeftDropPolygon[1]);
  762. vertIter.addValue(mLeftDropPolygon[2]);
  763. vertIter.addValue(mLeftDropPolygon[3]);
  764. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  765. color32 = color.getAsRGBA();
  766. colIter.addValue(color32);
  767. colIter.addValue(color32);
  768. colIter.addValue(color32);
  769. colIter.addValue(color32);
  770. // Right
  771. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  772. mRightDropPolygon[0] = inTopRight + rightOffset;
  773. mRightDropPolygon[1] = outTopRight + rightOffset;
  774. mRightDropPolygon[2] = outBotRight + rightOffset;
  775. mRightDropPolygon[3] = inBotRight + rightOffset;
  776. vertIter.addValue(mRightDropPolygon[0]);
  777. vertIter.addValue(mRightDropPolygon[1]);
  778. vertIter.addValue(mRightDropPolygon[2]);
  779. vertIter.addValue(mRightDropPolygon[3]);
  780. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  781. color32 = color.getAsRGBA();
  782. colIter.addValue(color32);
  783. colIter.addValue(color32);
  784. colIter.addValue(color32);
  785. colIter.addValue(color32);
  786. UINT32* indexData = meshData->getIndices32();
  787. // Top
  788. indexData[0] = 0;
  789. indexData[1] = 1;
  790. indexData[2] = 2;
  791. indexData[3] = 0;
  792. indexData[4] = 2;
  793. indexData[5] = 3;
  794. // Bottom
  795. indexData[6] = 4;
  796. indexData[7] = 5;
  797. indexData[8] = 6;
  798. indexData[9] = 4;
  799. indexData[10] = 6;
  800. indexData[11] = 7;
  801. // Left
  802. indexData[12] = 8;
  803. indexData[13] = 9;
  804. indexData[14] = 10;
  805. indexData[15] = 8;
  806. indexData[16] = 10;
  807. indexData[17] = 11;
  808. // Right
  809. indexData[18] = 12;
  810. indexData[19] = 13;
  811. indexData[20] = 14;
  812. indexData[21] = 12;
  813. indexData[22] = 14;
  814. indexData[23] = 15;
  815. mDropOverlayMesh = Mesh::create(meshData);
  816. }
  817. bool DockManager::_mouseEvent(const GUIMouseEvent& event)
  818. {
  819. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  820. {
  821. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  822. return false;
  823. const Vector2I& widgetRelPos = event.getPosition();
  824. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  825. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  826. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  827. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  828. mMouseOverContainer = mRootContainer.findAtPos(windowPos);
  829. if(mMouseOverContainer == nullptr)
  830. mMouseOverContainer = &mRootContainer;
  831. Rect2I overlayBounds;
  832. if(mMouseOverContainer != nullptr)
  833. overlayBounds = mMouseOverContainer->getContentBounds();
  834. // Update mesh if needed
  835. if(mLastOverlayBounds != overlayBounds)
  836. {
  837. if(overlayBounds.width <= 0 || overlayBounds.height <= 0)
  838. mDropOverlayMesh = HMesh();
  839. else
  840. updateDropOverlay(overlayBounds.x, overlayBounds.y, overlayBounds.width, overlayBounds.height);
  841. mLastOverlayBounds = overlayBounds;
  842. }
  843. // Check if we need to highlight any drop locations
  844. if(mMouseOverContainer != nullptr)
  845. {
  846. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  847. mHighlightedDropLoc = DockLocation::Top;
  848. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  849. mHighlightedDropLoc = DockLocation::Bottom;
  850. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  851. mHighlightedDropLoc = DockLocation::Left;
  852. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  853. mHighlightedDropLoc = DockLocation::Right;
  854. else
  855. mHighlightedDropLoc = DockLocation::None;
  856. if(overlayBounds.contains(windowPos))
  857. mShowOverlay = true;
  858. else
  859. mShowOverlay = false;
  860. }
  861. else
  862. mShowOverlay = false;
  863. return false;
  864. }
  865. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  866. {
  867. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  868. return false;
  869. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  870. const Vector2I& widgetRelPos = event.getPosition();
  871. const Matrix4& worldTfrm = _getParentWidget()->getWorldTfrm();
  872. Vector4 tfrmdPos = worldTfrm.multiplyAffine(Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f));
  873. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  874. Vector2I windowPos(Math::roundToInt(windowPosVec.x), Math::roundToInt(windowPosVec.y));
  875. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  876. if(mouseOverContainer == nullptr)
  877. {
  878. Rect2I overlayBounds = mRootContainer.getContentBounds();
  879. if(overlayBounds.contains(windowPos))
  880. {
  881. insert(nullptr, draggedWidget, DockLocation::None);
  882. return true;
  883. }
  884. }
  885. else
  886. {
  887. if (insidePolygon(mTopDropPolygon, 4, windowPosVec))
  888. {
  889. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Top);
  890. return true;
  891. }
  892. else if (insidePolygon(mBotDropPolygon, 4, windowPosVec))
  893. {
  894. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Bottom);
  895. return true;
  896. }
  897. else if (insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  898. {
  899. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Left);
  900. return true;
  901. }
  902. else if (insidePolygon(mRightDropPolygon, 4, windowPosVec))
  903. {
  904. insert(mouseOverContainer->mWidgets, draggedWidget, DockLocation::Right);
  905. return true;
  906. }
  907. }
  908. }
  909. return false;
  910. }
  911. // TODO - Move to a separate Polygon class?
  912. bool DockManager::insidePolygon(Vector2* polyPoints, UINT32 numPoints, Vector2 point) const
  913. {
  914. bool isInside = false;
  915. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  916. {
  917. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  918. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  919. isInside = !isInside;
  920. }
  921. return isInside;
  922. }
  923. const Color DockOverlayRenderer::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  924. const Color DockOverlayRenderer::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  925. DockOverlayRenderer::DockOverlayRenderer()
  926. :mHighlightedDropLoc(DockManager::DockLocation::None), mShowOverlay(false)
  927. {
  928. }
  929. DockOverlayRenderer::~DockOverlayRenderer()
  930. {
  931. if (mCamera != nullptr)
  932. {
  933. SPtr<CoreRenderer> activeRenderer = RendererManager::instance().getActive();
  934. activeRenderer->unregisterRenderCallback(mCamera.get(), 40);
  935. }
  936. }
  937. void DockOverlayRenderer::initialize(const SPtr<MaterialCore>& material)
  938. {
  939. mMaterial = material;
  940. mParams = material->createParamsSet();
  941. }
  942. void DockOverlayRenderer::updateData(const SPtr<CameraCore>& camera, const SPtr<MeshCore>& mesh, bool active,
  943. DockManager::DockLocation location)
  944. {
  945. if (mCamera != camera)
  946. {
  947. SPtr<CoreRenderer> activeRenderer = RendererManager::instance().getActive();
  948. if (mCamera != nullptr)
  949. activeRenderer->unregisterRenderCallback(mCamera.get(), 40);
  950. if (camera != nullptr)
  951. activeRenderer->registerRenderCallback(camera.get(), 40, std::bind(&DockOverlayRenderer::render, this), true);
  952. }
  953. mCamera = camera;
  954. mMesh = mesh;
  955. mShowOverlay = active;
  956. mHighlightedDropLoc = location;
  957. }
  958. void DockOverlayRenderer::render()
  959. {
  960. THROW_IF_NOT_CORE_THREAD;
  961. if (!mShowOverlay)
  962. return;
  963. SPtr<ViewportCore> viewport = mCamera->getViewport();
  964. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  965. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  966. mMaterial->setFloat("invViewportWidth", invViewportWidth);
  967. mMaterial->setFloat("invViewportHeight", invViewportHeight);
  968. mMaterial->setColor("tintColor", TINT_COLOR);
  969. mMaterial->setColor("highlightColor", HIGHLIGHT_COLOR);
  970. Color highlightColor;
  971. switch (mHighlightedDropLoc)
  972. {
  973. case DockManager::DockLocation::Top:
  974. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  975. break;
  976. case DockManager::DockLocation::Bottom:
  977. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  978. break;
  979. case DockManager::DockLocation::Left:
  980. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  981. break;
  982. case DockManager::DockLocation::Right:
  983. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  984. break;
  985. case DockManager::DockLocation::None:
  986. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  987. break;
  988. }
  989. mMaterial->setColor("highlightActive", highlightColor);
  990. mMaterial->updateParamsSet(mParams);
  991. gRendererUtility().setPass(mMaterial);
  992. gRendererUtility().setPassParams(mParams);
  993. gRendererUtility().draw(mMesh, mMesh->getProperties().getSubMesh(0));
  994. }
  995. }