BsDockManager.cpp 16 KB

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