CCTextFieldTTF.h
8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_TEXT_FIELD_H__
#define __CC_TEXT_FIELD_H__
#include "2d/CCLabel.h"
#include "base/CCIMEDelegate.h"
/**
* @addtogroup ui
* @{
*/
NS_CC_BEGIN
class TextFieldTTF;
/**
* A input protocol for TextField.
*/
class CC_DLL TextFieldDelegate
{
public:
/**
* Destructor for TextFieldDelegate.
* @js NA
*/
virtual ~TextFieldDelegate() {}
/**
*@brief If the sender doesn't want to attach to the IME, return true.
*/
virtual bool onTextFieldAttachWithIME(TextFieldTTF* sender);
/**
*@brief If the sender doesn't want to detach from the IME, return true.
*/
virtual bool onTextFieldDetachWithIME(TextFieldTTF* sender);
/**
*@brief If the sender doesn't want to insert the text, return true.
*/
virtual bool onTextFieldInsertText(TextFieldTTF* sender, const char* text, size_t nLen);
/**
*@brief If the sender doesn't want to delete the delText, return true.
*/
virtual bool onTextFieldDeleteBackward(TextFieldTTF* sender, const char* delText, size_t nLen);
/**
*@brief If the sender doesn't want to draw, return true.
* @js NA
*/
virtual bool onVisit(TextFieldTTF* sender, Renderer* renderer, const Mat4& transform, uint32_t flags);
};
/**
*@brief A simple text input field with TTF font.
*/
class CC_DLL TextFieldTTF : public Label, public IMEDelegate
{
public:
/**
* Default constructor.
* @js ctor
*/
TextFieldTTF();
/**
* Default destructor.
* @js NA
* @lua NA
*/
virtual ~TextFieldTTF();
/** Creates a TextFieldTTF from a fontname, alignment, dimension and font size.
* @js NA
*/
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.
* @js NA
*/
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
/** Initializes the TextFieldTTF with a font name, alignment, dimension and font size. */
bool initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
/** Initializes the TextFieldTTF with a font name and font size. */
bool initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
/**
*@brief Open keyboard and receive input text.
*/
virtual bool attachWithIME() override;
/**
*@brief End text input and close keyboard.
*/
virtual bool detachWithIME() override;
//////////////////////////////////////////////////////////////////////////
// properties
//////////////////////////////////////////////////////////////////////////
/**
* @lua NA
*/
TextFieldDelegate* getDelegate() const { return _delegate; }
/**
* @lua NA
*/
void setDelegate(TextFieldDelegate* delegate) { _delegate = delegate; }
/**
* Query the currently inputed character count.
*@return The total input character count.
*/
std::size_t getCharCount() const { return _charCount; }
/**
* Query the color of place holder.
*@return The place holder color.
*/
virtual const Color4B& getColorSpaceHolder();
/**
*@brief Change input placeholder color.
*@param color A color value in `Color3B`.
*/
virtual void setColorSpaceHolder(const Color3B& color);
/**
* Change the placeholder color.
*@param color The placeholder color in Color4B.
*/
virtual void setColorSpaceHolder(const Color4B& color);
/**
* Change the color of input text.
*@param textColor The text color in Color4B.
*/
virtual void setTextColor(const Color4B& textColor) override;
/**
* Change input text of TextField.
*@param text The input text of TextField.
*/
virtual void setString(const std::string& text) override;
/**
* Append to input text of TextField.
*@param text The append text of TextField.
*/
virtual void appendString(const std::string& text);
/**
* Query the input text of TextField.
*@return Get the input text of TextField.
*/
virtual const std::string& getString() const override;
/**
* Change placeholder text.
* place holder text displayed when there is no text in the text field.
*@param text The placeholder string.
*/
virtual void setPlaceHolder(const std::string& text);
/**
* Query the placeholder string.
*@return The placeholder string.
*/
virtual const std::string& getPlaceHolder() const;
/**
* Set enable secure text entry representation.
* If you want to display password in TextField, this option is very helpful.
*@param value Whether or not to display text with secure text entry.
* @js NA
*/
virtual void setSecureTextEntry(bool value);
virtual void setPasswordTextStyle(const std::string& text);
const std::string& getPasswordTextStyle() const;
/**
* Query whether the currently display mode is secure text entry or not.
*@return Whether current text is displayed as secure text entry.
* @js NA
*/
virtual bool isSecureTextEntry()const;
virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
virtual void update(float delta) override;
/**
* Set enable cursor use.
* @js NA
*/
void setCursorEnabled(bool enabled);
/**
* Set char showing cursor.
* @js NA
*/
void setCursorChar(char cursor);
/**
* Set cursor position, if enabled
* @js NA
*/
void setCursorPosition(std::size_t cursorPosition);
/**
* Set cursor position to hit letter, if enabled
* @js NA
*/
void setCursorFromPoint(const Vec2 &point, const Camera* camera);
protected:
//////////////////////////////////////////////////////////////////////////
// IMEDelegate interface
//////////////////////////////////////////////////////////////////////////
virtual bool canAttachWithIME() override;
virtual bool canDetachWithIME() override;
virtual void didAttachWithIME() override;
virtual void didDetachWithIME() override;
virtual void insertText(const char * text, size_t len) override;
virtual void deleteBackward() override;
virtual const std::string& getContentText() override;
virtual void controlKey(EventKeyboard::KeyCode keyCode) override;
TextFieldDelegate * _delegate;
std::size_t _charCount;
std::string _inputText;
std::string _placeHolder;
Color4B _colorSpaceHolder;
Color4B _colorText;
bool _secureTextEntry;
std::string _passwordStyleText;
// Need use cursor
bool _cursorEnabled;
// Current position cursor
std::size_t _cursorPosition;
// Char showing cursor
char _cursorChar;
// >0 - show, <0 - hide
float _cursorShowingTime;
bool _isAttachWithIME;
void makeStringSupportCursor(std::string& displayText);
void updateCursorDisplayText();
void setAttachWithIME(bool isAttachWithIME);
void setTextColorInternally(const Color4B& color);
private:
class LengthStack;
LengthStack * _lens;
};
NS_CC_END
// end of ui group
/// @}
#endif // __CC_TEXT_FIELD_H__