Cocos2d-x3.2 EditBoxの使用

5213 ワード

#include "cocos2d.h"

// 
#include "extensions/cocos-ext.h"
USING_NS_CC_EXT;

USING_NS_CC;

class GameScene : public cocos2d::Layer, public EditBoxDelegate
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    CREATE_FUNC(GameScene);
    
    void funCallback(Ref* pSender);
    
    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);
    void toExtensionsMainLayer(cocos2d::Ref *sender);
    
    
private:        // auto 
    Size size;
    Sprite *sprite;
    cocos2d::Label *_TTFShowEditReturn;
    cocos2d::extension::EditBox *_editName;
    cocos2d::extension::EditBox *_editPassword;
    cocos2d::extension::EditBox *_editEmail;
    bool _drag;
};
//  Created by Jacedy on 14-8-11.
//
//

#include "GameScene.h"

USING_NS_CC;

cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   // 
    auto layer = GameScene::create();   // 
    scene->addChild(layer);
    return scene;
}

// 
bool GameScene::init()
{
    if(!Layer::init())      // 
        return false;
    
    // 
    size = Director::getInstance()->getVisibleSize();
    //auto size = Director::getInstance()->getWinSize();
    
    // 
    sprite = Sprite::create("OnePiece_1.png");
    sprite->setPosition(Vec2(size.width/2, size.height/2));
    this->addChild(sprite);
    
    auto glview = Director::getInstance()->getOpenGLView();
    auto visibleOrigin = glview->getVisibleOrigin();
    CCLOG("%f", visibleOrigin.x);
    
    _TTFShowEditReturn = Label::createWithSystemFont("No edit Control return!", "Mark Felt", 30);
    _TTFShowEditReturn->setPosition(Point(visibleOrigin.x + size.width /2, visibleOrigin.y + size.height - 50));
    addChild(_TTFShowEditReturn);
    
    //Back Menu
    auto itemBack = MenuItemFont::create("Back", CC_CALLBACK_1(GameScene::toExtensionsMainLayer, this));
    itemBack->setPosition(Point(visibleOrigin.x + size.width - 50, visibleOrigin.y + 25));
    auto menuBack = Menu::create(itemBack, NULL);
    menuBack->setPosition(Point::ZERO);
    addChild(menuBack);
    
    auto editBoxSize = Size(size.width - 100, 60);
    
    _editName = EditBox::create(editBoxSize, Scale9Sprite::create("green_edit.png"));
    _editName->setPosition(Point(visibleOrigin.x + size.width /2, visibleOrigin.y + size.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(EditBox::KeyboardReturnType::DONE);
    _editName->setDelegate(this);
    addChild(_editName);
    
    _editPassword = EditBox::create(editBoxSize, Scale9Sprite::create("orange_edit.png"));
    _editPassword->setPosition(Point(visibleOrigin.x + size.width / 2, visibleOrigin.y + size.height / 2));
    _editPassword->setFont("American Typewriter", 30);
    _editPassword->setFontColor(Color3B::GREEN);
    _editPassword->setPlaceHolder("Password:");
    _editPassword->setMaxLength(6);
    _editPassword->setInputFlag(EditBox::InputFlag::PASSWORD);
    _editPassword->setInputMode(EditBox::InputMode::SINGLE_LINE);
    _editPassword->setDelegate(this);
    addChild(_editPassword);
    
    //bottom
    _editEmail = EditBox::create(Size(editBoxSize.width, editBoxSize.height), Scale9Sprite::create("yellow_edit.png"));
    _editEmail->setPosition(Point(visibleOrigin.x + size.width / 2, visibleOrigin.y + size.height / 4));
    _editEmail->setAnchorPoint(Point(0.5f, 1.0f));
    _editEmail->setPlaceHolder("Email:");
    _editEmail->setInputMode(EditBox::InputMode::EMAIL_ADDRESS);
    _editEmail->setDelegate(this);
    addChild(_editEmail);
    
    return true;
}

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

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

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

void GameScene::editBoxReturn(cocos2d::extension::EditBox *editBox)
{
    log("editBox %p was returned.", editBox);
    
    if(_editName == editBox)
    {
        _TTFShowEditReturn->setString("Name EditBox return.");
    }
    else if (_editEmail == editBox)
    {
        _TTFShowEditReturn->setString("Email EditBox return.");
    }
}

void GameScene::toExtensionsMainLayer(cocos2d::Ref *sender)
{
    
}

void GameScene::funCallback(Ref* pSender)
{
    
}