bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, Scale9Sprite* backgroundSprite) { if (Control::init()) { 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); if (nullptr != backgroundSprite) { setBackgroundSpriteForState(backgroundSprite, Control::State::NORMAL); } setLabelAnchorPoint(Vec2::ANCHOR_MIDDLE); // Layout update needsLayout(); return true; } //couldn't init the Control else { return false; } }
bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, Scale9Sprite* backgroundSprite) { if (Control::init()) { CCASSERT(node != NULL, "Label must not be nil."); LabelProtocol* label = dynamic_cast<LabelProtocol*>(node); RGBAProtocol* rgbaLabel = dynamic_cast<RGBAProtocol*>(node); CCASSERT(backgroundSprite != NULL, "Background sprite must not be nil."); CCASSERT(label != NULL || rgbaLabel!=NULL || backgroundSprite != NULL, ""); _parentInited = true; // Initialize the button state tables this->setTitleDispatchTable(Dictionary::create()); this->setTitleColorDispatchTable(Dictionary::create()); this->setTitleLabelDispatchTable(Dictionary::create()); this->setBackgroundSpriteDispatchTable(Dictionary::create()); setTouchEnabled(true); _isPushed = false; _zoomOnTouchDown = true; _currentTitle=NULL; // Adjust the background image by default setAdjustBackgroundImage(true); setPreferredSize(Size::ZERO); // Zooming button by default _zoomOnTouchDown = true; // 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 String* tempString = String::create(label->getString()); //tempString->autorelease(); setTitleForState(tempString, Control::State::NORMAL); setTitleColorForState(rgbaLabel->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; } //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 ControlButton::needsLayout() { if (!_parentInited) { return; } // Hide the background and the label if (_titleLabel != nullptr) { _titleLabel->setVisible(false); } if (_backgroundSprite) { _backgroundSprite->setVisible(false); } // Update anchor of all labels this->setLabelAnchorPoint(this->_labelAnchorPoint); // Update the label to match with the current state _currentTitle = getTitleForState(_state); _currentTitleColor = getTitleColorForState(_state); this->setTitleLabel(getTitleLabelForState(_state)); LabelProtocol* label = dynamic_cast<LabelProtocol*>(_titleLabel); if (label && !_currentTitle.empty()) { label->setString(_currentTitle); } if (_titleLabel) { _titleLabel->setColor(_currentTitleColor); } if (_titleLabel != nullptr) { _titleLabel->setPosition(Vec2 (getContentSize().width / 2, getContentSize().height / 2)); } // Update the background sprite this->setBackgroundSprite(this->getBackgroundSpriteForState(_state)); if (_backgroundSprite != nullptr) { _backgroundSprite->setPosition(Vec2 (getContentSize().width / 2, getContentSize().height / 2)); } // Get the title label size Size titleLabelSize; if (_titleLabel != nullptr) { titleLabelSize = _titleLabel->getBoundingBox().size; } // Adjust the background image if necessary if (_doesAdjustBackgroundImage) { // Add the margins if (_backgroundSprite != nullptr) { _backgroundSprite->setContentSize(Size(titleLabelSize.width + _marginH * 2, titleLabelSize.height + _marginV * 2)); } } else { //TODO: should this also have margins if one of the preferred sizes is relaxed? if (_backgroundSprite != nullptr) { Size preferredSize = _backgroundSprite->getPreferredSize(); if (preferredSize.width <= 0) { preferredSize.width = titleLabelSize.width; } if (preferredSize.height <= 0) { preferredSize.height = titleLabelSize.height; } _backgroundSprite->setContentSize(preferredSize); } } // Set the content size Rect rectTitle; if (_titleLabel != nullptr) { rectTitle = _titleLabel->getBoundingBox(); } Rect rectBackground; if (_backgroundSprite != nullptr) { rectBackground = _backgroundSprite->getBoundingBox(); } Rect maxRect = ControlUtils::RectUnion(rectTitle, rectBackground); setContentSize(Size(maxRect.size.width, maxRect.size.height)); if (_titleLabel != nullptr) { _titleLabel->setPosition(Vec2(getContentSize().width/2, getContentSize().height/2)); // Make visible the background and the label _titleLabel->setVisible(true); } if (_backgroundSprite != nullptr) { _backgroundSprite->setPosition(Vec2(getContentSize().width/2, getContentSize().height/2)); _backgroundSprite->setVisible(true); } }