BsGUIDropDownBox.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #include "BsGUIDropDownBox.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUITexture.h"
  5. #include "BsGUIButton.h"
  6. #include "BsGUIContent.h"
  7. #include "BsGUISkin.h"
  8. #include "BsViewport.h"
  9. #include "BsGUIListBox.h"
  10. #include "BsGUIDropDownBoxManager.h"
  11. #include "BsSceneObject.h"
  12. #include "BsGUIDropDownHitBox.h"
  13. namespace BansheeEngine
  14. {
  15. const UINT32 GUIDropDownBox::DROP_DOWN_BOX_WIDTH = 150;
  16. GUIDropDownDataEntry GUIDropDownDataEntry::separator()
  17. {
  18. GUIDropDownDataEntry data;
  19. data.mType = Type::Separator;
  20. data.mCallback = nullptr;
  21. return data;
  22. }
  23. GUIDropDownDataEntry GUIDropDownDataEntry::button(const WString& label, std::function<void()> callback)
  24. {
  25. GUIDropDownDataEntry data;
  26. data.mLabel = label;
  27. data.mType = Type::Entry;
  28. data.mCallback = callback;
  29. return data;
  30. }
  31. GUIDropDownDataEntry GUIDropDownDataEntry::subMenu(const WString& label, const GUIDropDownData& data)
  32. {
  33. GUIDropDownDataEntry dataEntry;
  34. dataEntry.mLabel = label;
  35. dataEntry.mType = Type::SubMenu;
  36. dataEntry.mChildData = data;
  37. return dataEntry;
  38. }
  39. GUIDropDownAreaPlacement GUIDropDownAreaPlacement::aroundPosition(const Vector2I& position)
  40. {
  41. GUIDropDownAreaPlacement instance;
  42. instance.mType = Type::Position;
  43. instance.mPosition = position;
  44. return instance;
  45. }
  46. GUIDropDownAreaPlacement GUIDropDownAreaPlacement::aroundBoundsVert(const RectI& bounds)
  47. {
  48. GUIDropDownAreaPlacement instance;
  49. instance.mType = Type::BoundsVert;
  50. instance.mBounds = bounds;
  51. return instance;
  52. }
  53. GUIDropDownAreaPlacement GUIDropDownAreaPlacement::aroundBoundsHorz(const RectI& bounds)
  54. {
  55. GUIDropDownAreaPlacement instance;
  56. instance.mType = Type::BoundsHorz;
  57. instance.mBounds = bounds;
  58. return instance;
  59. }
  60. GUIDropDownBox::GUIDropDownBox(const HSceneObject& parent, Viewport* target, const GUIDropDownAreaPlacement& placement,
  61. const GUIDropDownData& dropDownData, const GUISkin& skin, GUIDropDownType type)
  62. :GUIWidget(parent, target), mRootMenu(nullptr), mHitBox(nullptr), mCaptureHitBox(nullptr)
  63. {
  64. String stylePrefix = "";
  65. switch(type)
  66. {
  67. case GUIDropDownType::ContextMenu:
  68. stylePrefix = "ContextMenu";
  69. break;
  70. case GUIDropDownType::ListBox:
  71. stylePrefix = "ListBox";
  72. break;
  73. case GUIDropDownType::MenuBar:
  74. stylePrefix = "MenuBar";
  75. break;
  76. }
  77. mScrollUpStyle = stylePrefix + "ScrollUpBtn";
  78. mScrollDownStyle = stylePrefix + "ScrollDownBtn";
  79. mEntryBtnStyle = stylePrefix + "EntryBtn";
  80. mEntryExpBtnStyle = stylePrefix + "EntryExpBtn";
  81. mSeparatorStyle = stylePrefix + "Separator";
  82. mBackgroundStyle = stylePrefix + "Frame";
  83. mScrollUpBtnArrow = skin.getStyle(stylePrefix + "ScrollUpBtnArrow")->normal.texture;
  84. mScrollDownBtnArrow = skin.getStyle(stylePrefix + "ScrollDownBtnArrow")->normal.texture;
  85. setDepth(0); // Needs to be in front of everything
  86. setSkin(skin);
  87. mLocalizedEntryNames = dropDownData.localizedNames;
  88. mHitBox = GUIDropDownHitBox::create(false);
  89. mHitBox->onFocusLost.connect(std::bind(&GUIDropDownBox::dropDownFocusLost, this));
  90. mHitBox->setFocus(true);
  91. mHitBox->_setWidgetDepth(0);
  92. mHitBox->_setAreaDepth(0);
  93. mHitBox->_changeParentWidget(this);
  94. mCaptureHitBox = GUIDropDownHitBox::create(true);
  95. mCaptureHitBox->setBounds(RectI(0, 0, target->getWidth(), target->getHeight()));
  96. mCaptureHitBox->_setWidgetDepth(0);
  97. mCaptureHitBox->_setAreaDepth(200);
  98. mCaptureHitBox->_changeParentWidget(this);
  99. RectI availableBounds(target->getX(), target->getY(), target->getWidth(), target->getHeight());
  100. mRootMenu = bs_new<DropDownSubMenu>(this, placement, availableBounds, dropDownData, type, 0);
  101. }
  102. GUIDropDownBox::~GUIDropDownBox()
  103. {
  104. GUIElement::destroy(mHitBox);
  105. GUIElement::destroy(mCaptureHitBox);
  106. bs_delete(mRootMenu);
  107. }
  108. void GUIDropDownBox::dropDownFocusLost()
  109. {
  110. mRootMenu->closeSubMenu();
  111. GUIDropDownBoxManager::instance().closeDropDownBox();
  112. }
  113. void GUIDropDownBox::notifySubMenuOpened(DropDownSubMenu* subMenu)
  114. {
  115. Vector<RectI> bounds;
  116. while(subMenu != nullptr)
  117. {
  118. bounds.push_back(subMenu->getVisibleBounds());
  119. subMenu = subMenu->mSubMenu;
  120. }
  121. mHitBox->setBounds(bounds);
  122. }
  123. void GUIDropDownBox::notifySubMenuClosed(DropDownSubMenu* subMenu)
  124. {
  125. Vector<RectI> bounds;
  126. while(subMenu != nullptr)
  127. {
  128. bounds.push_back(subMenu->getVisibleBounds());
  129. subMenu = subMenu->mSubMenu;
  130. }
  131. mHitBox->setBounds(bounds);
  132. }
  133. GUIDropDownBox::DropDownSubMenu::DropDownSubMenu(GUIDropDownBox* owner, const GUIDropDownAreaPlacement& placement,
  134. const RectI& availableBounds, const GUIDropDownData& dropDownData, GUIDropDownType type, UINT32 depthOffset)
  135. :mOwner(owner), mPage(0), mBackgroundFrame(nullptr), mBackgroundArea(nullptr), mContentArea(nullptr),
  136. mContentLayout(nullptr), mScrollUpBtn(nullptr), mScrollDownBtn(nullptr), x(0), y(0), width(0), height(0),
  137. mType(type), mSubMenu(nullptr), mData(dropDownData), mOpenedUpward(false), mDepthOffset(depthOffset)
  138. {
  139. mAvailableBounds = availableBounds;
  140. const GUIElementStyle* scrollUpStyle = mOwner->getSkin().getStyle(mOwner->mScrollUpStyle);
  141. const GUIElementStyle* scrollDownStyle = mOwner->getSkin().getStyle(mOwner->mScrollDownStyle);
  142. const GUIElementStyle* backgroundStyle = mOwner->getSkin().getStyle(mOwner->mBackgroundStyle);
  143. RectI dropDownListBounds = placement.getBounds();
  144. int potentialLeftStart = 0;
  145. int potentialRightStart = 0;
  146. int potentialTopStart = 0;
  147. int potentialBottomStart = 0;
  148. switch(placement.getType())
  149. {
  150. case GUIDropDownAreaPlacement::Type::Position:
  151. potentialLeftStart = potentialRightStart = placement.getPosition().x;
  152. potentialTopStart = potentialBottomStart = placement.getPosition().y;
  153. break;
  154. case GUIDropDownAreaPlacement::Type::BoundsHorz:
  155. potentialRightStart = placement.getBounds().x;
  156. potentialLeftStart = placement.getBounds().x + placement.getBounds().width;
  157. potentialBottomStart = placement.getBounds().y + placement.getBounds().height;
  158. potentialTopStart = placement.getBounds().y;
  159. break;
  160. case GUIDropDownAreaPlacement::Type::BoundsVert:
  161. potentialRightStart = placement.getBounds().x + placement.getBounds().width;
  162. potentialLeftStart = placement.getBounds().x;
  163. potentialBottomStart = placement.getBounds().y;
  164. potentialTopStart = placement.getBounds().y + placement.getBounds().height;
  165. break;
  166. }
  167. // Determine x position and whether to align to left or right side of the drop down list
  168. UINT32 availableRightwardWidth = (UINT32)std::max(0, (availableBounds.x + availableBounds.width) - potentialRightStart);
  169. UINT32 availableLeftwardWidth = (UINT32)std::max(0, potentialLeftStart - availableBounds.x);
  170. //// Prefer right if possible
  171. if(DROP_DOWN_BOX_WIDTH <= availableRightwardWidth)
  172. {
  173. x = potentialRightStart;
  174. width = DROP_DOWN_BOX_WIDTH;
  175. }
  176. else
  177. {
  178. if(availableRightwardWidth >= availableLeftwardWidth)
  179. {
  180. x = potentialRightStart;
  181. width = std::min(DROP_DOWN_BOX_WIDTH, availableRightwardWidth);
  182. }
  183. else
  184. {
  185. x = potentialLeftStart - std::min(DROP_DOWN_BOX_WIDTH, availableLeftwardWidth);
  186. width = std::min(DROP_DOWN_BOX_WIDTH, availableLeftwardWidth);
  187. }
  188. }
  189. // Determine y position and whether to open upward or downward
  190. UINT32 availableDownwardHeight = (UINT32)std::max(0, (availableBounds.y + availableBounds.height) - potentialBottomStart);
  191. UINT32 availableUpwardHeight = (UINT32)std::max(0, potentialTopStart - availableBounds.y);
  192. //// Prefer down if possible
  193. UINT32 helperElementHeight = scrollUpStyle->height + scrollDownStyle->height +
  194. backgroundStyle->margins.top + backgroundStyle->margins.bottom;
  195. UINT32 maxNeededHeight = helperElementHeight;
  196. UINT32 numElements = (UINT32)dropDownData.entries.size();
  197. for(UINT32 i = 0; i < numElements; i++)
  198. maxNeededHeight += getElementHeight(i);
  199. height = 0;
  200. if(maxNeededHeight <= availableDownwardHeight)
  201. {
  202. y = potentialBottomStart;
  203. height = availableDownwardHeight;
  204. mOpenedUpward = false;
  205. }
  206. else
  207. {
  208. if(availableDownwardHeight >= availableUpwardHeight)
  209. {
  210. y = potentialBottomStart;
  211. height = availableDownwardHeight;
  212. mOpenedUpward = false;
  213. }
  214. else
  215. {
  216. y = potentialTopStart;
  217. height = availableUpwardHeight;
  218. mOpenedUpward = true;
  219. }
  220. }
  221. INT32 actualY = y;
  222. if(mOpenedUpward)
  223. actualY -= (INT32)std::min(maxNeededHeight, availableUpwardHeight);
  224. // Content area
  225. mContentArea = GUIArea::create(*mOwner, x, actualY, width, height);
  226. mContentArea->setDepth(100 - depthOffset * 2 - 1);
  227. mContentLayout = &mContentArea->getLayout().addLayoutY();
  228. // Background frame
  229. mBackgroundArea = GUIArea::create(*mOwner, x, actualY, width, height);
  230. mBackgroundArea->setDepth(100 - depthOffset * 2);
  231. mBackgroundFrame = GUITexture::create(GUIImageScaleMode::StretchToFit, mOwner->mBackgroundStyle);
  232. mBackgroundArea->getLayout().addElement(mBackgroundFrame);
  233. updateGUIElements();
  234. mOwner->notifySubMenuOpened(this);
  235. }
  236. GUIDropDownBox::DropDownSubMenu::~DropDownSubMenu()
  237. {
  238. closeSubMenu();
  239. mOwner->notifySubMenuClosed(this);
  240. for(auto& elem : mCachedSeparators)
  241. GUIElement::destroy(elem);
  242. for(auto& elem : mCachedEntryBtns)
  243. GUIElement::destroy(elem);
  244. for(auto& elem : mCachedExpEntryBtns)
  245. GUIElement::destroy(elem);
  246. if(mScrollUpBtn != nullptr)
  247. GUIElement::destroy(mScrollUpBtn);
  248. if(mScrollDownBtn != nullptr)
  249. GUIElement::destroy(mScrollDownBtn);
  250. GUIElement::destroy(mBackgroundFrame);
  251. GUIArea::destroy(mBackgroundArea);
  252. GUIArea::destroy(mContentArea);
  253. }
  254. void GUIDropDownBox::DropDownSubMenu::updateGUIElements()
  255. {
  256. // Remove all elements from content layout
  257. while(mContentLayout->getNumChildren() > 0)
  258. mContentLayout->removeChildAt(mContentLayout->getNumChildren() - 1);
  259. const GUIElementStyle* scrollUpStyle = mOwner->getSkin().getStyle(mOwner->mScrollUpStyle);
  260. const GUIElementStyle* scrollDownStyle = mOwner->getSkin().getStyle(mOwner->mScrollDownStyle);
  261. const GUIElementStyle* backgroundStyle = mOwner->getSkin().getStyle(mOwner->mBackgroundStyle);
  262. // Determine if we need scroll up and/or down buttons, number of visible elements and actual height
  263. bool needsScrollUp = mPage > 0;
  264. UINT32 numElements = (UINT32)mData.entries.size();
  265. UINT32 usedHeight = backgroundStyle->margins.top + backgroundStyle->margins.bottom;
  266. UINT32 pageStart = 0, pageEnd = 0;
  267. UINT32 curPage = 0;
  268. bool needsScrollDown = false;
  269. for(UINT32 i = 0; i < numElements; i++)
  270. {
  271. usedHeight += getElementHeight(i);
  272. pageEnd++;
  273. if(usedHeight > height)
  274. {
  275. usedHeight += scrollDownStyle->height;
  276. // Remove last few elements until we fit again
  277. while(usedHeight > height && i >= 0)
  278. {
  279. usedHeight -= getElementHeight(i);
  280. pageEnd--;
  281. i--;
  282. }
  283. // We found our page and are done
  284. if(curPage == mPage)
  285. {
  286. needsScrollDown = i != (numElements - 1);
  287. break;
  288. }
  289. // Nothing fits, break out of infinite loop
  290. if(pageStart == pageEnd)
  291. {
  292. needsScrollDown = i != (numElements - 1);
  293. break;
  294. }
  295. pageStart = pageEnd;
  296. usedHeight = backgroundStyle->margins.top + backgroundStyle->margins.bottom;
  297. usedHeight += scrollUpStyle->height;
  298. curPage++;
  299. }
  300. }
  301. // Add scroll up button
  302. if(needsScrollUp)
  303. {
  304. if(mScrollUpBtn == nullptr)
  305. {
  306. mScrollUpBtn = GUIButton::create(GUIContent(HString(L""), mOwner->mScrollUpBtnArrow), mOwner->mScrollUpStyle);
  307. mScrollUpBtn->onClick.connect(std::bind(&DropDownSubMenu::scrollUp, this));
  308. }
  309. mContentLayout->addElement(mScrollUpBtn);
  310. }
  311. else
  312. {
  313. if(mScrollUpBtn != nullptr)
  314. {
  315. GUIElement::destroy(mScrollUpBtn);
  316. mScrollUpBtn = nullptr;
  317. }
  318. }
  319. Vector<GUITexture*> newSeparators;
  320. Vector<GUIButton*> newEntryBtns;
  321. Vector<GUIButton*> newExpEntryBtns;
  322. for(UINT32 i = pageStart; i < pageEnd; i++)
  323. {
  324. GUIDropDownDataEntry& element = mData.entries[i];
  325. if(element.isSeparator())
  326. {
  327. GUITexture* separator = nullptr;
  328. if(!mCachedSeparators.empty())
  329. {
  330. separator = mCachedSeparators.back();
  331. mCachedSeparators.erase(mCachedSeparators.end() - 1);
  332. }
  333. else
  334. {
  335. separator = GUITexture::create(GUIImageScaleMode::StretchToFit, mOwner->mSeparatorStyle);
  336. }
  337. mContentLayout->addElement(separator);
  338. newSeparators.push_back(separator);
  339. }
  340. else if(element.isSubMenu())
  341. {
  342. GUIButton* expEntryBtn = nullptr;
  343. if(!mCachedExpEntryBtns.empty())
  344. {
  345. expEntryBtn = mCachedExpEntryBtns.back();
  346. mCachedExpEntryBtns.erase(mCachedExpEntryBtns.end() - 1);
  347. }
  348. else
  349. {
  350. expEntryBtn = GUIButton::create(getElementLocalizedName(i), mOwner->mEntryExpBtnStyle);
  351. expEntryBtn->onHover.connect(std::bind(&DropDownSubMenu::openSubMenu, this, expEntryBtn, i));
  352. }
  353. mContentLayout->addElement(expEntryBtn);
  354. newExpEntryBtns.push_back(expEntryBtn);
  355. }
  356. else
  357. {
  358. GUIButton* entryBtn = nullptr;
  359. if(!mCachedEntryBtns.empty())
  360. {
  361. entryBtn = mCachedEntryBtns.back();
  362. mCachedEntryBtns.erase(mCachedEntryBtns.end() - 1);
  363. }
  364. else
  365. {
  366. entryBtn = GUIButton::create(getElementLocalizedName(i), mOwner->mEntryBtnStyle);
  367. entryBtn->onHover.connect(std::bind(&DropDownSubMenu::closeSubMenu, this));
  368. entryBtn->onClick.connect(std::bind(&DropDownSubMenu::elementClicked, this, i));
  369. }
  370. mContentLayout->addElement(entryBtn);
  371. newEntryBtns.push_back(entryBtn);
  372. }
  373. }
  374. // Destroy all unused cached elements
  375. for(auto& elem : mCachedSeparators)
  376. GUIElement::destroy(elem);
  377. for(auto& elem : mCachedEntryBtns)
  378. GUIElement::destroy(elem);
  379. for(auto& elem : mCachedExpEntryBtns)
  380. GUIElement::destroy(elem);
  381. mCachedSeparators = newSeparators;
  382. mCachedEntryBtns = newEntryBtns;
  383. mCachedExpEntryBtns = newExpEntryBtns;
  384. // Add scroll down button
  385. if(needsScrollDown)
  386. {
  387. if(mScrollDownBtn == nullptr)
  388. {
  389. mScrollDownBtn = GUIButton::create(GUIContent(HString(L""), mOwner->mScrollDownBtnArrow), mOwner->mScrollDownStyle);
  390. mScrollDownBtn->onClick.connect(std::bind(&DropDownSubMenu::scrollDown, this));
  391. }
  392. mContentLayout->addElement(mScrollDownBtn);
  393. }
  394. else
  395. {
  396. if(mScrollDownBtn != nullptr)
  397. {
  398. GUIElement::destroy(mScrollDownBtn);
  399. mScrollDownBtn = nullptr;
  400. }
  401. }
  402. // Resize and reposition areas
  403. INT32 actualY = y;
  404. if(mOpenedUpward)
  405. actualY -= (INT32)usedHeight;
  406. mBackgroundArea->setSize(width, usedHeight);
  407. mBackgroundArea->setPosition(x, actualY);
  408. mVisibleBounds = RectI(x, actualY, width, usedHeight);
  409. UINT32 contentWidth = (UINT32)std::max(0, (INT32)width - (INT32)backgroundStyle->margins.left - (INT32)backgroundStyle->margins.right);
  410. UINT32 contentHeight = (UINT32)std::max(0, (INT32)usedHeight - (INT32)backgroundStyle->margins.top - (INT32)backgroundStyle->margins.bottom);
  411. mContentArea->setSize(contentWidth, contentHeight);
  412. mContentArea->setPosition(x + backgroundStyle->margins.left, actualY + backgroundStyle->margins.top);
  413. }
  414. UINT32 GUIDropDownBox::DropDownSubMenu::getElementHeight(UINT32 idx) const
  415. {
  416. if(mData.entries[idx].isSeparator())
  417. return mOwner->getSkin().getStyle(mOwner->mSeparatorStyle)->height;
  418. else if(mData.entries[idx].isSubMenu())
  419. return mOwner->getSkin().getStyle(mOwner->mEntryExpBtnStyle)->height;
  420. else
  421. return mOwner->getSkin().getStyle(mOwner->mEntryBtnStyle)->height;
  422. }
  423. HString GUIDropDownBox::DropDownSubMenu::getElementLocalizedName(UINT32 idx) const
  424. {
  425. const WString& label = mData.entries[idx].getLabel();
  426. auto findLocalizedName = mOwner->mLocalizedEntryNames.find(label);
  427. if(findLocalizedName != mOwner->mLocalizedEntryNames.end())
  428. return findLocalizedName->second;
  429. else
  430. return HString(label);
  431. }
  432. void GUIDropDownBox::DropDownSubMenu::scrollDown()
  433. {
  434. mPage++;
  435. updateGUIElements();
  436. closeSubMenu();
  437. }
  438. void GUIDropDownBox::DropDownSubMenu::scrollUp()
  439. {
  440. if(mPage > 0)
  441. {
  442. mPage--;
  443. updateGUIElements();
  444. }
  445. closeSubMenu();
  446. }
  447. void GUIDropDownBox::DropDownSubMenu::closeSubMenu()
  448. {
  449. if(mSubMenu != nullptr)
  450. {
  451. bs_delete(mSubMenu);
  452. mSubMenu = nullptr;
  453. }
  454. }
  455. void GUIDropDownBox::DropDownSubMenu::elementClicked(UINT32 idx)
  456. {
  457. closeSubMenu();
  458. auto callback = mData.entries[idx].getCallback();
  459. if(callback != nullptr)
  460. callback();
  461. GUIDropDownBoxManager::instance().closeDropDownBox();
  462. }
  463. void GUIDropDownBox::DropDownSubMenu::openSubMenu(GUIButton* source, UINT32 idx)
  464. {
  465. closeSubMenu();
  466. mSubMenu = bs_new<DropDownSubMenu>(mOwner, GUIDropDownAreaPlacement::aroundBoundsVert(source->_getCachedBounds()),
  467. mAvailableBounds, mData.entries[idx].getSubMenuData(), mType, mDepthOffset + 1);
  468. }
  469. }