TextInputTest.h
TextInputTest.cpp
TextFieldTTF
初始化函数:
/** creates a TextFieldTTF from a fontname, alignment, dimension and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
/** creates a TextFieldTTF from a fontname and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
使用:
auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
"fonts/Thonburi.ttf",
36);
IMEDelegate // ----键盘操作
class CC_DLL IMEDelegate
{
public:
/**
* @js NA
* @lua NA
*/
virtual ~IMEDelegate();
/**
* @js NA
* @lua NA
*/
virtual bool attachWithIME(); //打开键盘
/**
* @js NA
* @lua NA
*/
virtual bool detachWithIME(); //关闭键盘
protected:
friend class IMEDispatcher;
/**
@brief Decide if the delegate instance is ready to receive an IME message.
Called by IMEDispatcher.
* @js NA
* @lua NA
*/
virtual bool canAttachWithIME() { return false; }
/**
@brief When the delegate detaches from the IME, this method is called by IMEDispatcher.
* @js NA
* @lua NA
*/
virtual void didAttachWithIME() {}
/**
@brief Decide if the delegate instance can stop receiving IME messages.
* @js NA
* @lua NA
*/
virtual bool canDetachWithIME() { return false; }
/**
@brief When the delegate detaches from the IME, this method is called by IMEDispatcher.
* @js NA
* @lua NA
*/
virtual void didDetachWithIME() {}
/**
@brief Called by IMEDispatcher when text input received from the IME.
* @js NA
* @lua NA
*/
virtual void insertText(const char * text, size_t len) {CC_UNUSED_PARAM(text);CC_UNUSED_PARAM(len);}
/**
@brief Called by IMEDispatcher after the user clicks the backward key.
* @js NA
* @lua NA
*/
virtual void deleteBackward() {}
/**
@brief Called by IMEDispatcher for text stored in delegate.
* @js NA
* @lua NA
*/
virtual const std::string& getContentText() { return STD_STRING_EMPTY; }
//
// keyboard show/hide notification
//
/**
* @js NA
* @lua NA
*/
virtual void keyboardWillShow(IMEKeyboardNotificationInfo& info) {CC_UNUSED_PARAM(info);}
/**
* @js NA
* @lua NA
*/
virtual void keyboardDidShow(IMEKeyboardNotificationInfo& info) {CC_UNUSED_PARAM(info);}
/**
* @js NA
* @lua NA
*/
virtual void keyboardWillHide(IMEKeyboardNotificationInfo& info) {CC_UNUSED_PARAM(info);}
/**
* @js NA
* @lua NA
*/
virtual void keyboardDidHide(IMEKeyboardNotificationInfo& info) {CC_UNUSED_PARAM(info);}
protected:
/**
* @js NA
* @lua NA
*/
IMEDelegate();
};
TextFieldDelegate // 可以自己实现插入 删除 等的特效
声明是onVisit ,示例中用的是onDraw 难道是bug?
class CC_DLL TextFieldDelegate
{
public:
virtual ~TextFieldDelegate() {}
/**
@brief If the sender doesn't want to attach to the IME, return true;
*/
virtual bool onTextFieldAttachWithIME(TextFieldTTF * sender)<span style="font-family:Menlo;">//</span>当用户启动虚拟键盘时的回调函数
{
CC_UNUSED_PARAM(sender);
return false;
}
/**
@brief If the sender doesn't want to detach from the IME, return true;
*/
virtual bool onTextFieldDetachWithIME(TextFieldTTF * sender)<span style="font-family:Menlo;">//</span>当用户关闭虚拟键盘时的回调函数
{
CC_UNUSED_PARAM(sender);
return false;
}
/**
@brief If the sender doesn't want to insert the text, return true;
*/
virtual bool onTextFieldInsertText(TextFieldTTF * sender, const char * text, size_t nLen)<span style="font-family:Menlo;">//</span>当用户进行输入时的回调函数
{
CC_UNUSED_PARAM(sender);
CC_UNUSED_PARAM(text);
CC_UNUSED_PARAM(nLen);
return false;
}
/**
@brief If the sender doesn't want to delete the delText, return true;
*/
virtual bool onTextFieldDeleteBackward(TextFieldTTF * sender, const char * delText, size_t nLen)<span style="font-family:Menlo;color:#000000;"></span><span style="font-family:Menlo;">//</span>当用户删除文字时的回调函数
{
CC_UNUSED_PARAM(sender);
CC_UNUSED_PARAM(delText);
CC_UNUSED_PARAM(nLen);
return false;
}
/**
@brief If the sender doesn't want to draw, return true.
*/
virtual bool onVisit(TextFieldTTF * sender,Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
CC_UNUSED_PARAM(sender);
return false;
}
};<pre name="code" class="cpp">
点击后输入 的输入框实现
class KeyboardNotificationLayer : public Layer, public IMEDelegate
{
public:
KeyboardNotificationLayer(); //添加触摸事件
virtual std::string subtitle() const = 0;
virtual void onClickTrackNode(bool bClicked) = 0;
virtual void keyboardWillShow(IMEKeyboardNotificationInfo& info);//键盘弹出时被调用
//如果键盘覆盖输入框 则往上移动所有子节点
// Layer
bool onTouchBegan(Touch *touch, Event *event); //获取点击的坐标 _beginPos
void onTouchEnded(Touch *touch, Event *event); //获取松开的坐标,判断点击和松开的距离在允许范围内
//将松开坐标转换为节点坐标 ,调用onClickTrackNode(判断是否在 输入框上)
protected:
Node * _trackNode; //储存输入框
Vec2 _beginPos;
};
普通是实现 没用特效
class TextFieldTTFDefaultTest : public KeyboardNotificationLayer
{
public:
// KeyboardNotificationLayer
virtual std::string subtitle() const override;
virtual void onClickTrackNode(bool bClicked); //点中输入框 则弹出键盘 没有则隐藏输入框
// Layer
virtual void onEnter() override; //创建界面 并将输入框赋值给_trackNode
};
有特效的
_textField->setDelegate(this);
必须为TextFieldTTF 设置TextFieldDelegate 才能启用特效
class TextFieldTTFActionTest : public KeyboardNotificationLayer, public TextFieldDelegate
{
TextFieldTTF * _textField; // onEnter()中初始化的 输入框
Action * _textFieldAction; // onEnter()中初始化的 动作
bool _action; // onEnter()中复制为false
int _charLimit; // the textfield max char limit
public:
void callbackRemoveNodeWhenDidAction(Node * node);
// KeyboardNotificationLayer
virtual std::string subtitle() const override;
virtual void onClickTrackNode(bool bClicked);//点中输入框 则弹出键盘 没有则隐藏输入框
// Layer
virtual void onEnter() override;
virtual void onExit() override;
// TextFieldDelegate 用_textField->setDelegate(this); ,是由TextFieldTTF 调用的。
virtual bool onTextFieldAttachWithIME(TextFieldTTF * sender);//启动虚拟键盘时执行特效
virtual bool onTextFieldDetachWithIME(TextFieldTTF * sender);//关闭虚拟键盘时执行特效
virtual bool onTextFieldInsertText(TextFieldTTF * sender, const char * text, size_t nLen) override; //进行输入时执行特效,并不插入 只是执行特效<pre name="code" class="cpp"> //text 插入的内容 nLen 插入的长度
//返回 false: TextFieldTTF 将继续插入 true:不进行插入
virtual bool onTextFieldDeleteBackward(TextFieldTTF * sender, const char * delText, size_t nLen) override; //删除文字时执行特效
virtual bool onDraw(TextFieldTTF * sender); //return false;};