BsGUITabbedTitleBar.cpp 13 KB

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