| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- #include "BsGUIMenuBar.h"
- #include "BsGUIArea.h"
- #include "BsGUIElement.h"
- #include "BsGUIButton.h"
- #include "BsGUITexture.h"
- #include "BsGUILayout.h"
- #include "BsGUISpace.h"
- #include "BsGUIMenu.h"
- #include "BsGUIManager.h"
- #include "BsBuiltinResources.h"
- #include "BsGUIDropDownBoxManager.h"
- #include "BsSceneObject.h"
- #include "BsPlatform.h"
- namespace BansheeEngine
- {
- const UINT32 GUIMenuBar::NUM_ELEMENTS_AFTER_CONTENT = 8;
- GUIMenuBar::GUIMenuBar(GUIWidget* parent, RenderWindow* parentWindow)
- :mParentWidget(parent), mParentWindow(parentWindow), mMainArea(nullptr), mBackgroundArea(nullptr),
- mBgTexture(nullptr), mLogoTexture(nullptr), mSubMenuOpen(false), mSubMenuButton(nullptr)
- {
- mBackgroundArea = GUIArea::create(*parent, 0, 0, 1, 13, 9900);
- mMainArea = GUIArea::create(*parent, 0, 0, 1, 13, 9899);
- mBgTexture = GUITexture::create(GUIImageScaleMode::StretchToFit, GUIOptions(GUIOption::flexibleWidth(), GUIOption::flexibleHeight()), "MenuBarBg");
- mBackgroundArea->getLayout().addElement(mBgTexture);
- mLogoTexture = GUITexture::create(GUIImageScaleMode::StretchToFit, "MenuBarBansheeLogo");
- GUILayout& mainLayout = mMainArea->getLayout();
- mainLayout.addElement(mLogoTexture);
- mainLayout.addSpace(5);
- mainLayout.addFlexibleSpace();
- mMinBtn = GUIButton::create(HString(L""), "WinMinimizeBtn");
- mMaxBtn = GUIButton::create(HString(L""), "WinMaximizeBtn");
- mCloseBtn = GUIButton::create(HString(L""), "WinCloseBtn");
- mainLayout.addSpace(3);
- mainLayout.addElement(mMinBtn);
- mainLayout.addSpace(3);
- mainLayout.addElement(mMaxBtn);
- mainLayout.addSpace(3);
- mainLayout.addElement(mCloseBtn);
- mainLayout.addSpace(3);
- mMinBtn->onClick.connect(std::bind(&GUIMenuBar::onMinimizeClicked, this));
- mMaxBtn->onClick.connect(std::bind(&GUIMenuBar::onMaximizeClicked, this));
- mCloseBtn->onClick.connect(std::bind(&GUIMenuBar::onCloseClicked, this));
- refreshNonClientAreas();
- }
- GUIMenuBar::~GUIMenuBar()
- {
- closeSubMenu();
- for(auto& menu : mChildMenus)
- {
- bs_delete<PoolAlloc>(menu.menu);
- GUIElement::destroy(menu.button);
- }
- GUIElement::destroy(mMinBtn);
- GUIElement::destroy(mMaxBtn);
- GUIElement::destroy(mCloseBtn);
- GUIElement::destroy(mBgTexture);
- GUIElement::destroy(mLogoTexture);
- GUIArea::destroy(mMainArea);
- GUIArea::destroy(mBackgroundArea);
- }
- void GUIMenuBar::setArea(INT32 x, INT32 y, UINT32 width, UINT32 height)
- {
- mMainArea->setPosition(x, y);
- mBackgroundArea->setPosition(x, y);
- mMainArea->setSize(width, height);
- mBackgroundArea->setSize(width, height);
- refreshNonClientAreas();
- }
- const GUIMenuItem* GUIMenuBar::addMenuItem(const WString& path, std::function<void()> callback)
- {
- WString strippedPath = path;
- WString rootName;
- if(!stripPath(strippedPath, rootName))
- return nullptr;
- const GUIMenuBarData* subMenu = getSubMenu(rootName);
- if(subMenu == nullptr)
- {
- subMenu = addNewButton(rootName);
- refreshNonClientAreas();
- }
- return subMenu->menu->addMenuItem(strippedPath, callback);
- }
- const GUIMenuItem* GUIMenuBar::addSeparator(const WString& path)
- {
- WString strippedPath = path;
- WString rootName;
- if(!stripPath(strippedPath, rootName))
- return nullptr;
- const GUIMenuBarData* subMenu = getSubMenu(rootName);
- if(subMenu == nullptr)
- {
- subMenu = addNewButton(rootName);
- refreshNonClientAreas();
- }
- return subMenu->menu->addSeparator(strippedPath);
- }
- GUIMenuBar::GUIMenuBarData* GUIMenuBar::addNewButton(const WString& name)
- {
- mChildMenus.push_back(GUIMenuBarData());
- GUIMenuBarData& newSubMenu = mChildMenus.back();
- newSubMenu.name = name;
- newSubMenu.menu = bs_new<GUIMenu>();
- GUIButton* newButton = GUIButton::create(HString(name), "MenuBarBtn");
- newButton->onClick.connect(std::bind(&GUIMenuBar::openSubMenu, this, name));
- newButton->onHover.connect(std::bind(&GUIMenuBar::onSubMenuHover, this, name));
- mMainArea->getLayout().insertElement(mMainArea->getLayout().getNumChildren() - NUM_ELEMENTS_AFTER_CONTENT, newButton);
- newSubMenu.button = newButton;
- return &newSubMenu;
- }
- const GUIMenuItem* GUIMenuBar::getMenuItem(const WString& path) const
- {
- WString strippedPath = path;
- WString rootName;
- if(!stripPath(strippedPath, rootName))
- return nullptr;
- const GUIMenuBarData* subMenu = getSubMenu(rootName);
- if(subMenu == nullptr)
- return nullptr;
- return subMenu->menu->getMenuItem(strippedPath);
- }
- void GUIMenuBar::removeMenuItem(const WString& path)
- {
- WString strippedPath = path;
- WString rootName;
- if(!stripPath(strippedPath, rootName))
- return;
- if(strippedPath == L"")
- {
- UINT32 curIdx = 0;
- GUIMenuBarData* subMenuToRemove = nullptr;
- for(auto& subMenuData : mChildMenus)
- {
- if(subMenuData.name == rootName)
- {
- subMenuToRemove = &subMenuData;
- break;
- }
- curIdx++;
- }
- if(subMenuToRemove == nullptr)
- return;
- mMainArea->getLayout().removeElement(subMenuToRemove->button);
- GUIElement::destroy(subMenuToRemove->button);
- bs_delete<PoolAlloc>(subMenuToRemove->menu);
- mChildMenus.erase(mChildMenus.begin() + curIdx);
- refreshNonClientAreas();
- return;
- }
- const GUIMenuBarData* subMenu = getSubMenu(rootName);
- if(subMenu == nullptr)
- return;
- const GUIMenuItem* item = subMenu->menu->getMenuItem(strippedPath);
- if(item == nullptr)
- return;
- subMenu->menu->removeMenuItem(item);
- }
- const GUIMenuBar::GUIMenuBarData* GUIMenuBar::getSubMenu(const WString& name) const
- {
- for(auto& subMenu : mChildMenus)
- {
- if(subMenu.name == name)
- return &subMenu;
- }
- return nullptr;
- }
- bool GUIMenuBar::stripPath(WString& path, WString& pathRoot) const
- {
- Vector<WString> pathElements = StringUtil::split(path, L"/");
- if(pathElements.size() == 0)
- return false;
- pathRoot = pathElements[0];
- path.erase(0, pathRoot.size());
- if(path.size() > 0)
- path.erase(0, 1); // Forward slash
- return true;
- }
- void GUIMenuBar::openSubMenu(const WString& name)
- {
- const GUIMenuBarData* subMenu = getSubMenu(name);
- if(subMenu == nullptr)
- return;
- if(mSubMenuOpen)
- {
- bool closingExisting = subMenu->button == mSubMenuButton;
- closeSubMenu();
- if(closingExisting)
- return;
- }
- GUIDropDownData dropDownData = subMenu->menu->getDropDownData();
- GUIWidget* widget = subMenu->button->_getParentWidget();
- GUIDropDownAreaPlacement placement = GUIDropDownAreaPlacement::aroundBoundsHorz(subMenu->button->getBounds());
- GameObjectHandle<GUIDropDownBox> dropDownBox = GUIDropDownBoxManager::instance().openDropDownBox(widget->getTarget(),
- placement, dropDownData, widget->getSkin(), GUIDropDownType::MenuBar, std::bind(&GUIMenuBar::onSubMenuClosed, this));
- subMenu->button->_setOn(true);
- mSubMenuButton = subMenu->button;
- mSubMenuOpen = true;
- }
- void GUIMenuBar::closeSubMenu()
- {
- if(mSubMenuOpen)
- {
- GUIDropDownBoxManager::instance().closeDropDownBox();
- mSubMenuButton->_setOn(false);
- mSubMenuOpen = false;
- }
- }
- void GUIMenuBar::onSubMenuHover(const WString& name)
- {
- if(mSubMenuOpen)
- {
- const GUIMenuBarData* subMenu = getSubMenu(name);
- if(subMenu == nullptr)
- return;
- if(mSubMenuButton != subMenu->button)
- openSubMenu(name);
- }
- }
- void GUIMenuBar::onSubMenuClosed()
- {
- mSubMenuButton->_setOn(false);
- mSubMenuOpen = false;
- }
- void GUIMenuBar::onMinimizeClicked()
- {
- // TODO
- }
- void GUIMenuBar::onMaximizeClicked()
- {
- // TODO
- }
- void GUIMenuBar::onCloseClicked()
- {
- // TODO
- }
- void GUIMenuBar::refreshNonClientAreas()
- {
- // If the size or contents of the area changed this frame the layout won't be updated yet,
- // so force the update right away so we get correct element bounds
- mMainArea->_update();
- Vector<RectI> nonClientAreas;
- nonClientAreas.push_back(mLogoTexture->getBounds());
- if(mChildMenus.size() > 0)
- {
- RectI lastButtonBounds = mChildMenus.back().button->getBounds();
- RectI minButtonBounds = mMinBtn->getBounds();
- RectI emptyArea(lastButtonBounds.x + lastButtonBounds.width, mMainArea->y(),
- minButtonBounds.x - (lastButtonBounds.x + lastButtonBounds.width), mMainArea->height());
- nonClientAreas.push_back(emptyArea);
- }
- Platform::setCaptionNonClientAreas(*mParentWindow, nonClientAreas);
- }
- }
|