当前位置: 首页>>代码示例>>C++>>正文


C++ CCMenuItemImage::getScaleY方法代码示例

本文整理汇总了C++中CCMenuItemImage::getScaleY方法的典型用法代码示例。如果您正苦于以下问题:C++ CCMenuItemImage::getScaleY方法的具体用法?C++ CCMenuItemImage::getScaleY怎么用?C++ CCMenuItemImage::getScaleY使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CCMenuItemImage的用法示例。


在下文中一共展示了CCMenuItemImage::getScaleY方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

CCMenuItemImage* Objects2dFactory::textButton(CCNode* scene, CCMenu* menu, string value, string normalImagePath, string selectedImagePath, string disabledImagePath, 
	float positionX, float positionY, float width, float height, SEL_MenuHandler selector, void* selectorArg, AlignX alignX, AlignY alignY, ccColor3B textColor, int zOrder)
{
	// Check arguments validity
	if(scene == NULL ||menu == NULL || !selector)
		return NULL;

	// Create a button menu item
    CCMenuItemImage *button = CCMenuItemImage::create(
        normalImagePath.c_str(),
        selectedImagePath.c_str(),
		disabledImagePath.c_str(),
        scene,
        selector);
    if(!button)
		return NULL;

	if (selectorArg != NULL)
	{
		button->setUserData(selectorArg);
	}

    // Place the menu item in the given position
    button->setPosition(ccp(positionX, positionY));

	// Set menu item anchor
	button->setAnchorPoint(ccp(Constants::getAnchorValue(alignX), Constants::getAnchorValue(alignY)));
	
	// Set menu item height
	float buttonHeight = button->boundingBox().size.height;
	float buttonPosScaleHeight = buttonHeight;
	if (height > 0) // if height is defined, it is setted
	{
		button->setScaleY(height / buttonHeight);
		buttonPosScaleHeight = height;
	}
	
    // Create a label and initialize with the given string
	CCLabelTTF* pLabel = CCLabelTTF::create(value.c_str(), "Arial"/*string(Constants::getResourcesPath() + "SOResources/Fonts/AlphaFridgeMagnetsAllCap.ttf").c_str()*/, 
		buttonPosScaleHeight * 0.6f);
    if(!pLabel)
		return NULL;
	
    // Set color
    pLabel->setColor(textColor);
	
	// Set menu item width
	float buttonWidth = button->boundingBox().size.width;
	float buttonPosScaleWidth = buttonWidth;
	if (width > 0) // if width is defined, it is setted
	{
		button->setScaleX(width / buttonWidth);
		buttonPosScaleWidth = width;
	}
	else // otherwise, we will compare the text width with the button width
	{
		float textWidth = pLabel->boundingBox().size.width;
		if (textWidth * 1.25 > buttonWidth) // if the width adapted to the text is bigger than the button width, adapted width must be setted, otherwise, button width must stay as it was
		{
			button->setScaleX(textWidth * 1.25f / buttonWidth);
			buttonPosScaleWidth = textWidth * 1.25f;
		}
	}
	
    // Place the label; must be in the center of the button
	pLabel->setPosition(ccp(buttonPosScaleWidth * 0.5f * 1/button->getScaleX(), buttonPosScaleHeight * 0.5f * 1/button->getScaleY()));
	
	// Set label centered anchor
	pLabel->setAnchorPoint(ccp(Constants::getAnchorValue(Centered), Constants::getAnchorValue(Middle)));
	
	// Add label to button
	button->addChild(pLabel);

	// Set label scale; the inverse of button scale, so label can be as it would be if it was a child of scene
	pLabel->setScaleY(1/button->getScaleY());
	pLabel->setScaleX(1/button->getScaleX());

	// If label is larger than button
	if (pLabel->boundingBox().size.width * button->getScaleX() > button->boundingBox().size.width)
	{
		// Label must be rescaled
		float newScale = button->boundingBox().size.width / (pLabel->boundingBox().size.width * button->getScaleX());
		pLabel->setScaleX(pLabel->getScaleX() * newScale);
	}

	// Add button to the menu
	menu->addChild(button, zOrder);
	
	return button;
}
开发者ID:Cnotinfor,项目名称:TopQX_2D,代码行数:90,代码来源:Objects2dFactory.cpp


注:本文中的CCMenuItemImage::getScaleY方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。