| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include "BsGUIToggle.h"
- #include "BsImageSprite.h"
- #include "BsCGUIWidget.h"
- #include "BsGUISkin.h"
- #include "BsSpriteTexture.h"
- #include "BsTextSprite.h"
- #include "BsGUIDimensions.h"
- #include "BsGUIMouseEvent.h"
- #include "BsGUIToggleGroup.h"
- #include "BsGUIHelper.h"
- #include "BsTexture.h"
- namespace BansheeEngine
- {
- const String& GUIToggle::getGUITypeName()
- {
- static String name = "Toggle";
- return name;
- }
- GUIToggle::GUIToggle(const String& styleName, const GUIContent& content, std::shared_ptr<GUIToggleGroup> toggleGroup, const GUIDimensions& dimensions)
- :GUIButtonBase(styleName, content, dimensions), mIsToggled(false), mToggleGroup(nullptr)
- {
- if(toggleGroup != nullptr)
- toggleGroup->_add(this);
- }
- GUIToggle::~GUIToggle()
- {
- if(mToggleGroup != nullptr)
- {
- mToggleGroup->_remove(this);
- }
- }
- GUIToggle* GUIToggle::create(const HString& text, const String& styleName)
- {
- return create(GUIContent(text), styleName);
- }
- GUIToggle* GUIToggle::create(const HString& text, const GUIOptions& options, const String& styleName)
- {
- return create(GUIContent(text), options, styleName);
- }
- GUIToggle* GUIToggle::create(const HString& text, std::shared_ptr<GUIToggleGroup> toggleGroup, const String& styleName)
- {
- return create(GUIContent(text), toggleGroup, styleName);
- }
- GUIToggle* GUIToggle::create(const HString& text, std::shared_ptr<GUIToggleGroup> toggleGroup,
- const GUIOptions& options, const String& styleName)
- {
- return create(GUIContent(text), toggleGroup, options, styleName);
- }
- GUIToggle* GUIToggle::create(const GUIContent& content, const String& styleName)
- {
- return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, nullptr, GUIDimensions::create());
- }
- GUIToggle* GUIToggle::create(const GUIContent& content, const GUIOptions& options, const String& styleName)
- {
- return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, nullptr, GUIDimensions::create(options));
- }
- GUIToggle* GUIToggle::create(const GUIContent& content, std::shared_ptr<GUIToggleGroup> toggleGroup, const String& styleName)
- {
- return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, toggleGroup, GUIDimensions::create());
- }
- GUIToggle* GUIToggle::create(const GUIContent& content, std::shared_ptr<GUIToggleGroup> toggleGroup,
- const GUIOptions& options, const String& styleName)
- {
- return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, toggleGroup, GUIDimensions::create(options));
- }
- std::shared_ptr<GUIToggleGroup> GUIToggle::createToggleGroup(bool allowAllOff)
- {
- std::shared_ptr<GUIToggleGroup> toggleGroup = std::shared_ptr<GUIToggleGroup>(new GUIToggleGroup(allowAllOff));
- toggleGroup->initialize(toggleGroup);
- return toggleGroup;
- }
- void GUIToggle::_setToggleGroup(std::shared_ptr<GUIToggleGroup> toggleGroup)
- {
- mToggleGroup = toggleGroup;
- bool isToggled = false;
- if(mToggleGroup != nullptr) // If in group ensure at least one element is toggled on
- {
- for(auto& toggleElem : mToggleGroup->mButtons)
- {
- if(isToggled)
- {
- if(toggleElem->mIsToggled)
- toggleElem->toggleOff();
- }
- else
- {
- if(toggleElem->mIsToggled)
- isToggled = true;
- }
- }
- if(!isToggled && !toggleGroup->mAllowAllOff)
- toggleOn();
- }
- }
- void GUIToggle::toggleOn()
- {
- if(mIsToggled)
- return;
- mIsToggled = true;
- if(!onToggled.empty())
- onToggled(mIsToggled);
- if(mToggleGroup != nullptr)
- {
- for(auto& toggleElem : mToggleGroup->mButtons)
- {
- if(toggleElem != this)
- toggleElem->toggleOff();
- }
- }
- _setOn(true);
- }
- void GUIToggle::toggleOff()
- {
- if(!mIsToggled)
- return;
- bool canBeToggledOff = false;
- if(mToggleGroup != nullptr) // If in group ensure at least one element is toggled on
- {
- for(auto& toggleElem : mToggleGroup->mButtons)
- {
- if(toggleElem != this)
- {
- if(toggleElem->mIsToggled)
- {
- canBeToggledOff = true;
- break;
- }
- }
- }
- }
- else
- canBeToggledOff = true;
- if (canBeToggledOff || mToggleGroup->mAllowAllOff)
- {
- mIsToggled = false;
- if(!onToggled.empty())
- onToggled(mIsToggled);
- _setOn(false);
- }
- }
- bool GUIToggle::_mouseEvent(const GUIMouseEvent& ev)
- {
- bool processed = GUIButtonBase::_mouseEvent(ev);
- if(ev.getType() == GUIMouseEventType::MouseUp)
- {
- if (!_isDisabled())
- {
- if (mIsToggled)
- toggleOff();
- else
- toggleOn();
- }
- processed = true;
- }
- return processed;
- }
- }
|