BsGUITabbedTitleBar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "BsBuiltinResources.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. #include "BsDragAndDropManager.h"
  11. #include "BsEditorWidget.h"
  12. #include "BsMath.h"
  13. #include "BsPlatform.h"
  14. #include "BsRenderTarget.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& maxBtnStyle, 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), mMaximizeBtnStyle(maxBtnStyle), mCloseBtnStyle(closeBtnStyle), mTempDraggedWidget(nullptr),
  26. mTempDraggedTabIdx(0)
  27. {
  28. if(mBackgroundStyle == StringUtil::BLANK)
  29. mBackgroundStyle = "TitleBarBackground";
  30. if(mMaximizeBtnStyle == StringUtil::BLANK)
  31. mMaximizeBtnStyle = "WinMinimizeBtn";
  32. if(mCloseBtnStyle == StringUtil::BLANK)
  33. mCloseBtnStyle = "WinCloseBtn";
  34. if(mTabBtnStyle == StringUtil::BLANK)
  35. mTabBtnStyle = "TabbedBarBtn";
  36. mMinBtn = GUIButton::create(HString(L""), mMaximizeBtnStyle);
  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& maxBtnStyle, const String& closeBtnStyle)
  53. {
  54. return new (bs_alloc<GUITabbedTitleBar>()) GUITabbedTitleBar(backgroundStyle, tabBtnStyle,
  55. maxBtnStyle, closeBtnStyle, GUIDimensions::create());
  56. }
  57. GUITabbedTitleBar* GUITabbedTitleBar::create(const GUIOptions& options, const String& backgroundStyle,
  58. const String& tabBtnStyle, const String& maxBtnStyle, const String& closeBtnStyle)
  59. {
  60. return new (bs_alloc<GUITabbedTitleBar>()) GUITabbedTitleBar(backgroundStyle, tabBtnStyle,
  61. maxBtnStyle, closeBtnStyle, GUIDimensions::create(options));
  62. }
  63. UINT32 GUITabbedTitleBar::addTab(const HString& name)
  64. {
  65. return 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::updateTabName(UINT32 uniqueIdx, const HString& name)
  90. {
  91. INT32 idx = uniqueIdxToSeqIdx(uniqueIdx);
  92. if (idx == -1)
  93. return;
  94. idx = (INT32)Math::clamp((UINT32)idx, 0U, (UINT32)mTabButtons.size() - 1);
  95. mTabButtons[idx]->setContent(GUIContent(name));
  96. }
  97. void GUITabbedTitleBar::setActive(UINT32 uniqueIdx)
  98. {
  99. mTabButtons[uniqueIdxToSeqIdx(uniqueIdx)]->toggleOn();
  100. }
  101. UINT32 GUITabbedTitleBar::getTabIdx(UINT32 position) const
  102. {
  103. return mTabButtons[position]->getIndex();
  104. }
  105. bool GUITabbedTitleBar::_mouseEvent(const GUIMouseEvent& event)
  106. {
  107. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  108. {
  109. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  110. return false;
  111. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  112. const Vector2I& widgetRelPos = event.getPosition();
  113. if(mTempDraggedWidget == nullptr)
  114. {
  115. UINT32 numTabButtons = (UINT32)mTabButtons.size();
  116. for(UINT32 i = 0; i < numTabButtons; i++)
  117. {
  118. UINT32 width = mTabButtons[i]->_getLayoutData().area.width;
  119. INT32 centerX = mTabButtons[i]->_getLayoutData().area.x + width / 2;
  120. if((i + 1) == numTabButtons)
  121. {
  122. if(i == 0 && widgetRelPos.x <= centerX)
  123. {
  124. insertTab(0, draggedWidget->getDisplayName());
  125. mTempDraggedTabIdx = mTabButtons[0]->getIndex();
  126. break;
  127. }
  128. else if(widgetRelPos.x > centerX)
  129. {
  130. addTab(draggedWidget->getDisplayName());
  131. mTempDraggedTabIdx = mTabButtons[i + 1]->getIndex();
  132. break;
  133. }
  134. }
  135. else
  136. {
  137. if(i == 0 && widgetRelPos.x <= centerX)
  138. {
  139. insertTab(0, draggedWidget->getDisplayName());
  140. mTempDraggedTabIdx = mTabButtons[0]->getIndex();
  141. break;
  142. }
  143. else
  144. {
  145. UINT32 nextWidth = mTabButtons[i + 1]->_getLayoutData().area.width;
  146. INT32 nextCenterX = mTabButtons[i + 1]->_getLayoutData().area.x + nextWidth / 2;
  147. if(widgetRelPos.x > centerX && widgetRelPos.x < nextCenterX)
  148. {
  149. insertTab(i + 1, draggedWidget->getDisplayName());
  150. mTempDraggedTabIdx = mTabButtons[i + 1]->getIndex();
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. mTempDraggedWidget = draggedWidget;
  157. startDrag(uniqueIdxToSeqIdx(mTempDraggedTabIdx), Vector2I());
  158. mInitialDragOffset = Math::roundToInt(mDraggedBtn->_getOptimalSize().x * 0.5f);
  159. }
  160. if(mTempDraggedWidget != nullptr)
  161. tabDragged(mTempDraggedTabIdx, widgetRelPos);
  162. return true;
  163. }
  164. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  165. {
  166. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  167. return false;
  168. EditorWidgetBase* draggedWidget = reinterpret_cast<EditorWidgetBase*>(DragAndDropManager::instance().getDragData());
  169. const Vector2I& widgetRelPos = event.getPosition();
  170. if(mTempDraggedWidget != nullptr)
  171. {
  172. UINT32 seqIdx = uniqueIdxToSeqIdx(mTempDraggedTabIdx);
  173. removeTab(mTempDraggedTabIdx);
  174. endDrag();
  175. if(!onTabDraggedOn.empty())
  176. onTabDraggedOn(seqIdx);
  177. }
  178. return true;
  179. }
  180. else if(event.getType() == GUIMouseEventType::MouseDragAndDropLeft)
  181. {
  182. if(mTempDraggedWidget != nullptr)
  183. {
  184. removeTab(mTempDraggedTabIdx);
  185. endDrag();
  186. }
  187. }
  188. return false;
  189. }
  190. void GUITabbedTitleBar::updateClippedBounds()
  191. {
  192. mClippedBounds = mLayoutData.area;
  193. }
  194. Vector2I GUITabbedTitleBar::_getOptimalSize() const
  195. {
  196. Vector2I optimalSize = mMinBtn->_getOptimalSize();
  197. optimalSize.x += OPTION_BTN_SPACING + 1;
  198. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  199. optimalSize.x += closeBtnOptimalSize.x;
  200. optimalSize.y = std::max(optimalSize.y, closeBtnOptimalSize.y);
  201. for (UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  202. {
  203. GUITabButton* btn = mTabButtons[i];
  204. Vector2I btnOptimalSize = btn->_getOptimalSize();
  205. optimalSize.x += btnOptimalSize.x + TAB_SPACING;
  206. optimalSize.y = std::max(optimalSize.y, btnOptimalSize.y);
  207. }
  208. return optimalSize;
  209. }
  210. void GUITabbedTitleBar::_updateLayoutInternal(const GUILayoutData& data)
  211. {
  212. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  213. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  214. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  215. Rect2I tabClipRect = data.clipRect;
  216. tabClipRect.width -= endButtonWidth;
  217. {
  218. Vector2I optimalSize = mBackgroundImage->_getOptimalSize();
  219. GUILayoutData childData = data;
  220. childData.area.x += 1;
  221. childData.area.y += 1;
  222. childData.area.width -= 2;
  223. childData.area.height = optimalSize.y;
  224. mBackgroundImage->_setLayoutData(childData);
  225. }
  226. UINT32 curX = data.area.x + 1;
  227. UINT32 curY = data.area.y;
  228. UINT32 tabBtnHeight = 0;
  229. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  230. {
  231. GUITabButton* btn = mTabButtons[i];
  232. Vector2I optimalSize = btn->_getOptimalSize();
  233. tabBtnHeight = optimalSize.y;
  234. curX += TAB_SPACING;
  235. Vector2I offset;
  236. if(!mDragInProgress || mDraggedBtn != btn)
  237. {
  238. offset = Vector2I(curX, curY);
  239. }
  240. else if(mDragInProgress && mDraggedBtn == btn)
  241. {
  242. offset.x = mDragBtnOffset;
  243. offset.y = curY;
  244. }
  245. GUILayoutData childData = data;
  246. childData.area.x = offset.x;
  247. childData.area.y = offset.y;
  248. childData.area.width = optimalSize.x;
  249. childData.area.height = optimalSize.y;
  250. childData.clipRect = tabClipRect;
  251. btn->_setLayoutData(childData);
  252. curX += optimalSize.x;
  253. }
  254. INT32 optionBtnXPos = data.area.x + data.area.width - endButtonWidth - 1;
  255. {
  256. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - minBtnOptimalSize.y) * 0.5f);
  257. Vector2I offset(optionBtnXPos, optionBtnYPos);
  258. GUILayoutData childData = data;
  259. childData.area.x = offset.x;
  260. childData.area.y = offset.y;
  261. childData.area.width = minBtnOptimalSize.x;
  262. childData.area.height = minBtnOptimalSize.y;
  263. mMinBtn->_setLayoutData(childData);
  264. }
  265. optionBtnXPos += minBtnOptimalSize.x + OPTION_BTN_SPACING;
  266. {
  267. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - closeBtnOptimalSize.y) * 0.5f);
  268. Vector2I offset(optionBtnXPos, optionBtnYPos);
  269. GUILayoutData childData = data;
  270. childData.area.x = offset.x;
  271. childData.area.y = offset.y;
  272. childData.area.width = closeBtnOptimalSize.x;
  273. childData.area.height = closeBtnOptimalSize.y;
  274. mCloseBtn->_setLayoutData(childData);
  275. }
  276. }
  277. Vector<Rect2I> GUITabbedTitleBar::calcDraggableAreas(INT32 x, INT32 y, UINT32 width, UINT32 height) const
  278. {
  279. Vector<Rect2I> draggableAreas;
  280. UINT32 curX = x + 1;
  281. UINT32 curY = y;
  282. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  283. {
  284. GUITabButton* btn = mTabButtons[i];
  285. Vector2I optimalSize = btn->_getOptimalSize();
  286. draggableAreas.push_back(Rect2I(curX, curY, TAB_SPACING, height));
  287. curX += TAB_SPACING + optimalSize.x;
  288. }
  289. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  290. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  291. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  292. UINT32 remainingWidth = (UINT32)std::max(0, (INT32)(width - curX - endButtonWidth - 1));
  293. if(remainingWidth > 0)
  294. draggableAreas.push_back(Rect2I(curX, curY, remainingWidth, height));
  295. return draggableAreas;
  296. }
  297. void GUITabbedTitleBar::tabToggled(UINT32 tabIdx, bool toggledOn)
  298. {
  299. if(!toggledOn)
  300. return;
  301. if(!onTabActivated.empty())
  302. onTabActivated(tabIdx);
  303. mActiveTabIdx = tabIdx;
  304. }
  305. void GUITabbedTitleBar::tabClosed()
  306. {
  307. removeTab(mActiveTabIdx);
  308. if(!onTabClosed.empty())
  309. onTabClosed(mActiveTabIdx);
  310. if(mTabButtons.size() > 0)
  311. mActiveTabIdx = mTabButtons[0]->getIndex();
  312. }
  313. void GUITabbedTitleBar::startDrag(UINT32 seqIdx, const Vector2I& startDragPos)
  314. {
  315. if(!mDragInProgress)
  316. {
  317. for(auto& btn : mTabButtons)
  318. btn->_setDraggedState(true);
  319. mDraggedBtn = mTabButtons[seqIdx];
  320. mInitialDragOffset = (startDragPos.x - mDraggedBtn->_getLayoutData().area.x);
  321. mDragInProgress = true;
  322. }
  323. }
  324. void GUITabbedTitleBar::endDrag()
  325. {
  326. for(auto& btn : mTabButtons)
  327. btn->_setDraggedState(false);
  328. mTempDraggedWidget = nullptr;
  329. mDragInProgress = false;
  330. mDraggedBtn = nullptr;
  331. }
  332. void GUITabbedTitleBar::tabDragged(UINT32 tabIdx, const Vector2I& dragPos)
  333. {
  334. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  335. if(idx != -1)
  336. {
  337. Rect2I bounds = _getLayoutData().area;
  338. if(bounds.contains(dragPos))
  339. {
  340. if(!mDragInProgress)
  341. startDrag(idx, dragPos);
  342. mDragBtnOffset = dragPos.x - mInitialDragOffset;
  343. for(INT32 i = 0; i < idx; i++)
  344. {
  345. UINT32 width = mTabButtons[i]->_getLayoutData().area.width;
  346. INT32 centerX = mTabButtons[i]->_getLayoutData().area.x + width / 2;
  347. if(dragPos.x < centerX)
  348. {
  349. GUITabButton* temp = mTabButtons[i];
  350. mTabButtons[i] = mTabButtons[idx];
  351. mTabButtons[idx] = temp;
  352. break;
  353. }
  354. }
  355. for(UINT32 i = idx + 1; i < (UINT32)mTabButtons.size(); i++)
  356. {
  357. UINT32 width = mTabButtons[i]->_getLayoutData().area.width;
  358. INT32 centerX = mTabButtons[i]->_getLayoutData().area.x + width / 2;
  359. if(dragPos.x > centerX)
  360. {
  361. GUITabButton* temp = mTabButtons[i];
  362. mTabButtons[i] = mTabButtons[idx];
  363. mTabButtons[idx] = temp;
  364. break;
  365. }
  366. }
  367. _markLayoutAsDirty();
  368. }
  369. else
  370. {
  371. endDrag();
  372. _markLayoutAsDirty();
  373. if(!onTabDraggedOff.empty())
  374. onTabDraggedOff(tabIdx);
  375. }
  376. }
  377. }
  378. void GUITabbedTitleBar::tabDragEnd(UINT32 tabIdx, const Vector2I& dragPos)
  379. {
  380. endDrag();
  381. if(mActiveTabIdx != tabIdx)
  382. tabToggled(tabIdx, true);
  383. _markLayoutAsDirty();
  384. }
  385. INT32 GUITabbedTitleBar::uniqueIdxToSeqIdx(UINT32 uniqueIdx) const
  386. {
  387. UINT32 idx = 0;
  388. for(auto& tab : mTabButtons)
  389. {
  390. if(tab->getIndex() == uniqueIdx)
  391. return idx;
  392. idx++;
  393. }
  394. return -1;
  395. }
  396. const String& GUITabbedTitleBar::getGUITypeName()
  397. {
  398. static String typeName = "TabbedTitleBar";
  399. return typeName;
  400. }
  401. }