BsGUITabButton.cpp 4.3 KB

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