BsGUIMenuBar.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "BsGUIMenuBar.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUIElement.h"
  4. #include "BsGUIButton.h"
  5. #include "BsGUILayout.h"
  6. #include "BsGUIMenu.h"
  7. #include "BsGUIManager.h"
  8. using namespace CamelotFramework;
  9. namespace BansheeEngine
  10. {
  11. GUIMenuBar::GUIMenuBar(BS::GUIWidget* parent)
  12. :mParentWidget(parent), mMainArea(nullptr), mBackgroundArea(nullptr)
  13. {
  14. mBackgroundArea = GUIArea::create(*parent, 0, 0, 1, 13, 9900);
  15. mMainArea = GUIArea::create(*parent, 0, 0, 1, 13, 9899);
  16. }
  17. GUIMenuBar::~GUIMenuBar()
  18. {
  19. for(auto& menu : mChildMenus)
  20. {
  21. cm_delete<PoolAlloc>(menu.menu);
  22. GUIElement::destroy(menu.button);
  23. }
  24. GUIArea::destroy(mMainArea);
  25. GUIArea::destroy(mBackgroundArea);
  26. }
  27. void GUIMenuBar::setArea(CM::INT32 x, CM::INT32 y, CM::UINT32 width, CM::UINT32 height)
  28. {
  29. mMainArea->setPosition(x, y);
  30. mBackgroundArea->setPosition(x, y);
  31. mMainArea->setSize(width, height);
  32. mBackgroundArea->setSize(width, height);
  33. }
  34. const GUIMenuItem* GUIMenuBar::addMenuItem(const CM::WString& path, std::function<void()> callback)
  35. {
  36. WString strippedPath = path;
  37. WString rootName;
  38. if(!stripPath(strippedPath, rootName))
  39. return nullptr;
  40. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  41. if(subMenu == nullptr)
  42. {
  43. mChildMenus.push_back(GUIMenuBarData());
  44. GUIMenuBarData& newSubMenu = mChildMenus.back();
  45. newSubMenu.name = rootName;
  46. newSubMenu.menu = cm_new<GUIMenu>();
  47. GUIButton* newButton = GUIButton::create(*mParentWidget, rootName);
  48. mMainArea->getLayout().addElement(newButton);
  49. newSubMenu.button = newButton;
  50. subMenu = &newSubMenu;
  51. }
  52. return subMenu->menu->addMenuItem(strippedPath, callback);
  53. }
  54. const GUIMenuItem* GUIMenuBar::addSeparator(const CM::WString& path)
  55. {
  56. WString strippedPath = path;
  57. WString rootName;
  58. if(!stripPath(strippedPath, rootName))
  59. return nullptr;
  60. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  61. if(subMenu == nullptr)
  62. {
  63. mChildMenus.push_back(GUIMenuBarData());
  64. GUIMenuBarData& newSubMenu = mChildMenus.back();
  65. newSubMenu.name = rootName;
  66. newSubMenu.menu = cm_new<GUIMenu>();
  67. GUIButton* newButton = GUIButton::create(*mParentWidget, rootName);
  68. newButton->onClick.connect(boost::bind(&GUIMenuBar::openSubMenu, this, rootName));
  69. mMainArea->getLayout().addElement(newButton);
  70. newSubMenu.button = newButton;
  71. subMenu = &newSubMenu;
  72. }
  73. return subMenu->menu->addSeparator(strippedPath);
  74. }
  75. const GUIMenuItem* GUIMenuBar::getMenuItem(const CM::WString& path) const
  76. {
  77. WString strippedPath = path;
  78. WString rootName;
  79. if(!stripPath(strippedPath, rootName))
  80. return nullptr;
  81. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  82. if(subMenu == nullptr)
  83. return nullptr;
  84. return subMenu->menu->getMenuItem(strippedPath);
  85. }
  86. void GUIMenuBar::removeMenuItem(const CM::WString& path)
  87. {
  88. WString strippedPath = path;
  89. WString rootName;
  90. if(!stripPath(strippedPath, rootName))
  91. return;
  92. const GUIMenuBarData* subMenu = getSubMenu(rootName);
  93. if(subMenu == nullptr)
  94. return;
  95. const GUIMenuItem* item = subMenu->menu->getMenuItem(strippedPath);
  96. if(item == nullptr)
  97. return;
  98. subMenu->menu->removeMenuItem(item);
  99. }
  100. const GUIMenuBar::GUIMenuBarData* GUIMenuBar::getSubMenu(const CM::WString& name) const
  101. {
  102. for(auto& subMenu : mChildMenus)
  103. {
  104. if(subMenu.name == name)
  105. return &subMenu;
  106. }
  107. return nullptr;
  108. }
  109. bool GUIMenuBar::stripPath(CM::WString& path, CM::WString& pathRoot) const
  110. {
  111. Vector<WString>::type pathElements = StringUtil::split(path, L"/");
  112. if(pathElements.size() == 0)
  113. return false;
  114. pathRoot = pathElements[0];
  115. path.erase(0, pathRoot.size());
  116. if(path.size() > 0)
  117. path.erase(0, 1); // Forward slash
  118. return true;
  119. }
  120. void GUIMenuBar::openSubMenu(const CM::WString& name)
  121. {
  122. const GUIMenuBarData* subMenu = getSubMenu(name);
  123. if(subMenu == nullptr)
  124. return;
  125. GUIManager::instance().openMenuBarMenu(subMenu->button, subMenu->menu);
  126. }
  127. }