cocos2dx之彩色编辑输入框的设计(2)
来源:未知 责任编辑:责任编辑 发表时间:2015-09-17 09:43 点击:次
再来具体看看实现部分:
首先初始化:
bool ColorEdit::myInit(Scale9Sprite * pBgSprite,Node * parent,EditLocation editLocation,EditType editType) { if (!Layer::init()) { return false; } m_pEditBg = pBgSprite; m_pTarget = parent; m_editLocation = editLocation; m_editType = editType; this->addChild(m_pEditBg); auto centerSize = m_pEditBg->getContentSize(); //这里要设置好九宫图中间的矩形的大小,因为我们要把输入的内容显示在上面,否则会看起来乱七八糟 m_pEditBg->setCapInsets(Rect(0,0,centerSize.width * 0.9,centerSize.height * 0.9)); auto pLabel = Label::createWithTTF(,fonts/Marker Felt.ttf,centerSize.height * 0.75); pLabel->setColor(Color3B::WHITE); pLabel->setAnchorPoint(Vec2(0,0.5)); pLabel->setTag(103); pLabel->setPosition(Vec2(centerSize.width * 0.08,centerSize.height * 0.5)); m_pEditBg->addChild(pLabel,2); //如果是全屏显示类型 if (m_editLocation == Location_Screen) { //添加阴影层,先隐藏 auto keyLayer = LayerColor::create(Color4B(0,0,0,100)); keyLayer->setPosition(Point::ZERO); keyLayer->setTag(100); m_pTarget->addChild(keyLayer,this->getMaxZOrder(parent) + 2); keyLayer->setVisible(false); //添加上方的输入框 auto key_bigbg = Scale9Sprite::create(coloredit/key_bigbg.png); key_bigbg->setContentSize(Size(Director::getInstance()->getWinSize().width,key_bigbg->getContentSize().height)); key_bigbg->setTag(101); key_bigbg->setAnchorPoint(Vec2(0.5,0)); auto upSize = key_bigbg->getContentSize(); key_bigbg->setPosition(Vec2(m_pTarget->getContentSize().width / 2,m_pTarget->getContentSize().height)); m_pTarget->addChild(key_bigbg,this->getMaxZOrder(parent) + 2); //同样设置九宫图中间的矩形 key_bigbg->setCapInsets(Rect(0,0,upSize.width * 0.9,upSize.height * 0.9)); auto label_up = Label::createWithTTF(,fonts/Marker Felt.ttf,upSize.height * 0.75); label_up->setColor(Color3B::WHITE); label_up->setAnchorPoint(Vec2(0,0.5)); label_up->setTag(102); label_up->setPosition(Vec2(upSize.width * 0.08,upSize.height / 2)); key_bigbg->addChild(label_up); } m_keyBg = Sprite::create(coloredit/key_bg.png); m_keyBg->setAnchorPoint(Vec2(0.5,1)); m_keyBg->setPosition(Vec2(m_pTarget->getContentSize().width / 2,0)); m_pTarget->addChild(m_keyBg,this->getMaxZOrder(parent) + 2); auto bgSize = m_keyBg->getContentSize(); auto pMenu = Menu::create(); pMenu->setPosition(Vec2::ZERO); m_keyBg->addChild(pMenu); if (m_editType == Edit_Number)//添加数字键盘 { for (int i = 0; i < 2; i++) { for (int j = 0; j < 5; j++) { auto numSprNor = Sprite::create(__String::createWithFormat(coloredit/num_%d.png,i * 5 + j)->getCString()); auto numSprSel = Sprite::create(__String::createWithFormat(coloredit/num_%d.png,i * 5 + j)->getCString()); auto numBtn = MenuItemSprite::create(numSprNor,numSprSel,CC_CALLBACK_1(ColorEdit::onNumBtnCallback,this)); numBtn->setTag(Key_Num_0 + i * 5 + j); numBtn->setPosition(Vec2(bgSize.width / 10 * (j * 2 + 1),bgSize.height / 6 * ((3 - i) * 2 - 1))); pMenu->addChild(numBtn); } } auto delSprNor = Sprite::create(coloredit/btn_del.png); auto delSprSel = Sprite::create(coloredit/btn_del.png); auto delBtn = MenuItemSprite::create(delSprNor,delSprSel,CC_CALLBACK_1(ColorEdit::onFunBtnCallback,this)); delBtn->setTag(Key_Delete); delBtn->setPosition(Vec2(bgSize.width / 4 - 15,bgSize.height / 6)); pMenu->addChild(delBtn); auto sureSprNor = Sprite::create(coloredit/btn_sure.png); auto sureSprSel = Sprite::create(coloredit/btn_sure.png); auto sureBtn = MenuItemSprite::create(sureSprNor,sureSprSel,CC_CALLBACK_1(ColorEdit::onFunBtnCallback,this)); sureBtn->setTag(Key_Sure); sureBtn->setPosition(Vec2(bgSize.width / 4 * 3 + 15,bgSize.height / 6)); pMenu->addChild(sureBtn); } else if (m_editType == Edit_Alphabet)//和number差不多,多了些按钮而已 { } else if (m_editType == Edit_PinYin)//这个要是实现的话,就成输入法了,咱们还是调用系统的吧。 { } //简单暴力的屏幕适配 auto rate_x = Director::getInstance()->getWinSize().width / bgSize.width; m_keyBg->setScaleX(rate_x); auto listenerT = EventListenerTouchOneByOne::create(); listenerT->onTouchBegan = CC_CALLBACK_2(ColorEdit::onTouchBegan,this); listenerT->setSwallowTouches(false); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerT,this); return true; }
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>