BsGUIMenuBar.cpp 8.6 KB

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