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
|
2013-06-20 14:15:53 +08:00
|
|
|
// ControlSwitchSprite
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class ControlSwitchSprite : public Sprite, public ActionTweenDelegate
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitchSprite();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual ~ControlSwitchSprite();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-17 16:39:00 +08:00
|
|
|
bool initWithMaskSprite(
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite *maskSprite,
|
|
|
|
Sprite *onSprite,
|
|
|
|
Sprite *offSprite,
|
|
|
|
Sprite *thumbSprite,
|
|
|
|
LabelTTF* onLabel,
|
|
|
|
LabelTTF* offLabel);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-17 16:00:18 +08:00
|
|
|
void draw();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-17 16:00:18 +08:00
|
|
|
void needsLayout();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-08-01 15:30:12 +08:00
|
|
|
void setSliderXPosition(float sliderXPosition);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
float getSliderXPosition() {return _sliderXPosition;}
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-08-01 15:30:12 +08:00
|
|
|
float onSideWidth();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-08-01 15:30:12 +08:00
|
|
|
float offSideWidth();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
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. */
|
2013-06-15 14:03:30 +08:00
|
|
|
float _sliderXPosition;
|
|
|
|
CC_SYNTHESIZE(float, _onPosition, OnPosition)
|
|
|
|
CC_SYNTHESIZE(float, _offPosition, OffPosition)
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
CC_SYNTHESIZE_RETAIN(Texture2D*, _maskTexture, MaskTexture)
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SYNTHESIZE(GLuint, _textureLocation, TextureLocation)
|
|
|
|
CC_SYNTHESIZE(GLuint, _maskLocation, MaskLocation)
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
CC_SYNTHESIZE_RETAIN(Sprite*, _onSprite, OnSprite)
|
|
|
|
CC_SYNTHESIZE_RETAIN(Sprite*, _offSprite, OffSprite)
|
|
|
|
CC_SYNTHESIZE_RETAIN(Sprite*, _thumbSprite, ThumbSprite)
|
|
|
|
CC_SYNTHESIZE_RETAIN(LabelTTF*, _onLabel, OnLabel)
|
|
|
|
CC_SYNTHESIZE_RETAIN(LabelTTF*, _offLabel, OffLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitchSprite::ControlSwitchSprite()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _sliderXPosition(0.0f)
|
|
|
|
, _onPosition(0.0f)
|
|
|
|
, _offPosition(0.0f)
|
|
|
|
, _maskTexture(NULL)
|
|
|
|
, _textureLocation(0)
|
|
|
|
, _maskLocation(0)
|
|
|
|
, _onSprite(NULL)
|
|
|
|
, _offSprite(NULL)
|
|
|
|
, _thumbSprite(NULL)
|
|
|
|
, _onLabel(NULL)
|
|
|
|
, _offLabel(NULL)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitchSprite::~ControlSwitchSprite()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_onSprite);
|
|
|
|
CC_SAFE_RELEASE(_offSprite);
|
|
|
|
CC_SAFE_RELEASE(_thumbSprite);
|
|
|
|
CC_SAFE_RELEASE(_onLabel);
|
|
|
|
CC_SAFE_RELEASE(_offLabel);
|
|
|
|
CC_SAFE_RELEASE(_maskTexture);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlSwitchSprite::initWithMaskSprite(
|
|
|
|
Sprite *maskSprite,
|
|
|
|
Sprite *onSprite,
|
|
|
|
Sprite *offSprite,
|
|
|
|
Sprite *thumbSprite,
|
|
|
|
LabelTTF* onLabel,
|
|
|
|
LabelTTF* offLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Sprite::initWithTexture(maskSprite->getTexture()))
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
// Sets the default values
|
2013-06-15 14:03:30 +08:00
|
|
|
_onPosition = 0;
|
|
|
|
_offPosition = -onSprite->getContentSize().width + thumbSprite->getContentSize().width / 2;
|
|
|
|
_sliderXPosition = _onPosition;
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
setOnSprite(onSprite);
|
|
|
|
setOffSprite(offSprite);
|
|
|
|
setThumbSprite(thumbSprite);
|
|
|
|
setOnLabel(onLabel);
|
|
|
|
setOffLabel(offLabel);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_thumbSprite);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
// Set up the mask with the Mask shader
|
|
|
|
setMaskTexture(maskSprite->getTexture());
|
2013-06-20 14:15:53 +08:00
|
|
|
GLProgram* pProgram = new GLProgram();
|
2012-04-17 16:00:18 +08:00
|
|
|
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccExSwitchMask_frag);
|
|
|
|
setShaderProgram(pProgram);
|
|
|
|
pProgram->release();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2012-04-17 16:00:18 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->link();
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->updateUniforms();
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "u_texture");
|
|
|
|
_maskLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "u_mask");
|
2012-04-17 16:00:18 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
setContentSize(_maskTexture->getContentSize());
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
needsLayout();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitchSprite::updateTweenAction(float value, const char* key)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
CCLOG("key = %s, value = %f", key, value);
|
|
|
|
setSliderXPosition(value);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitchSprite::draw()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
CC_NODE_DRAW_SETUP();
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
|
|
|
GL::blendFunc(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
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2DN(0, getTexture()->getName());
|
2013-06-15 14:03:30 +08:00
|
|
|
glUniform1i(_textureLocation, 0);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2DN(1, _maskTexture->getName());
|
2013-06-15 14:03:30 +08:00
|
|
|
glUniform1i(_maskLocation, 1);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quad.bl)
|
2013-04-09 12:09:09 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
long offset = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
setGLBufferData(&_quad, 4 * kQuadSize, 0);
|
2013-04-09 12:09:09 +08:00
|
|
|
#else
|
2013-06-15 14:03:30 +08:00
|
|
|
long offset = (long)&_quad;
|
2013-04-09 12:09:09 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
// vertex
|
2013-07-05 16:49:22 +08:00
|
|
|
int diff = offsetof( V3F_C4B_T2F, vertices);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
// texCoods
|
2013-07-05 16:49:22 +08:00
|
|
|
diff = offsetof( V3F_C4B_T2F, texCoords);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
// color
|
2013-07-05 16:49:22 +08:00
|
|
|
diff = offsetof( V3F_C4B_T2F, colors);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-07-09 17:23:13 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2DN(0, 0);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitchSprite::needsLayout()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_onSprite->setPosition(Point(_onSprite->getContentSize().width / 2 + _sliderXPosition,
|
2013-06-15 14:03:30 +08:00
|
|
|
_onSprite->getContentSize().height / 2));
|
2013-07-12 14:11:55 +08:00
|
|
|
_offSprite->setPosition(Point(_onSprite->getContentSize().width + _offSprite->getContentSize().width / 2 + _sliderXPosition,
|
2013-06-15 14:03:30 +08:00
|
|
|
_offSprite->getContentSize().height / 2));
|
2013-07-12 14:11:55 +08:00
|
|
|
_thumbSprite->setPosition(Point(_onSprite->getContentSize().width + _sliderXPosition,
|
2013-06-15 14:03:30 +08:00
|
|
|
_maskTexture->getContentSize().height / 2));
|
|
|
|
|
|
|
|
if (_onLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_onLabel->setPosition(Point(_onSprite->getPosition().x - _thumbSprite->getContentSize().width / 6,
|
2013-06-15 14:03:30 +08:00
|
|
|
_onSprite->getContentSize().height / 2));
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_offLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_offLabel->setPosition(Point(_offSprite->getPosition().x + _thumbSprite->getContentSize().width / 6,
|
2013-06-15 14:03:30 +08:00
|
|
|
_offSprite->getContentSize().height / 2));
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
RenderTexture *rt = RenderTexture::create((int)_maskTexture->getContentSize().width, (int)_maskTexture->getContentSize().height);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
rt->begin();
|
2013-06-15 14:03:30 +08:00
|
|
|
_onSprite->visit();
|
|
|
|
_offSprite->visit();
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_onLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_onLabel->visit();
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_offLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_offLabel->visit();
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt->end();
|
|
|
|
|
|
|
|
setTexture(rt->getSprite()->getTexture());
|
|
|
|
setFlipY(true);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitchSprite::setSliderXPosition(float sliderXPosition)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (sliderXPosition <= _offPosition)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
// Off
|
2013-06-15 14:03:30 +08:00
|
|
|
sliderXPosition = _offPosition;
|
|
|
|
} else if (sliderXPosition >= _onPosition)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
// On
|
2013-06-15 14:03:30 +08:00
|
|
|
sliderXPosition = _onPosition;
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_sliderXPosition = sliderXPosition;
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlSwitchSprite::onSideWidth()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _onSprite->getContentSize().width;
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlSwitchSprite::offSideWidth()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offSprite->getContentSize().height;
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
// ControlSwitch
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitch::ControlSwitch()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _switchSprite(NULL)
|
|
|
|
, _initialTouchXPosition(0.0f)
|
|
|
|
, _moved(false)
|
|
|
|
, _on(false)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitch::~ControlSwitch()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_switchSprite);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlSwitch::initWithMaskSprite(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
return initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitch* ControlSwitch::create(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitch* pRet = new ControlSwitch();
|
2012-04-17 16:00:18 +08:00
|
|
|
if (pRet && pRet->initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, NULL, NULL))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlSwitch::initWithMaskSprite(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite, LabelTTF* onLabel, LabelTTF* offLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Control::init())
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
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-04-17 16:00:18 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2013-06-15 14:03:30 +08:00
|
|
|
_on = true;
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
_switchSprite = new ControlSwitchSprite();
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->initWithMaskSprite(maskSprite,
|
2012-04-17 16:00:18 +08:00
|
|
|
onSprite,
|
|
|
|
offSprite,
|
|
|
|
thumbSprite,
|
|
|
|
onLabel,
|
|
|
|
offLabel);
|
2013-07-12 14:11:55 +08:00
|
|
|
_switchSprite->setPosition(Point(_switchSprite->getContentSize().width / 2, _switchSprite->getContentSize().height / 2));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_switchSprite);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
ignoreAnchorPointForPosition(false);
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(0.5f, 0.5f));
|
2013-06-15 14:03:30 +08:00
|
|
|
setContentSize(_switchSprite->getContentSize());
|
2012-04-17 16:00:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitch* ControlSwitch::create(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite, LabelTTF* onLabel, LabelTTF* offLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSwitch* pRet = new ControlSwitch();
|
2012-04-17 16:00:18 +08:00
|
|
|
if (pRet && pRet->initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, onLabel, offLabel))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitch::setOn(bool isOn)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
setOn(isOn, false);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitch::setOn(bool isOn, bool animated)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_on = isOn;
|
2013-01-31 22:12:37 +08:00
|
|
|
|
|
|
|
if (animated) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->runAction
|
2013-01-31 22:12:37 +08:00
|
|
|
(
|
2013-06-20 14:15:53 +08:00
|
|
|
ActionTween::create
|
2013-01-31 22:12:37 +08:00
|
|
|
(
|
|
|
|
0.2f,
|
|
|
|
"sliderXPosition",
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->getSliderXPosition(),
|
|
|
|
(_on) ? _switchSprite->getOnPosition() : _switchSprite->getOffPosition()
|
2013-01-31 22:12:37 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->setSliderXPosition((_on) ? _switchSprite->getOnPosition() : _switchSprite->getOffPosition());
|
2013-01-31 22:12:37 +08:00
|
|
|
}
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitch::setEnabled(bool enabled)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabled = enabled;
|
|
|
|
if (_switchSprite != NULL)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->setOpacity((enabled) ? 255 : 128);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point ControlSwitch::locationFromTouch(Touch* pTouch)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point 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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlSwitch::ccTouchBegan(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!isTouchInside(pTouch) || !isEnabled() || !isVisible())
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_moved = false;
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = this->locationFromTouch(pTouch);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_initialTouchXPosition = location.x - _switchSprite->getSliderXPosition();
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-07-08 18:11:32 +08:00
|
|
|
_switchSprite->getThumbSprite()->setColor(Color3B::GRAY);
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->needsLayout();
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitch::ccTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = this->locationFromTouch(pTouch);
|
2013-07-12 14:11:55 +08:00
|
|
|
location = Point(location.x - _initialTouchXPosition, 0);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_moved = true;
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_switchSprite->setSliderXPosition(location.x);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitch::ccTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = this->locationFromTouch(pTouch);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-07-08 18:11:32 +08:00
|
|
|
_switchSprite->getThumbSprite()->setColor(Color3B::WHITE);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
if (hasMoved())
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
setOn(!(location.x < _switchSprite->getContentSize().width / 2), true);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
setOn(!_on, true);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitch::ccTouchCancelled(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = this->locationFromTouch(pTouch);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-07-08 18:11:32 +08:00
|
|
|
_switchSprite->getThumbSprite()->setColor(Color3B::WHITE);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
if (hasMoved())
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
setOn(!(location.x < _switchSprite->getContentSize().width / 2), true);
|
2012-04-17 16:00:18 +08:00
|
|
|
} else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
setOn(!_on, true);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|