BsGUITabButton.cpp 4.3 KB

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