BsGUIMenuBar.cpp 10 KB

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