BsDockManager.cpp 16 KB

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