ControlButton* ButtonUtils::createButton(const std::string& normalFrame,
							const std::string& highLightFrame,
							const std::string& disableFrame,
							Size size,
							const std::string& label,
							float labelSize ,
							Color3B labelColor)
{
	auto normal = Scale9Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(normalFrame));
	auto highLight = Scale9Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(highLightFrame));
	auto disable = Scale9Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(disableFrame));

	auto btn = ControlButton::create(label,"Arial",labelSize);

	//ÉèÖÃÏÔʾ³ß´ç
	btn->setPreferredSize(size);
	//¸ù¾Ý״̬ÉèÖñ³¾°
	btn->setBackgroundSpriteForState(normal,Control::State::NORMAL);
	btn->setBackgroundSpriteForState(highLight,Control::State::HIGH_LIGHTED);
	btn->setBackgroundSpriteForState(disable,Control::State::DISABLED);

	//¸ù¾Ý״̬ÉèÖð´Å¥ÎÄ×ÖÑÕÉ«
	btn->setTitleColorForState(labelColor,Control::State::NORMAL);
	btn->setTitleColorForState(labelColor,Control::State::HIGH_LIGHTED);
	btn->setTitleColorForState(labelColor,Control::State::DISABLED);

	//--TODOÉèÖô¥Ãþ¼¶±ð

	return btn;
}
void CCControlToggleButton::setToggleValue(bool val)
{
    m_toggleValue = val;
    
    if (m_toggleValue)
    {
        setBackgroundSpriteForState(toggleOn_, Control::State::NORMAL);
    }
    else
    {
        setBackgroundSpriteForState(toggleOff_, Control::State::NORMAL);
    }
}
Beispiel #3
0
void HelloWorld::testControlBtn()
{
    //创建正常状态下九宫格精灵
    auto btnNormal = Scale9Sprite::create("switch-on.png");
    
    //创建点击状态下九宫格精灵
    auto  btnSelected = Scale9Sprite::create("switch-off.png");
    
    /*创建标题*/
    auto title = LabelTTF::create("", "Marker Felt", 30);
    
    
    /* 根据标题和九宫格图片创建按钮 */
    auto  controlBtn = ControlButton::create(title, btnNormal);
    //setPreferredSize 设置大小*/
    controlBtn->setPreferredSize(Size(128, 128));
    
    /* 设置点击时候显示的图片 */
    controlBtn->setBackgroundSpriteForState(btnSelected, Control::State::HIGH_LIGHTED);
    
    controlBtn->setPosition(Point(200, 200));
    //注册事件
    controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::btnClicked), Control::EventType::TOUCH_UP_INSIDE);
    controlBtn->setTag(10);
    this->addChild(controlBtn);
}
void MyControlButton::setBlendFunc(){
    BlendFunc blend = {GL_SRC_ALPHA , GL_ONE};
    
    _bgSprite = ui::Scale9Sprite::createWithSpriteFrame(getBackgroundSprite()->getSprite()->getSpriteFrame());
    for (Node *  child : _bgSprite->_protectedChildren) {
        ((Sprite*)child)->setBlendFunc(blend);
    }
    setBackgroundSpriteForState(_bgSprite, Control::State::HIGH_LIGHTED);
}
bool MyControlButton::initWithLabelAndBackgroundSprite(Node* node, ui::Scale9Sprite* backgroundSprite,bool isSallowTouch)
{
    if (init(isSallowTouch))
    {
        CCASSERT(node != nullptr, "node must not be nil.");
        LabelProtocol* label = dynamic_cast<LabelProtocol*>(node);
        CCASSERT(backgroundSprite != nullptr, "Background sprite must not be nil.");
        CCASSERT(label != nullptr, "label must not be nil.");
        
        _parentInited = true;
        
        _isPushed = false;
        
        // Adjust the background image by default
        setAdjustBackgroundImage(true);
        setPreferredSize(Size::ZERO);
        // Zooming button by default
        _zoomOnTouchDown = true;
        _scaleRatio = 1.1f;
        
        // Set the default anchor point
        ignoreAnchorPointForPosition(false);
        setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        
        // Set the nodes
        setTitleLabel(node);
        setBackgroundSprite(backgroundSprite);
        
        // Set the default color and opacity
        setColor(Color3B::WHITE);
        setOpacity(255.0f);
        setOpacityModifyRGB(true);
        
        // Initialize the dispatch table
        
        setTitleForState(label->getString(), Control::State::NORMAL);
        setTitleColorForState(node->getColor(), Control::State::NORMAL);
        setTitleLabelForState(node, Control::State::NORMAL);
        setBackgroundSpriteForState(backgroundSprite, Control::State::NORMAL);
        
        setLabelAnchorPoint(Vec2::ANCHOR_MIDDLE);
        
        // Layout update
        needsLayout();
        
        return true;
    }
    //couldn't init the Control
    else
    {
        return false;
    }
}
ControlButton* ChoiceLayer::createButton(int idx, string title)
{
	string choiceBtnBg = GlobalConfig::getInstance()->getChoiceMenuBgPath();
	Size screenSize = Director::getInstance()->getVisibleSize();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
	GBKToUTF(title);
#endif 
	auto button = ControlButton::create(title, 
		GlobalConfig::getInstance()->getChoiceMenuFontName(), 
		GlobalConfig::getInstance()->getChoiceMenuFontSize());
	button->addTargetWithActionForControlEvents(this, cccontrol_selector(ChoiceLayer::choicesCallback), Control::EventType::TOUCH_UP_INSIDE);
	button->setBackgroundSpriteForState(Scale9Sprite::create(choiceBtnBg), Control::State::NORMAL);
	button->setPreferredSize(Size(screenSize.width*0.6, GlobalConfig::getInstance()->getChoiceMenuFontSize() + 10));
	button->setOpacity(0);
	button->setTitleColorForState(GlobalConfig::getInstance()->getChoiceMenuFontColor(), Control::State::NORMAL);
	addChild(button, 0, idx);
	return button;
}
bool ark_SwallowButton::init()
{
	if (Layer::init())
	{
		// Initialise instance variables
		_state=Control::State::NORMAL;
		setEnabled(true);
		setSelected(false);
		setHighlighted(false);

		auto dispatcher = Director::getInstance()->getEventDispatcher();
		auto touchListener = EventListenerTouchOneByOne::create();
		touchListener->setSwallowTouches(true);

		touchListener->onTouchBegan = CC_CALLBACK_2(Control::onTouchBegan, this);
		touchListener->onTouchMoved = CC_CALLBACK_2(Control::onTouchMoved, this);
		touchListener->onTouchEnded = CC_CALLBACK_2(Control::onTouchEnded, this);
		touchListener->onTouchCancelled = CC_CALLBACK_2(Control::onTouchCancelled, this);

		dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

		Scale9Sprite * backgroundSprite = Scale9Sprite::create();
		Label *node = Label::createWithSystemFont("", "Helvetica", 12);
		CCASSERT(node != nullptr, "Label must not be nil.");
		LabelProtocol* label = dynamic_cast<LabelProtocol*>(node);
		CCASSERT(backgroundSprite != nullptr, "Background sprite must not be nil.");
		CCASSERT(label != nullptr || backgroundSprite != nullptr, "");

		_parentInited = true;

		_isPushed = false;

		// Adjust the background image by default
		setAdjustBackgroundImage(true);
		setPreferredSize(Size::ZERO);
		// Zooming button by default
		_zoomOnTouchDown = true;
		_scaleRatio = 1.1f;

		// Set the default anchor point
		ignoreAnchorPointForPosition(false);
		setAnchorPoint(Point(0.5f, 0.5f));

		// Set the nodes
		setTitleLabel(node);
		setBackgroundSprite(backgroundSprite);

		// Set the default color and opacity
		setColor(Color3B(255.0f, 255.0f, 255.0f));
		setOpacity(255.0f);
		setOpacityModifyRGB(true);

		// Initialize the dispatch table

		setTitleForState(label->getString(), Control::State::NORMAL);
		setTitleColorForState(node->getColor(), Control::State::NORMAL);
		setTitleLabelForState(node, Control::State::NORMAL);
		setBackgroundSpriteForState(backgroundSprite, Control::State::NORMAL);

		setLabelAnchorPoint(Point(0.5f, 0.5f));

		// Layout update
		needsLayout();

		return true;
	}
	else
	{
		return false;
	}
}