2012-04-17 16:00:18 +08:00
|
|
|
/*
|
2012-09-25 17:26:09 +08:00
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* http://www.cocos2d-x.org
|
|
|
|
*
|
2012-04-17 16:00:18 +08:00
|
|
|
* Copyright 2012 Yannick Loriot. All rights reserved.
|
|
|
|
* http://yannickloriot.com
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCControlSwitch.h"
|
2012-04-27 18:47:49 +08:00
|
|
|
#include "cocos2d.h"
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-04-17 16:00:18 +08:00
|
|
|
// CCControlSwitchSprite
|
|
|
|
|
|
|
|
class CCControlSwitchSprite : public CCSprite, public CCActionTweenDelegate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCControlSwitchSprite();
|
|
|
|
virtual ~CCControlSwitchSprite();
|
2012-04-17 16:39:00 +08:00
|
|
|
bool initWithMaskSprite(
|
2012-04-17 16:00:18 +08:00
|
|
|
CCSprite *maskSprite,
|
|
|
|
CCSprite *onSprite,
|
|
|
|
CCSprite *offSprite,
|
|
|
|
CCSprite *thumbSprite,
|
|
|
|
CCLabelTTF* onLabel,
|
|
|
|
CCLabelTTF* offLabel);
|
|
|
|
void draw();
|
|
|
|
void needsLayout();
|
2012-08-01 15:30:12 +08:00
|
|
|
void setSliderXPosition(float sliderXPosition);
|
|
|
|
float getSliderXPosition() {return m_fSliderXPosition;}
|
|
|
|
float onSideWidth();
|
|
|
|
float offSideWidth();
|
2012-04-17 16:00:18 +08:00
|
|
|
virtual void updateTweenAction(float value, const char* key);
|
|
|
|
/** Contains the position (in x-axis) of the slider inside the receiver. */
|
2012-08-01 15:30:12 +08:00
|
|
|
float m_fSliderXPosition;
|
|
|
|
CC_SYNTHESIZE(float, m_fOnPosition, OnPosition)
|
|
|
|
CC_SYNTHESIZE(float, m_fOffPosition, OffPosition)
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
CC_SYNTHESIZE_RETAIN(CCTexture2D*, m_pMaskTexture, MaskTexture)
|
|
|
|
CC_SYNTHESIZE(GLuint, m_uTextureLocation, TextureLocation)
|
|
|
|
CC_SYNTHESIZE(GLuint, m_uMaskLocation, MaskLocation)
|
|
|
|
|
|
|
|
CC_SYNTHESIZE_RETAIN(CCSprite*, m_pOnSprite, OnSprite)
|
|
|
|
CC_SYNTHESIZE_RETAIN(CCSprite*, m_pOffSprite, OffSprite)
|
|
|
|
CC_SYNTHESIZE_RETAIN(CCSprite*, m_ThumbSprite, ThumbSprite)
|
|
|
|
CC_SYNTHESIZE_RETAIN(CCLabelTTF*, m_pOnLabel, OnLabel)
|
|
|
|
CC_SYNTHESIZE_RETAIN(CCLabelTTF*, m_pOffLabel, OffLabel)
|
|
|
|
};
|
|
|
|
|
|
|
|
CCControlSwitchSprite::CCControlSwitchSprite()
|
|
|
|
: m_fSliderXPosition(0.0f)
|
|
|
|
, m_fOnPosition(0.0f)
|
|
|
|
, m_fOffPosition(0.0f)
|
|
|
|
, m_pMaskTexture(NULL)
|
|
|
|
, m_uTextureLocation(0)
|
|
|
|
, m_uMaskLocation(0)
|
|
|
|
, m_pOnSprite(NULL)
|
|
|
|
, m_pOffSprite(NULL)
|
|
|
|
, m_ThumbSprite(NULL)
|
|
|
|
, m_pOnLabel(NULL)
|
|
|
|
, m_pOffLabel(NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCControlSwitchSprite::~CCControlSwitchSprite()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(m_pOnSprite);
|
|
|
|
CC_SAFE_RELEASE(m_pOffSprite);
|
|
|
|
CC_SAFE_RELEASE(m_ThumbSprite);
|
|
|
|
CC_SAFE_RELEASE(m_pOnLabel);
|
|
|
|
CC_SAFE_RELEASE(m_pOffLabel);
|
|
|
|
CC_SAFE_RELEASE(m_pMaskTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlSwitchSprite::initWithMaskSprite(
|
|
|
|
CCSprite *maskSprite,
|
|
|
|
CCSprite *onSprite,
|
|
|
|
CCSprite *offSprite,
|
|
|
|
CCSprite *thumbSprite,
|
|
|
|
CCLabelTTF* onLabel,
|
|
|
|
CCLabelTTF* offLabel)
|
|
|
|
{
|
|
|
|
if (CCSprite::initWithTexture(maskSprite->getTexture()))
|
|
|
|
{
|
|
|
|
// Sets the default values
|
|
|
|
m_fOnPosition = 0;
|
|
|
|
m_fOffPosition = -onSprite->getContentSize().width + thumbSprite->getContentSize().width / 2;
|
|
|
|
m_fSliderXPosition = m_fOnPosition;
|
|
|
|
|
|
|
|
setOnSprite(onSprite);
|
|
|
|
setOffSprite(offSprite);
|
|
|
|
setThumbSprite(thumbSprite);
|
|
|
|
setOnLabel(onLabel);
|
|
|
|
setOffLabel(offLabel);
|
|
|
|
|
|
|
|
addChild(m_ThumbSprite);
|
|
|
|
|
|
|
|
// Set up the mask with the Mask shader
|
|
|
|
setMaskTexture(maskSprite->getTexture());
|
|
|
|
CCGLProgram* pProgram = new CCGLProgram();
|
|
|
|
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccExSwitchMask_frag);
|
|
|
|
setShaderProgram(pProgram);
|
|
|
|
pProgram->release();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->link();
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->updateUniforms();
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
m_uTextureLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "u_texture");
|
|
|
|
m_uMaskLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "u_mask");
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
setContentSize(m_pMaskTexture->getContentSize());
|
|
|
|
|
|
|
|
needsLayout();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitchSprite::updateTweenAction(float value, const char* key)
|
|
|
|
{
|
|
|
|
CCLOG("key = %s, value = %f", key, value);
|
|
|
|
setSliderXPosition(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitchSprite::draw()
|
|
|
|
{
|
|
|
|
CC_NODE_DRAW_SETUP();
|
|
|
|
|
|
|
|
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
|
|
|
|
ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2012-11-16 14:51:48 +08:00
|
|
|
getShaderProgram()->setUniformsForBuiltins();
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture( GL_TEXTURE_2D, getTexture()->getName());
|
|
|
|
glUniform1i(m_uTextureLocation, 0);
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glBindTexture( GL_TEXTURE_2D, m_pMaskTexture->getName() );
|
|
|
|
glUniform1i(m_uMaskLocation, 1);
|
|
|
|
|
|
|
|
#define kQuadSize sizeof(m_sQuad.bl)
|
|
|
|
long offset = (long)&m_sQuad;
|
|
|
|
|
|
|
|
// vertex
|
|
|
|
int diff = offsetof( ccV3F_C4B_T2F, vertices);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
|
|
|
|
|
|
|
// texCoods
|
|
|
|
diff = offsetof( ccV3F_C4B_T2F, texCoords);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
// color
|
|
|
|
diff = offsetof( ccV3F_C4B_T2F, colors);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitchSprite::needsLayout()
|
|
|
|
{
|
|
|
|
m_pOnSprite->setPosition(ccp(m_pOnSprite->getContentSize().width / 2 + m_fSliderXPosition,
|
|
|
|
m_pOnSprite->getContentSize().height / 2));
|
|
|
|
m_pOffSprite->setPosition(ccp(m_pOnSprite->getContentSize().width + m_pOffSprite->getContentSize().width / 2 + m_fSliderXPosition,
|
|
|
|
m_pOffSprite->getContentSize().height / 2));
|
|
|
|
m_ThumbSprite->setPosition(ccp(m_pOnSprite->getContentSize().width + m_fSliderXPosition,
|
|
|
|
m_pMaskTexture->getContentSize().height / 2));
|
|
|
|
|
|
|
|
if (m_pOnLabel)
|
|
|
|
{
|
|
|
|
m_pOnLabel->setPosition(ccp(m_pOnSprite->getPosition().x - m_ThumbSprite->getContentSize().width / 6,
|
|
|
|
m_pOnSprite->getContentSize().height / 2));
|
|
|
|
}
|
|
|
|
if (m_pOffLabel)
|
|
|
|
{
|
|
|
|
m_pOffLabel->setPosition(ccp(m_pOffSprite->getPosition().x + m_ThumbSprite->getContentSize().width / 6,
|
|
|
|
m_pOffSprite->getContentSize().height / 2));
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCRenderTexture *rt = CCRenderTexture::create((int)m_pMaskTexture->getContentSize().width, (int)m_pMaskTexture->getContentSize().height);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
rt->begin();
|
|
|
|
m_pOnSprite->visit();
|
|
|
|
m_pOffSprite->visit();
|
|
|
|
|
|
|
|
if (m_pOnLabel)
|
|
|
|
{
|
|
|
|
m_pOnLabel->visit();
|
|
|
|
}
|
|
|
|
if (m_pOffLabel)
|
|
|
|
{
|
|
|
|
m_pOffLabel->visit();
|
|
|
|
}
|
|
|
|
|
|
|
|
rt->end();
|
|
|
|
|
|
|
|
setTexture(rt->getSprite()->getTexture());
|
|
|
|
setFlipY(true);
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
void CCControlSwitchSprite::setSliderXPosition(float sliderXPosition)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
if (sliderXPosition <= m_fOffPosition)
|
|
|
|
{
|
|
|
|
// Off
|
|
|
|
sliderXPosition = m_fOffPosition;
|
|
|
|
} else if (sliderXPosition >= m_fOnPosition)
|
|
|
|
{
|
|
|
|
// On
|
|
|
|
sliderXPosition = m_fOnPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_fSliderXPosition = sliderXPosition;
|
|
|
|
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCControlSwitchSprite::onSideWidth()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
return m_pOnSprite->getContentSize().width;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCControlSwitchSprite::offSideWidth()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
return m_pOffSprite->getContentSize().height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CCControlSwitch
|
|
|
|
|
|
|
|
CCControlSwitch::CCControlSwitch()
|
2012-09-25 16:57:51 +08:00
|
|
|
: m_pSwitchSprite(NULL)
|
|
|
|
, m_fInitialTouchXPosition(0.0f)
|
|
|
|
, m_bMoved(false)
|
|
|
|
, m_bOn(false)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCControlSwitch::~CCControlSwitch()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(m_pSwitchSprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlSwitch::initWithMaskSprite(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite)
|
|
|
|
{
|
|
|
|
return initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlSwitch* CCControlSwitch::create(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
CCControlSwitch* pRet = new CCControlSwitch();
|
|
|
|
if (pRet && pRet->initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, NULL, NULL))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlSwitch::initWithMaskSprite(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite, CCLabelTTF* onLabel, CCLabelTTF* offLabel)
|
|
|
|
{
|
|
|
|
if (CCControl::init())
|
|
|
|
{
|
|
|
|
CCAssert(maskSprite, "Mask must not be nil.");
|
|
|
|
CCAssert(onSprite, "onSprite must not be nil.");
|
|
|
|
CCAssert(offSprite, "offSprite must not be nil.");
|
|
|
|
CCAssert(thumbSprite, "thumbSprite must not be nil.");
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-04-17 16:00:18 +08:00
|
|
|
m_bOn = true;
|
|
|
|
|
|
|
|
m_pSwitchSprite = new CCControlSwitchSprite();
|
|
|
|
m_pSwitchSprite->initWithMaskSprite(maskSprite,
|
|
|
|
onSprite,
|
|
|
|
offSprite,
|
|
|
|
thumbSprite,
|
|
|
|
onLabel,
|
|
|
|
offLabel);
|
|
|
|
m_pSwitchSprite->setPosition(ccp (m_pSwitchSprite->getContentSize().width / 2, m_pSwitchSprite->getContentSize().height / 2));
|
|
|
|
addChild(m_pSwitchSprite);
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
ignoreAnchorPointForPosition(false);
|
2012-04-17 16:00:18 +08:00
|
|
|
setAnchorPoint(ccp (0.5f, 0.5f));
|
|
|
|
setContentSize(m_pSwitchSprite->getContentSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlSwitch* CCControlSwitch::create(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite, CCLabelTTF* onLabel, CCLabelTTF* offLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
CCControlSwitch* pRet = new CCControlSwitch();
|
|
|
|
if (pRet && pRet->initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, onLabel, offLabel))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitch::setOn(bool isOn)
|
|
|
|
{
|
|
|
|
setOn(isOn, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitch::setOn(bool isOn, bool animated)
|
|
|
|
{
|
|
|
|
m_bOn = isOn;
|
2013-01-31 22:12:37 +08:00
|
|
|
|
|
|
|
if (animated) {
|
|
|
|
m_pSwitchSprite->runAction
|
|
|
|
(
|
|
|
|
CCActionTween::create
|
|
|
|
(
|
|
|
|
0.2f,
|
|
|
|
"sliderXPosition",
|
|
|
|
m_pSwitchSprite->getSliderXPosition(),
|
|
|
|
(m_bOn) ? m_pSwitchSprite->getOnPosition() : m_pSwitchSprite->getOffPosition()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_pSwitchSprite->setSliderXPosition((m_bOn) ? m_pSwitchSprite->getOnPosition() : m_pSwitchSprite->getOffPosition());
|
|
|
|
}
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
sendActionsForControlEvents(CCControlEventValueChanged);
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
void CCControlSwitch::setEnabled(bool enabled)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
m_bEnabled = enabled;
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_pSwitchSprite != NULL)
|
|
|
|
{
|
|
|
|
m_pSwitchSprite->setOpacity((enabled) ? 255 : 128);
|
|
|
|
}
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint CCControlSwitch::locationFromTouch(CCTouch* pTouch)
|
|
|
|
{
|
2012-07-31 17:41:53 +08:00
|
|
|
CCPoint touchLocation = pTouch->getLocation(); // Get the touch position
|
2012-04-17 16:00:18 +08:00
|
|
|
touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
|
|
|
|
|
|
|
|
return touchLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlSwitch::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!isTouchInside(pTouch) || !isEnabled() || !isVisible())
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bMoved = false;
|
|
|
|
|
|
|
|
CCPoint location = this->locationFromTouch(pTouch);
|
|
|
|
|
|
|
|
m_fInitialTouchXPosition = location.x - m_pSwitchSprite->getSliderXPosition();
|
|
|
|
|
|
|
|
m_pSwitchSprite->getThumbSprite()->setColor(ccGRAY);
|
|
|
|
m_pSwitchSprite->needsLayout();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitch::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCPoint location = this->locationFromTouch(pTouch);
|
|
|
|
location = ccp (location.x - m_fInitialTouchXPosition, 0);
|
|
|
|
|
|
|
|
m_bMoved = true;
|
|
|
|
|
|
|
|
m_pSwitchSprite->setSliderXPosition(location.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitch::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCPoint location = this->locationFromTouch(pTouch);
|
|
|
|
|
|
|
|
m_pSwitchSprite->getThumbSprite()->setColor(ccWHITE);
|
|
|
|
|
|
|
|
if (hasMoved())
|
|
|
|
{
|
|
|
|
setOn(!(location.x < m_pSwitchSprite->getContentSize().width / 2), true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setOn(!m_bOn, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSwitch::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCPoint location = this->locationFromTouch(pTouch);
|
|
|
|
|
|
|
|
m_pSwitchSprite->getThumbSprite()->setColor(ccWHITE);
|
|
|
|
|
|
|
|
if (hasMoved())
|
|
|
|
{
|
|
|
|
setOn(!(location.x < m_pSwitchSprite->getContentSize().width / 2), true);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
setOn(!m_bOn, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|