// // REDNodeButton.cpp // cocos2d_libs // // Created by ZhengSong on 2020/4/14. // #include "REDNodeButton.h" #include "2d/CCAction.h" #include "2d/CCActionInterval.h" #include "RPRedAudio.hpp" using namespace cocos2d; namespace redream { enum { kZoomActionTag = 0xCCCB0001, }; REDNodeButton* REDNodeButton::create() { REDNodeButton *pControlButton = new (std::nothrow) REDNodeButton(); if (pControlButton && pControlButton->init()) { pControlButton->autorelease(); return pControlButton; } CC_SAFE_DELETE(pControlButton); return nullptr; } REDNodeButton::REDNodeButton() : _isPushed(false) , _zoomOnTouchDown(false) { } bool REDNodeButton::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 REDNodeButton::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 REDNodeButton::setZoomOnTouchDown(bool zoomOnTouchDown) { _zoomOnTouchDown = zoomOnTouchDown; } bool REDNodeButton::getZoomOnTouchDown() const { return _zoomOnTouchDown; } void REDNodeButton::setPreferredSize(const Size& size) { _preferredSize = size; this->setContentSize(size); needsLayout(); } void REDNodeButton::setContentSize(const Size& size){ Control::setContentSize(size); } const Size& REDNodeButton::getPreferredSize() const { return _preferredSize; } bool REDNodeButton::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); if(_wiseBnkFile4TouchDown.size() > 0 && _wiseEvent4TouchDown.size() > 0){ redprotocol::RedAudio::getInstance()->playAudio(_wiseBnkFile4TouchDown, _wiseEvent4TouchDown, _forcePostEvent4TouchDown, _audioParams4TouchDown); } return true; } void REDNodeButton::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 REDNodeButton::onTouchEnded(Touch *pTouch, Event* /*pEvent*/) { _isPushed = false; setHighlighted(false); if (isTouchInside(pTouch)) { sendActionsForControlEvents(Control::EventType::TOUCH_UP_INSIDE); if(_wiseBnkFile4TouchUpInside.size() > 0 && _wiseEvent4TouchUpInside.size() > 0){ redprotocol::RedAudio::getInstance()->playAudio(_wiseBnkFile4TouchUpInside, _wiseEvent4TouchUpInside, _forcePostEvent4TouchUpInside, _audioParams4TouchUpInside); } } else { sendActionsForControlEvents(Control::EventType::TOUCH_UP_OUTSIDE); } } void REDNodeButton::onTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/) { _isPushed = false; setHighlighted(false); sendActionsForControlEvents(Control::EventType::TOUCH_CANCEL); } void REDNodeButton::setTouchDownAudioData(const std::string& wiseBnkFile,const std::string& wiseEvent, const std::vector& audioParams, bool forcePostEvent) { _wiseBnkFile4TouchDown = wiseBnkFile; _wiseEvent4TouchDown = wiseEvent; _audioParams4TouchDown = audioParams; _forcePostEvent4TouchDown = forcePostEvent; } void REDNodeButton::setTouchUpInsideAudioData(const std::string& wiseBnkFile,const std::string& wiseEvent, const std::vector& audioParams, bool forcePostEvent) { _wiseBnkFile4TouchUpInside = wiseBnkFile; _wiseEvent4TouchUpInside = wiseEvent; _audioParams4TouchUpInside = audioParams; _forcePostEvent4TouchUpInside = forcePostEvent; } }//namespace