BsGUITabbedTitleBar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include "BsGUITabbedTitleBar.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUITexture.h"
  4. #include "BsGUIButton.h"
  5. #include "BsGUITabButton.h"
  6. #include "BsGUISpace.h"
  7. #include "BsGUIWindowDropArea.h"
  8. #include "BsBuiltinResources.h"
  9. #include "BsGUIWidget.h"
  10. #include "BsGUIMouseEvent.h"
  11. #include "BsDragAndDropManager.h"
  12. #include "BsEditorWidget.h"
  13. #include "BsMath.h"
  14. #include "BsPlatform.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. const UINT32 GUITabbedTitleBar::TAB_SPACING = 20;
  19. const UINT32 GUITabbedTitleBar::OPTION_BTN_SPACING = 3;
  20. GUITabbedTitleBar::GUITabbedTitleBar(const String& backgroundStyle, const String& tabBtnStyle,
  21. const String& minBtnStyle, const String& closeBtnStyle, const GUIDimensions& dimensions)
  22. :GUIElementContainer(dimensions), mMinBtn(nullptr),
  23. mCloseBtn(nullptr), mBackgroundImage(nullptr), mUniqueTabIdx(0), mActiveTabIdx(0),
  24. mDragInProgress(false), mDraggedBtn(nullptr), mDragBtnOffset(0), mInitialDragOffset(0), mBackgroundStyle(backgroundStyle),
  25. mTabBtnStyle(tabBtnStyle), mMinimizeBtnStyle(minBtnStyle), mCloseBtnStyle(closeBtnStyle), mTempDraggedWidget(nullptr),
  26. mTempDraggedTabIdx(0)
  27. {
  28. if(mBackgroundStyle == StringUtil::BLANK)
  29. mBackgroundStyle = "TitleBarBackground";
  30. if(mMinimizeBtnStyle == StringUtil::BLANK)
  31. mMinimizeBtnStyle = "WinMinimizeBtn";
  32. if(mCloseBtnStyle == StringUtil::BLANK)
  33. mCloseBtnStyle = "WinCloseBtn";
  34. if(mTabBtnStyle == StringUtil::BLANK)
  35. mTabBtnStyle = "TabbedBarBtn";
  36. mMinBtn = GUIButton::create(HString(L""), mMinimizeBtnStyle);
  37. mMinBtn->_setElementDepth(1);
  38. _registerChildElement(mMinBtn);
  39. mCloseBtn = GUIButton::create(HString(L""), mCloseBtnStyle);
  40. mCloseBtn->_setElementDepth(1);
  41. _registerChildElement(mCloseBtn);
  42. mBackgroundImage = GUITexture::create(mBackgroundStyle);
  43. mBackgroundImage->_setElementDepth(mMinBtn->_getRenderElementDepthRange() + 1);
  44. _registerChildElement(mBackgroundImage);
  45. mCloseBtn->onClick.connect(std::bind(&GUITabbedTitleBar::tabClosed, this));
  46. mTabToggleGroup = GUIToggle::createToggleGroup();
  47. }
  48. GUITabbedTitleBar::~GUITabbedTitleBar()
  49. {
  50. }
  51. GUITabbedTitleBar* GUITabbedTitleBar::create(const String& backgroundStyle, const String& tabBtnStyle,
  52. const String& minBtnStyle, const String& closeBtnStyle)
  53. {
  54. return new (bs_alloc<GUITabbedTitleBar, PoolAlloc>()) GUITabbedTitleBar(backgroundStyle, tabBtnStyle,
  55. minBtnStyle, closeBtnStyle, GUIDimensions::create());
  56. }
  57. GUITabbedTitleBar* GUITabbedTitleBar::create(const GUIDimensions& dimensions, const String& backgroundStyle,
  58. const String& tabBtnStyle, const String& minBtnStyle, const String& closeBtnStyle)
  59. {
  60. return new (bs_alloc<GUITabbedTitleBar, PoolAlloc>()) GUITabbedTitleBar(backgroundStyle, tabBtnStyle,
  61. minBtnStyle, closeBtnStyle, dimensions);
  62. }
  63. void GUITabbedTitleBar::addTab(const HString& name)
  64. {
  65. insertTab((UINT32)mTabButtons.size(), name);
  66. }
  67. UINT32 GUITabbedTitleBar::insertTab(UINT32 position, const HString& name)
  68. {
  69. GUITabButton* newTabToggle = GUITabButton::create(mTabToggleGroup, mUniqueTabIdx, name, mTabBtnStyle);
  70. newTabToggle->_setElementDepth(1);
  71. _registerChildElement(newTabToggle);
  72. position = Math::clamp(position, 0U, (UINT32)mTabButtons.size());
  73. UINT32 uniqueIdx = mUniqueTabIdx++;
  74. newTabToggle->onToggled.connect(std::bind(&GUITabbedTitleBar::tabToggled, this, uniqueIdx, _1));
  75. newTabToggle->onDragged.connect(std::bind(&GUITabbedTitleBar::tabDragged, this, _1, _2));
  76. newTabToggle->onDragEnd.connect(std::bind(&GUITabbedTitleBar::tabDragEnd, this, _1, _2));
  77. mTabButtons.insert(mTabButtons.begin() + position, newTabToggle);
  78. return uniqueIdx;
  79. }
  80. void GUITabbedTitleBar::removeTab(UINT32 uniqueIdx)
  81. {
  82. INT32 idx = uniqueIdxToSeqIdx(uniqueIdx);
  83. if(idx == -1)
  84. return;
  85. idx = (INT32)Math::clamp((UINT32)idx, 0U, (UINT32)mTabButtons.size() - 1);
  86. GUIElement::destroy(mTabButtons[idx]);
  87. mTabButtons.erase(mTabButtons.begin() + idx);
  88. }
  89. void GUITabbedTitleBar::setActive(UINT32 uniqueIdx)
  90. {
  91. mTabButtons[uniqueIdxToSeqIdx(uniqueIdx)]->toggleOn();
  92. }
  93. UINT32 GUITabbedTitleBar::getTabIdx(UINT32 position) const
  94. {
  95. return mTabButtons[position]->getIndex();
  96. }
  97. bool GUITabbedTitleBar::_mouseEvent(const GUIMouseEvent& event)
  98. {
  99. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  100. {
  101. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  102. return false;
  103. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  104. const Vector2I& widgetRelPos = event.getPosition();
  105. if(mTempDraggedWidget == nullptr)
  106. {
  107. UINT32 numTabButtons = (UINT32)mTabButtons.size();
  108. for(UINT32 i = 0; i < numTabButtons; i++)
  109. {
  110. UINT32 width = mTabButtons[i]->_getWidth();
  111. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  112. if((i + 1) == numTabButtons)
  113. {
  114. if(i == 0 && widgetRelPos.x <= centerX)
  115. {
  116. insertTab(0, draggedWidget->getDisplayName());
  117. mTempDraggedTabIdx = mTabButtons[0]->getIndex();
  118. break;
  119. }
  120. else if(widgetRelPos.x > centerX)
  121. {
  122. addTab(draggedWidget->getDisplayName());
  123. mTempDraggedTabIdx = mTabButtons[i + 1]->getIndex();
  124. break;
  125. }
  126. }
  127. else
  128. {
  129. if(i == 0 && widgetRelPos.x <= centerX)
  130. {
  131. insertTab(0, draggedWidget->getDisplayName());
  132. mTempDraggedTabIdx = mTabButtons[0]->getIndex();
  133. break;
  134. }
  135. else
  136. {
  137. UINT32 nextWidth = mTabButtons[i + 1]->_getWidth();
  138. INT32 nextCenterX = mTabButtons[i + 1]->_getOffset().x + nextWidth / 2;
  139. if(widgetRelPos.x > centerX && widgetRelPos.x < nextCenterX)
  140. {
  141. insertTab(i + 1, draggedWidget->getDisplayName());
  142. mTempDraggedTabIdx = mTabButtons[i + 1]->getIndex();
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. mTempDraggedWidget = draggedWidget;
  149. startDrag(uniqueIdxToSeqIdx(mTempDraggedTabIdx), Vector2I());
  150. mInitialDragOffset = Math::roundToInt(mDraggedBtn->_getOptimalSize().x * 0.5f);
  151. }
  152. if(mTempDraggedWidget != nullptr)
  153. tabDragged(mTempDraggedTabIdx, widgetRelPos);
  154. return true;
  155. }
  156. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  157. {
  158. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  159. return false;
  160. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  161. const Vector2I& widgetRelPos = event.getPosition();
  162. if(mTempDraggedWidget != nullptr)
  163. {
  164. UINT32 seqIdx = uniqueIdxToSeqIdx(mTempDraggedTabIdx);
  165. removeTab(mTempDraggedTabIdx);
  166. endDrag();
  167. if(!onTabDraggedOn.empty())
  168. onTabDraggedOn(seqIdx);
  169. }
  170. return true;
  171. }
  172. else if(event.getType() == GUIMouseEventType::MouseOut)
  173. {
  174. if(mTempDraggedWidget != nullptr)
  175. {
  176. removeTab(mTempDraggedTabIdx);
  177. endDrag();
  178. }
  179. }
  180. return false;
  181. }
  182. void GUITabbedTitleBar::updateClippedBounds()
  183. {
  184. Vector2I offset = _getOffset();
  185. mClippedBounds = Rect2I(offset.x, offset.y, _getWidth(), _getHeight());
  186. }
  187. void GUITabbedTitleBar::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  188. Rect2I clipRect, UINT8 widgetDepth, INT16 panelDepth, UINT16 panelDepthRangeMin, UINT16 panelDepthRangeMax)
  189. {
  190. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  191. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  192. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  193. Rect2I tabClipRect = clipRect;
  194. tabClipRect.width -= endButtonWidth;
  195. {
  196. Vector2I optimalSize = mBackgroundImage->_getOptimalSize();
  197. Vector2I offset(x + 1, y + 1);
  198. mBackgroundImage->_setPosition(offset);
  199. mBackgroundImage->_setWidth(width - 2);
  200. mBackgroundImage->_setHeight(optimalSize.y);
  201. mBackgroundImage->_setAreaDepth(panelDepth);
  202. mBackgroundImage->_setWidgetDepth(widgetDepth);
  203. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  204. mBackgroundImage->_setClipRect(elemClipRect);
  205. }
  206. UINT32 curX = x + 1;
  207. UINT32 curY = y;
  208. UINT32 tabBtnHeight = 0;
  209. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  210. {
  211. GUITabButton* btn = mTabButtons[i];
  212. Vector2I optimalSize = btn->_getOptimalSize();
  213. tabBtnHeight = optimalSize.y;
  214. curX += TAB_SPACING;
  215. Vector2I offset;
  216. if(!mDragInProgress || mDraggedBtn != btn)
  217. {
  218. offset = Vector2I(curX, curY);
  219. }
  220. else if(mDragInProgress && mDraggedBtn == btn)
  221. {
  222. offset = btn->_getOffset();
  223. offset.x = mDragBtnOffset;
  224. }
  225. btn->_setPosition(offset);
  226. btn->_setWidth(optimalSize.x);
  227. btn->_setHeight(optimalSize.y);
  228. btn->_setAreaDepth(panelDepth);
  229. btn->_setWidgetDepth(widgetDepth);
  230. Rect2I elemClipRect(tabClipRect.x - offset.x, tabClipRect.y - offset.y, tabClipRect.width, tabClipRect.height);
  231. btn->_setClipRect(elemClipRect);
  232. curX += optimalSize.x;
  233. }
  234. INT32 optionBtnXPos = x + width - endButtonWidth - 1;
  235. {
  236. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - minBtnOptimalSize.y) * 0.5f);
  237. Vector2I offset(optionBtnXPos, optionBtnYPos);
  238. mMinBtn->_setPosition(offset);
  239. mMinBtn->_setWidth(minBtnOptimalSize.x);
  240. mMinBtn->_setHeight(minBtnOptimalSize.y);
  241. mMinBtn->_setAreaDepth(panelDepth);
  242. mMinBtn->_setWidgetDepth(widgetDepth);
  243. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  244. mMinBtn->_setClipRect(elemClipRect);
  245. }
  246. optionBtnXPos += minBtnOptimalSize.x + OPTION_BTN_SPACING;
  247. {
  248. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - closeBtnOptimalSize.y) * 0.5f);
  249. Vector2I offset(optionBtnXPos, optionBtnYPos);
  250. mCloseBtn->_setPosition(offset);
  251. mCloseBtn->_setWidth(closeBtnOptimalSize.x);
  252. mCloseBtn->_setHeight(closeBtnOptimalSize.y);
  253. mCloseBtn->_setAreaDepth(panelDepth);
  254. mCloseBtn->_setWidgetDepth(widgetDepth);
  255. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  256. mCloseBtn->_setClipRect(elemClipRect);
  257. }
  258. }
  259. Vector<Rect2I> GUITabbedTitleBar::calcDraggableAreas(INT32 x, INT32 y, UINT32 width, UINT32 height) const
  260. {
  261. Vector<Rect2I> draggableAreas;
  262. UINT32 curX = x + 1;
  263. UINT32 curY = y;
  264. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  265. {
  266. GUITabButton* btn = mTabButtons[i];
  267. Vector2I optimalSize = btn->_getOptimalSize();
  268. draggableAreas.push_back(Rect2I(curX, curY, TAB_SPACING, height));
  269. curX += TAB_SPACING + optimalSize.x;
  270. }
  271. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  272. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  273. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  274. UINT32 remainingWidth = (UINT32)std::max(0, (INT32)(width - curX - endButtonWidth - 1));
  275. if(remainingWidth > 0)
  276. draggableAreas.push_back(Rect2I(curX, curY, remainingWidth, height));
  277. return draggableAreas;
  278. }
  279. void GUITabbedTitleBar::tabToggled(UINT32 tabIdx, bool toggledOn)
  280. {
  281. if(!toggledOn)
  282. return;
  283. if(!onTabActivated.empty())
  284. onTabActivated(tabIdx);
  285. mActiveTabIdx = tabIdx;
  286. }
  287. void GUITabbedTitleBar::tabClosed()
  288. {
  289. removeTab(mActiveTabIdx);
  290. if(!onTabClosed.empty())
  291. onTabClosed(mActiveTabIdx);
  292. if(mTabButtons.size() > 0)
  293. mActiveTabIdx = mTabButtons[0]->getIndex();
  294. }
  295. void GUITabbedTitleBar::startDrag(UINT32 seqIdx, const Vector2I& startDragPos)
  296. {
  297. if(!mDragInProgress)
  298. {
  299. for(auto& btn : mTabButtons)
  300. btn->_setDraggedState(true);
  301. mDraggedBtn = mTabButtons[seqIdx];
  302. Vector2I offset = mDraggedBtn->_getOffset();
  303. mInitialDragOffset = (startDragPos.x - offset.x);
  304. mDragInProgress = true;
  305. }
  306. }
  307. void GUITabbedTitleBar::endDrag()
  308. {
  309. for(auto& btn : mTabButtons)
  310. btn->_setDraggedState(false);
  311. mTempDraggedWidget = nullptr;
  312. mDragInProgress = false;
  313. mDraggedBtn = nullptr;
  314. }
  315. void GUITabbedTitleBar::tabDragged(UINT32 tabIdx, const Vector2I& dragPos)
  316. {
  317. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  318. if(idx != -1)
  319. {
  320. Rect2I bounds = _getCachedBounds();
  321. if(bounds.contains(dragPos))
  322. {
  323. if(!mDragInProgress)
  324. startDrag(idx, dragPos);
  325. mDragBtnOffset = dragPos.x - mInitialDragOffset;
  326. for(INT32 i = 0; i < idx; i++)
  327. {
  328. UINT32 width = mTabButtons[i]->_getWidth();
  329. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  330. if(dragPos.x < centerX)
  331. {
  332. GUITabButton* temp = mTabButtons[i];
  333. mTabButtons[i] = mTabButtons[idx];
  334. mTabButtons[idx] = temp;
  335. break;
  336. }
  337. }
  338. for(UINT32 i = idx + 1; i < (UINT32)mTabButtons.size(); i++)
  339. {
  340. UINT32 width = mTabButtons[i]->_getWidth();
  341. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  342. if(dragPos.x > centerX)
  343. {
  344. GUITabButton* temp = mTabButtons[i];
  345. mTabButtons[i] = mTabButtons[idx];
  346. mTabButtons[idx] = temp;
  347. break;
  348. }
  349. }
  350. markContentAsDirty();
  351. }
  352. else
  353. {
  354. endDrag();
  355. markContentAsDirty();
  356. if(!onTabDraggedOff.empty())
  357. onTabDraggedOff(tabIdx);
  358. }
  359. }
  360. }
  361. void GUITabbedTitleBar::tabDragEnd(UINT32 tabIdx, const Vector2I& dragPos)
  362. {
  363. endDrag();
  364. if(mActiveTabIdx != tabIdx)
  365. tabToggled(tabIdx, true);
  366. markContentAsDirty();
  367. }
  368. INT32 GUITabbedTitleBar::uniqueIdxToSeqIdx(UINT32 uniqueIdx) const
  369. {
  370. UINT32 idx = 0;
  371. for(auto& tab : mTabButtons)
  372. {
  373. if(tab->getIndex() == uniqueIdx)
  374. return idx;
  375. idx++;
  376. }
  377. return -1;
  378. }
  379. const String& GUITabbedTitleBar::getGUITypeName()
  380. {
  381. static String typeName = "TabbedTitleBar";
  382. return typeName;
  383. }
  384. }