void ScoresLayer::DisplayText(char *text, cocos2d::ccColor3B color, cocos2d::CCPoint ccp, int size) { CCLabelTTF* pLabel = CCLabelTTF::create(text, "Arial", size * 2); pLabel->setColor(color); pLabel->setScale(0.5); pLabel->setPosition(ccp(ccp.x + pLabel->getContentSize().width * pLabel->getScaleX() / 2, ccp.y)); int tag = ccp.x + ccp.y * 10000; this->removeChildByTag(tag); this->addChild(pLabel, 10, tag); }
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; }