void ChoiceLayer::setChoices(vector<string> choices) { size_t btnCount = getChildrenCount(); size_t choiceCount = choices.size(); float btnHeight = GlobalConfig::getInstance()->getChoiceMenuFontSize() + 10; float spaceBetweenBtn = btnHeight; float deltaUnit = btnHeight + spaceBetweenBtn; float initHeight = choiceCount % 2 == 0 ? spaceBetweenBtn*0.5 + (choiceCount / 2 - 1)*deltaUnit : spaceBetweenBtn + btnHeight*0.5 + ((choiceCount - 1) / 2 - 1)*deltaUnit; if (choiceCount > btnCount) for (int i = btnCount; i<choices.size(); i++) { auto btn = createButton(i, choices[i]); } for (int i = 0; i < choiceCount; i++) { auto btn = (ControlButton*)getChildByTag(i); string choice = choices.at(i); btn->setPosition(Vec2(0, initHeight - i*deltaUnit)); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) GBKToUTF(choice); #endif btn->setTitleForState(choice, Control::State::NORMAL); btn->setOpacity(255); } }
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; } }
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; } }
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); } }