BsGUITabbedTitleBar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. childData.clipRect.x -= childData.area.x;
  201. childData.clipRect.y -= childData.area.y;
  202. mBackgroundImage->_setLayoutData(childData);
  203. }
  204. UINT32 curX = data.area.x + 1;
  205. UINT32 curY = data.area.y;
  206. UINT32 tabBtnHeight = 0;
  207. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  208. {
  209. GUITabButton* btn = mTabButtons[i];
  210. Vector2I optimalSize = btn->_getOptimalSize();
  211. tabBtnHeight = optimalSize.y;
  212. curX += TAB_SPACING;
  213. Vector2I offset;
  214. if(!mDragInProgress || mDraggedBtn != btn)
  215. {
  216. offset = Vector2I(curX, curY);
  217. }
  218. else if(mDragInProgress && mDraggedBtn == btn)
  219. {
  220. offset.x = mDragBtnOffset;
  221. offset.y = btn->_getLayoutData().area.y;
  222. }
  223. GUILayoutData childData = data;
  224. childData.area.x = offset.x;
  225. childData.area.y = offset.y;
  226. childData.area.width = optimalSize.x;
  227. childData.area.height = optimalSize.y;
  228. childData.clipRect = tabClipRect;
  229. childData.clipRect.x -= childData.area.x;
  230. childData.clipRect.y -= childData.area.y;
  231. btn->_setLayoutData(childData);
  232. curX += optimalSize.x;
  233. }
  234. INT32 optionBtnXPos = data.area.x + data.area.width - endButtonWidth - 1;
  235. {
  236. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - minBtnOptimalSize.y) * 0.5f);
  237. Vector2I offset(optionBtnXPos, optionBtnYPos);
  238. GUILayoutData childData = data;
  239. childData.area.x = offset.x;
  240. childData.area.y = offset.y;
  241. childData.area.width = minBtnOptimalSize.x;
  242. childData.area.height = minBtnOptimalSize.y;
  243. childData.clipRect.x -= childData.area.x;
  244. childData.clipRect.y -= childData.area.y;
  245. mMinBtn->_setLayoutData(childData);
  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. GUILayoutData childData = data;
  252. childData.area.x = offset.x;
  253. childData.area.y = offset.y;
  254. childData.area.width = closeBtnOptimalSize.x;
  255. childData.area.height = closeBtnOptimalSize.y;
  256. childData.clipRect.x -= childData.area.x;
  257. childData.clipRect.y -= childData.area.y;
  258. mCloseBtn->_setLayoutData(childData);
  259. }
  260. }
  261. Vector<Rect2I> GUITabbedTitleBar::calcDraggableAreas(INT32 x, INT32 y, UINT32 width, UINT32 height) const
  262. {
  263. Vector<Rect2I> draggableAreas;
  264. UINT32 curX = x + 1;
  265. UINT32 curY = y;
  266. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  267. {
  268. GUITabButton* btn = mTabButtons[i];
  269. Vector2I optimalSize = btn->_getOptimalSize();
  270. draggableAreas.push_back(Rect2I(curX, curY, TAB_SPACING, height));
  271. curX += TAB_SPACING + optimalSize.x;
  272. }
  273. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  274. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  275. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  276. UINT32 remainingWidth = (UINT32)std::max(0, (INT32)(width - curX - endButtonWidth - 1));
  277. if(remainingWidth > 0)
  278. draggableAreas.push_back(Rect2I(curX, curY, remainingWidth, height));
  279. return draggableAreas;
  280. }
  281. void GUITabbedTitleBar::tabToggled(UINT32 tabIdx, bool toggledOn)
  282. {
  283. if(!toggledOn)
  284. return;
  285. if(!onTabActivated.empty())
  286. onTabActivated(tabIdx);
  287. mActiveTabIdx = tabIdx;
  288. }
  289. void GUITabbedTitleBar::tabClosed()
  290. {
  291. removeTab(mActiveTabIdx);
  292. if(!onTabClosed.empty())
  293. onTabClosed(mActiveTabIdx);
  294. if(mTabButtons.size() > 0)
  295. mActiveTabIdx = mTabButtons[0]->getIndex();
  296. }
  297. void GUITabbedTitleBar::startDrag(UINT32 seqIdx, const Vector2I& startDragPos)
  298. {
  299. if(!mDragInProgress)
  300. {
  301. for(auto& btn : mTabButtons)
  302. btn->_setDraggedState(true);
  303. mDraggedBtn = mTabButtons[seqIdx];
  304. mInitialDragOffset = (startDragPos.x - mDraggedBtn->_getLayoutData().area.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 = _getLayoutData().area;
  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]->_getLayoutData().area.width;
  330. INT32 centerX = mTabButtons[i]->_getLayoutData().area.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]->_getLayoutData().area.width;
  342. INT32 centerX = mTabButtons[i]->_getLayoutData().area.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. }