BsGUITabButton.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUITabButton.h"
  4. #include "GUI/BsCGUIWidget.h"
  5. #include "GUI/BsGUISkin.h"
  6. #include "GUI/BsGUIDimensions.h"
  7. #include "GUI/BsGUIMouseEvent.h"
  8. #include "GUI/BsGUITabbedTitleBar.h"
  9. namespace bs
  10. {
  11. const UINT32 GUITabButton::DRAG_MIN_DISTANCE = 3;
  12. const String& GUITabButton::getGUITypeName()
  13. {
  14. static String name = "TabButton";
  15. return name;
  16. }
  17. GUITabButton::GUITabButton(const String& styleName, const SPtr<GUIToggleGroup>& toggleGroup,
  18. UINT32 index, const GUIContent& content, const GUIDimensions& dimensions)
  19. :GUIToggle(styleName, content, toggleGroup, dimensions), mIndex(index), mDraggedState(false)
  20. {
  21. }
  22. GUITabButton* GUITabButton::create(const SPtr<GUIToggleGroup>& toggleGroup, UINT32 index,
  23. const HString& text, const String& styleName)
  24. {
  25. return new (bs_alloc<GUITabButton>()) GUITabButton(
  26. getStyleName<GUITabButton>(styleName), toggleGroup, index, GUIContent(text), GUIDimensions::create());
  27. }
  28. GUITabButton* GUITabButton::create(const SPtr<GUIToggleGroup>& toggleGroup, UINT32 index,
  29. const HString& text, const GUIOptions& options, const String& styleName)
  30. {
  31. return new (bs_alloc<GUITabButton>()) GUITabButton(
  32. getStyleName<GUITabButton>(styleName), toggleGroup, index, GUIContent(text), GUIDimensions::create(options));
  33. }
  34. GUITabButton* GUITabButton::create(const SPtr<GUIToggleGroup>& toggleGroup, UINT32 index,
  35. const GUIContent& content, const String& styleName)
  36. {
  37. return new (bs_alloc<GUITabButton>()) GUITabButton(
  38. getStyleName<GUITabButton>(styleName), toggleGroup, index, content, GUIDimensions::create());
  39. }
  40. GUITabButton* GUITabButton::create(const SPtr<GUIToggleGroup>& toggleGroup, UINT32 index,
  41. const GUIContent& content, const GUIOptions& options, const String& styleName)
  42. {
  43. return new (bs_alloc<GUITabButton>()) GUITabButton(
  44. getStyleName<GUITabButton>(styleName), toggleGroup, index, content, GUIDimensions::create(options));
  45. }
  46. void GUITabButton::_toggleOn(bool triggerEvents)
  47. {
  48. _setElementDepth(0);
  49. GUIToggle::_toggleOn(triggerEvents);
  50. }
  51. void GUITabButton::_toggleOff(bool triggerEvents)
  52. {
  53. _setElementDepth(2);
  54. GUIToggle::_toggleOff(triggerEvents);
  55. }
  56. void GUITabButton::_setDraggedState(bool active)
  57. {
  58. if(mDraggedState == active)
  59. return;
  60. mDraggedState = active;
  61. if(mDraggedState)
  62. {
  63. mInactiveState = getState();
  64. if(mInactiveState != GUIElementState::Normal)
  65. _setState(GUIElementState::Normal);
  66. }
  67. else
  68. {
  69. if(getState() != mInactiveState)
  70. _setState(mInactiveState);
  71. }
  72. }
  73. bool GUITabButton::_mouseEvent(const GUIMouseEvent& ev)
  74. {
  75. if(ev.getType() == GUIMouseEventType::MouseOver)
  76. {
  77. if (!_isDisabled())
  78. {
  79. GUIElementState state = _isOn() ? GUIElementState::HoverOn : GUIElementState::Hover;
  80. if (!mDraggedState)
  81. {
  82. _setState(state);
  83. if (!onHover.empty())
  84. onHover();
  85. }
  86. else
  87. mInactiveState = state;
  88. }
  89. return true;
  90. }
  91. else if(ev.getType() == GUIMouseEventType::MouseOut)
  92. {
  93. if (!_isDisabled())
  94. {
  95. GUIElementState state = _isOn() ? GUIElementState::NormalOn : GUIElementState::Normal;
  96. if (!mDraggedState)
  97. {
  98. _setState(state);
  99. if (!onOut.empty())
  100. onOut();
  101. }
  102. else
  103. mInactiveState = state;
  104. }
  105. return true;
  106. }
  107. else if(ev.getType() == GUIMouseEventType::MouseDown)
  108. {
  109. if (!_isDisabled())
  110. {
  111. if (!mDraggedState)
  112. _setState(_isOn() ? GUIElementState::ActiveOn : GUIElementState::Active);
  113. _setElementDepth(0);
  114. }
  115. return true;
  116. }
  117. else if(ev.getType() == GUIMouseEventType::MouseUp)
  118. {
  119. if (!_isDisabled())
  120. {
  121. if (!mDraggedState)
  122. {
  123. _setState(_isOn() ? GUIElementState::HoverOn : GUIElementState::Hover);
  124. if (!onClick.empty())
  125. onClick();
  126. if (!mIsToggled)
  127. _toggleOn(true);
  128. }
  129. }
  130. return true;
  131. }
  132. else if(ev.getType() == GUIMouseEventType::MouseDragStart)
  133. {
  134. if (!_isDisabled())
  135. mDragStartPosition = ev.getPosition();
  136. return true;
  137. }
  138. else if(ev.getType() == GUIMouseEventType::MouseDrag)
  139. {
  140. if (!_isDisabled())
  141. {
  142. UINT32 dist = mDragStartPosition.manhattanDist(ev.getPosition());
  143. if (dist > DRAG_MIN_DISTANCE)
  144. {
  145. if (!onDragged.empty())
  146. onDragged(mIndex, ev.getPosition());
  147. }
  148. }
  149. return true;
  150. }
  151. else if(ev.getType() == GUIMouseEventType::MouseDragEnd)
  152. {
  153. if (!_isDisabled())
  154. {
  155. UINT32 dist = mDragStartPosition.manhattanDist(ev.getPosition());
  156. if (dist > DRAG_MIN_DISTANCE)
  157. {
  158. if (!onDragEnd.empty())
  159. onDragEnd(mIndex, ev.getPosition());
  160. }
  161. }
  162. return true;
  163. }
  164. return false;
  165. }
  166. }