BsGUIDropDownBox.cpp 15 KB

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