cocos 2 dx開発のTextFieldテキストボックスを用いた登録ページの初歩的な開発(コード内注釈詳細)
3101 ワード
#pragma once
#include "cocos2d.h"
using namespace cocos2d;
#include "ui/CocosGUI.h"
using namespace ui;
class TextFieldDemo :public Scene
{
public:
virtual bool init();
CREATE_FUNC(TextFieldDemo);
static Scene* createScene();
char* FontToUTF8(const char* font);
private:
TextField* id; //id
TextField* key; //keyword
Button* go; //
};
以下はcppファイルです
#include "TextFieldDemo.h"
#include "loding.h"
Scene* TextFieldDemo::createScene()
{
return TextFieldDemo::create();
}
bool TextFieldDemo::init()
{
if (!Scene::init())
{
return false;
}
//
go = Button::create("112.png", "113.png", "114.png");
this->addChild(go);
//
go->setEnabled(false);
//
auto put_in = Sprite::create("denglu.png");
put_in->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
put_in->setPosition(Director::getInstance()->getVisibleSize()/2);
this->addChild(put_in);
//
id = TextField::create();
//
id->setFontName("arial");
//
id->setFontSize(36);
//
auto _id = String::create(TextFieldDemo::FontToUTF8(" "));
id->setPlaceHolder(_id->getCString());
//
id->setColor(Color3B::RED);
this->addChild(id);
//
id->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
id->setPosition(put_in->getPosition()+Vec2(30,-50));
//
key = TextField::create();
key->setFontName("arial");
key->setFontSize(36);
auto _key = String::create(TextFieldDemo::FontToUTF8(" "));
key->setPlaceHolder(_key->getCString());
key->setColor(Color3B::RED);
this->addChild(key);
key->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
key->setPosition(put_in->getPosition() + Vec2(30, -160));
//
key->addEventListener([&](Ref* ref, ui::TextField::EventType type)
{
//
auto temp = dynamic_cast(ref);
// 8 go
if (temp->getStringLength() > 8 && id->getStringLength() > 8)
{
go->setEnabled(true);
}
else //
{
go->setEnabled(false);
}
});
//
go->addTouchEventListener([&](Ref* ref, ui::Button::TouchEventType type)
{
if (type == Button::TouchEventType::ENDED)
{
Director::getInstance()->replaceScene(loding::createScene());
}
});
go->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
go->setPosition(key->getPosition() + Vec2(0, -100));
return true;
}
// cocos
char* TextFieldDemo::FontToUTF8(const char* font) {
int len = MultiByteToWideChar(CP_ACP, 0, font, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, font, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr)delete[] wstr;
return str;
}