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;
    }
}
Пример #2
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;
	}
}
Пример #3
0
void CCtrlPage::SetBgAni(const char* pszAni)
{
	if (NULL == pszAni)
	{
		return;
	}

	clearBackgroundSprite();

	m_strAniSection = std::string(pszAni);

	if (m_strAniSection.empty())
	{
		return;
	}

	CMyIniPtr pAni = g_objIniMgr.GetMyIniPtr(m_strAniPath.c_str());

	if (!pAni)
	{
		return;
	}

	int nFrame = 0;
	std::string strFrame = FORMAT("Frame%d")<<nFrame;
	const char *pszValue = pAni->GetString(m_strAniSection.c_str(), strFrame.c_str());

	if (NULL == pszValue)
	{
		return;
	}

	CCScale9Sprite *backgroundSprite = CCScale9Sprite::create(pszValue);

	if (!backgroundSprite)
	{
		return;
	};

	// 不为9宫格方式
	if (enumUISTRETCH_NORMAL == m_nAniStretch)
	{
		CCRect rectCapInsets = CCRectMake(0, 0, backgroundSprite->getContentSize().width, backgroundSprite->getContentSize().height);
		backgroundSprite->removeAllChildren();
		CCScale9Sprite *backgroundSpriteResize =  backgroundSprite->resizableSpriteWithCapInsets(rectCapInsets);
		CHECK(backgroundSpriteResize);
		setBackgroundSprite(backgroundSpriteResize);
	}
	else if (enumUISTRETCH_STRETCH == m_nAniStretch)
	{
		float fOrgWidth = backgroundSprite->getContentSize().width;
		float fOrgHeight = backgroundSprite->getContentSize().height;
		CCRect rectCapInsets = CCRectMake(0, 0, fOrgWidth, fOrgHeight);
		backgroundSprite->removeAllChildren();
		CCScale9Sprite *backgroundSpriteResize =  backgroundSprite->resizableSpriteWithCapInsets(rectCapInsets);
		CHECK(backgroundSpriteResize);
		float fScaleX = 1.0f;
		float fScaleY = 1.0f;

		if (0.0f != fOrgWidth && 0.0f != fOrgHeight)
		{
			fScaleX = (float)m_nWidth / fOrgWidth;
			fScaleY = (float)m_nHeight / fOrgHeight;
		}

		backgroundSpriteResize->setScaleX(fScaleX);
		backgroundSpriteResize->setScaleY(fScaleY);
		setBackgroundSprite(backgroundSpriteResize);
	}
	else
	{
		setBackgroundSprite(backgroundSprite);
	}

	CHECK(m_backgroundSprite);
	m_backgroundSprite->setAnchorPoint(ccp(0, 0));
	m_backgroundSprite->setPosition(ccp(0, 0));
	this->addChild(m_backgroundSprite, 0);
}