BsGUITabbedTitleBar.cpp 14 KB

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