2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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 "platform/CCPlatformConfig.h"
|
|
|
|
|
|
|
|
#include "VibrateTest.h"
|
|
|
|
#include "ui/CocosGUI.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2022-08-29 20:51:22 +08:00
|
|
|
using namespace ax::ui;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
VibrateTests::VibrateTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(VibrateControlTest);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
namespace
|
|
|
|
{
|
2022-10-18 19:17:36 +08:00
|
|
|
class TextButton : public ax::Label
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static TextButton* create(std::string_view text, const std::function<void(TextButton*)>& onTriggered)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto ret = new TextButton();
|
|
|
|
|
|
|
|
TTFConfig ttfconfig("fonts/arial.ttf", 25);
|
|
|
|
if (ret->setTTFConfig(ttfconfig))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
ret->setString(text);
|
|
|
|
ret->_onTriggered = onTriggered;
|
|
|
|
|
|
|
|
ret->autorelease();
|
|
|
|
|
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_enabled = enabled;
|
|
|
|
if (_enabled)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
this->setColor(Color3B::WHITE);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
this->setColor(Color3B::GRAY);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TextButton()
|
|
|
|
{
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchBegan = AX_CALLBACK_2(TextButton::onTouchBegan, this);
|
|
|
|
listener->onTouchEnded = AX_CALLBACK_2(TextButton::onTouchEnded, this);
|
|
|
|
listener->onTouchCancelled = AX_CALLBACK_2(TextButton::onTouchCancelled, this);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool touchHits(Touch* touch)
|
|
|
|
{
|
|
|
|
auto hitPos = this->convertToNodeSpace(touch->getLocation());
|
|
|
|
if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width && hitPos.y <= _contentSize.height)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onTouchBegan(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
auto hits = touchHits(touch);
|
|
|
|
if (hits)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
scaleButtonTo(0.95f);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
return hits;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onTouchEnded(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
if (_enabled)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto hits = touchHits(touch);
|
|
|
|
if (hits && _onTriggered)
|
|
|
|
{
|
|
|
|
_onTriggered(this);
|
2021-12-31 11:00:35 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
scaleButtonTo(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onTouchCancelled(Touch* touch, Event* event) { scaleButtonTo(1); }
|
|
|
|
|
|
|
|
void scaleButtonTo(float scale)
|
|
|
|
{
|
|
|
|
auto action = ScaleTo::create(0.05f, scale);
|
|
|
|
action->setTag(10000);
|
|
|
|
stopActionByTag(10000);
|
|
|
|
runAction(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(TextButton*)> _onTriggered = nullptr;
|
|
|
|
|
|
|
|
bool _enabled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 SliderEx();
|
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
ret->_callback = nullptr;
|
|
|
|
ret->loadBarTexture("ccs-res/cocosui/sliderTrack.png");
|
|
|
|
ret->loadSlidBallTextures("ccs-res/cocosui/sliderThumb.png", "ccs-res/cocosui/sliderThumb.png", "");
|
|
|
|
ret->loadProgressBarTexture("ccs-res/cocosui/sliderProgress.png");
|
|
|
|
|
|
|
|
ret->autorelease();
|
|
|
|
|
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2021-12-31 12:12:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCallBack(const ccSliderExCallback& callback) { _callback = callback; }
|
|
|
|
|
|
|
|
void setRatio(float ratio)
|
|
|
|
{
|
|
|
|
if (ratio > 1.0f)
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
ratio = 1.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else if (ratio < 0.0f)
|
2021-12-28 21:27:32 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
ratio = 0.0f;
|
2021-12-28 21:00:45 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
_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));
|
2021-12-31 11:00:35 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto spriteRenderer = _progressBarRenderer->getSprite();
|
|
|
|
|
|
|
|
if (nullptr != spriteRenderer)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
Rect rect = spriteRenderer->getTextureRect();
|
|
|
|
rect.size.width = _progressBarTextureSize.width * _ratio;
|
|
|
|
spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
|
2021-12-31 11:00:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-11-23 20:27:39 +08:00
|
|
|
_ratio = 0.0f;
|
2021-12-31 12:12:40 +08:00
|
|
|
else if (_ratio > 1.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
_ratio = 1.0f;
|
2021-12-31 12:12:40 +08:00
|
|
|
_callback(this, _ratio, _touchEvent);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
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);
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-12-28 21:27:32 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onTouchCancelled(Touch* touch, Event* unusedEvent) override
|
|
|
|
{
|
|
|
|
_touchEvent = TouchEvent::CANCEL;
|
|
|
|
Slider::onTouchCancelled(touch, unusedEvent);
|
|
|
|
|
|
|
|
if (_callback)
|
|
|
|
{
|
|
|
|
_callback(this, _ratio, _touchEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TouchEvent _touchEvent;
|
|
|
|
float _ratio;
|
|
|
|
ccSliderExCallback _callback;
|
|
|
|
};
|
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void VibrateTestDemo::onExit()
|
|
|
|
{
|
|
|
|
TestCase::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string VibrateTestDemo::title() const
|
|
|
|
{
|
|
|
|
return "Vibrate Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
// VibrateControlTest
|
|
|
|
bool VibrateControlTest::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto ret = VibrateTestDemo::init();
|
2019-11-23 20:27:39 +08:00
|
|
|
_duration = 0.1f;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fontFilePath = "fonts/arial.ttf";
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto& layerSize = this->getContentSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto vibrateItem = TextButton::create("vibrate", [&](TextButton* button) { Device::vibrate(_duration); });
|
2019-11-23 20:27:39 +08:00
|
|
|
vibrateItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.7f);
|
|
|
|
addChild(vibrateItem);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto durationLabelValue = StringUtils::format("duration: %.3fs", _duration);
|
|
|
|
|
|
|
|
auto durationLabel = Label::createWithTTF(durationLabelValue, fontFilePath, 20);
|
|
|
|
durationLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
|
|
|
durationLabel->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
|
|
|
|
addChild(durationLabel);
|
|
|
|
_durationLabel = durationLabel;
|
|
|
|
|
|
|
|
auto durationSlider = SliderEx::create();
|
|
|
|
durationSlider->setPercent(0);
|
2021-12-31 12:12:40 +08:00
|
|
|
durationSlider->setCallBack([&](SliderEx* sender, float ratio, SliderEx::TouchEvent event) {
|
|
|
|
_duration = ratio * 1.9f + 0.1f; // From 0.1s to 2s
|
2019-11-23 20:27:39 +08:00
|
|
|
auto durationLabelValue = StringUtils::format("duration: %.3fs", _duration);
|
|
|
|
(static_cast<Label*>(_durationLabel))->setString(durationLabelValue);
|
|
|
|
});
|
|
|
|
durationSlider->setPosition(Vec2(layerSize.width * 0.5f, layerSize.height * 0.35f));
|
|
|
|
addChild(durationSlider);
|
|
|
|
_durationSlider = durationSlider;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
VibrateControlTest::~VibrateControlTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string VibrateControlTest::title() const
|
|
|
|
{
|
|
|
|
return "vibrate control test";
|
|
|
|
}
|