mirror of https://github.com/axmolengine/axmol.git
Refine C++ test case
This commit is contained in:
parent
da4e062614
commit
07fdea50de
|
@ -143,23 +143,14 @@ namespace {
|
|||
class SliderEx : public Slider
|
||||
{
|
||||
public:
|
||||
enum class TouchEvent
|
||||
{
|
||||
DOWN,
|
||||
MOVE,
|
||||
UP,
|
||||
CANCEL
|
||||
};
|
||||
typedef std::function<void(SliderEx*,float,TouchEvent)> ccSliderExCallback;
|
||||
|
||||
static SliderEx* create(){
|
||||
auto ret = new (std::nothrow) SliderEx();
|
||||
if (ret && ret->init())
|
||||
{
|
||||
ret->_callback = nullptr;
|
||||
ret->loadBarTexture("cocosui/sliderTrack.png");
|
||||
ret->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
||||
ret->loadProgressBarTexture("cocosui/sliderProgress.png");
|
||||
ret->setTouchEnabled(true);
|
||||
|
||||
ret->autorelease();
|
||||
|
||||
|
@ -169,94 +160,19 @@ namespace {
|
|||
return ret;
|
||||
}
|
||||
|
||||
void setCallBack(const ccSliderExCallback& callback){
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
void setRatio(float ratio) {
|
||||
if (ratio > 1.0f){
|
||||
ratio = 1.0f;
|
||||
}
|
||||
else if (ratio < 0.0f){
|
||||
ratio = 0.0f;
|
||||
}
|
||||
ratio = clampf(ratio, 0.0f, 1.0f);
|
||||
|
||||
_ratio = ratio;
|
||||
_percent = 100 * _ratio;
|
||||
|
||||
float dis = _barLength * _ratio;
|
||||
_slidBallRenderer->setPosition(Vec2(dis, _contentSize.height / 2.0f));
|
||||
if (_scale9Enabled){
|
||||
_progressBarRenderer->setPreferredSize(Size(dis,_progressBarTextureSize.height));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto spriteRenderer = _progressBarRenderer->getSprite();
|
||||
|
||||
if (nullptr != spriteRenderer) {
|
||||
Rect rect = spriteRenderer->getTextureRect();
|
||||
rect.size.width = _progressBarTextureSize.width * _ratio;
|
||||
spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
|
||||
}
|
||||
}
|
||||
setPercent(100 * _ratio);
|
||||
}
|
||||
|
||||
virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override{
|
||||
auto ret = Slider::onTouchBegan(touch, unusedEvent);
|
||||
if(ret && _callback){
|
||||
_touchEvent = TouchEvent::DOWN;
|
||||
Vec2 nsp = convertToNodeSpace(_touchBeganPosition);
|
||||
_ratio = nsp.x / _barLength;
|
||||
if(_ratio < 0.0f)
|
||||
_ratio = 0.0f;
|
||||
else if(_ratio > 1.0f)
|
||||
_ratio = 1.0f;
|
||||
_callback(this,_ratio,_touchEvent);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
virtual void onTouchMoved(Touch *touch, Event *unusedEvent) override{
|
||||
_touchEvent = TouchEvent::MOVE;
|
||||
Slider::onTouchMoved(touch, unusedEvent);
|
||||
Vec2 nsp = convertToNodeSpace(_touchMovePosition);
|
||||
_ratio = nsp.x / _barLength;
|
||||
if(_ratio < 0.0f)
|
||||
_ratio = 0.0f;
|
||||
else if(_ratio > 1.0f)
|
||||
_ratio = 1.0f;
|
||||
if(_callback){
|
||||
_callback(this,_ratio,_touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void onTouchEnded(Touch *touch, Event *unusedEvent) override{
|
||||
_touchEvent = TouchEvent::UP;
|
||||
Slider::onTouchEnded(touch, unusedEvent);
|
||||
Vec2 nsp = convertToNodeSpace(_touchEndPosition);
|
||||
_ratio = nsp.x / _barLength;
|
||||
if(_ratio < 0.0f)
|
||||
_ratio = 0.0f;
|
||||
else if(_ratio > 1.0f)
|
||||
_ratio = 1.0f;
|
||||
if(_callback){
|
||||
_callback(this,_ratio,_touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void onTouchCancelled(Touch *touch, Event *unusedEvent) override{
|
||||
_touchEvent = TouchEvent::CANCEL;
|
||||
Slider::onTouchCancelled(touch, unusedEvent);
|
||||
|
||||
if(_callback){
|
||||
_callback(this,_ratio,_touchEvent);
|
||||
}
|
||||
float getRatio () {
|
||||
return _ratio;
|
||||
}
|
||||
|
||||
private:
|
||||
TouchEvent _touchEvent;
|
||||
float _ratio;
|
||||
ccSliderExCallback _callback;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -366,8 +282,9 @@ bool AudioControlTest::init()
|
|||
|
||||
auto volumeSlider = SliderEx::create();
|
||||
volumeSlider->setPercent(100);
|
||||
volumeSlider->setCallBack([&](SliderEx* sender,float ratio,SliderEx::TouchEvent event){
|
||||
_volume = ratio;
|
||||
volumeSlider->addEventListener([&](Ref* sender, Slider::EventType event){
|
||||
SliderEx *slider = dynamic_cast<SliderEx *>(sender);
|
||||
_volume = slider->getRatio();
|
||||
if (_audioID != AudioEngine::INVALID_AUDIO_ID ) {
|
||||
AudioEngine::setVolume(_audioID, _volume);
|
||||
}
|
||||
|
@ -376,17 +293,21 @@ bool AudioControlTest::init()
|
|||
addChild(volumeSlider);
|
||||
|
||||
auto timeSlider = SliderEx::create();
|
||||
timeSlider->setCallBack([&](SliderEx* sender,float ratio,SliderEx::TouchEvent event){
|
||||
timeSlider->addEventListener([&](Ref* sender, Slider::EventType event){
|
||||
SliderEx *slider = dynamic_cast<SliderEx *>(sender);
|
||||
switch(event){
|
||||
case SliderEx::TouchEvent::MOVE:
|
||||
case SliderEx::TouchEvent::DOWN:
|
||||
case Slider::EventType::ON_PERCENTAGE_CHANGED:
|
||||
case Slider::EventType::ON_SLIDEBALL_DOWN:
|
||||
_updateTimeSlider = false;
|
||||
break;
|
||||
case SliderEx::TouchEvent::UP:
|
||||
case Slider::EventType::ON_SLIDEBALL_UP:
|
||||
if (_audioID != AudioEngine::INVALID_AUDIO_ID && _duration != AudioEngine::TIME_UNKNOWN) {
|
||||
AudioEngine::setCurrentTime(_audioID,_duration * ratio);
|
||||
float ratio = (float)slider->getPercent() / 100;
|
||||
ratio = clampf(ratio, 0.0f, 1.0f);
|
||||
AudioEngine::setCurrentTime(_audioID, _duration * ratio);
|
||||
slider->setRatio(ratio);
|
||||
}
|
||||
case SliderEx::TouchEvent::CANCEL:
|
||||
case Slider::EventType::ON_SLIDEBALL_CANCEL:
|
||||
_updateTimeSlider = true;
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue