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;
    }
}
Esempio n. 2
0
void StartScene::appendButton(Vec2 point)
{
    auto playButton = ControlButton::create(Scale9Sprite::create("btn-play-normal.png"));
    playButton->setAdjustBackgroundImage(false);
    playButton->setPosition(point.x, point.y);
    playButton->addTargetWithActionForControlEvents(
        this,
        cccontrol_selector(StartScene::onTapPlayButton),
        Control::EventType::TOUCH_UP_INSIDE);

    addChild(playButton);

    auto scoreButton = ControlButton::create(Scale9Sprite::create("Images/SendScoreButton.png"));
    scoreButton->setAdjustBackgroundImage(false);
    scoreButton->setPosition(point.x, point.y - 100);
    scoreButton->addTargetWithActionForControlEvents(
        this,
        cccontrol_selector(StartScene::onTapScoreButton),
        Control::EventType::TOUCH_UP_INSIDE);

    addChild(scoreButton);
}
Esempio n. 3
0
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;
	}
}
Esempio n. 4
0
void EstateController::showButton1(std::vector<std::string> filename)
{
    Size winSize = Director::getInstance()->getVisibleSize();
    int temp_h = 0;
    int i = 0;
    for (std::vector<std::string>::iterator it = filename.begin(); it != filename.end(); ++it) {
        //        cout << *it << '\n';
        //ボタンを作成する
        auto button = ControlButton::create(Scale9Sprite::create(*it));
        //画像を引き延ばさない設定
        button->setAdjustBackgroundImage(false);
        Size a = button->getContentSize();
        //ボタンの位置設定
        button->setPosition(winSize.width/2, winSize.height-temp_h-a.height);
        temp_h += a.height;
        button->setScaleRatio(0.5f);
        //ボタンをタップしたときに呼び出す関数の設定
        switch (i) {
            case 0:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton1),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 1:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton2),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 2:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton3),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 3:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton4),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 4:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton5),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 5:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton6),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 6:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton7),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
            case 7:
                button->addTargetWithActionForControlEvents(this,
                                                            cccontrol_selector(EstateController::onTapButton8),
                                                            Control::EventType::TOUCH_UP_INSIDE);
                break;
                
            default:
                break;
        }
        i++;
        //ボタンに表示する文字
        button->setTitleForState("", Control::State::NORMAL);
        //画面に追加する
        addChild(button);
    }
}