// // EXControlButton.cpp // cocos2d_libs // // Created by ZhengSong on 2020/4/14. // #include "EXControlButton.h" #include "2d/CCAction.h" #include "2d/CCActionInterval.h" NS_CC_EXT_BEGIN enum { kZoomActionTag = 0xCCCB0001, }; EXControlButton* EXControlButton::create() { EXControlButton *pControlButton = new (std::nothrow) EXControlButton(); if (pControlButton && pControlButton->init()) { pControlButton->autorelease(); return pControlButton; } CC_SAFE_DELETE(pControlButton); return nullptr; } EXControlButton::EXControlButton() : _isPushed(false) , _zoomOnTouchDown(false) { } bool EXControlButton::init(){ if (Control::init()) { _isPushed = false; // Adjust the background image by adjustBackGroundSize setPreferredSize(Size::ZERO); // Zooming button by default _zoomOnTouchDown = true; _scaleRatio = 1.1f; // Set the default anchor point setIgnoreAnchorPointForPosition(false); setAnchorPoint(Vec2::ANCHOR_MIDDLE); // Set the default color and opacity setColor(Color3B::WHITE); setOpacity(255.0f); setOpacityModifyRGB(true); // Layout update needsLayout(); return true; } //couldn't init the Control else { return false; } } void EXControlButton::setHighlighted(bool enabled) { if (enabled == true) { _state = Control::State::HIGH_LIGHTED; } else { _state = Control::State::NORMAL; } Control::setHighlighted(enabled); Action *action = getActionByTag(kZoomActionTag); if (action) { stopAction(action); } needsLayout(); if( _zoomOnTouchDown ) { float scaleValue = (isHighlighted() && isEnabled() && !isSelected()) ? _scaleRatio : 1.0f; Action *zoomAction = ScaleTo::create(0.05f, scaleValue); zoomAction->setTag(kZoomActionTag); runAction(zoomAction); } } void EXControlButton::setZoomOnTouchDown(bool zoomOnTouchDown) { _zoomOnTouchDown = zoomOnTouchDown; } bool EXControlButton::getZoomOnTouchDown() const { return _zoomOnTouchDown; } void EXControlButton::setPreferredSize(const Size& size) { _preferredSize = size; this->setContentSize(size); needsLayout(); } void EXControlButton::setContentSize(const Size& size){ Control::setContentSize(size); } const Size& EXControlButton::getPreferredSize() const { return _preferredSize; } bool EXControlButton::onTouchBegan(Touch *pTouch, Event* /*pEvent*/) { if (!isTouchInside(pTouch) || !isEnabled() || !isVisible() || !hasVisibleParents() ) { return false; } for (Node *c = this->_parent; c != nullptr; c = c->getParent()) { if (c->isVisible() == false) { return false; } } _isPushed = true; this->setHighlighted(true); sendActionsForControlEvents(Control::EventType::TOUCH_DOWN); return true; } void EXControlButton::onTouchMoved(Touch *pTouch, Event* /*pEvent*/) { if (!isEnabled() || !isPushed() || isSelected()) { if (isHighlighted()) { setHighlighted(false); } return; } bool isTouchMoveInside = isTouchInside(pTouch); if (isTouchMoveInside && !isHighlighted()) { setHighlighted(true); sendActionsForControlEvents(Control::EventType::DRAG_ENTER); } else if (isTouchMoveInside && isHighlighted()) { sendActionsForControlEvents(Control::EventType::DRAG_INSIDE); } else if (!isTouchMoveInside && isHighlighted()) { setHighlighted(false); sendActionsForControlEvents(Control::EventType::DRAG_EXIT); } else if (!isTouchMoveInside && !isHighlighted()) { sendActionsForControlEvents(Control::EventType::DRAG_OUTSIDE); } } void EXControlButton::onTouchEnded(Touch *pTouch, Event* /*pEvent*/) { _isPushed = false; setHighlighted(false); if (isTouchInside(pTouch)) { sendActionsForControlEvents(Control::EventType::TOUCH_UP_INSIDE); } else { sendActionsForControlEvents(Control::EventType::TOUCH_UP_OUTSIDE); } } void EXControlButton::onTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/) { _isPushed = false; setHighlighted(false); sendActionsForControlEvents(Control::EventType::TOUCH_CANCEL); } NS_CC_EXT_END