Cocos2d-x EditBox 带有监听功能

前端之家收集整理的这篇文章主要介绍了Cocos2d-x EditBox 带有监听功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

TestEditBox2.h

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "extensions/cocos-ext.h"
USING_NS_CC;
using namespace ui;

class TestEditBox2 : public Layout,public cocos2d::extension::EditBoxDelegate
{
public:
TestEditBox2();
virtual ~TestEditBox2();
void toExtensionsMainLayer(cocos2d::Ref *sender);

virtual void editBoxEditingDidBegin(cocos2d::extension::EditBox* editBox);
virtual void editBoxEditingDidEnd(cocos2d::extension::EditBox* editBox);
virtual void editBoxTextChanged(cocos2d::extension::EditBox* editBox,const std::string& text);
virtual void editBoxReturn(cocos2d::extension::EditBox* editBox);
private:
cocos2d::Label* _TTFShowEditReturn;
cocos2d::extension::EditBox* _editName;
cocos2d::extension::EditBox* _editPassword;
cocos2d::extension::EditBox* _editEmail;
};


TestEditBox2.cpp

#include "TestEditBox2.h"

USING_NS_CC;
USING_NS_CC_EXT;


TestEditBox2::TestEditBox2()
{
auto glview = Director::getInstance()->getOpenGLView();
auto visibleOrigin = glview->getVisibleOrigin();
auto visibleSize = glview->getVisibleSize();

auto pBg = Sprite::create("HelloWorld.png");
pBg->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2,visibleOrigin.y + visibleSize.height / 2));
addChild(pBg);

_TTFShowEditReturn = Label::createWithSystemFont("No edit control return!","",30);
_TTFShowEditReturn->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2,visibleOrigin.y + visibleSize.height - 50));
addChild(_TTFShowEditReturn);

// Back Menu
auto itemBack = MenuItemFont::create("Back",CC_CALLBACK_1(TestEditBox2::toExtensionsMainLayer,this));
itemBack->setPosition(Vec2(visibleOrigin.x + visibleSize.width - 50,visibleOrigin.y + 25));
auto menuBack = Menu::create(itemBack,NULL);
menuBack->setPosition(Vec2::ZERO);
addChild(menuBack);

auto editBoxSize = Size(visibleSize.width - 100,60);

// top
_editName = cocos2d::ui::EditBox::create(editBoxSize,cocos2d::ui::Scale9Sprite::create("green_edit.png"));
_editName->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2,visibleOrigin.y + visibleSize.height * 3 / 4));
_editName->setFontName("Paint Boy");
_editName->setFontSize(25);
_editName->setFontColor(Color3B::RED);
_editName->setPlaceHolder("Name:");
_editName->setPlaceholderFontColor(Color3B::WHITE);
_editName->setMaxLength(8);
_editName->setReturnType(cocos2d::ui::EditBox::KeyboardReturnType::DONE);
_editName->setDelegate(this);
addChild(_editName);

// middle
_editPassword = cocos2d::ui::EditBox::create(editBoxSize,cocos2d::ui::Scale9Sprite::create("green_edit.png"));
_editPassword->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2,visibleOrigin.y + visibleSize.height / 2));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
_editPassword->setFont("American Typewriter",30);
#else
_editPassword->setFont("American Typewriter",80);
_editPassword->setPlaceholderFont("American Typewriter",80);
#endif
_editPassword->setFontColor(Color3B::GREEN);
_editPassword->setPlaceHolder("Password:");
_editPassword->setMaxLength(6);
_editPassword->setInputFlag(cocos2d::ui::EditBox::InputFlag::PASSWORD);
_editPassword->setInputMode(cocos2d::ui::EditBox::InputMode::SINGLE_LINE);
_editPassword->setDelegate(this);
addChild(_editPassword);

// bottom
_editEmail = cocos2d::ui::EditBox::create(Size(editBoxSize.width,editBoxSize.height),cocos2d::ui::Scale9Sprite::create("green_edit.png"));
_editEmail->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2,visibleOrigin.y + visibleSize.height / 4));
_editEmail->setAnchorPoint(Vec2(0.5,1.0f));
_editEmail->setPlaceHolder("Email:");
_editEmail->setInputMode(cocos2d::ui::EditBox::InputMode::EMAIL_ADDRESS);
_editEmail->setDelegate(this);
addChild(_editEmail);

this->setPosition(Vec2(10,20));
}

TestEditBox2::~TestEditBox2()
{

}

void TestEditBox2::toExtensionsMainLayer(cocos2d::Ref *sender)
{
// auto scene = new ExtensionsTestScene();
// scene->runThisTest();
// scene->release();
}

void TestEditBox2::editBoxEditingDidBegin(cocos2d::extension::EditBox* editBox)
{
log("editBox %p DidBegin !",editBox);
}

void TestEditBox2::editBoxEditingDidEnd(cocos2d::extension::EditBox* editBox)
{
log("editBox %p DidEnd !",editBox);
}

void TestEditBox2::editBoxTextChanged(cocos2d::extension::EditBox* editBox,const std::string& text)
{
log("editBox %p TextChanged,text: %s ",editBox,text.c_str());
}

void TestEditBox2::editBoxReturn(cocos2d::ui::EditBox* editBox)
{
log("editBox %p was returned !",editBox);

if (_editName == editBox)
{
_TTFShowEditReturn->setString("Name EditBox return !");
}
else if (_editPassword == editBox)
{
_TTFShowEditReturn->setString("Password EditBox return !");
}
else if (_editEmail == editBox)
{
_TTFShowEditReturn->setString("Email EditBox return !");
}
}


使用方式:

TestEditBox2 *layer = new TestEditBox2();
addChild(layer);

需要注意,避免产生二义性。

cocos2d::ui::

cocos2d::extension::


原文链接:https://www.f2er.com/cocos2dx/340286.html

猜你在找的Cocos2d-x相关文章