axmol/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp

305 lines
10 KiB
C++
Raw Normal View History

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())
{
2013-12-23 15:35:35 +08:00
Size widgetSize = _widget->getSize();
2013-09-16 20:54:13 +08:00
// Add a label in which the button events will be displayed
_displayValueLabel = gui::Text::create();
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText("No Event");
_displayValueLabel->setFontName("Marker Felt");
_displayValueLabel->setFontSize(32);
_displayValueLabel->setAnchorPoint(Point(0.5f, -1.0f));
_displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
2013-09-16 20:54:13 +08:00
// Add the alert
gui::Text* alert = gui::Text::create();
2013-09-16 20:54:13 +08:00
alert->setText("Button");
2013-12-23 15:35:35 +08:00
alert->setFontName("Marker Felt");
2013-09-16 20:54:13 +08:00
alert->setFontSize(30);
alert->setColor(Color3B(159, 168, 176));
2013-12-23 15:35:35 +08:00
alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
_uiLayer->addChild(alert);
2013-09-16 20:54:13 +08:00
// Create the button
2013-12-23 15:35:35 +08:00
Button* button = Button::create();
2013-09-16 20:54:13 +08:00
button->setTouchEnabled(true);
button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", "");
button->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(button);
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UIButtonTest::touchEvent(Object *pSender, TouchEventType type)
{
switch (type)
{
case gui::TOUCH_EVENT_BEGAN:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Down")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_MOVED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Move")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_ENDED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Up")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_CANCELED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(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())
{
2013-12-23 15:35:35 +08:00
Size widgetSize = _widget->getSize();
2013-09-16 20:54:13 +08:00
// Add a label in which the button events will be displayed
_displayValueLabel = gui::Text::create();
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText("No Event");
_displayValueLabel->setFontName("Marker Felt");
_displayValueLabel->setFontSize(32);
_displayValueLabel->setAnchorPoint(Point(0.5f, -1.0f));
_displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
2013-09-16 20:54:13 +08:00
// Add the alert
gui::Text* alert = gui::Text::create();
2013-09-16 20:54:13 +08:00
alert->setText("Button scale9 render");
2013-12-23 15:35:35 +08:00
alert->setFontName("Marker Felt");
2013-09-16 20:54:13 +08:00
alert->setFontSize(30);
alert->setColor(Color3B(159, 168, 176));
2013-12-23 15:35:35 +08:00
alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
_uiLayer->addChild(alert);
2013-09-16 20:54:13 +08:00
// Create the button
2013-12-23 15:35:35 +08:00
Button* button = Button::create();
2013-09-16 20:54:13 +08:00
button->setTouchEnabled(true);
// open scale9 render
button->setScale9Enabled(true);
button->loadTextures("cocosgui/button.png", "cocosgui/buttonHighlighted.png", "");
button->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->setSize(Size(150, button->getContentSize().height * 1.5f));
button->addTouchEventListener(this, toucheventselector(UIButtonTest_Scale9::touchEvent));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(button);
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UIButtonTest_Scale9::touchEvent(Object *pSender, TouchEventType type)
{
switch (type)
{
case gui::TOUCH_EVENT_BEGAN:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Down")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_MOVED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Move")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_ENDED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Up")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_CANCELED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(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())
{
2013-12-23 15:35:35 +08:00
Size widgetSize = _widget->getSize();
2013-09-16 20:54:13 +08:00
// Add a label in which the button events will be displayed
_displayValueLabel = gui::Text::create();
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText("No Event");
_displayValueLabel->setFontName("Marker Felt");
_displayValueLabel->setFontSize(32);
_displayValueLabel->setAnchorPoint(Point(0.5f, -1.0f));
_displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
2013-09-16 20:54:13 +08:00
// Add the alert
gui::Text* alert = gui::Text::create();
2013-09-16 20:54:13 +08:00
alert->setText("Button Pressed Action");
2013-12-23 15:35:35 +08:00
alert->setFontName("Marker Felt");
2013-09-16 20:54:13 +08:00
alert->setFontSize(30);
alert->setColor(Color3B(159, 168, 176));
2013-12-23 15:35:35 +08:00
alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
_uiLayer->addChild(alert);
2013-09-16 20:54:13 +08:00
// Create the button
2013-12-23 15:35:35 +08:00
Button* button = Button::create();
2013-09-16 20:54:13 +08:00
button->setTouchEnabled(true);
button->setPressedActionEnabled(true);
button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", "");
button->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(this, toucheventselector(UIButtonTest_PressedAction::touchEvent));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(button);
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UIButtonTest_PressedAction::touchEvent(Object *pSender, TouchEventType type)
{
switch (type)
{
case gui::TOUCH_EVENT_BEGAN:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Down")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_MOVED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Move")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_ENDED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Up")->getCString());
2013-09-16 20:54:13 +08:00
break;
case gui::TOUCH_EVENT_CANCELED:
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText(String::createWithFormat("Touch Cancelled")->getCString());
break;
default:
break;
}
}
// UIButtonTest_Title
UIButtonTest_Title::UIButtonTest_Title()
: _displayValueLabel(nullptr)
{
}
UIButtonTest_Title::~UIButtonTest_Title()
{
}
bool UIButtonTest_Title::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getSize();
// Add a label in which the text button events will be displayed
_displayValueLabel = gui::Text::create();
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText("No Event");
_displayValueLabel->setFontName("Marker Felt");
_displayValueLabel->setFontSize(32);
_displayValueLabel->setAnchorPoint(Point(0.5f, -1));
_displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
gui::Text* alert = gui::Text::create();
2013-12-23 15:35:35 +08:00
alert->setText("Button with title");
alert->setFontName("Marker Felt");
alert->setFontSize(30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
_uiLayer->addChild(alert);
// Create the button with title
Button* button = Button::create();
button->setTouchEnabled(true);
button->loadTextures("cocosgui/backtotoppressed.png", "cocosgui/backtotopnormal.png", "");
button->setTitleText("Title Button");
button->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(this, toucheventselector(UIButtonTest_Title::touchEvent));
_uiLayer->addChild(button);
return true;
}
return false;
}
void UIButtonTest_Title::touchEvent(Object *pSender, TouchEventType type)
{
switch (type)
{
case TOUCH_EVENT_BEGAN:
_displayValueLabel->setText(String::createWithFormat("Touch Down")->getCString());
break;
case TOUCH_EVENT_MOVED:
_displayValueLabel->setText(String::createWithFormat("Touch Move")->getCString());
break;
case TOUCH_EVENT_ENDED:
_displayValueLabel->setText(String::createWithFormat("Touch Up")->getCString());
break;
case TOUCH_EVENT_CANCELED:
_displayValueLabel->setText(String::createWithFormat("Touch Cancelled")->getCString());
2013-09-16 20:54:13 +08:00
break;
default:
break;
}
}