EXControlButton.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // EXControlButton.cpp
  3. // cocos2d_libs
  4. //
  5. // Created by ZhengSong on 2020/4/14.
  6. //
  7. #include "EXControlButton.h"
  8. #include "2d/CCAction.h"
  9. #include "2d/CCActionInterval.h"
  10. NS_CC_EXT_BEGIN
  11. enum
  12. {
  13. kZoomActionTag = 0xCCCB0001,
  14. };
  15. EXControlButton* EXControlButton::create()
  16. {
  17. EXControlButton *pControlButton = new (std::nothrow) EXControlButton();
  18. if (pControlButton && pControlButton->init())
  19. {
  20. pControlButton->autorelease();
  21. return pControlButton;
  22. }
  23. CC_SAFE_DELETE(pControlButton);
  24. return nullptr;
  25. }
  26. EXControlButton::EXControlButton()
  27. : _isPushed(false)
  28. , _zoomOnTouchDown(false)
  29. {
  30. }
  31. bool EXControlButton::init(){
  32. if (Control::init())
  33. {
  34. _isPushed = false;
  35. // Adjust the background image by adjustBackGroundSize
  36. setPreferredSize(Size::ZERO);
  37. // Zooming button by default
  38. _zoomOnTouchDown = true;
  39. _scaleRatio = 1.1f;
  40. // Set the default anchor point
  41. setIgnoreAnchorPointForPosition(false);
  42. setAnchorPoint(Vec2::ANCHOR_MIDDLE);
  43. // Set the default color and opacity
  44. setColor(Color3B::WHITE);
  45. setOpacity(255.0f);
  46. setOpacityModifyRGB(true);
  47. // Layout update
  48. needsLayout();
  49. return true;
  50. }
  51. //couldn't init the Control
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. void EXControlButton::setHighlighted(bool enabled)
  58. {
  59. if (enabled == true)
  60. {
  61. _state = Control::State::HIGH_LIGHTED;
  62. }
  63. else
  64. {
  65. _state = Control::State::NORMAL;
  66. }
  67. Control::setHighlighted(enabled);
  68. Action *action = getActionByTag(kZoomActionTag);
  69. if (action)
  70. {
  71. stopAction(action);
  72. }
  73. needsLayout();
  74. if( _zoomOnTouchDown )
  75. {
  76. float scaleValue = (isHighlighted() && isEnabled() && !isSelected()) ? _scaleRatio : 1.0f;
  77. Action *zoomAction = ScaleTo::create(0.05f, scaleValue);
  78. zoomAction->setTag(kZoomActionTag);
  79. runAction(zoomAction);
  80. }
  81. }
  82. void EXControlButton::setZoomOnTouchDown(bool zoomOnTouchDown)
  83. {
  84. _zoomOnTouchDown = zoomOnTouchDown;
  85. }
  86. bool EXControlButton::getZoomOnTouchDown() const
  87. {
  88. return _zoomOnTouchDown;
  89. }
  90. void EXControlButton::setPreferredSize(const Size& size)
  91. {
  92. _preferredSize = size;
  93. this->setContentSize(size);
  94. needsLayout();
  95. }
  96. void EXControlButton::setContentSize(const Size& size){
  97. Control::setContentSize(size);
  98. }
  99. const Size& EXControlButton::getPreferredSize() const
  100. {
  101. return _preferredSize;
  102. }
  103. bool EXControlButton::onTouchBegan(Touch *pTouch, Event* /*pEvent*/)
  104. {
  105. if (!isTouchInside(pTouch) || !isEnabled() || !isVisible() || !hasVisibleParents() )
  106. {
  107. return false;
  108. }
  109. for (Node *c = this->_parent; c != nullptr; c = c->getParent())
  110. {
  111. if (c->isVisible() == false)
  112. {
  113. return false;
  114. }
  115. }
  116. _isPushed = true;
  117. this->setHighlighted(true);
  118. sendActionsForControlEvents(Control::EventType::TOUCH_DOWN);
  119. return true;
  120. }
  121. void EXControlButton::onTouchMoved(Touch *pTouch, Event* /*pEvent*/)
  122. {
  123. if (!isEnabled() || !isPushed() || isSelected())
  124. {
  125. if (isHighlighted())
  126. {
  127. setHighlighted(false);
  128. }
  129. return;
  130. }
  131. bool isTouchMoveInside = isTouchInside(pTouch);
  132. if (isTouchMoveInside && !isHighlighted())
  133. {
  134. setHighlighted(true);
  135. sendActionsForControlEvents(Control::EventType::DRAG_ENTER);
  136. }
  137. else if (isTouchMoveInside && isHighlighted())
  138. {
  139. sendActionsForControlEvents(Control::EventType::DRAG_INSIDE);
  140. }
  141. else if (!isTouchMoveInside && isHighlighted())
  142. {
  143. setHighlighted(false);
  144. sendActionsForControlEvents(Control::EventType::DRAG_EXIT);
  145. }
  146. else if (!isTouchMoveInside && !isHighlighted())
  147. {
  148. sendActionsForControlEvents(Control::EventType::DRAG_OUTSIDE);
  149. }
  150. }
  151. void EXControlButton::onTouchEnded(Touch *pTouch, Event* /*pEvent*/)
  152. {
  153. _isPushed = false;
  154. setHighlighted(false);
  155. if (isTouchInside(pTouch))
  156. {
  157. sendActionsForControlEvents(Control::EventType::TOUCH_UP_INSIDE);
  158. }
  159. else
  160. {
  161. sendActionsForControlEvents(Control::EventType::TOUCH_UP_OUTSIDE);
  162. }
  163. }
  164. void EXControlButton::onTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/)
  165. {
  166. _isPushed = false;
  167. setHighlighted(false);
  168. sendActionsForControlEvents(Control::EventType::TOUCH_CANCEL);
  169. }
  170. NS_CC_EXT_END