BsGUIToggle.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "BsGUIToggle.h"
  2. #include "BsImageSprite.h"
  3. #include "BsCGUIWidget.h"
  4. #include "BsGUISkin.h"
  5. #include "BsSpriteTexture.h"
  6. #include "BsTextSprite.h"
  7. #include "BsGUIDimensions.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsGUIToggleGroup.h"
  10. #include "BsGUIHelper.h"
  11. #include "BsTexture.h"
  12. namespace BansheeEngine
  13. {
  14. const String& GUIToggle::getGUITypeName()
  15. {
  16. static String name = "Toggle";
  17. return name;
  18. }
  19. GUIToggle::GUIToggle(const String& styleName, const GUIContent& content, std::shared_ptr<GUIToggleGroup> toggleGroup, const GUIDimensions& dimensions)
  20. :GUIButtonBase(styleName, content, dimensions), mIsToggled(false), mToggleGroup(nullptr)
  21. {
  22. if(toggleGroup != nullptr)
  23. toggleGroup->_add(this);
  24. }
  25. GUIToggle::~GUIToggle()
  26. {
  27. if(mToggleGroup != nullptr)
  28. {
  29. mToggleGroup->_remove(this);
  30. }
  31. }
  32. GUIToggle* GUIToggle::create(const HString& text, const String& styleName)
  33. {
  34. return create(GUIContent(text), styleName);
  35. }
  36. GUIToggle* GUIToggle::create(const HString& text, const GUIOptions& options, const String& styleName)
  37. {
  38. return create(GUIContent(text), options, styleName);
  39. }
  40. GUIToggle* GUIToggle::create(const HString& text, std::shared_ptr<GUIToggleGroup> toggleGroup, const String& styleName)
  41. {
  42. return create(GUIContent(text), toggleGroup, styleName);
  43. }
  44. GUIToggle* GUIToggle::create(const HString& text, std::shared_ptr<GUIToggleGroup> toggleGroup,
  45. const GUIOptions& options, const String& styleName)
  46. {
  47. return create(GUIContent(text), toggleGroup, options, styleName);
  48. }
  49. GUIToggle* GUIToggle::create(const GUIContent& content, const String& styleName)
  50. {
  51. return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, nullptr, GUIDimensions::create());
  52. }
  53. GUIToggle* GUIToggle::create(const GUIContent& content, const GUIOptions& options, const String& styleName)
  54. {
  55. return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, nullptr, GUIDimensions::create(options));
  56. }
  57. GUIToggle* GUIToggle::create(const GUIContent& content, std::shared_ptr<GUIToggleGroup> toggleGroup, const String& styleName)
  58. {
  59. return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, toggleGroup, GUIDimensions::create());
  60. }
  61. GUIToggle* GUIToggle::create(const GUIContent& content, std::shared_ptr<GUIToggleGroup> toggleGroup,
  62. const GUIOptions& options, const String& styleName)
  63. {
  64. return new (bs_alloc<GUIToggle>()) GUIToggle(getStyleName<GUIToggle>(styleName), content, toggleGroup, GUIDimensions::create(options));
  65. }
  66. std::shared_ptr<GUIToggleGroup> GUIToggle::createToggleGroup(bool allowAllOff)
  67. {
  68. std::shared_ptr<GUIToggleGroup> toggleGroup = std::shared_ptr<GUIToggleGroup>(new GUIToggleGroup(allowAllOff));
  69. toggleGroup->initialize(toggleGroup);
  70. return toggleGroup;
  71. }
  72. void GUIToggle::_setToggleGroup(std::shared_ptr<GUIToggleGroup> toggleGroup)
  73. {
  74. mToggleGroup = toggleGroup;
  75. bool isToggled = false;
  76. if(mToggleGroup != nullptr) // If in group ensure at least one element is toggled on
  77. {
  78. for(auto& toggleElem : mToggleGroup->mButtons)
  79. {
  80. if(isToggled)
  81. {
  82. if(toggleElem->mIsToggled)
  83. toggleElem->toggleOff();
  84. }
  85. else
  86. {
  87. if(toggleElem->mIsToggled)
  88. isToggled = true;
  89. }
  90. }
  91. if(!isToggled && !toggleGroup->mAllowAllOff)
  92. toggleOn();
  93. }
  94. }
  95. void GUIToggle::toggleOn()
  96. {
  97. if(mIsToggled)
  98. return;
  99. mIsToggled = true;
  100. if(!onToggled.empty())
  101. onToggled(mIsToggled);
  102. if(mToggleGroup != nullptr)
  103. {
  104. for(auto& toggleElem : mToggleGroup->mButtons)
  105. {
  106. if(toggleElem != this)
  107. toggleElem->toggleOff();
  108. }
  109. }
  110. _setOn(true);
  111. }
  112. void GUIToggle::toggleOff()
  113. {
  114. if(!mIsToggled)
  115. return;
  116. bool canBeToggledOff = false;
  117. if(mToggleGroup != nullptr) // If in group ensure at least one element is toggled on
  118. {
  119. for(auto& toggleElem : mToggleGroup->mButtons)
  120. {
  121. if(toggleElem != this)
  122. {
  123. if(toggleElem->mIsToggled)
  124. {
  125. canBeToggledOff = true;
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. else
  132. canBeToggledOff = true;
  133. if (canBeToggledOff || mToggleGroup->mAllowAllOff)
  134. {
  135. mIsToggled = false;
  136. if(!onToggled.empty())
  137. onToggled(mIsToggled);
  138. _setOn(false);
  139. }
  140. }
  141. bool GUIToggle::_mouseEvent(const GUIMouseEvent& ev)
  142. {
  143. bool processed = GUIButtonBase::_mouseEvent(ev);
  144. if(ev.getType() == GUIMouseEventType::MouseUp)
  145. {
  146. if (!_isDisabled())
  147. {
  148. if (mIsToggled)
  149. toggleOff();
  150. else
  151. toggleOn();
  152. }
  153. processed = true;
  154. }
  155. return processed;
  156. }
  157. }