BsGUITabbedTitleBar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "CmMath.h"
  12. #include "CmPlatform.h"
  13. using namespace CamelotFramework;
  14. using namespace BansheeEngine;
  15. namespace BansheeEditor
  16. {
  17. const UINT32 GUITabbedTitleBar::TAB_SPACING = 20;
  18. const UINT32 GUITabbedTitleBar::OPTION_BTN_SPACING = 3;
  19. GUITabbedTitleBar::GUITabbedTitleBar(GUIWidget& parent, RenderWindow* parentWindow, GUIElementStyle* backgroundStyle, GUIElementStyle* tabBtnStyle,
  20. GUIElementStyle* minBtnStyle, GUIElementStyle* closeBtnStyle, const GUILayoutOptions& layoutOptions)
  21. :GUIElement(parent, &GUISkin::DefaultStyle, layoutOptions, false), mParentWindow(parentWindow), mMinBtn(nullptr),
  22. mCloseBtn(nullptr), mParentWidget(&parent), mBackgroundImage(nullptr), mUniqueTabIdx(0), mActiveTabIdx(0),
  23. mDragInProgress(false), mDraggedBtn(nullptr), mDragBtnOffset(0), mInitialDragOffset(0), mBackgroundStyle(backgroundStyle),
  24. mTabBtnStyle(tabBtnStyle), mMinimizeBtnStyle(minBtnStyle), mCloseBtnStyle(closeBtnStyle)
  25. {
  26. if(mBackgroundStyle == nullptr)
  27. mBackgroundStyle = parent.getSkin().getStyle("TitleBarBackground");
  28. if(mMinimizeBtnStyle == nullptr)
  29. mMinimizeBtnStyle = parent.getSkin().getStyle("WinMinimizeBtn");
  30. if(mCloseBtnStyle == nullptr)
  31. mCloseBtnStyle = parent.getSkin().getStyle("WinCloseBtn");
  32. if(mTabBtnStyle == nullptr)
  33. mTabBtnStyle = parent.getSkin().getStyle("TabbedBarBtn");
  34. mBackgroundImage = GUITexture::create(parent, mBackgroundStyle);
  35. mMinBtn = GUIButton::create(parent, HString(L""), mMinimizeBtnStyle);
  36. mCloseBtn = GUIButton::create(parent, HString(L""), mCloseBtnStyle);
  37. mCloseBtn->onClick.connect(boost::bind(&GUITabbedTitleBar::tabClosed, this));
  38. mTabToggleGroup = GUIToggle::createToggleGroup();
  39. }
  40. GUITabbedTitleBar::~GUITabbedTitleBar()
  41. {
  42. GUIElement::destroy(mBackgroundImage);
  43. GUIElement::destroy(mMinBtn);
  44. GUIElement::destroy(mCloseBtn);
  45. for(auto& tabButton : mTabButtons)
  46. {
  47. GUIElement::destroy(tabButton);
  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. void GUITabbedTitleBar::insertTab(UINT32 idx, const CM::HString& name)
  72. {
  73. GUITabButton* newTabToggle = GUITabButton::create(*mParentWidget, mTabToggleGroup, mUniqueTabIdx, name, mTabBtnStyle);
  74. idx = Math::clamp(idx, 0U, (UINT32)mTabButtons.size());
  75. newTabToggle->onToggled.connect(boost::bind(&GUITabbedTitleBar::tabToggled, this, mUniqueTabIdx));
  76. newTabToggle->onDragged.connect(boost::bind(&GUITabbedTitleBar::tabDragged, this, _1, _2));
  77. newTabToggle->onDragEnd.connect(boost::bind(&GUITabbedTitleBar::tabDragEnd, this, _1, _2));
  78. mTabButtons.insert(mTabButtons.begin() + idx, newTabToggle);
  79. mUniqueTabIdx++;
  80. }
  81. void GUITabbedTitleBar::removeTab(UINT32 idx)
  82. {
  83. if(mTabButtons.size() == 0)
  84. return;
  85. idx = Math::clamp(idx, 0U, (UINT32)mTabButtons.size() - 1);
  86. GUIElement::destroy(mTabButtons[idx]);
  87. mTabButtons.erase(mTabButtons.begin() + idx);
  88. }
  89. UINT32 GUITabbedTitleBar::getNumRenderElements() const
  90. {
  91. return 0;
  92. }
  93. const GUIMaterialInfo& GUITabbedTitleBar::getMaterial(UINT32 renderElementIdx) const
  94. {
  95. CM_EXCEPT(InvalidStateException, "Trying to retrieve a material from an element with no render elements.");
  96. }
  97. UINT32 GUITabbedTitleBar::getNumQuads(UINT32 renderElementIdx) const
  98. {
  99. return 0;
  100. }
  101. void GUITabbedTitleBar::updateClippedBounds()
  102. {
  103. mClippedBounds = RectI(0, 0, 0, 0); // We don't want any mouse input for this element. This is just a container.
  104. }
  105. void GUITabbedTitleBar::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  106. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  107. { }
  108. bool GUITabbedTitleBar::mouseEvent(const GUIMouseEvent& ev)
  109. {
  110. // TODO
  111. return false;
  112. }
  113. Vector2I GUITabbedTitleBar::_getOptimalSize() const
  114. {
  115. return Vector2I();
  116. }
  117. void GUITabbedTitleBar::_changeParentWidget(GUIWidget* widget)
  118. {
  119. GUIElement::_changeParentWidget(widget);
  120. mBackgroundImage->_changeParentWidget(widget);
  121. mMinBtn->_changeParentWidget(widget);
  122. mCloseBtn->_changeParentWidget(widget);
  123. for(auto& tabButton : mTabButtons)
  124. {
  125. tabButton->_changeParentWidget(widget);
  126. }
  127. }
  128. void GUITabbedTitleBar::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  129. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  130. {
  131. CM::Vector<CM::RectI>::type nonClientAreas;
  132. {
  133. Vector2I optimalSize = mBackgroundImage->_getOptimalSize();
  134. Vector2I offset(x + 1, y + 1);
  135. mBackgroundImage->_setOffset(offset);
  136. mBackgroundImage->_setWidth(width - 2);
  137. mBackgroundImage->_setHeight(optimalSize.y);
  138. mBackgroundImage->_setAreaDepth(areaDepth + 1);
  139. mBackgroundImage->_setWidgetDepth(widgetDepth);
  140. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  141. mBackgroundImage->_setClipRect(elemClipRect);
  142. }
  143. UINT32 curX = x + 1;
  144. UINT32 curY = y;
  145. UINT32 tabBtnHeight = 0;
  146. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  147. {
  148. GUITabButton* btn = mTabButtons[i];
  149. Vector2I optimalSize = btn->_getOptimalSize();
  150. tabBtnHeight = optimalSize.y;
  151. nonClientAreas.push_back(RectI(curX, curY, TAB_SPACING, tabBtnHeight));
  152. curX += TAB_SPACING;
  153. Vector2I offset(curX, curY);
  154. btn->_setOffset(offset);
  155. btn->_setWidth(optimalSize.x);
  156. btn->_setHeight(optimalSize.y);
  157. btn->_setAreaDepth(areaDepth);
  158. btn->_setWidgetDepth(widgetDepth);
  159. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  160. btn->_setClipRect(elemClipRect);
  161. curX += optimalSize.x;
  162. }
  163. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  164. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  165. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  166. UINT32 remainingWidth = (UINT32)std::max(0, (INT32)(width - curX - endButtonWidth - 1));
  167. nonClientAreas.push_back(RectI(curX, curY, remainingWidth, tabBtnHeight));
  168. INT32 optionBtnXPos = x + width - endButtonWidth - 1;
  169. {
  170. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - minBtnOptimalSize.y) * 0.5f);
  171. Vector2I offset(optionBtnXPos, optionBtnYPos);
  172. mMinBtn->_setOffset(offset);
  173. mMinBtn->_setWidth(minBtnOptimalSize.x);
  174. mMinBtn->_setHeight(minBtnOptimalSize.y);
  175. mMinBtn->_setAreaDepth(areaDepth);
  176. mMinBtn->_setWidgetDepth(widgetDepth);
  177. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  178. mMinBtn->_setClipRect(elemClipRect);
  179. }
  180. optionBtnXPos += minBtnOptimalSize.x + OPTION_BTN_SPACING;
  181. {
  182. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - closeBtnOptimalSize.y) * 0.5f);
  183. Vector2I offset(optionBtnXPos, optionBtnYPos);
  184. mCloseBtn->_setOffset(offset);
  185. mCloseBtn->_setWidth(closeBtnOptimalSize.x);
  186. mCloseBtn->_setHeight(closeBtnOptimalSize.y);
  187. mCloseBtn->_setAreaDepth(areaDepth);
  188. mCloseBtn->_setWidgetDepth(widgetDepth);
  189. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  190. mCloseBtn->_setClipRect(elemClipRect);
  191. }
  192. Platform::setCaptionNonClientAreas(*mParentWindow, nonClientAreas);
  193. }
  194. void GUITabbedTitleBar::tabToggled(CM::UINT32 tabIdx)
  195. {
  196. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  197. if(idx != -1)
  198. {
  199. if(!onTabActivated.empty())
  200. onTabActivated(idx);
  201. }
  202. mActiveTabIdx = tabIdx;
  203. }
  204. void GUITabbedTitleBar::tabClosed()
  205. {
  206. INT32 idx = uniqueIdxToSeqIdx(mActiveTabIdx);
  207. if(idx != -1)
  208. {
  209. removeTab(idx);
  210. if(mTabButtons.size() > 0)
  211. mActiveTabIdx = mTabButtons[0]->getIndex();
  212. if(!onTabClosed.empty())
  213. onTabClosed(idx);
  214. }
  215. }
  216. void GUITabbedTitleBar::tabDragged(CM::UINT32 tabIdx, const Vector2I& dragPos)
  217. {
  218. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  219. if(idx != -1)
  220. {
  221. RectI bounds = getBounds();
  222. if(bounds.contains(dragPos))
  223. {
  224. mDraggedBtn = mTabButtons[idx];
  225. if(!mDragInProgress)
  226. {
  227. Vector2I offset = mDraggedBtn->_getOffset();
  228. mInitialDragOffset = (dragPos.x - offset.x);
  229. mDragInProgress = true;
  230. }
  231. mDragBtnOffset = dragPos.x - mInitialDragOffset;
  232. Vector2I offset = mDraggedBtn->_getOffset();
  233. INT32 diff = mDragBtnOffset - offset.x;
  234. offset.x += diff;
  235. mDraggedBtn->_setOffset(offset);
  236. RectI clipRect = mDraggedBtn->_getClipRect();
  237. clipRect.x -= diff;
  238. mDraggedBtn->_setClipRect(clipRect);
  239. mDragInProgress = true;
  240. for(INT32 i = 0; i < idx; i++)
  241. {
  242. UINT32 width = mTabButtons[i]->_getWidth();
  243. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  244. if(dragPos.x < centerX)
  245. {
  246. GUITabButton* temp = mTabButtons[i];
  247. mTabButtons[i] = mTabButtons[idx];
  248. mTabButtons[idx] = temp;
  249. break;
  250. }
  251. }
  252. for(UINT32 i = idx + 1; i < (UINT32)mTabButtons.size(); i++)
  253. {
  254. UINT32 width = mTabButtons[i]->_getWidth();
  255. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  256. if(dragPos.x > centerX)
  257. {
  258. GUITabButton* temp = mTabButtons[i];
  259. mTabButtons[i] = mTabButtons[idx];
  260. mTabButtons[idx] = temp;
  261. break;
  262. }
  263. }
  264. }
  265. else
  266. {
  267. mDragInProgress = false;
  268. mDraggedBtn = nullptr;
  269. if(!onTabDraggedOff.empty())
  270. onTabDraggedOff(idx);
  271. }
  272. }
  273. }
  274. void GUITabbedTitleBar::tabDragEnd(CM::UINT32 tabIdx, const Vector2I& dragPos)
  275. {
  276. mDragInProgress = false;
  277. mDraggedBtn = nullptr;
  278. markContentAsDirty();
  279. }
  280. void GUITabbedTitleBar::tabDraggedOn(CM::UINT32 tabIdx)
  281. {
  282. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  283. if(idx != -1)
  284. {
  285. if(!onTabDraggedOn.empty())
  286. onTabDraggedOn(idx + 1);
  287. }
  288. }
  289. CM::INT32 GUITabbedTitleBar::uniqueIdxToSeqIdx(CM::UINT32 uniqueIdx) const
  290. {
  291. UINT32 idx = 0;
  292. for(auto& tab : mTabButtons)
  293. {
  294. if(tab->getIndex() == uniqueIdx)
  295. return idx;
  296. idx++;
  297. }
  298. return -1;
  299. }
  300. const String& GUITabbedTitleBar::getGUITypeName()
  301. {
  302. static String typeName = "TabbedTitleBar";
  303. return typeName;
  304. }
  305. }