BsGUITabButton.cpp 4.1 KB

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