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"
|
2014-01-17 13:35:58 +08:00
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CCActionTween.h"
|
2014-03-14 21:06:40 +08:00
|
|
|
#include "CCLabel.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
#include "CCClippingNode.h"
|
|
|
|
#include "ccShaders.h"
|
|
|
|
#include "CCRenderTexture.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-11-16 04:04:47 +08:00
|
|
|
/** creates an autorelease instance of ControlSwitchSprite */
|
2013-11-26 16:19:12 +08:00
|
|
|
static ControlSwitchSprite* create(
|
2013-11-16 04:04:47 +08:00
|
|
|
Sprite *maskSprite,
|
|
|
|
Sprite *onSprite,
|
|
|
|
Sprite *offSprite,
|
|
|
|
Sprite *thumbSprite,
|
2014-03-14 21:06:40 +08:00
|
|
|
Label* onLabel,
|
|
|
|
Label* offLabel);
|
2013-11-16 04:04:47 +08:00
|
|
|
|
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
|
|
|
|
*/
|
2013-11-16 04:04:47 +08:00
|
|
|
virtual void updateTweenAction(float value, const std::string& key) override;
|
|
|
|
|
2012-04-17 16:00:18 +08:00
|
|
|
/** 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)
|
2014-03-14 21:06:40 +08:00
|
|
|
CC_SYNTHESIZE_RETAIN(Label*, _onLabel, OnLabel)
|
|
|
|
CC_SYNTHESIZE_RETAIN(Label*, _offLabel, OffLabel)
|
2014-01-03 19:53:14 +08:00
|
|
|
|
|
|
|
Sprite* _clipperStencil;
|
2013-11-16 04:04:47 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
ControlSwitchSprite();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~ControlSwitchSprite();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
bool initWithMaskSprite(
|
|
|
|
Sprite *maskSprite,
|
|
|
|
Sprite *onSprite,
|
|
|
|
Sprite *offSprite,
|
|
|
|
Sprite *thumbSprite,
|
2014-03-14 21:06:40 +08:00
|
|
|
Label* onLabel,
|
|
|
|
Label* offLabel);
|
2013-11-16 04:04:47 +08:00
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(ControlSwitchSprite);
|
2012-04-17 16:00:18 +08:00
|
|
|
};
|
|
|
|
|
2013-11-26 16:19:12 +08:00
|
|
|
ControlSwitchSprite* ControlSwitchSprite::create(Sprite *maskSprite,
|
2013-11-16 04:04:47 +08:00
|
|
|
Sprite *onSprite,
|
|
|
|
Sprite *offSprite,
|
|
|
|
Sprite *thumbSprite,
|
2014-03-14 21:06:40 +08:00
|
|
|
Label* onLabel,
|
|
|
|
Label* offLabel)
|
2013-11-16 04:04:47 +08:00
|
|
|
{
|
|
|
|
auto ret = new ControlSwitchSprite();
|
|
|
|
ret->initWithMaskSprite(maskSprite, onSprite, offSprite, thumbSprite, onLabel, offLabel);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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)
|
2014-04-09 22:53:59 +08:00
|
|
|
, _maskTexture(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _textureLocation(0)
|
|
|
|
, _maskLocation(0)
|
2014-04-09 22:53:59 +08:00
|
|
|
, _onSprite(nullptr)
|
|
|
|
, _offSprite(nullptr)
|
|
|
|
, _thumbSprite(nullptr)
|
|
|
|
, _onLabel(nullptr)
|
|
|
|
, _offLabel(nullptr)
|
2014-01-03 19:53:14 +08:00
|
|
|
, _clipperStencil(nullptr)
|
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);
|
2014-01-03 19:53:14 +08:00
|
|
|
CC_SAFE_RELEASE(_clipperStencil);
|
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,
|
2014-03-14 21:06:40 +08:00
|
|
|
Label* onLabel,
|
|
|
|
Label* 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);
|
2014-04-02 11:26:39 +08:00
|
|
|
|
2014-01-03 19:53:14 +08:00
|
|
|
ClippingNode* clipper = ClippingNode::create();
|
|
|
|
_clipperStencil = Sprite::createWithTexture(maskSprite->getTexture());
|
|
|
|
_clipperStencil->retain();
|
|
|
|
clipper->setAlphaThreshold(0.1f);
|
|
|
|
|
|
|
|
clipper->setStencil(_clipperStencil);
|
|
|
|
|
|
|
|
clipper->addChild(onSprite);
|
|
|
|
clipper->addChild(offSprite);
|
|
|
|
clipper->addChild(onLabel);
|
|
|
|
clipper->addChild(offLabel);
|
2014-03-05 14:03:44 +08:00
|
|
|
clipper->addChild(thumbSprite);
|
2014-01-03 19:53:14 +08:00
|
|
|
|
|
|
|
addChild(clipper);
|
2012-04-17 16:00:18 +08:00
|
|
|
|
|
|
|
// Set up the mask with the Mask shader
|
|
|
|
setMaskTexture(maskSprite->getTexture());
|
|
|
|
|
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-11-16 04:04:47 +08:00
|
|
|
void ControlSwitchSprite::updateTweenAction(float value, const std::string& key)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-02 11:26:39 +08:00
|
|
|
CCLOGINFO("key = %s, value = %f", key.c_str(), value);
|
2012-04-17 16:00:18 +08:00
|
|
|
setSliderXPosition(value);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSwitchSprite::needsLayout()
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
_onSprite->setPosition(Vector2(_onSprite->getContentSize().width / 2 + _sliderXPosition,
|
2013-06-15 14:03:30 +08:00
|
|
|
_onSprite->getContentSize().height / 2));
|
2014-04-15 18:23:40 +08:00
|
|
|
_offSprite->setPosition(Vector2(_onSprite->getContentSize().width + _offSprite->getContentSize().width / 2 + _sliderXPosition,
|
2013-06-15 14:03:30 +08:00
|
|
|
_offSprite->getContentSize().height / 2));
|
2014-04-15 18:23:40 +08:00
|
|
|
_thumbSprite->setPosition(Vector2(_onSprite->getContentSize().width + _sliderXPosition,
|
2013-06-15 14:03:30 +08:00
|
|
|
_maskTexture->getContentSize().height / 2));
|
|
|
|
|
2014-04-15 18:23:40 +08:00
|
|
|
_clipperStencil->setPosition(Vector2(_maskTexture->getContentSize().width/2,
|
2014-01-03 19:53:14 +08:00
|
|
|
_maskTexture->getContentSize().height / 2));
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_onLabel)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
_onLabel->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
|
|
|
|
_onLabel->setPosition(Vector2(_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
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
_offLabel->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
|
|
|
|
_offLabel->setPosition(Vector2(_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-09-16 20:38:03 +08:00
|
|
|
setFlippedY(true);
|
2012-04-17 16:00:18 +08:00
|
|
|
}
|
|
|
|
|
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()
|
2014-04-09 22:53:59 +08:00
|
|
|
: _switchSprite(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _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;
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
bool ControlSwitch::initWithMaskSprite(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite, Label* onLabel, Label* 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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_on = true;
|
2012-04-17 16:00:18 +08:00
|
|
|
|
2013-11-26 16:19:12 +08:00
|
|
|
_switchSprite = ControlSwitchSprite::create(maskSprite,
|
2013-11-16 04:04:47 +08:00
|
|
|
onSprite,
|
|
|
|
offSprite,
|
|
|
|
thumbSprite,
|
|
|
|
onLabel,
|
|
|
|
offLabel);
|
|
|
|
_switchSprite->retain();
|
2014-04-15 18:23:40 +08:00
|
|
|
_switchSprite->setPosition(Vector2(_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);
|
2014-04-15 18:23:40 +08:00
|
|
|
setAnchorPoint(Vector2(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;
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
ControlSwitch* ControlSwitch::create(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite, Label* onLabel, Label* 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
|
|
|
{
|
2014-04-02 11:26:39 +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
|
|
|
}
|
|
|
|
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 ControlSwitch::locationFromTouch(Touch* pTouch)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 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-09-03 18:22:03 +08:00
|
|
|
bool ControlSwitch::onTouchBegan(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
|
|
|
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 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-09-03 18:22:03 +08:00
|
|
|
void ControlSwitch::onTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 location = this->locationFromTouch(pTouch);
|
|
|
|
location = Vector2(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-09-03 18:22:03 +08:00
|
|
|
void ControlSwitch::onTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 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-09-03 18:22:03 +08:00
|
|
|
void ControlSwitch::onTouchCancelled(Touch *pTouch, Event *pEvent)
|
2012-04-17 16:00:18 +08:00
|
|
|
{
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 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
|