2
0

BsGUITabbedTitleBar.cpp 14 KB

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