2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include "UIButtonTest.h"
|
|
|
|
|
|
|
|
|
|
|
|
// UIButtonTest
|
|
|
|
UIButtonTest::UIButtonTest()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UIButtonTest::~UIButtonTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIButtonTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add a label in which the button events will be displayed
|
2014-04-03 10:31:10 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 10:31:10 +08:00
|
|
|
Text* alert = Text::create("Button","fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
alert->setPosition(Vec2(widgetSize.width / 2.0f,
|
2014-06-20 11:18:53 +08:00
|
|
|
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2014-04-03 10:31:10 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the button
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png",
|
|
|
|
"cocosui/animationbuttonpressed.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-05-09 14:30:39 +08:00
|
|
|
// button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));
|
|
|
|
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this));
|
2014-04-17 14:08:25 +08:00
|
|
|
_uiLayer->addChild(button);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
void UIButtonTest::touchEvent(Ref *pSender, Widget::TouchEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Down")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Move")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Up")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Cancelled")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UIButtonTest_Scale9
|
|
|
|
UIButtonTest_Scale9::UIButtonTest_Scale9()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIButtonTest_Scale9::~UIButtonTest_Scale9()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIButtonTest_Scale9::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add a label in which the button events will be displayed
|
2014-04-03 10:31:10 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 10:31:10 +08:00
|
|
|
Text* alert = Text::create("Button scale9 render", "fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-05-15 01:07:09 +08:00
|
|
|
alert->setPosition(Vec2(widgetSize.width / 2.0f,
|
2014-06-20 11:18:53 +08:00
|
|
|
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the button
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
2013-09-16 20:54:13 +08:00
|
|
|
// open scale9 render
|
|
|
|
button->setScale9Enabled(true);
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-06-20 14:03:33 +08:00
|
|
|
button->setContentSize(Size(150, 70));
|
2014-05-09 14:30:39 +08:00
|
|
|
// button->addTouchEventListener(this, toucheventselector(UIButtonTest_Scale9::touchEvent));
|
|
|
|
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(button);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
void UIButtonTest_Scale9::touchEvent(Ref *pSender, Widget::TouchEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Down")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Move")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Up")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Cancelled")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonTest_PressAction
|
|
|
|
UIButtonTest_PressedAction::UIButtonTest_PressedAction()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UIButtonTest_PressedAction::~UIButtonTest_PressedAction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIButtonTest_PressedAction::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add a label in which the button events will be displayed
|
2014-04-03 10:31:10 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 10:31:10 +08:00
|
|
|
Text* alert = Text::create("Button Pressed Action", "fonts/Marker Felt.ttf", 30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
alert->setPosition(Vec2(widgetSize.width / 2.0f,
|
2014-06-20 11:18:53 +08:00
|
|
|
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2014-04-03 10:31:10 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the button
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2013-09-16 20:54:13 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-05-09 14:30:39 +08:00
|
|
|
// button->addTouchEventListener(this, toucheventselector(UIButtonTest_PressedAction::touchEvent));
|
|
|
|
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_PressedAction::touchEvent, this));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(button);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
void UIButtonTest_PressedAction::touchEvent(Ref *pSender, Widget::TouchEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Down")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Move")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Up")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Cancelled")->getCString());
|
2013-12-23 15:35:35 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonTest_Title
|
|
|
|
UIButtonTest_Title::UIButtonTest_Title()
|
|
|
|
: _displayValueLabel(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIButtonTest_Title::~UIButtonTest_Title()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIButtonTest_Title::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2013-12-23 15:35:35 +08:00
|
|
|
|
|
|
|
// Add a label in which the text button events will be displayed
|
2014-04-03 10:31:10 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 10:31:10 +08:00
|
|
|
Text* alert = Text::create("Button with title", "fonts/Marker Felt.ttf", 30);
|
2013-12-23 15:35:35 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-05-15 01:07:09 +08:00
|
|
|
alert->setPosition(Vec2(widgetSize.width / 2.0f,
|
2014-06-20 11:18:53 +08:00
|
|
|
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2014-04-03 10:31:10 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
// Create the button with title
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png");
|
2013-12-23 15:35:35 +08:00
|
|
|
button->setTitleText("Title Button");
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-06-25 13:47:29 +08:00
|
|
|
button->setTitleColor(Color3B::YELLOW);
|
|
|
|
CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!");
|
2014-05-09 14:30:39 +08:00
|
|
|
// button->addTouchEventListener(this, toucheventselector(UIButtonTest_Title::touchEvent));
|
|
|
|
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Title::touchEvent, this));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(button);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
void UIButtonTest_Title::touchEvent(Ref *pSender, Widget::TouchEventType type)
|
2013-12-23 15:35:35 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Down")->getCString());
|
2013-12-23 15:35:35 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Move")->getCString());
|
2013-12-23 15:35:35 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Up")->getCString());
|
2013-12-23 15:35:35 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-09 14:30:39 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
2014-05-14 15:26:14 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("Touch Cancelled")->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|