BsGUIDropDownBox.cpp 15 KB

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