BsGUITabbedTitleBar.cpp 14 KB

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