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; }
bool UndercoverGameScene::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ControlButton* back = ControlButton::create("Back", "Arial", 40); back->addTargetWithActionForControlEvents(this, cccontrol_selector(UndercoverGameScene::buttonBack), Control::EventType::TOUCH_UP_INSIDE); back->setAnchorPoint(Point(0.5,0.5)); back->setZoomOnTouchDown(true); back->setPosition(Vec2(visibleSize.width - 100, visibleSize.height - 100)); this->addChild(back, 1); return true; }
ControlButton * createBtn(float x, float y, const char * normalBg, const char * pressBg, Ref* target, Control::Handler action) { Scale9Sprite * spNormalBg = createDotaScale9Sprite(normalBg); Scale9Sprite * spPressBg = createDotaScale9Sprite(pressBg); ControlButton *btn = ControlButton::create(spNormalBg); if (spPressBg) btn->setBackgroundSpriteForState(spPressBg, Control::State::HIGH_LIGHTED); btn->setAnchorPoint(Vec2(0, 0)); btn->setPosition(x, y); btn->setZoomOnTouchDown(false); btn->setPreferredSize(spNormalBg->getOriginalSize()); if (target && action) btn->addTargetWithActionForControlEvents(target, action, Control::EventType::TOUCH_UP_INSIDE); return btn; }