BsGUITabbedTitleBar.cpp 13 KB

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