BsDockManager.cpp 19 KB

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