bool ControlButtonTest_Event::init() { if (ControlScene::init()) { auto screenSize = Director::getInstance()->getWinSize(); // Add a label in which the button events will be displayed setDisplayValueLabel(Label::createWithTTF("No Event", "fonts/Marker Felt.ttf", 32)); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1)); _displayValueLabel->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f); addChild(_displayValueLabel, 1); setDisplayBitmaskLabel(Label::createWithTTF("No bitmask event", "fonts/Marker Felt.ttf", 24)); _displayBitmaskLabel->setAnchorPoint(Vec2(0.5f, -1)); Vec2 bitmaskLabelPos = _displayValueLabel->getPosition() - Vec2(0, _displayBitmaskLabel->getBoundingBox().size.height); _displayBitmaskLabel->setPosition(bitmaskLabelPos); addChild(_displayBitmaskLabel, 1); // Add the button auto backgroundButton = ui::Scale9Sprite::create("extensions/button.png"); auto backgroundHighlightedButton = ui::Scale9Sprite::create("extensions/buttonHighlighted.png"); auto titleButton = Label::createWithTTF("Touch Me!", "fonts/Marker Felt.ttf", 30); titleButton->setColor(Color3B(159, 168, 176)); ControlButton *controlButton = ControlButton::create(titleButton, backgroundButton); controlButton->setBackgroundSpriteForState(backgroundHighlightedButton, Control::State::HIGH_LIGHTED); controlButton->setTitleColorForState(Color3B::WHITE, Control::State::HIGH_LIGHTED); controlButton->setAnchorPoint(Vec2(0.5f, 1)); controlButton->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f); addChild(controlButton, 1); // Add the black background auto background = ui::Scale9Sprite::create("extensions/buttonBackground.png"); background->setContentSize(Size(300, 170)); background->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f); addChild(background); // Sets up event handlers controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchDownAction), Control::EventType::TOUCH_DOWN); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchDragInsideAction), Control::EventType::DRAG_INSIDE); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchDragOutsideAction), Control::EventType::DRAG_OUTSIDE); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchDragEnterAction), Control::EventType::DRAG_ENTER); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchDragExitAction), Control::EventType::DRAG_EXIT); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchUpInsideAction), Control::EventType::TOUCH_UP_INSIDE); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchUpOutsideAction), Control::EventType::TOUCH_UP_OUTSIDE); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchCancelAction), Control::EventType::TOUCH_CANCEL); // test for issue 2882 controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButtonTest_Event::touchBitmaskAction), Control::EventType::TOUCH_DOWN | Control::EventType::DRAG_INSIDE | Control::EventType::DRAG_OUTSIDE | Control::EventType::DRAG_ENTER | Control::EventType::DRAG_EXIT | Control::EventType::TOUCH_UP_INSIDE | Control::EventType::TOUCH_UP_OUTSIDE | Control::EventType::TOUCH_CANCEL | Control::EventType::VALUE_CHANGED); return true; } return false; }
ControlButton* ButtonUtils::createButton(const char *normalFrame, const char *highLightFrame, const char *disableFrame, cocos2d::Size size, const char *label, float labelSize, Color3B labelColor) { Scale9Sprite *normal = Scale9Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(normalFrame)); Scale9Sprite *highLight = Scale9Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(highLightFrame)); Scale9Sprite *disable = Scale9Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(disableFrame)); ControlButton *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); //btn->setTouchPriority(0); return btn; }
ControlButton *ControlButtonTest_HelloVariableSize::standardButtonWithTitle(const char * title) { /** Creates and return a button with a default background and title color. */ auto backgroundButton = ui::Scale9Sprite::create("extensions/button.png"); auto backgroundHighlightedButton = ui::Scale9Sprite::create("extensions/buttonHighlighted.png"); auto titleButton = Label::createWithTTF(title, "fonts/Marker Felt.ttf", 30); titleButton->setColor(Color3B(159, 168, 176)); ControlButton *button = ControlButton::create(titleButton, backgroundButton); button->setBackgroundSpriteForState(backgroundHighlightedButton, Control::State::HIGH_LIGHTED); button->setTitleColorForState(Color3B::WHITE, Control::State::HIGH_LIGHTED); return button; }
ControlButton *AboutLayer::standardButtonWithTitle(const std::string& string) { /** Creates and return a button with a default background and title color. */ auto backgroundButton = Scale9Sprite::create("button.png"); auto backgroundHighlightedButton = Scale9Sprite::create("buttonHighlighted.png"); auto titleButton = LabelTTF::create(string, "Marker Felt", 50); //auto label = LabelTTF::create("中国", "Marker Felt", 30); titleButton->setColor(Color3B(159, 168, 176)); ControlButton *button = ControlButton::create(titleButton, backgroundButton); button->setBackgroundSpriteForState(backgroundHighlightedButton, Control::State::HIGH_LIGHTED); button->setTitleColorForState(Color3B::WHITE, Control::State::HIGH_LIGHTED); button->addTargetWithActionForControlEvents(this, cccontrol_selector(AboutLayer::touchUpInsideAction), Control::EventType::TOUCH_UP_INSIDE); this->addChild(button); return button; }