PuzzleSDK
VibrateTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2014-2016 Chukong Technologies Inc.
3 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4
5 http://www.cocos2d-x.org
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 ****************************************************************************/
25
26#include "platform/CCPlatformConfig.h"
27
28#include "VibrateTest.h"
29#include "ui/CocosGUI.h"
30
31using namespace cocos2d;
32using namespace cocos2d::ui;
33
34VibrateTests::VibrateTests()
35{
37}
38
39namespace {
40 class TextButton : public cocos2d::Label
41 {
42 public:
43
44 static TextButton *create(const std::string& text, const std::function<void(TextButton*)> &onTriggered)
45 {
46 auto ret = new (std::nothrow) TextButton();
47
48 TTFConfig ttfconfig("fonts/arial.ttf",25);
49 if (ret && ret->setTTFConfig(ttfconfig)) {
50 ret->setString(text);
51 ret->_onTriggered = onTriggered;
52
53 ret->autorelease();
54
55 return ret;
56 }
57
58 delete ret;
59 return nullptr;
60 }
61
62 void setEnabled(bool enabled)
63 {
64 _enabled = enabled;
65 if(_enabled){
66 this->setColor(Color3B::WHITE);
67 }
68 else {
69 this->setColor(Color3B::GRAY);
70 }
71 }
72
73 private:
74 TextButton()
75 {
76 auto listener = EventListenerTouchOneByOne::create();
77 listener->setSwallowTouches(true);
78
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);
82
83 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
84 }
85
86 bool touchHits(Touch *touch)
87 {
88 auto hitPos = this->convertToNodeSpace(touch->getLocation());
89 if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width && hitPos.y <= _contentSize.height) {
90 return true;
91 }
92 return false;
93 }
94
95 bool onTouchBegan(Touch *touch, Event *event)
96 {
97 auto hits = touchHits(touch);
98 if (hits){
99 scaleButtonTo(0.95f);
100 }
101 return hits;
102 }
103
104 void onTouchEnded(Touch *touch, Event *event)
105 {
106 if(_enabled) {
107 auto hits = touchHits(touch);
108 if (hits && _onTriggered){
109 _onTriggered(this);
110 }
111 }
112
113 scaleButtonTo(1);
114 }
115
116 void onTouchCancelled(Touch *touch, Event *event)
117 {
118 scaleButtonTo(1);
119 }
120
121 void scaleButtonTo(float scale)
122 {
123 auto action = ScaleTo::create(0.05f, scale);
124 action->setTag(10000);
125 stopActionByTag(10000);
126 runAction(action);
127 }
128
129 std::function<void(TextButton*)> _onTriggered = nullptr;
130
131 bool _enabled = true;
132 };
133
134 class SliderEx : public Slider
135 {
136 public:
137 enum class TouchEvent
138 {
139 DOWN,
140 MOVE,
141 UP,
142 CANCEL
143 };
144 typedef std::function<void(SliderEx*,float,TouchEvent)> ccSliderExCallback;
145
146 static SliderEx* create(){
147 auto ret = new (std::nothrow) SliderEx();
148 if (ret && ret->init())
149 {
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");
154
155 ret->autorelease();
156
157 return ret;
158 }
159 CC_SAFE_DELETE(ret);
160 return ret;
161 }
162
163 void setCallBack(const ccSliderExCallback& callback){
164 _callback = callback;
165 }
166
167 void setRatio(float ratio) {
168 if (ratio > 1.0f){
169 ratio = 1.0f;
170 }
171 else if (ratio < 0.0f){
172 ratio = 0.0f;
173 }
174
175 _ratio = ratio;
176 _percent = 100 * _ratio;
177
178 float dis = _barLength * _ratio;
179 _slidBallRenderer->setPosition(Vec2(dis, _contentSize.height / 2.0f));
180 if (_scale9Enabled){
181 _progressBarRenderer->setPreferredSize(Size(dis,_progressBarTextureSize.height));
182 }
183 else
184 {
185 auto spriteRenderer = _progressBarRenderer->getSprite();
186
187 if (nullptr != spriteRenderer) {
188 Rect rect = spriteRenderer->getTextureRect();
189 rect.size.width = _progressBarTextureSize.width * _ratio;
190 spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
191 }
192 }
193 }
194
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;
201 if(_ratio < 0.0f)
202 _ratio = 0.0f;
203 else if(_ratio > 1.0f)
204 _ratio = 1.0f;
205 _callback(this,_ratio,_touchEvent);
206 }
207 return ret;
208 }
209
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;
215 if(_ratio < 0.0f)
216 _ratio = 0.0f;
217 else if(_ratio > 1.0f)
218 _ratio = 1.0f;
219 if(_callback){
220 _callback(this,_ratio,_touchEvent);
221 }
222 }
223
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;
229 if(_ratio < 0.0f)
230 _ratio = 0.0f;
231 else if(_ratio > 1.0f)
232 _ratio = 1.0f;
233 if(_callback){
234 _callback(this,_ratio,_touchEvent);
235 }
236 }
237
238 virtual void onTouchCancelled(Touch *touch, Event *unusedEvent) override{
239 _touchEvent = TouchEvent::CANCEL;
240 Slider::onTouchCancelled(touch, unusedEvent);
241
242 if(_callback){
243 _callback(this,_ratio,_touchEvent);
244 }
245 }
246
247 private:
248 TouchEvent _touchEvent;
249 float _ratio;
250 ccSliderExCallback _callback;
251 };
252}
253
255{
256 TestCase::onExit();
257}
258
259std::string VibrateTestDemo::title() const
260{
261 return "Vibrate Test";
262}
263
264// VibrateControlTest
266{
267 auto ret = VibrateTestDemo::init();
268 _duration = 0.1f;
269
270 std::string fontFilePath = "fonts/arial.ttf";
271
272 auto& layerSize = this->getContentSize();
273
274 auto vibrateItem = TextButton::create("vibrate", [&](TextButton* button){
275 Device::vibrate(_duration);
276 });
277 vibrateItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.7f);
278 addChild(vibrateItem);
279
280 auto durationLabelValue = StringUtils::format("duration: %.3fs", _duration);
281
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);
286 _durationLabel = durationLabel;
287
288 auto durationSlider = SliderEx::create();
289 durationSlider->setPercent(0);
290 durationSlider->setCallBack([&](SliderEx* sender, float ratio, SliderEx::TouchEvent event){
291 _duration = ratio * 1.9f + 0.1f; // From 0.1s to 2s
292 auto durationLabelValue = StringUtils::format("duration: %.3fs", _duration);
293 (static_cast<Label*>(_durationLabel))->setString(durationLabelValue);
294 });
295 durationSlider->setPosition(Vec2(layerSize.width * 0.5f, layerSize.height * 0.35f));
296 addChild(durationSlider);
297 _durationSlider = durationSlider;
298
299 return ret;
300}
301
303{
304}
305
306std::string VibrateControlTest::title() const
307{
308 return "vibrate control test";
309}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
virtual ~VibrateControlTest()
void * _durationSlider
Definition: VibrateTest.h:57
virtual bool init() override
virtual std::string title() const override
virtual std::string title() const override
virtual void onExit() override