BsGUIMenuBar.cpp 8.5 KB

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