axmol/samples/Cpp/TestCpp/Classes/ExtensionsTest/ControlExtensionTest/CCControlSwitchTest/CCControlSwitchTest.cpp

99 lines
3.6 KiB
C++
Raw Normal View History

2012-04-16 18:58:43 +08:00
/*
* ControlSwitchTest.m
2012-04-16 18:58:43 +08:00
*
* Copyright (c) 2012 Yannick Loriot
* http://yannickloriot.com
*
* 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 "CCControlSwitchTest.h"
2012-04-16 18:58:43 +08:00
ControlSwitchTest::~ControlSwitchTest()
2012-04-16 18:58:43 +08:00
{
CC_SAFE_RELEASE(_displayValueLabel);
2012-04-16 18:58:43 +08:00
}
bool ControlSwitchTest::init()
2012-04-16 18:58:43 +08:00
{
if (ControlScene::init())
2012-04-16 18:58:43 +08:00
{
Size screenSize = Director::sharedDirector()->getWinSize();
2012-04-16 18:58:43 +08:00
Node *layer = Node::create();
2013-07-12 14:11:55 +08:00
layer->setPosition(Point(screenSize.width / 2, screenSize.height / 2));
addChild(layer, 1);
2012-04-16 18:58:43 +08:00
double layer_width = 0;
// Add the black background for the text
Scale9Sprite *background = Scale9Sprite::create("extensions/buttonBackground.png");
background->setContentSize(CCSizeMake(80, 50));
2013-07-12 14:11:55 +08:00
background->setPosition(Point(layer_width + background->getContentSize().width / 2.0f, 0));
layer->addChild(background);
2012-04-16 18:58:43 +08:00
layer_width += background->getContentSize().width;
2012-04-16 18:58:43 +08:00
_displayValueLabel = LabelTTF::create("#color" ,"Marker Felt" ,30);
_displayValueLabel->retain();
_displayValueLabel->setPosition(background->getPosition());
layer->addChild(_displayValueLabel);
2012-04-16 18:58:43 +08:00
// Create the switch
ControlSwitch *switchControl = ControlSwitch::create
(
Sprite::create("extensions/switch-mask.png"),
Sprite::create("extensions/switch-on.png"),
Sprite::create("extensions/switch-off.png"),
Sprite::create("extensions/switch-thumb.png"),
LabelTTF::create("On", "Arial-BoldMT", 16),
LabelTTF::create("Off", "Arial-BoldMT", 16)
);
2013-07-12 14:11:55 +08:00
switchControl->setPosition(Point(layer_width + 10 + switchControl->getContentSize().width / 2, 0));
layer->addChild(switchControl);
2012-04-16 18:58:43 +08:00
switchControl->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlSwitchTest::valueChanged), ControlEventValueChanged);
2012-04-16 18:58:43 +08:00
// Set the layer size
layer->setContentSize(CCSizeMake(layer_width, 0));
2013-07-12 14:11:55 +08:00
layer->setAnchorPoint(Point(0.5f, 0.5f));
2012-04-16 18:58:43 +08:00
// Update the value label
valueChanged(switchControl, ControlEventValueChanged);
return true;
}
return false;
2012-04-16 18:58:43 +08:00
}
void ControlSwitchTest::valueChanged(Object* sender, ControlEvent controlEvent)
2012-04-16 18:58:43 +08:00
{
ControlSwitch* pSwitch = (ControlSwitch*)sender;
if (pSwitch->isOn())
2012-04-16 18:58:43 +08:00
{
_displayValueLabel->setString("On");
}
else
2012-04-16 18:58:43 +08:00
{
_displayValueLabel->setString("Off");
2012-04-16 18:58:43 +08:00
}
}