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; }
void MailInfoListLayer::setSystemMail(Email mail) { //CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-132,true); ListMenu *menu = ListMenu::create(); menu->setPosition(CCPointZero); menu->setAnchorPoint(CCPointZero); menu->setContentSize(this->getContentSize()); addChild(menu); m_mMenu = menu; //m_mMenu->runAction(CCCallFuncND::create(this, callfuncND_selector(MailInfoListLayer::changeMenuPriority_callfuncND), (void*)(-133))); m_eEmail = mail; sortMail(mail); int count = 0; int height = m_nHeight; int width = count * 100; m_nWidth = width; count = 0; int side = 50; int bWidth = 0; int _width = 0; int pos_x = 0; for (map<string,string>::iterator iter = m_lThingsList.begin(); iter != m_lThingsList.end() ; iter ++) { CCSprite *cailiaoSpr = CCSprite::create("daoju_kuang_bg.png"); cailiaoSpr->setScale(0.7f); bWidth = cailiaoSpr->getContentSize().width * cailiaoSpr->getScaleX(); cailiaoSpr->setPosition(ccp(count * bWidth + ( side * ( count * 2 + 1 ) ) + bWidth / 2 ,height - cailiaoSpr->getContentSize().height * cailiaoSpr->getScaleY() / 2 - 5)); addChild(cailiaoSpr); CCSprite *daojuSpr = CCSprite::create(iter->first.c_str()); daojuSpr->setScale(0.7f); bWidth = daojuSpr->getContentSize().width * cailiaoSpr->getScaleX(); daojuSpr->setPosition(ccp(cailiaoSpr->getContentSize().width / 2, cailiaoSpr->getContentSize().height / 2)); cailiaoSpr->addChild(daojuSpr); CCLabelTTF *nameTTF = CCLabelTTF::create(iter->second.c_str(),fontStr_kaiti,25.0f); nameTTF->setPosition(ccp(cailiaoSpr->getPositionX(),nameTTF->getContentSize().height / 2 + 5)); addChild(nameTTF); nameTTF->setColor(ccWHITE); _width += cailiaoSpr->getPositionX() - pos_x; pos_x = cailiaoSpr->getPositionX(); count ++; } _width += bWidth / 2 + side; for (vector<Flat_DuanzaoGroupZBInfo>::iterator iter = mail.equipVec.begin(); iter != mail.equipVec.end() ; iter ++) { CCLabelTTF *nameTTF = CCLabelTTF::create(iter->zhuangbei_name.c_str(),fontStr_kaiti,25.0f); nameTTF->setPosition(ccp(count * bWidth + ( side * ( count * 2 + 1 ) ) + bWidth / 2 + nameTTF->getContentSize().height / 2 ,nameTTF->getContentSize().height / 2 + 20)); addChild(nameTTF); nameTTF->setColor(ccWHITE); //string nameTemp = CS::getItemName(iter->zb_group_id) + LFStrings::getValue("zhi") + LFStrings::getValue("suipian") + getStrByInt(iter->suipian_id); string equipfile = iter->zhuangbei_pic; equipfile = "minute_" + equipfile + ".png"; m_lThingsList.insert(map<string,string>::value_type(equipfile,iter->zhuangbei_name)); string diwenStr = ImageManager::getManager()->getImageData_DiWen_WithBianKuang(iter->zhuangbei_colour); CCMenuItemImage *eqDiwenImg = CCMenuItemImage::create(diwenStr.c_str(), diwenStr.c_str(), this, menu_selector(MailInfoListLayer::menuItemClicked_Equip)); m_mMenu->addChild(eqDiwenImg); eqDiwenImg->setTag(iter->zb_group_id); eqDiwenImg->setScale(0.8f); bWidth = eqDiwenImg->getContentSize().width * eqDiwenImg->getScaleX(); eqDiwenImg->setPosition(ccp( nameTTF->getPositionX(),nameTTF->getPositionY() + nameTTF->getContentSize().height / 2 + eqDiwenImg->getContentSize().height / 2 + 5)); CCSprite *daojuSpr = CCSprite::create(equipfile.c_str()); daojuSpr->setScale(0.24f); bWidth = daojuSpr->getContentSize().width * eqDiwenImg->getScaleX(); daojuSpr->setPosition(ccp(eqDiwenImg->getContentSize().width / 2, eqDiwenImg->getContentSize().height / 2)); eqDiwenImg->addChild(daojuSpr); /*CCSprite *cailiaoSpr = CCSprite::create("daoju_kuang_bg.png"); cailiaoSpr->setScale(0.7f); bWidth = cailiaoSpr->getContentSize().width * cailiaoSpr->getScaleX(); cailiaoSpr->setPosition(ccp(count * bWidth + ( side * ( count * 2 + 1 ) ) + bWidth / 2 ,height - cailiaoSpr->getContentSize().height * cailiaoSpr->getScaleY() / 2 - 5)); addChild(cailiaoSpr);*/ _width += eqDiwenImg->getPositionX() - pos_x; pos_x = eqDiwenImg->getPositionX(); count ++; } _width += bWidth / 2 + side; m_nWidth = _width; this->setContentSize(CCSizeMake(m_nWidth,m_nHeight)); }