BsGUITabbedTitleBar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. void GUITabbedTitleBar::insertTab(UINT32 idx, const CM::HString& name)
  72. {
  73. GUITabButton* newTabToggle = GUITabButton::create(*mParent, mTabToggleGroup, mUniqueTabIdx, name, mTabBtnStyle);
  74. _registerChildElement(newTabToggle);
  75. idx = Math::clamp(idx, 0U, (UINT32)mTabButtons.size());
  76. newTabToggle->onToggled.connect(boost::bind(&GUITabbedTitleBar::tabToggled, this, mUniqueTabIdx, _1));
  77. newTabToggle->onDragged.connect(boost::bind(&GUITabbedTitleBar::tabDragged, this, _1, _2));
  78. newTabToggle->onDragEnd.connect(boost::bind(&GUITabbedTitleBar::tabDragEnd, this, _1, _2));
  79. mTabButtons.insert(mTabButtons.begin() + idx, newTabToggle);
  80. mUniqueTabIdx++;
  81. }
  82. void GUITabbedTitleBar::removeTab(UINT32 idx)
  83. {
  84. if(mTabButtons.size() == 0)
  85. return;
  86. idx = Math::clamp(idx, 0U, (UINT32)mTabButtons.size() - 1);
  87. GUIElement::destroy(mTabButtons[idx]);
  88. mTabButtons.erase(mTabButtons.begin() + idx);
  89. }
  90. void GUITabbedTitleBar::setActive(UINT32 idx)
  91. {
  92. mTabButtons[idx]->toggleOn();
  93. }
  94. bool GUITabbedTitleBar::mouseEvent(const GUIMouseEvent& event)
  95. {
  96. if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  97. {
  98. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  99. return false;
  100. EditorWidget* draggedWidget = reinterpret_cast<EditorWidget*>(DragAndDropManager::instance().getDragData());
  101. const Vector2I& widgetRelPos = event.getPosition();
  102. if(mTempDraggedWidget == nullptr)
  103. {
  104. UINT32 numTabButtons = (UINT32)mTabButtons.size();
  105. for(UINT32 i = 0; i < numTabButtons; i++)
  106. {
  107. UINT32 width = mTabButtons[i]->_getWidth();
  108. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  109. if((i + 1) == numTabButtons)
  110. {
  111. if(i == 0 && widgetRelPos.x <= centerX)
  112. {
  113. insertTab(0, draggedWidget->getName());
  114. mTempDraggedTabIdx = mTabButtons[0]->getIndex();
  115. break;
  116. }
  117. else if(widgetRelPos.x > centerX)
  118. {
  119. addTab(draggedWidget->getName());
  120. mTempDraggedTabIdx = mTabButtons[i + 1]->getIndex();
  121. break;
  122. }
  123. }
  124. else
  125. {
  126. if(i == 0 && widgetRelPos.x <= centerX)
  127. {
  128. insertTab(0, draggedWidget->getName());
  129. mTempDraggedTabIdx = mTabButtons[0]->getIndex();
  130. break;
  131. }
  132. else
  133. {
  134. UINT32 nextWidth = mTabButtons[i + 1]->_getWidth();
  135. INT32 nextCenterX = mTabButtons[i + 1]->_getOffset().x + nextWidth / 2;
  136. if(widgetRelPos.x > centerX && widgetRelPos.x < nextCenterX)
  137. {
  138. insertTab(i + 1, draggedWidget->getName());
  139. mTempDraggedTabIdx = mTabButtons[i + 1]->getIndex();
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. mTempDraggedWidget = draggedWidget;
  146. mDraggedBtn = mTabButtons[uniqueIdxToSeqIdx(mTempDraggedTabIdx)];
  147. mDraggedBtn->_setDraggedState(true);
  148. mInitialDragOffset = Math::roundToInt(mDraggedBtn->_getWidth() * 0.5f);
  149. mDragInProgress = true;
  150. }
  151. if(mTempDraggedWidget != nullptr)
  152. tabDragged(mTempDraggedTabIdx, widgetRelPos);
  153. return true;
  154. }
  155. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  156. {
  157. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  158. return false;
  159. EditorWidget* draggedWidget = reinterpret_cast<EditorWidget*>(DragAndDropManager::instance().getDragData());
  160. const Vector2I& widgetRelPos = event.getPosition();
  161. if(mTempDraggedWidget != nullptr)
  162. {
  163. UINT32 tabSeqIdx = uniqueIdxToSeqIdx(mTempDraggedTabIdx);
  164. removeTab(tabSeqIdx);
  165. mTempDraggedWidget = nullptr;
  166. mDragInProgress = false;
  167. mDraggedBtn = nullptr;
  168. if(!onTabDraggedOn.empty())
  169. onTabDraggedOn(tabSeqIdx);
  170. }
  171. return true;
  172. }
  173. else if(event.getType() == GUIMouseEventType::MouseOut)
  174. {
  175. if(mTempDraggedWidget != nullptr)
  176. {
  177. removeTab(uniqueIdxToSeqIdx(mTempDraggedTabIdx));
  178. mTempDraggedWidget = nullptr;
  179. mDragInProgress = false;
  180. mDraggedBtn = nullptr;
  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. CM::Vector<CM::RectI>::type nonClientAreas;
  194. {
  195. Vector2I optimalSize = mBackgroundImage->_getOptimalSize();
  196. Vector2I offset(x + 1, y + 1);
  197. mBackgroundImage->_setOffset(offset);
  198. mBackgroundImage->_setWidth(width - 2);
  199. mBackgroundImage->_setHeight(optimalSize.y);
  200. mBackgroundImage->_setAreaDepth(areaDepth + 2);
  201. mBackgroundImage->_setWidgetDepth(widgetDepth);
  202. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  203. mBackgroundImage->_setClipRect(elemClipRect);
  204. }
  205. UINT32 curX = x + 1;
  206. UINT32 curY = y;
  207. UINT32 tabBtnHeight = 0;
  208. for(UINT32 i = 0; i < (UINT32)mTabButtons.size(); i++)
  209. {
  210. GUITabButton* btn = mTabButtons[i];
  211. Vector2I optimalSize = btn->_getOptimalSize();
  212. tabBtnHeight = optimalSize.y;
  213. nonClientAreas.push_back(RectI(curX, curY, TAB_SPACING, tabBtnHeight));
  214. curX += TAB_SPACING;
  215. Vector2I offset;
  216. if(!mDragInProgress || mDraggedBtn != btn)
  217. {
  218. offset = Vector2I(curX, curY);
  219. }
  220. else if(mDragInProgress && mDraggedBtn == btn)
  221. {
  222. offset = btn->_getOffset();
  223. offset.x = mDragBtnOffset;
  224. }
  225. btn->_setOffset(offset);
  226. btn->_setWidth(optimalSize.x);
  227. btn->_setHeight(optimalSize.y);
  228. btn->_setAreaDepth(areaDepth + 1);
  229. btn->_setWidgetDepth(widgetDepth);
  230. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  231. btn->_setClipRect(elemClipRect);
  232. curX += optimalSize.x;
  233. }
  234. Vector2I minBtnOptimalSize = mMinBtn->_getOptimalSize();
  235. Vector2I closeBtnOptimalSize = mCloseBtn->_getOptimalSize();
  236. UINT32 endButtonWidth = minBtnOptimalSize.x + closeBtnOptimalSize.x + OPTION_BTN_SPACING;
  237. UINT32 remainingWidth = (UINT32)std::max(0, (INT32)(width - curX - endButtonWidth - 1));
  238. nonClientAreas.push_back(RectI(curX, curY, remainingWidth, tabBtnHeight));
  239. INT32 optionBtnXPos = x + width - endButtonWidth - 1;
  240. {
  241. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - minBtnOptimalSize.y) * 0.5f);
  242. Vector2I offset(optionBtnXPos, optionBtnYPos);
  243. mMinBtn->_setOffset(offset);
  244. mMinBtn->_setWidth(minBtnOptimalSize.x);
  245. mMinBtn->_setHeight(minBtnOptimalSize.y);
  246. mMinBtn->_setAreaDepth(areaDepth + 1);
  247. mMinBtn->_setWidgetDepth(widgetDepth);
  248. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  249. mMinBtn->_setClipRect(elemClipRect);
  250. }
  251. optionBtnXPos += minBtnOptimalSize.x + OPTION_BTN_SPACING;
  252. {
  253. INT32 optionBtnYPos = curY + Math::floorToInt((tabBtnHeight - closeBtnOptimalSize.y) * 0.5f);
  254. Vector2I offset(optionBtnXPos, optionBtnYPos);
  255. mCloseBtn->_setOffset(offset);
  256. mCloseBtn->_setWidth(closeBtnOptimalSize.x);
  257. mCloseBtn->_setHeight(closeBtnOptimalSize.y);
  258. mCloseBtn->_setAreaDepth(areaDepth + 1);
  259. mCloseBtn->_setWidgetDepth(widgetDepth);
  260. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  261. mCloseBtn->_setClipRect(elemClipRect);
  262. }
  263. Platform::setCaptionNonClientAreas(*mParentWindow, nonClientAreas);
  264. }
  265. void GUITabbedTitleBar::tabToggled(CM::UINT32 tabIdx, bool toggledOn)
  266. {
  267. if(!toggledOn)
  268. return;
  269. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  270. if(idx != -1)
  271. {
  272. if(!onTabActivated.empty())
  273. onTabActivated(idx);
  274. }
  275. mActiveTabIdx = tabIdx;
  276. }
  277. void GUITabbedTitleBar::tabClosed()
  278. {
  279. INT32 idx = uniqueIdxToSeqIdx(mActiveTabIdx);
  280. if(idx != -1)
  281. {
  282. removeTab(idx);
  283. if(mTabButtons.size() > 0)
  284. mActiveTabIdx = mTabButtons[0]->getIndex();
  285. if(!onTabClosed.empty())
  286. onTabClosed(idx);
  287. }
  288. }
  289. void GUITabbedTitleBar::tabDragged(CM::UINT32 tabIdx, const Vector2I& dragPos)
  290. {
  291. INT32 idx = uniqueIdxToSeqIdx(tabIdx);
  292. if(idx != -1)
  293. {
  294. RectI bounds = getBounds();
  295. if(bounds.contains(dragPos))
  296. {
  297. if(!mDragInProgress)
  298. {
  299. mDraggedBtn = mTabButtons[idx];
  300. mDraggedBtn->_setDraggedState(false);
  301. Vector2I offset = mDraggedBtn->_getOffset();
  302. mInitialDragOffset = (dragPos.x - offset.x);
  303. mDragInProgress = true;
  304. }
  305. mDragBtnOffset = dragPos.x - mInitialDragOffset;
  306. for(INT32 i = 0; i < idx; i++)
  307. {
  308. UINT32 width = mTabButtons[i]->_getWidth();
  309. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  310. if(dragPos.x < centerX)
  311. {
  312. GUITabButton* temp = mTabButtons[i];
  313. mTabButtons[i] = mTabButtons[idx];
  314. mTabButtons[idx] = temp;
  315. break;
  316. }
  317. }
  318. for(UINT32 i = idx + 1; i < (UINT32)mTabButtons.size(); i++)
  319. {
  320. UINT32 width = mTabButtons[i]->_getWidth();
  321. INT32 centerX = mTabButtons[i]->_getOffset().x + width / 2;
  322. if(dragPos.x > centerX)
  323. {
  324. GUITabButton* temp = mTabButtons[i];
  325. mTabButtons[i] = mTabButtons[idx];
  326. mTabButtons[idx] = temp;
  327. break;
  328. }
  329. }
  330. markContentAsDirty();
  331. }
  332. else
  333. {
  334. mDragInProgress = false;
  335. if(mDraggedBtn != nullptr)
  336. mDraggedBtn->_setDraggedState(false);
  337. mDraggedBtn = nullptr;
  338. markContentAsDirty();
  339. if(!onTabDraggedOff.empty())
  340. onTabDraggedOff(idx);
  341. }
  342. }
  343. }
  344. void GUITabbedTitleBar::tabDragEnd(CM::UINT32 tabIdx, const Vector2I& dragPos)
  345. {
  346. mDragInProgress = false;
  347. if(mDraggedBtn != nullptr)
  348. mDraggedBtn->_setDraggedState(false);
  349. mDraggedBtn = nullptr;
  350. if(mActiveTabIdx != tabIdx)
  351. tabToggled(tabIdx, true);
  352. markContentAsDirty();
  353. }
  354. CM::INT32 GUITabbedTitleBar::uniqueIdxToSeqIdx(CM::UINT32 uniqueIdx) const
  355. {
  356. UINT32 idx = 0;
  357. for(auto& tab : mTabButtons)
  358. {
  359. if(tab->getIndex() == uniqueIdx)
  360. return idx;
  361. idx++;
  362. }
  363. return -1;
  364. }
  365. const String& GUITabbedTitleBar::getGUITypeName()
  366. {
  367. static String typeName = "TabbedTitleBar";
  368. return typeName;
  369. }
  370. }