BsGUIMenuBar.cpp 9.5 KB

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