26#include "platform/CCPlatformConfig.h"
29#include "ui/CocosGUI.h"
32using namespace cocos2d::ui;
34VibrateTests::VibrateTests()
40 class TextButton :
public cocos2d::Label
44 static TextButton *create(
const std::string& text,
const std::function<
void(TextButton*)> &onTriggered)
46 auto ret =
new (std::nothrow) TextButton();
48 TTFConfig ttfconfig(
"fonts/arial.ttf",25);
49 if (ret && ret->setTTFConfig(ttfconfig)) {
51 ret->_onTriggered = onTriggered;
62 void setEnabled(
bool enabled)
66 this->setColor(Color3B::WHITE);
69 this->setColor(Color3B::GRAY);
76 auto listener = EventListenerTouchOneByOne::create();
77 listener->setSwallowTouches(
true);
79 listener->onTouchBegan = CC_CALLBACK_2(TextButton::onTouchBegan,
this);
80 listener->onTouchEnded = CC_CALLBACK_2(TextButton::onTouchEnded,
this);
81 listener->onTouchCancelled = CC_CALLBACK_2(TextButton::onTouchCancelled,
this);
83 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
86 bool touchHits(Touch *touch)
88 auto hitPos = this->convertToNodeSpace(touch->getLocation());
89 if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width && hitPos.y <= _contentSize.height) {
95 bool onTouchBegan(Touch *touch, Event *event)
97 auto hits = touchHits(touch);
104 void onTouchEnded(Touch *touch, Event *event)
107 auto hits = touchHits(touch);
108 if (hits && _onTriggered){
116 void onTouchCancelled(Touch *touch, Event *event)
121 void scaleButtonTo(
float scale)
123 auto action = ScaleTo::create(0.05f, scale);
124 action->setTag(10000);
125 stopActionByTag(10000);
129 std::function<void(TextButton*)> _onTriggered =
nullptr;
131 bool _enabled =
true;
134 class SliderEx :
public Slider
137 enum class TouchEvent
144 typedef std::function<void(SliderEx*,
float,TouchEvent)> ccSliderExCallback;
146 static SliderEx* create(){
147 auto ret =
new (std::nothrow) SliderEx();
148 if (ret && ret->init())
150 ret->_callback =
nullptr;
151 ret->loadBarTexture(
"ccs-res/cocosui/sliderTrack.png");
152 ret->loadSlidBallTextures(
"ccs-res/cocosui/sliderThumb.png",
"ccs-res/cocosui/sliderThumb.png",
"");
153 ret->loadProgressBarTexture(
"ccs-res/cocosui/sliderProgress.png");
163 void setCallBack(
const ccSliderExCallback& callback){
164 _callback = callback;
167 void setRatio(
float ratio) {
171 else if (ratio < 0.0f){
176 _percent = 100 * _ratio;
178 float dis = _barLength * _ratio;
179 _slidBallRenderer->setPosition(Vec2(dis, _contentSize.height / 2.0f));
181 _progressBarRenderer->setPreferredSize(Size(dis,_progressBarTextureSize.height));
185 auto spriteRenderer = _progressBarRenderer->getSprite();
187 if (
nullptr != spriteRenderer) {
188 Rect rect = spriteRenderer->getTextureRect();
189 rect.size.width = _progressBarTextureSize.width * _ratio;
190 spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
195 virtual bool onTouchBegan(Touch *touch, Event *unusedEvent)
override{
196 auto ret = Slider::onTouchBegan(touch, unusedEvent);
197 if(ret && _callback){
198 _touchEvent = TouchEvent::DOWN;
199 Vec2 nsp = convertToNodeSpace(_touchBeganPosition);
200 _ratio = nsp.x / _barLength;
203 else if(_ratio > 1.0f)
205 _callback(
this,_ratio,_touchEvent);
210 virtual void onTouchMoved(Touch *touch, Event *unusedEvent)
override{
211 _touchEvent = TouchEvent::MOVE;
212 Slider::onTouchMoved(touch, unusedEvent);
213 Vec2 nsp = convertToNodeSpace(_touchMovePosition);
214 _ratio = nsp.x / _barLength;
217 else if(_ratio > 1.0f)
220 _callback(
this,_ratio,_touchEvent);
224 virtual void onTouchEnded(Touch *touch, Event *unusedEvent)
override{
225 _touchEvent = TouchEvent::UP;
226 Slider::onTouchEnded(touch, unusedEvent);
227 Vec2 nsp = convertToNodeSpace(_touchEndPosition);
228 _ratio = nsp.x / _barLength;
231 else if(_ratio > 1.0f)
234 _callback(
this,_ratio,_touchEvent);
238 virtual void onTouchCancelled(Touch *touch, Event *unusedEvent)
override{
239 _touchEvent = TouchEvent::CANCEL;
240 Slider::onTouchCancelled(touch, unusedEvent);
243 _callback(
this,_ratio,_touchEvent);
248 TouchEvent _touchEvent;
250 ccSliderExCallback _callback;
261 return "Vibrate Test";
267 auto ret = VibrateTestDemo::init();
270 std::string fontFilePath =
"fonts/arial.ttf";
272 auto& layerSize = this->getContentSize();
274 auto vibrateItem = TextButton::create(
"vibrate", [&](TextButton* button){
277 vibrateItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.7f);
278 addChild(vibrateItem);
280 auto durationLabelValue = StringUtils::format(
"duration: %.3fs",
_duration);
282 auto durationLabel = Label::createWithTTF(durationLabelValue, fontFilePath, 20);
283 durationLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
284 durationLabel->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
285 addChild(durationLabel);
288 auto durationSlider = SliderEx::create();
289 durationSlider->setPercent(0);
290 durationSlider->setCallBack([&](SliderEx* sender,
float ratio, SliderEx::TouchEvent event){
292 auto durationLabelValue = StringUtils::format(
"duration: %.3fs",
_duration);
293 (
static_cast<Label*
>(
_durationLabel))->setString(durationLabelValue);
295 durationSlider->setPosition(Vec2(layerSize.width * 0.5f, layerSize.height * 0.35f));
296 addChild(durationSlider);
308 return "vibrate control test";
#define ADD_TEST_CASE(__className__)
virtual ~VibrateControlTest()
virtual bool init() override
virtual std::string title() const override
virtual std::string title() const override
virtual void onExit() override