BsGUIMenuBar.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "BsGUIMenuBar.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUIElement.h"
  4. #include "BsGUIButton.h"
  5. #include "BsGUITexture.h"
  6. #include "BsGUILayout.h"
  7. #include "BsGUISpace.h"
  8. #include "BsGUIMenu.h"
  9. #include "BsGUIManager.h"
  10. #include "BsBuiltinResources.h"
  11. #include "BsGUIDropDownBoxManager.h"
  12. #include "BsSceneObject.h"
  13. #include "BsPlatform.h"
  14. namespace BansheeEngine
  15. {
  16. const UINT32 GUIMenuBar::NUM_ELEMENTS_AFTER_CONTENT = 8;
  17. GUIMenuBar::GUIMenuBar(GUIWidget* parent, RenderWindow* parentWindow)
  18. :mParentWidget(parent), mParentWindow(parentWindow), mMainArea(nullptr), mBackgroundArea(nullptr),
  19. mBgTexture(nullptr), mLogoTexture(nullptr), mSubMenuOpen(false), mSubMenuButton(nullptr)
  20. {
  21. mBackgroundArea = GUIArea::create(*parent, 0, 0, 1, 13, 9900);
  22. mMainArea = GUIArea::create(*parent, 0, 0, 1, 13, 9899);
  23. mBgTexture = GUITexture::create(GUIImageScaleMode::StretchToFit, GUIOptions(GUIOption::flexibleWidth(), GUIOption::flexibleHeight()), "MenuBarBg");
  24. mBackgroundArea->getLayout().addElement(mBgTexture);
  25. mLogoTexture = GUITexture::create(GUIImageScaleMode::StretchToFit, "MenuBarBansheeLogo");
  26. GUILayout& mainLayout = mMainArea->getLayout();
  27. mainLayout.addElement(mLogoTexture);
  28. mainLayout.addSpace(5);
  29. mainLayout.addFlexibleSpace();
  30. mMinBtn = GUIButton::create(HString(L""), "WinMinimizeBtn");
  31. mMaxBtn = GUIButton::create(HString(L""), "WinMaximizeBtn");
  32. mCloseBtn = GUIButton::create(HString(L""), "WinCloseBtn");
  33. mainLayout.addSpace(3);
  34. mainLayout.addElement(mMinBtn);
  35. mainLayout.addSpace(3);
  36. mainLayout.addElement(mMaxBtn);
  37. mainLayout.addSpace(3);
  38. mainLayout.addElement(mCloseBtn);
  39. mainLayout.addSpace(3);
  40. mMinBtn->onClick.connect(std::bind(&GUIMenuBar::onMinimizeClicked, this));
  41. mMaxBtn->onClick.connect(std::bind(&GUIMenuBar::onMaximizeClicked, this));
  42. mCloseBtn->onClick.connect(std::bind(&GUIMenuBar::onCloseClicked, this));
  43. refreshNonClientAreas();
  44. }
  45. GUIMenuBar::~GUIMenuBar()
  46. {
  47. closeSubMenu();
  48. for(auto& menu : mChildMenus)
  49. {
  50. bs_delete<PoolAlloc>(menu.menu);
  51. GUIElement::destroy(menu.button);
  52. }
  53. GUIElement::destroy(mMinBtn);
  54. GUIElement::destroy(mMaxBtn);
  55. GUIElement::destroy(mCloseBtn);
  56. GUIElement::destroy(mBgTexture);
  57. GUIElement::destroy(mLogoTexture);
  58. GUIArea::destroy(mMainArea);
  59. GUIArea::destroy(mBackgroundArea);
  60. }
  61. void GUIMenuBar::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
  62. {
  63. mMainArea->setPosition(x, y);
  64. mBackgroundArea->setPosition(x, y);
  65. mMainArea->setSize(width, height);
  66. mBackgroundArea->setSize(width, height);
  67. refreshNonClientAreas();
  68. }
  69. const GUIMenuItem* GUIMenuBar::addMenuItem(const WString& path, std::function<void()> callback)
  70. {
  71. WString strippedPath = path;
  72. WString rootName;
  73. if(!stripPath(strippedPath, rootName))
  74. return nullptr;
  75. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  76. if(subMenu == nullptr)
  77. {
  78. subMenu = addNewButton(rootName);
  79. refreshNonClientAreas();
  80. }
  81. return subMenu->menu->addMenuItem(strippedPath, callback);
  82. }
  83. const GUIMenuItem* GUIMenuBar::addSeparator(const WString& path)
  84. {
  85. WString strippedPath = path;
  86. WString rootName;
  87. if(!stripPath(strippedPath, rootName))
  88. return nullptr;
  89. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  90. if(subMenu == nullptr)
  91. {
  92. subMenu = addNewButton(rootName);
  93. refreshNonClientAreas();
  94. }
  95. return subMenu->menu->addSeparator(strippedPath);
  96. }
  97. GUIMenuBar::GUIMenuBarData* GUIMenuBar::addNewButton(const WString& name)
  98. {
  99. mChildMenus.push_back(GUIMenuBarData());
  100. GUIMenuBarData& newSubMenu = mChildMenus.back();
  101. newSubMenu.name = name;
  102. newSubMenu.menu = bs_new<GUIMenu>();
  103. GUIButton* newButton = GUIButton::create(HString(name), "MenuBarBtn");
  104. newButton->onClick.connect(std::bind(&GUIMenuBar::openSubMenu, this, name));
  105. newButton->onHover.connect(std::bind(&GUIMenuBar::onSubMenuHover, this, name));
  106. mMainArea->getLayout().insertElement(mMainArea->getLayout().getNumChildren() - NUM_ELEMENTS_AFTER_CONTENT, newButton);
  107. newSubMenu.button = newButton;
  108. return &newSubMenu;
  109. }
  110. const GUIMenuItem* GUIMenuBar::getMenuItem(const WString& path) const
  111. {
  112. WString strippedPath = path;
  113. WString rootName;
  114. if(!stripPath(strippedPath, rootName))
  115. return nullptr;
  116. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  117. if(subMenu == nullptr)
  118. return nullptr;
  119. return subMenu->menu->getMenuItem(strippedPath);
  120. }
  121. void GUIMenuBar::removeMenuItem(const WString& path)
  122. {
  123. WString strippedPath = path;
  124. WString rootName;
  125. if(!stripPath(strippedPath, rootName))
  126. return;
  127. if(strippedPath == L"")
  128. {
  129. UINT32 curIdx = 0;
  130. GUIMenuBarData* subMenuToRemove = nullptr;
  131. for(auto& subMenuData : mChildMenus)
  132. {
  133. if(subMenuData.name == rootName)
  134. {
  135. subMenuToRemove = &subMenuData;
  136. break;
  137. }
  138. curIdx++;
  139. }
  140. if(subMenuToRemove == nullptr)
  141. return;
  142. mMainArea->getLayout().removeElement(subMenuToRemove->button);
  143. GUIElement::destroy(subMenuToRemove->button);
  144. bs_delete<PoolAlloc>(subMenuToRemove->menu);
  145. mChildMenus.erase(mChildMenus.begin() + curIdx);
  146. refreshNonClientAreas();
  147. return;
  148. }
  149. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  150. if(subMenu == nullptr)
  151. return;
  152. const GUIMenuItem* item = subMenu->menu->getMenuItem(strippedPath);
  153. if(item == nullptr)
  154. return;
  155. subMenu->menu->removeMenuItem(item);
  156. }
  157. const GUIMenuBar::GUIMenuBarData* GUIMenuBar::getSubMenu(const WString& name) const
  158. {
  159. for(auto& subMenu : mChildMenus)
  160. {
  161. if(subMenu.name == name)
  162. return &subMenu;
  163. }
  164. return nullptr;
  165. }
  166. bool GUIMenuBar::stripPath(WString& path, WString& pathRoot) const
  167. {
  168. Vector<WString> pathElements = StringUtil::split(path, L"/");
  169. if(pathElements.size() == 0)
  170. return false;
  171. pathRoot = pathElements[0];
  172. path.erase(0, pathRoot.size());
  173. if(path.size() > 0)
  174. path.erase(0, 1); // Forward slash
  175. return true;
  176. }
  177. void GUIMenuBar::openSubMenu(const WString& name)
  178. {
  179. const GUIMenuBarData* subMenu = getSubMenu(name);
  180. if(subMenu == nullptr)
  181. return;
  182. if(mSubMenuOpen)
  183. {
  184. bool closingExisting = subMenu->button == mSubMenuButton;
  185. closeSubMenu();
  186. if(closingExisting)
  187. return;
  188. }
  189. GUIDropDownData dropDownData = subMenu->menu->getDropDownData();
  190. GUIWidget* widget = subMenu->button->_getParentWidget();
  191. GUIDropDownAreaPlacement placement = GUIDropDownAreaPlacement::aroundBoundsHorz(subMenu->button->getBounds());
  192. GameObjectHandle<GUIDropDownBox> dropDownBox = GUIDropDownBoxManager::instance().openDropDownBox(widget->getTarget(),
  193. placement, dropDownData, widget->getSkin(), GUIDropDownType::MenuBar, std::bind(&GUIMenuBar::onSubMenuClosed, this));
  194. subMenu->button->_setOn(true);
  195. mSubMenuButton = subMenu->button;
  196. mSubMenuOpen = true;
  197. }
  198. void GUIMenuBar::closeSubMenu()
  199. {
  200. if(mSubMenuOpen)
  201. {
  202. GUIDropDownBoxManager::instance().closeDropDownBox();
  203. mSubMenuButton->_setOn(false);
  204. mSubMenuOpen = false;
  205. }
  206. }
  207. void GUIMenuBar::onSubMenuHover(const WString& name)
  208. {
  209. if(mSubMenuOpen)
  210. {
  211. const GUIMenuBarData* subMenu = getSubMenu(name);
  212. if(subMenu == nullptr)
  213. return;
  214. if(mSubMenuButton != subMenu->button)
  215. openSubMenu(name);
  216. }
  217. }
  218. void GUIMenuBar::onSubMenuClosed()
  219. {
  220. mSubMenuButton->_setOn(false);
  221. mSubMenuOpen = false;
  222. }
  223. void GUIMenuBar::onMinimizeClicked()
  224. {
  225. // TODO
  226. }
  227. void GUIMenuBar::onMaximizeClicked()
  228. {
  229. // TODO
  230. }
  231. void GUIMenuBar::onCloseClicked()
  232. {
  233. // TODO
  234. }
  235. void GUIMenuBar::refreshNonClientAreas()
  236. {
  237. // If the size or contents of the area changed this frame the layout won't be updated yet,
  238. // so force the update right away so we get correct element bounds
  239. mMainArea->_update();
  240. Vector<RectI> nonClientAreas;
  241. nonClientAreas.push_back(mLogoTexture->getBounds());
  242. if(mChildMenus.size() > 0)
  243. {
  244. RectI lastButtonBounds = mChildMenus.back().button->getBounds();
  245. RectI minButtonBounds = mMinBtn->getBounds();
  246. RectI emptyArea(lastButtonBounds.x + lastButtonBounds.width, mMainArea->y(),
  247. minButtonBounds.x - (lastButtonBounds.x + lastButtonBounds.width), mMainArea->height());
  248. nonClientAreas.push_back(emptyArea);
  249. }
  250. Platform::setCaptionNonClientAreas(*mParentWindow, nonClientAreas);
  251. }
  252. }