BsGUITabbedTitleBar.cpp 14 KB

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