BsDockManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #include "BsDockManager.h"
  2. #include "BsEditorWidgetContainer.h"
  3. #include "CmMath.h"
  4. #include "CmException.h"
  5. #include "CmMesh.h"
  6. #include "CmMaterial.h"
  7. #include "CmVector2.h"
  8. #include "CmRenderQueue.h"
  9. #include "CmApplication.h"
  10. #include "CmRendererManager.h"
  11. #include "CmRenderer.h"
  12. #include "CmSceneObject.h"
  13. #include "BsGUIManager.h"
  14. #include "BsBuiltinMaterialManager.h"
  15. #include "BsGUIWidget.h"
  16. #include "BsCamera.h"
  17. using namespace CamelotFramework;
  18. using namespace BansheeEngine;
  19. namespace BansheeEditor
  20. {
  21. const CM::UINT32 DockManager::DockContainer::SliderSize = 4;
  22. const CM::Color DockManager::TINT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.22f);
  23. const CM::Color DockManager::HIGHLIGHT_COLOR = Color(0.44f, 0.44f, 0.44f, 0.42f);
  24. DockManager::DockContainer::DockContainer()
  25. :mIsLeaf(false), mWidgets(nullptr), mX(0), mY(0), mWidth(0), mHeight(0), mSplitPosition(0.5f),
  26. mIsHorizontal(false)
  27. {
  28. mChildren[0] = nullptr;
  29. mChildren[1] = nullptr;
  30. }
  31. DockManager::DockContainer::~DockContainer()
  32. {
  33. if(mIsLeaf && mWidgets != nullptr)
  34. cm_delete(mWidgets);
  35. if(!mIsLeaf)
  36. {
  37. if(mChildren[0] != nullptr)
  38. cm_delete(mChildren[0]);
  39. if(mChildren[1] != nullptr)
  40. cm_delete(mChildren[1]);
  41. }
  42. // TODO - Clean up slider
  43. }
  44. void DockManager::DockContainer::setArea(CM::INT32 x, CM::INT32 y, UINT32 width, UINT32 height)
  45. {
  46. mX = x;
  47. mY = y;
  48. mWidth = width;
  49. mHeight = height;
  50. if(mIsLeaf)
  51. {
  52. if(mWidgets != nullptr)
  53. {
  54. mWidgets->setPosition(x, y);
  55. mWidgets->setSize(width, height);
  56. }
  57. }
  58. else if(mChildren[0] != nullptr && mChildren[1] != nullptr)
  59. {
  60. if(mIsHorizontal)
  61. {
  62. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mHeight - (INT32)SliderSize);
  63. UINT32 sizeTop = Math::FloorToInt(remainingSize * mSplitPosition);
  64. UINT32 sizeBottom = remainingSize - sizeTop;
  65. mChildren[0]->setArea(mX, mY, mWidth, sizeTop);
  66. mChildren[1]->setArea(mX, mY + sizeTop + SliderSize, mWidth, sizeBottom);
  67. // TODO - Set slider position
  68. }
  69. else
  70. {
  71. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mWidth - (INT32)SliderSize);
  72. UINT32 sizeLeft = Math::FloorToInt(remainingSize * mSplitPosition);
  73. UINT32 sizeRight = remainingSize - sizeLeft;
  74. mChildren[0]->setArea(mX, mY, sizeLeft, mHeight);
  75. mChildren[1]->setArea(mX + sizeLeft + SliderSize, mY, sizeRight, mHeight);
  76. // TODO - Set slider position
  77. }
  78. }
  79. }
  80. void DockManager::DockContainer::makeLeaf(GUIWidget* widgetParent, RenderWindow* parentWindow, EditorWidget* widget)
  81. {
  82. mIsLeaf = true;
  83. mWidgets = cm_new<EditorWidgetContainer>(widgetParent, parentWindow);
  84. mWidgets->add(*widget);
  85. mWidgets->setPosition(mX, mY);
  86. mWidgets->setSize(mWidth, mHeight);
  87. }
  88. void DockManager::DockContainer::addLeft(BS::GUIWidget* widgetParent, RenderWindow* parentWindow, EditorWidget* widget)
  89. {
  90. splitContainer(widgetParent, parentWindow, widget, false, true);
  91. }
  92. void DockManager::DockContainer::addRight(BS::GUIWidget* widgetParent, RenderWindow* parentWindow, EditorWidget* widget)
  93. {
  94. splitContainer(widgetParent, parentWindow, widget, false, false);
  95. }
  96. void DockManager::DockContainer::addTop(BS::GUIWidget* widgetParent, RenderWindow* parentWindow, EditorWidget* widget)
  97. {
  98. splitContainer(widgetParent, parentWindow, widget, true, true);
  99. }
  100. void DockManager::DockContainer::addBottom(BS::GUIWidget* widgetParent, RenderWindow* parentWindow, EditorWidget* widget)
  101. {
  102. splitContainer(widgetParent, parentWindow, widget, true, false);
  103. }
  104. void DockManager::DockContainer::splitContainer(BS::GUIWidget* widgetParent, RenderWindow* parentWindow, EditorWidget* widget, bool horizontal, bool newChildIsFirst)
  105. {
  106. UINT32 idxA = newChildIsFirst ? 0 : 1;
  107. UINT32 idxB = (idxA + 1) % 2;
  108. mChildren[idxA] = cm_new<DockContainer>();
  109. mChildren[idxB] = cm_new<DockContainer>(*this);
  110. mChildren[idxA]->makeLeaf(widgetParent, parentWindow, widget);
  111. mIsLeaf = false;
  112. mIsHorizontal = horizontal;
  113. mWidgets = nullptr;
  114. mSplitPosition = 0.5f;
  115. // TODO - Add slider
  116. setArea(mX, mY, mWidth, mHeight);
  117. }
  118. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  119. {
  120. if(mIsLeaf)
  121. {
  122. if(mWidgets == widgetContainer)
  123. return this;
  124. else
  125. return nullptr;
  126. }
  127. else
  128. {
  129. if(mChildren[0] != nullptr && mChildren[0]->find(widgetContainer) != nullptr)
  130. return mChildren[0];
  131. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  132. return mChildren[1];
  133. }
  134. return nullptr;
  135. }
  136. DockManager::DockContainer* DockManager::DockContainer::findAtPos(const CM::Int2& pos)
  137. {
  138. if(mIsLeaf)
  139. {
  140. if(pos.x >= (float)mX && pos.x < (float)(mX + mWidth) &&
  141. pos.y >= (float)mY && pos.y < (float)(mY + mHeight))
  142. {
  143. return this;
  144. }
  145. }
  146. else
  147. {
  148. if(mChildren[0] != nullptr && mChildren[0]->findAtPos(pos) != nullptr)
  149. return mChildren[0];
  150. if(mChildren[1] != nullptr && mChildren[1]->findAtPos(pos) != nullptr)
  151. return mChildren[1];
  152. }
  153. return nullptr;
  154. }
  155. DockManager::DockManager(BS::GUIWidget* parent, CM::RenderWindow* parentWindow)
  156. :mParent(parent), mParentWindow(parentWindow), mMouseOverContainer(nullptr), mHighlightedDropLoc(DockLocation::None)
  157. {
  158. mTopDropPolygon = cm_newN<Vector2>(4);
  159. mBotDropPolygon = cm_newN<Vector2>(4);
  160. mLeftDropPolygon = cm_newN<Vector2>(4);
  161. mRightDropPolygon = cm_newN<Vector2>(4);
  162. mDropOverlayMat = BuiltinMaterialManager::instance().createDockDropOverlayMaterial();
  163. RendererManager::instance().getActive()->addRenderCallback(parent->getTarget(), boost::bind(&DockManager::render, this, _1, _2));
  164. GUIManager::instance().mouseEventFilter.connect(boost::bind(&DockManager::onGUIMouseEvent, this, _1, _2, _3));
  165. }
  166. DockManager::~DockManager()
  167. {
  168. cm_deleteN(mTopDropPolygon, 4);
  169. cm_deleteN(mBotDropPolygon, 4);
  170. cm_deleteN(mLeftDropPolygon, 4);
  171. cm_deleteN(mRightDropPolygon, 4);
  172. }
  173. void DockManager::render(const Viewport* viewport, CM::RenderQueue& renderQueue)
  174. {
  175. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  176. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  177. if(mDropOverlayMesh == nullptr || !mDropOverlayMesh.isLoaded() || !mDropOverlayMesh->isInitialized())
  178. return;
  179. if(mDropOverlayMat == nullptr || !mDropOverlayMat.isLoaded() || !mDropOverlayMat->isInitialized())
  180. return;
  181. mDropOverlayMat->setFloat("invViewportWidth", invViewportWidth);
  182. mDropOverlayMat->setFloat("invViewportHeight", invViewportHeight);
  183. mDropOverlayMat->setColor("tintColor", TINT_COLOR);
  184. mDropOverlayMat->setColor("highlightColor", HIGHLIGHT_COLOR);
  185. Color highlightColor;
  186. switch(mHighlightedDropLoc)
  187. {
  188. case DockLocation::Top:
  189. highlightColor = Color(1.0f, 0.0f, 0.0f, 0.0f);
  190. break;
  191. case DockLocation::Bottom:
  192. highlightColor = Color(0.0f, 1.0f, 0.0f, 0.0f);
  193. break;
  194. case DockLocation::Left:
  195. highlightColor = Color(0.0f, 0.0f, 1.0f, 0.0f);
  196. break;
  197. case DockLocation::Right:
  198. highlightColor = Color(0.0f, 0.0f, 0.0f, 1.0f);
  199. break;
  200. case DockLocation::None:
  201. highlightColor = Color(0.0f, 0.0f, 0.0f, 0.0f);
  202. break;
  203. }
  204. mDropOverlayMat->setColor("highlightActive", highlightColor);
  205. renderQueue.add(mDropOverlayMat, mDropOverlayMesh, 0, Vector3::ZERO);
  206. }
  207. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidget* widgetToInsert, DockLocation location)
  208. {
  209. if(relativeTo != nullptr)
  210. {
  211. DockContainer* container = mRootContainer.find(relativeTo);
  212. if(container == nullptr)
  213. CM_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  214. switch(location)
  215. {
  216. case DockLocation::Left:
  217. container->addLeft(mParent, mParentWindow, widgetToInsert);
  218. break;
  219. case DockLocation::Right:
  220. container->addRight(mParent, mParentWindow, widgetToInsert);
  221. break;
  222. case DockLocation::Top:
  223. container->addTop(mParent, mParentWindow, widgetToInsert);
  224. break;
  225. case DockLocation::Bottom:
  226. container->addBottom(mParent, mParentWindow, widgetToInsert);
  227. break;
  228. }
  229. }
  230. else
  231. {
  232. if(mRootContainer.mWidgets != nullptr)
  233. CM_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  234. mRootContainer.makeLeaf(mParent, mParentWindow, widgetToInsert);
  235. }
  236. }
  237. void DockManager::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  238. {
  239. mRootContainer.setArea(x, y, width, height);
  240. updateDropOverlay(x, y, width, height);
  241. }
  242. void DockManager::updateDropOverlay(INT32 x, INT32 y, UINT32 width, UINT32 height)
  243. {
  244. const static int spacing = 10;
  245. const static float innerScale = 0.75f;
  246. UINT32 outWidth = std::max(0, (INT32)width - spacing * 2);
  247. UINT32 outHeight = std::max(0, (INT32)height - spacing * 2);
  248. UINT32 inWidth = (UINT32)Math::FloorToInt(innerScale * outWidth);
  249. UINT32 inHeight = (UINT32)Math::FloorToInt(innerScale * outHeight);
  250. INT32 inXOffset = Math::FloorToInt((outWidth - inWidth) * 0.5f);
  251. INT32 inYOffset = Math::FloorToInt((outHeight - inHeight) * 0.5f);
  252. Vector2 outTopLeft((float)x, (float)y);
  253. Vector2 outTopRight((float)(x + outWidth), (float)y);
  254. Vector2 outBotLeft((float)x, (float)(y + outHeight));
  255. Vector2 outBotRight((float)(x + outWidth), (float)(y + outHeight));
  256. Vector2 inTopLeft((float)(x + inXOffset), (float)(y + inYOffset));
  257. Vector2 inTopRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset));
  258. Vector2 inBotLeft((float)(x + inXOffset), (float)(y + inYOffset + inHeight));
  259. Vector2 inBotRight((float)(x + inXOffset + inWidth), (float)(y + inYOffset + inHeight));
  260. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(16);
  261. meshData->beginDesc();
  262. meshData->addSubMesh(24, 0, DOT_TRIANGLE_LIST);
  263. meshData->addVertElem(VET_FLOAT2, VES_POSITION);
  264. meshData->addVertElem(VET_COLOR, VES_COLOR);
  265. meshData->endDesc();
  266. auto vertIter = meshData->getVec2DataIter(VES_POSITION);
  267. auto colIter = meshData->getDWORDDataIter(VES_COLOR);
  268. // Top
  269. Vector2 topOffset((float)spacing, 0.0f);
  270. mTopDropPolygon[0] = outTopLeft + topOffset;
  271. mTopDropPolygon[1] = outTopRight + topOffset;
  272. mTopDropPolygon[2] = inTopRight + topOffset;
  273. mTopDropPolygon[3] = inTopLeft + topOffset;
  274. vertIter.addValue(mTopDropPolygon[0]);
  275. vertIter.addValue(mTopDropPolygon[1]);
  276. vertIter.addValue(mTopDropPolygon[2]);
  277. vertIter.addValue(mTopDropPolygon[3]);
  278. Color color(1.0f, 0.0f, 0.0f, 0.0f);
  279. UINT32 color32 = color.getAsRGBA();
  280. colIter.addValue(color32);
  281. colIter.addValue(color32);
  282. colIter.addValue(color32);
  283. colIter.addValue(color32);
  284. // Bottom
  285. Vector2 botOffset((float)spacing, (float)spacing * 2.0f);
  286. mBotDropPolygon[0] = inBotLeft + botOffset;
  287. mBotDropPolygon[1] = inBotRight + botOffset;
  288. mBotDropPolygon[2] = outBotRight + botOffset;
  289. mBotDropPolygon[3] = outBotLeft + botOffset;
  290. vertIter.addValue(mBotDropPolygon[0]);
  291. vertIter.addValue(mBotDropPolygon[1]);
  292. vertIter.addValue(mBotDropPolygon[2]);
  293. vertIter.addValue(mBotDropPolygon[3]);
  294. color = Color(0.0f, 1.0f, 0.0f, 0.0f);
  295. color32 = color.getAsRGBA();
  296. colIter.addValue(color32);
  297. colIter.addValue(color32);
  298. colIter.addValue(color32);
  299. colIter.addValue(color32);
  300. // Left
  301. Vector2 leftOffset(0.0f, (float)spacing);
  302. mLeftDropPolygon[0] = outTopLeft + leftOffset;
  303. mLeftDropPolygon[1] = inTopLeft + leftOffset;
  304. mLeftDropPolygon[2] = inBotLeft + leftOffset;
  305. mLeftDropPolygon[3] = outBotLeft + leftOffset;
  306. vertIter.addValue(mLeftDropPolygon[0]);
  307. vertIter.addValue(mLeftDropPolygon[1]);
  308. vertIter.addValue(mLeftDropPolygon[2]);
  309. vertIter.addValue(mLeftDropPolygon[3]);
  310. color = Color(0.0f, 0.0f, 1.0f, 0.0f);
  311. color32 = color.getAsRGBA();
  312. colIter.addValue(color32);
  313. colIter.addValue(color32);
  314. colIter.addValue(color32);
  315. colIter.addValue(color32);
  316. // Right
  317. Vector2 rightOffset((float)spacing * 2.0f, (float)spacing);
  318. mRightDropPolygon[0] = inTopRight + rightOffset;
  319. mRightDropPolygon[1] = outTopRight + rightOffset;
  320. mRightDropPolygon[2] = outBotRight + rightOffset;
  321. mRightDropPolygon[3] = inBotRight + rightOffset;
  322. vertIter.addValue(mRightDropPolygon[0]);
  323. vertIter.addValue(mRightDropPolygon[1]);
  324. vertIter.addValue(mRightDropPolygon[2]);
  325. vertIter.addValue(mRightDropPolygon[3]);
  326. color = Color(0.0f, 0.0f, 0.0f, 1.0f);
  327. color32 = color.getAsRGBA();
  328. colIter.addValue(color32);
  329. colIter.addValue(color32);
  330. colIter.addValue(color32);
  331. colIter.addValue(color32);
  332. UINT32* indexData = meshData->getIndices32();
  333. // Top
  334. indexData[0] = 0;
  335. indexData[1] = 1;
  336. indexData[2] = 2;
  337. indexData[3] = 0;
  338. indexData[4] = 2;
  339. indexData[5] = 3;
  340. // Bottom
  341. indexData[6] = 4;
  342. indexData[7] = 5;
  343. indexData[8] = 6;
  344. indexData[9] = 4;
  345. indexData[10] = 6;
  346. indexData[11] = 7;
  347. // Left
  348. indexData[12] = 8;
  349. indexData[13] = 9;
  350. indexData[14] = 10;
  351. indexData[15] = 8;
  352. indexData[16] = 10;
  353. indexData[17] = 11;
  354. // Right
  355. indexData[18] = 12;
  356. indexData[19] = 13;
  357. indexData[20] = 14;
  358. indexData[21] = 12;
  359. indexData[22] = 14;
  360. indexData[23] = 15;
  361. mDropOverlayMesh = Mesh::create();
  362. gMainSyncedCA().writeSubresource(mDropOverlayMesh.getInternalPtr(), 0, meshData);
  363. }
  364. void DockManager::onGUIMouseEvent(GUIWidget* widget, GUIElement* element, const GUIMouseEvent& event)
  365. {
  366. if(event.getType() != GUIMouseEventType::MouseMove) // TODO - Replace with DragAndDrop event
  367. return;
  368. if(widget->getTarget() != mParent->getTarget())
  369. return;
  370. const Int2& widgetRelPos = event.getPosition();
  371. const Matrix4& worldTfrm = widget->SO()->getWorldTfrm();
  372. Vector4 tfrmdPos = worldTfrm * Vector4((float)widgetRelPos.x, (float)widgetRelPos.y, 0.0f, 1.0f);
  373. Vector2 windowPosVec(tfrmdPos.x, tfrmdPos.y);
  374. Int2 windowPos(Math::RoundToInt(windowPosVec.x), Math::RoundToInt(windowPosVec.y));
  375. DockContainer* mouseOverContainer = mRootContainer.findAtPos(windowPos);
  376. // DEBUG ONLY
  377. mouseOverContainer = &mRootContainer;
  378. // END DEBUG ONLY
  379. // Check if we need to highlight any drop locations
  380. if(mouseOverContainer)
  381. {
  382. if(insidePolygon(mTopDropPolygon, 4, windowPosVec))
  383. mHighlightedDropLoc = DockLocation::Top;
  384. else if(insidePolygon(mBotDropPolygon, 4, windowPosVec))
  385. mHighlightedDropLoc = DockLocation::Bottom;
  386. else if(insidePolygon(mLeftDropPolygon, 4, windowPosVec))
  387. mHighlightedDropLoc = DockLocation::Left;
  388. else if(insidePolygon(mRightDropPolygon, 4, windowPosVec))
  389. mHighlightedDropLoc = DockLocation::Right;
  390. else
  391. mHighlightedDropLoc = DockLocation::None;
  392. }
  393. // Update mesh if needed
  394. if(mouseOverContainer == mMouseOverContainer)
  395. return;
  396. mMouseOverContainer = mouseOverContainer;
  397. if(mMouseOverContainer == nullptr)
  398. {
  399. mDropOverlayMesh = HMesh();
  400. }
  401. else
  402. {
  403. updateDropOverlay(mMouseOverContainer->mX, mMouseOverContainer->mY, mMouseOverContainer->mWidth, mMouseOverContainer->mHeight);
  404. }
  405. }
  406. // TODO - Move to a separate Polygon class?
  407. bool DockManager::insidePolygon(CM::Vector2* polyPoints, CM::UINT32 numPoints, CM::Vector2 point) const
  408. {
  409. bool isInside = false;
  410. for (UINT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  411. {
  412. float lineVal = (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x;
  413. if (((polyPoints[i].y > point.y) != (polyPoints[j].y > point.y)) && (point.x < lineVal))
  414. isInside = !isInside;
  415. }
  416. return isInside;
  417. }
  418. }