BsGUITabbedTitleBar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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]->_getLayoutData().area.width;
  111. INT32 centerX = mTabButtons[i]->_getLayoutData().area.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]->_getLayoutData().area.width;
  138. INT32 nextCenterX = mTabButtons[i + 1]->_getLayoutData().area.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. mClippedBounds = mLayoutData.area;
  185. }
  186. Vector2I GUITabbedTitleBar::_getOptimalSize() const
  187. {
  188. Vector2I optimalSize = mMinBtn->_getOptimalSize();
  189. optimalSize.x += OPTION_BTN_SPACING + 1;
  190. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  191. optimalSize.x += closeBtnOptimalSize.x;
  192. optimalSize.y = std::max(optimalSize.y, closeBtnOptimalSize.y);
  193. for (UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  194. {
  195. GUITabButton* btn = mTabButtons[i];
  196. Vector2I btnOptimalSize = btn->_getOptimalSize();
  197. optimalSize.x += btnOptimalSize.x + TAB_SPACING;
  198. optimalSize.y = std::max(optimalSize.y, btnOptimalSize.y);
  199. }
  200. return optimalSize;
  201. }
  202. void GUITabbedTitleBar::_updateLayoutInternal(const GUILayoutData& data)
  203. {
  204. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  205. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  206. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  207. Rect2I tabClipRect = data.clipRect;
  208. tabClipRect.width -= endButtonWidth;
  209. {
  210. Vector2I optimalSize = mBackgroundImage->_getOptimalSize();
  211. GUILayoutData childData = data;
  212. childData.area.x += 1;
  213. childData.area.y += 1;
  214. childData.area.width -= 2;
  215. childData.area.height = optimalSize.y;
  216. mBackgroundImage->_setLayoutData(childData);
  217. }
  218. UINT32 curX = data.area.x + 1;
  219. UINT32 curY = data.area.y;
  220. UINT32 tabBtnHeight = 0;
  221. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  222. {
  223. GUITabButton* btn = mTabButtons[i];
  224. Vector2I optimalSize = btn->_getOptimalSize();
  225. tabBtnHeight = optimalSize.y;
  226. curX += TAB_SPACING;
  227. Vector2I offset;
  228. if(!mDragInProgress || mDraggedBtn != btn)
  229. {
  230. offset = Vector2I(curX, curY);
  231. }
  232. else if(mDragInProgress && mDraggedBtn == btn)
  233. {
  234. offset.x = mDragBtnOffset;
  235. offset.y = btn->_getLayoutData().area.y;
  236. }
  237. GUILayoutData childData = data;
  238. childData.area.x = offset.x;
  239. childData.area.y = offset.y;
  240. childData.area.width = optimalSize.x;
  241. childData.area.height = optimalSize.y;
  242. childData.clipRect = tabClipRect;
  243. btn->_setLayoutData(childData);
  244. curX += optimalSize.x;
  245. }
  246. INT32 optionBtnXPos = data.area.x + data.area.width - endButtonWidth - 1;
  247. {
  248. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - minBtnOptimalSize.y) * 0.5f);
  249. Vector2I offset(optionBtnXPos, optionBtnYPos);
  250. GUILayoutData childData = data;
  251. childData.area.x = offset.x;
  252. childData.area.y = offset.y;
  253. childData.area.width = minBtnOptimalSize.x;
  254. childData.area.height = minBtnOptimalSize.y;
  255. mMinBtn->_setLayoutData(childData);
  256. }
  257. optionBtnXPos += minBtnOptimalSize.x + OPTION_BTN_SPACING;
  258. {
  259. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - closeBtnOptimalSize.y) * 0.5f);
  260. Vector2I offset(optionBtnXPos, optionBtnYPos);
  261. GUILayoutData childData = data;
  262. childData.area.x = offset.x;
  263. childData.area.y = offset.y;
  264. childData.area.width = closeBtnOptimalSize.x;
  265. childData.area.height = closeBtnOptimalSize.y;
  266. mCloseBtn->_setLayoutData(childData);
  267. }
  268. }
  269. Vector<Rect2I> GUITabbedTitleBar::calcDraggableAreas(INT32 x, INT32 y, UINT32 width, UINT32 height) const
  270. {
  271. Vector<Rect2I> draggableAreas;
  272. UINT32 curX = x + 1;
  273. UINT32 curY = y;
  274. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  275. {
  276. GUITabButton* btn = mTabButtons[i];
  277. Vector2I optimalSize = btn->_getOptimalSize();
  278. draggableAreas.push_back(Rect2I(curX, curY, TAB_SPACING, height));
  279. curX += TAB_SPACING + optimalSize.x;
  280. }
  281. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  282. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  283. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  284. UINT32 remainingWidth = (UINT32)std::max(0, (INT32)(width - curX - endButtonWidth - 1));
  285. if(remainingWidth > 0)
  286. draggableAreas.push_back(Rect2I(curX, curY, remainingWidth, height));
  287. return draggableAreas;
  288. }
  289. void GUITabbedTitleBar::tabToggled(UINT32 tabIdx, bool toggledOn)
  290. {
  291. if(!toggledOn)
  292. return;
  293. if(!onTabActivated.empty())
  294. onTabActivated(tabIdx);
  295. mActiveTabIdx = tabIdx;
  296. }
  297. void GUITabbedTitleBar::tabClosed()
  298. {
  299. removeTab(mActiveTabIdx);
  300. if(!onTabClosed.empty())
  301. onTabClosed(mActiveTabIdx);
  302. if(mTabButtons.size() > 0)
  303. mActiveTabIdx = mTabButtons[0]->getIndex();
  304. }
  305. void GUITabbedTitleBar::startDrag(UINT32 seqIdx, const Vector2I& startDragPos)
  306. {
  307. if(!mDragInProgress)
  308. {
  309. for(auto& btn : mTabButtons)
  310. btn->_setDraggedState(true);
  311. mDraggedBtn = mTabButtons[seqIdx];
  312. mInitialDragOffset = (startDragPos.x - mDraggedBtn->_getLayoutData().area.x);
  313. mDragInProgress = true;
  314. }
  315. }
  316. void GUITabbedTitleBar::endDrag()
  317. {
  318. for(auto& btn : mTabButtons)
  319. btn->_setDraggedState(false);
  320. mTempDraggedWidget = nullptr;
  321. mDragInProgress = false;
  322. mDraggedBtn = nullptr;
  323. }
  324. void GUITabbedTitleBar::tabDragged(UINT32 tabIdx, const Vector2I& dragPos)
  325. {
  326. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  327. if(idx != -1)
  328. {
  329. Rect2I bounds = _getLayoutData().area;
  330. if(bounds.contains(dragPos))
  331. {
  332. if(!mDragInProgress)
  333. startDrag(idx, dragPos);
  334. mDragBtnOffset = dragPos.x - mInitialDragOffset;
  335. for(INT32 i = 0; i < idx; i++)
  336. {
  337. UINT32 width = mTabButtons[i]->_getLayoutData().area.width;
  338. INT32 centerX = mTabButtons[i]->_getLayoutData().area.x + width / 2;
  339. if(dragPos.x < centerX)
  340. {
  341. GUITabButton* temp = mTabButtons[i];
  342. mTabButtons[i] = mTabButtons[idx];
  343. mTabButtons[idx] = temp;
  344. break;
  345. }
  346. }
  347. for(UINT32 i = idx + 1; i < (UINT32)mTabButtons.size(); i++)
  348. {
  349. UINT32 width = mTabButtons[i]->_getLayoutData().area.width;
  350. INT32 centerX = mTabButtons[i]->_getLayoutData().area.x + width / 2;
  351. if(dragPos.x > centerX)
  352. {
  353. GUITabButton* temp = mTabButtons[i];
  354. mTabButtons[i] = mTabButtons[idx];
  355. mTabButtons[idx] = temp;
  356. break;
  357. }
  358. }
  359. _markContentAsDirty();
  360. }
  361. else
  362. {
  363. endDrag();
  364. _markContentAsDirty();
  365. if(!onTabDraggedOff.empty())
  366. onTabDraggedOff(tabIdx);
  367. }
  368. }
  369. }
  370. void GUITabbedTitleBar::tabDragEnd(UINT32 tabIdx, const Vector2I& dragPos)
  371. {
  372. endDrag();
  373. if(mActiveTabIdx != tabIdx)
  374. tabToggled(tabIdx, true);
  375. _markContentAsDirty();
  376. }
  377. INT32 GUITabbedTitleBar::uniqueIdxToSeqIdx(UINT32 uniqueIdx) const
  378. {
  379. UINT32 idx = 0;
  380. for(auto& tab : mTabButtons)
  381. {
  382. if(tab->getIndex() == uniqueIdx)
  383. return idx;
  384. idx++;
  385. }
  386. return -1;
  387. }
  388. const String& GUITabbedTitleBar::getGUITypeName()
  389. {
  390. static String typeName = "TabbedTitleBar";
  391. return typeName;
  392. }
  393. }