BsGUIDropDownMenu.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIDropDownMenu.h"
  4. #include "BsGUIPanel.h"
  5. #include "BsGUILayoutY.h"
  6. #include "BsGUILayoutX.h"
  7. #include "BsGUITexture.h"
  8. #include "BsGUILabel.h"
  9. #include "BsGUIButton.h"
  10. #include "BsGUISpace.h"
  11. #include "BsGUIContent.h"
  12. #include "BsGUISkin.h"
  13. #include "BsViewport.h"
  14. #include "BsGUIListBox.h"
  15. #include "BsGUIDropDownBoxManager.h"
  16. #include "BsSceneObject.h"
  17. #include "BsGUIDropDownHitBox.h"
  18. #include "BsGUIDropDownContent.h"
  19. #include "BsCamera.h"
  20. #include "BsDebug.h"
  21. using namespace std::placeholders;
  22. namespace BansheeEngine
  23. {
  24. const UINT32 GUIDropDownMenu::DROP_DOWN_BOX_WIDTH = 250;
  25. GUIDropDownDataEntry GUIDropDownDataEntry::separator()
  26. {
  27. GUIDropDownDataEntry data;
  28. data.mType = Type::Separator;
  29. data.mCallback = nullptr;
  30. return data;
  31. }
  32. GUIDropDownDataEntry GUIDropDownDataEntry::button(const WString& label, std::function<void()> callback, const WString& shortcutTag)
  33. {
  34. GUIDropDownDataEntry data;
  35. data.mLabel = label;
  36. data.mType = Type::Entry;
  37. data.mCallback = callback;
  38. data.mShortcutTag = shortcutTag;
  39. return data;
  40. }
  41. GUIDropDownDataEntry GUIDropDownDataEntry::subMenu(const WString& label, const GUIDropDownData& data)
  42. {
  43. GUIDropDownDataEntry dataEntry;
  44. dataEntry.mLabel = label;
  45. dataEntry.mType = Type::SubMenu;
  46. dataEntry.mChildData = data;
  47. return dataEntry;
  48. }
  49. GUIDropDownMenu::GUIDropDownMenu(const HSceneObject& parent, const DROP_DOWN_BOX_DESC& desc, GUIDropDownType type)
  50. :CGUIWidget(parent, desc.camera), mRootMenu(nullptr), mFrontHitBox(nullptr), mCaptureHitBox(nullptr), mBackHitBox(nullptr)
  51. {
  52. String stylePrefix = "";
  53. switch(type)
  54. {
  55. case GUIDropDownType::ContextMenu:
  56. stylePrefix = "ContextMenu";
  57. break;
  58. case GUIDropDownType::ListBox:
  59. case GUIDropDownType::MultiListBox:
  60. stylePrefix = "ListBox";
  61. break;
  62. case GUIDropDownType::MenuBar:
  63. stylePrefix = "MenuBar";
  64. break;
  65. }
  66. mScrollUpStyle = stylePrefix + "ScrollUpBtn";
  67. mScrollDownStyle = stylePrefix + "ScrollDownBtn";
  68. mBackgroundStyle = stylePrefix + "Frame";
  69. mContentStyle = stylePrefix + "Content";
  70. mSideBackgroundStyle = stylePrefix + "SidebarBg";
  71. mHandleStyle = stylePrefix + "Handle";
  72. setDepth(0); // Needs to be in front of everything
  73. setSkin(desc.skin);
  74. mFrontHitBox = GUIDropDownHitBox::create(false, false);
  75. mFrontHitBox->onFocusLost.connect(std::bind(&GUIDropDownMenu::dropDownFocusLost, this));
  76. mFrontHitBox->setFocus(true);
  77. GUILayoutData hitboxLayoutData = mFrontHitBox->_getLayoutData();
  78. hitboxLayoutData.setWidgetDepth(0);
  79. hitboxLayoutData.setPanelDepth(std::numeric_limits<INT16>::min());
  80. mFrontHitBox->_setLayoutData(hitboxLayoutData);
  81. mFrontHitBox->_changeParentWidget(_getInternal());
  82. mFrontHitBox->_markLayoutAsDirty();
  83. mBackHitBox = GUIDropDownHitBox::create(false, true);
  84. GUILayoutData backHitboxLayoutData = mBackHitBox->_getLayoutData();
  85. backHitboxLayoutData.setWidgetDepth(0);
  86. backHitboxLayoutData.setPanelDepth(std::numeric_limits<INT16>::max());
  87. mBackHitBox->_setLayoutData(backHitboxLayoutData);
  88. mBackHitBox->_changeParentWidget(_getInternal());
  89. mBackHitBox->_markLayoutAsDirty();
  90. ViewportPtr viewport = desc.camera->getViewport();
  91. Rect2I targetBounds(0, 0, viewport->getWidth(), viewport->getHeight());
  92. Vector<Rect2I> captureBounds;
  93. targetBounds.cut(desc.additionalBounds, captureBounds);
  94. mCaptureHitBox = GUIDropDownHitBox::create(true, false);
  95. mCaptureHitBox->setBounds(captureBounds);
  96. GUILayoutData captureHitboxLayoutData = mCaptureHitBox->_getLayoutData();
  97. captureHitboxLayoutData.setWidgetDepth(0);
  98. captureHitboxLayoutData.setPanelDepth(std::numeric_limits<INT16>::max());
  99. mCaptureHitBox->_setLayoutData(captureHitboxLayoutData);
  100. mCaptureHitBox->_changeParentWidget(_getInternal());
  101. mCaptureHitBox->_markLayoutAsDirty();
  102. mAdditionalCaptureBounds = desc.additionalBounds;
  103. Rect2I availableBounds(viewport->getX(), viewport->getY(), viewport->getWidth(), viewport->getHeight());
  104. mRootMenu = bs_new<DropDownSubMenu>(this, nullptr, desc.placement, availableBounds, desc.dropDownData, type, 0);
  105. }
  106. GUIDropDownMenu::~GUIDropDownMenu()
  107. {
  108. }
  109. void GUIDropDownMenu::onDestroyed()
  110. {
  111. GUIElement::destroy(mFrontHitBox);
  112. GUIElement::destroy(mBackHitBox);
  113. GUIElement::destroy(mCaptureHitBox);
  114. bs_delete(mRootMenu);
  115. mRootMenu = nullptr;
  116. CGUIWidget::onDestroyed();
  117. }
  118. void GUIDropDownMenu::dropDownFocusLost()
  119. {
  120. mRootMenu->closeSubMenu();
  121. GUIDropDownBoxManager::instance().closeDropDownBox();
  122. }
  123. void GUIDropDownMenu::notifySubMenuOpened(DropDownSubMenu* subMenu)
  124. {
  125. Vector<Rect2I> bounds;
  126. while(subMenu != nullptr)
  127. {
  128. bounds.push_back(subMenu->getVisibleBounds());
  129. subMenu = subMenu->mParent;
  130. }
  131. mBackHitBox->setBounds(bounds);
  132. for (auto& additionalBound : mAdditionalCaptureBounds)
  133. bounds.push_back(additionalBound);
  134. mFrontHitBox->setBounds(bounds);
  135. }
  136. void GUIDropDownMenu::notifySubMenuClosed(DropDownSubMenu* subMenu)
  137. {
  138. Vector<Rect2I> bounds;
  139. while(subMenu != nullptr)
  140. {
  141. bounds.push_back(subMenu->getVisibleBounds());
  142. subMenu = subMenu->mParent;
  143. }
  144. mBackHitBox->setBounds(bounds);
  145. for (auto& additionalBound : mAdditionalCaptureBounds)
  146. bounds.push_back(additionalBound);
  147. mFrontHitBox->setBounds(bounds);
  148. }
  149. GUIDropDownMenu::DropDownSubMenu::DropDownSubMenu(GUIDropDownMenu* owner, DropDownSubMenu* parent, const DropDownAreaPlacement& placement,
  150. const Rect2I& availableBounds, const GUIDropDownData& dropDownData, GUIDropDownType type, UINT32 depthOffset)
  151. :mOwner(owner), mParent(parent), mPage(0), mBackgroundFrame(nullptr), mBackgroundPanel(nullptr), mContentPanel(nullptr),
  152. mContentLayout(nullptr), x(0), y(0), width(0), height(0), mSidebarPanel(nullptr), mType(type), mSubMenu(nullptr),
  153. mData(dropDownData), mOpenedUpward(false), mDepthOffset(depthOffset), mContent(nullptr)
  154. {
  155. mAvailableBounds = availableBounds;
  156. const GUIElementStyle* backgroundStyle = mOwner->getSkin().getStyle(mOwner->mBackgroundStyle);
  157. const GUIElementStyle* sideBarStyle = mOwner->getSkin().getStyle(mOwner->mSideBackgroundStyle);
  158. // Create content GUI element
  159. mContent = GUIDropDownContent::create(this, dropDownData, mOwner->mContentStyle);
  160. mContent->setKeyboardFocus(true);
  161. // Content area
  162. mContentPanel = mOwner->getPanel()->addNewElement<GUIPanel>();
  163. mContentPanel->setWidth(width);
  164. mContentPanel->setHeight(height);
  165. mContentPanel->setDepthRange(100 - depthOffset * 2 - 1);
  166. // Background frame
  167. mBackgroundPanel = mOwner->getPanel()->addNewElement<GUIPanel>();
  168. mBackgroundPanel->setWidth(width);
  169. mBackgroundPanel->setHeight(height);
  170. mBackgroundPanel->setDepthRange(100 - depthOffset * 2);
  171. GUILayout* backgroundLayout = mBackgroundPanel->addNewElement<GUILayoutX>();
  172. mBackgroundFrame = GUITexture::create(GUIImageScaleMode::StretchToFit, mOwner->mBackgroundStyle);
  173. backgroundLayout->addElement(mBackgroundFrame);
  174. mContentLayout = mContentPanel->addNewElement<GUILayoutY>();
  175. mContentLayout->addElement(mContent); // Note: It's important this is added to the layout before we
  176. // use it for size calculations, in order for its skin to be assigned
  177. UINT32 dropDownBoxWidth = DROP_DOWN_BOX_WIDTH + sideBarStyle->width;
  178. UINT32 maxNeededHeight = backgroundStyle->margins.top + backgroundStyle->margins.bottom;
  179. UINT32 numElements = (UINT32)dropDownData.entries.size();
  180. for (UINT32 i = 0; i < numElements; i++)
  181. maxNeededHeight += mContent->getElementHeight(i);
  182. DropDownAreaPlacement::HorzDir horzDir;
  183. DropDownAreaPlacement::VertDir vertDir;
  184. Rect2I placementBounds = placement.getOptimalBounds(dropDownBoxWidth, maxNeededHeight, availableBounds, horzDir, vertDir);
  185. mOpenedUpward = vertDir == DropDownAreaPlacement::VertDir::Up;
  186. UINT32 actualY = placementBounds.y;
  187. if (mOpenedUpward)
  188. y = placementBounds.y + placementBounds.height;
  189. else
  190. y = placementBounds.y;
  191. x = placementBounds.x;
  192. width = placementBounds.width;
  193. height = placementBounds.height;
  194. mContentPanel->setPosition(x, actualY);
  195. mBackgroundPanel->setPosition(x, actualY);
  196. updateGUIElements();
  197. mOwner->notifySubMenuOpened(this);
  198. }
  199. GUIDropDownMenu::DropDownSubMenu::~DropDownSubMenu()
  200. {
  201. closeSubMenu();
  202. mOwner->notifySubMenuClosed(this);
  203. GUIElement::destroy(mContent);
  204. GUIElement::destroy(mBackgroundFrame);
  205. GUILayout::destroy(mBackgroundPanel);
  206. GUILayout::destroy(mContentPanel);
  207. if (mSidebarPanel != nullptr)
  208. GUIPanel::destroy(mSidebarPanel);
  209. }
  210. Vector<GUIDropDownMenu::DropDownSubMenu::PageInfo> GUIDropDownMenu::DropDownSubMenu::getPageInfos() const
  211. {
  212. const GUIElementStyle* backgroundStyle = mOwner->getSkin().getStyle(mOwner->mBackgroundStyle);
  213. INT32 numElements = (INT32)mData.entries.size();
  214. PageInfo curPageInfo;
  215. curPageInfo.start = 0;
  216. curPageInfo.end = 0;
  217. curPageInfo.idx = 0;
  218. curPageInfo.height = backgroundStyle->margins.top + backgroundStyle->margins.bottom;
  219. Vector<PageInfo> pageInfos;
  220. for (INT32 i = 0; i < numElements; i++)
  221. {
  222. curPageInfo.height += mContent->getElementHeight((UINT32)i);
  223. curPageInfo.end++;
  224. if (curPageInfo.height > height)
  225. {
  226. // Remove last few elements until we fit again
  227. while (curPageInfo.height > height && i >= 0)
  228. {
  229. curPageInfo.height -= mContent->getElementHeight((UINT32)i);
  230. curPageInfo.end--;
  231. i--;
  232. }
  233. // Nothing fits, break out of infinite loop
  234. if (curPageInfo.start >= curPageInfo.end)
  235. break;
  236. pageInfos.push_back(curPageInfo);
  237. curPageInfo.start = curPageInfo.end;
  238. curPageInfo.height = backgroundStyle->margins.top + backgroundStyle->margins.bottom;
  239. curPageInfo.idx++;
  240. }
  241. }
  242. if (curPageInfo.start < curPageInfo.end)
  243. pageInfos.push_back(curPageInfo);
  244. return pageInfos;
  245. }
  246. void GUIDropDownMenu::DropDownSubMenu::updateGUIElements()
  247. {
  248. // Remove all elements from content layout
  249. while(mContentLayout->getNumChildren() > 0)
  250. mContentLayout->removeElementAt(mContentLayout->getNumChildren() - 1);
  251. mContentLayout->addElement(mContent); // Note: Needs to be added first so that size calculations have proper skin to work with
  252. const GUIElementStyle* backgroundStyle = mOwner->getSkin().getStyle(mOwner->mBackgroundStyle);
  253. const GUIElementStyle* sideBarStyle = mOwner->getSkin().getStyle(mOwner->mSideBackgroundStyle);
  254. const GUIElementStyle* scrollUpStyle = mOwner->getSkin().getStyle(mOwner->mScrollUpStyle);
  255. const GUIElementStyle* scrollDownStyle = mOwner->getSkin().getStyle(mOwner->mScrollDownStyle);
  256. const GUIElementStyle* handleStyle = mOwner->getSkin().getStyle(mOwner->mHandleStyle);
  257. Vector<PageInfo> pageInfos = getPageInfos();
  258. UINT32 pageStart = 0, pageEnd = 0;
  259. UINT32 pageHeight = 0;
  260. UINT32 pageCount = (UINT32)pageInfos.size();
  261. if (pageCount > mPage)
  262. {
  263. pageStart = pageInfos[mPage].start;
  264. pageEnd = pageInfos[mPage].end;
  265. pageHeight = pageInfos[mPage].height;
  266. }
  267. INT32 actualY = y;
  268. if (mOpenedUpward)
  269. actualY -= (INT32)pageHeight;
  270. // Add sidebar if needed
  271. UINT32 contentOffset = 0;
  272. if (pageInfos.size() > 1)
  273. {
  274. UINT32 sidebarHeight = pageHeight - 2;
  275. contentOffset = sideBarStyle->width;
  276. if (mSidebarPanel == nullptr)
  277. {
  278. mSidebarPanel = mOwner->getPanel()->addNewElement<GUIPanel>();
  279. mScrollUpBtn = GUIButton::create(HString(L""), mOwner->mScrollUpStyle);
  280. mScrollUpBtn->onClick.connect(std::bind(&DropDownSubMenu::scrollUp, this));
  281. mScrollDownBtn = GUIButton::create(HString(L""), mOwner->mScrollDownStyle);
  282. mScrollDownBtn->onClick.connect(std::bind(&DropDownSubMenu::scrollDown, this));
  283. mHandle = GUITexture::create(mOwner->mHandleStyle);
  284. GUITexture* background = GUITexture::create(mOwner->mSideBackgroundStyle);
  285. background->_setElementDepth(2);
  286. mSidebarPanel->addElement(background);
  287. mSidebarPanel->addElement(mScrollUpBtn);
  288. mSidebarPanel->addElement(mScrollDownBtn);
  289. mSidebarPanel->addElement(mHandle);
  290. }
  291. mScrollUpBtn->setPosition(1, 1);
  292. mScrollDownBtn->setPosition(1, sidebarHeight - 1 - scrollDownStyle->height);
  293. UINT32 maxHandleSize = std::max(0, (INT32)sidebarHeight - (INT32)scrollDownStyle->height - (INT32)scrollUpStyle->height - 2);
  294. UINT32 handleSize = maxHandleSize / pageCount;
  295. INT32 handlePos = 1 + scrollUpStyle->height + mPage * handleSize;
  296. mHandle->setPosition(1, handlePos);
  297. mHandle->setHeight(handleSize);
  298. mSidebarPanel->setPosition(x, actualY);
  299. mSidebarPanel->setWidth(sideBarStyle->width);
  300. mSidebarPanel->setHeight(sidebarHeight);
  301. }
  302. else
  303. {
  304. if (mSidebarPanel != nullptr)
  305. {
  306. GUIPanel::destroy(mSidebarPanel);
  307. mSidebarPanel = nullptr;
  308. }
  309. }
  310. mContent->setRange(pageStart, pageEnd);
  311. if (mSubMenu == nullptr)
  312. mContent->setKeyboardFocus(true);
  313. // Resize and reposition areas
  314. mBackgroundPanel->setWidth(width - contentOffset);
  315. mBackgroundPanel->setHeight(pageHeight);
  316. mBackgroundPanel->setPosition(x + contentOffset, actualY);
  317. mVisibleBounds = Rect2I(x, actualY, width, pageHeight);
  318. UINT32 contentWidth = (UINT32)std::max(0, (INT32)width - (INT32)backgroundStyle->margins.left - (INT32)backgroundStyle->margins.right - (INT32)contentOffset);
  319. UINT32 contentHeight = (UINT32)std::max(0, (INT32)pageHeight - (INT32)backgroundStyle->margins.top - (INT32)backgroundStyle->margins.bottom);
  320. mContentPanel->setWidth(contentWidth);
  321. mContentPanel->setHeight(contentHeight);
  322. mContentPanel->setPosition(x + contentOffset + backgroundStyle->margins.left, actualY + backgroundStyle->margins.top);
  323. }
  324. void GUIDropDownMenu::DropDownSubMenu::scrollDown()
  325. {
  326. mPage++;
  327. if (mPage == (UINT32)getPageInfos().size())
  328. mPage = 0;
  329. updateGUIElements();
  330. closeSubMenu();
  331. }
  332. void GUIDropDownMenu::DropDownSubMenu::scrollUp()
  333. {
  334. if (mPage > 0)
  335. mPage--;
  336. else
  337. mPage = (UINT32)getPageInfos().size() - 1;
  338. updateGUIElements();
  339. closeSubMenu();
  340. }
  341. void GUIDropDownMenu::DropDownSubMenu::scrollToTop()
  342. {
  343. mPage = 0;
  344. updateGUIElements();
  345. closeSubMenu();
  346. }
  347. void GUIDropDownMenu::DropDownSubMenu::scrollToBottom()
  348. {
  349. mPage = (UINT32)(getPageInfos().size() - 1);
  350. updateGUIElements();
  351. closeSubMenu();
  352. }
  353. void GUIDropDownMenu::DropDownSubMenu::closeSubMenu()
  354. {
  355. if(mSubMenu != nullptr)
  356. {
  357. bs_delete(mSubMenu);
  358. mSubMenu = nullptr;
  359. mContent->setKeyboardFocus(true);
  360. }
  361. }
  362. void GUIDropDownMenu::DropDownSubMenu::elementActivated(UINT32 idx, const Rect2I& bounds)
  363. {
  364. closeSubMenu();
  365. if (!mData.entries[idx].isSubMenu())
  366. {
  367. auto callback = mData.entries[idx].getCallback();
  368. if (callback != nullptr)
  369. callback();
  370. if (mType != GUIDropDownType::MultiListBox)
  371. GUIDropDownBoxManager::instance().closeDropDownBox();
  372. }
  373. else
  374. {
  375. mContent->setKeyboardFocus(false);
  376. mSubMenu = bs_new<DropDownSubMenu>(mOwner, this, DropDownAreaPlacement::aroundBoundsVert(bounds),
  377. mAvailableBounds, mData.entries[idx].getSubMenuData(), mType, mDepthOffset + 1);
  378. }
  379. }
  380. void GUIDropDownMenu::DropDownSubMenu::close()
  381. {
  382. if (mParent != nullptr)
  383. mParent->closeSubMenu();
  384. else // We're the last sub-menu, close the whole thing
  385. GUIDropDownBoxManager::instance().closeDropDownBox();
  386. }
  387. void GUIDropDownMenu::DropDownSubMenu::elementSelected(UINT32 idx)
  388. {
  389. closeSubMenu();
  390. }
  391. }