UITextField.h
16.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/****************************************************************************
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 __UITEXTFIELD_H__
#define __UITEXTFIELD_H__
#include "ui/UIWidget.h"
#include "2d/CCTextFieldTTF.h"
#include "ui/GUIExport.h"
NS_CC_BEGIN
/**
* @addtogroup ui
* @{
*/
namespace ui {
/**
* @brief A helper class which inherit from @see `TextFieldTTF` and implements the @see `TextFieldDelegate` protocol.
* It is mainly be used internally by @see `UITextField` class.
* @js NA
* @lua NA
*/
class CC_GUI_DLL UICCTextField: public TextFieldTTF, public TextFieldDelegate
{
public:
/**
* @brief Create an empty UICCTextField.
*
* @return A UICCTextField instance.
*/
static UICCTextField* create();
/**
* Default constructor
*/
UICCTextField();
/**
* Default destructor
*/
~UICCTextField();
virtual void onEnter() override;
/**
* Create a UICCTextField instance with a placeholder, a fontName and a fontSize.
*@param placeholder Placeholder in string.
*@param fontName Font name in string.
*@param fontSize Font size in float.
*@return A UICCTextField instance.
*/
static UICCTextField* create(const std::string& placeholder,
const std::string& fontName,
float fontSize);
//override functions
virtual bool onTextFieldAttachWithIME(TextFieldTTF *pSender) override;
virtual bool onTextFieldDetachWithIME(TextFieldTTF * pSender) override;
virtual bool onTextFieldInsertText(TextFieldTTF * pSender,
const char * text,
size_t nLen) override;
virtual bool onTextFieldDeleteBackward(TextFieldTTF * pSender,
const char * delText,
size_t nLen) override;
void insertText(const char* text, size_t len) override;
/**
* Open up the IME.
*/
void openIME();
/**
* Close the IME.
*/
void closeIME();
/**
* Toggle enable max length limitation.
*@param enable True to enable max length, false otherwise.
*/
void setMaxLengthEnabled(bool enable);
/**
* Query max length enable state.
*@return Whether max length is enabled or not.
*/
bool isMaxLengthEnabled()const;
/**
* Set maximize length.
*@param length The maximize length in integer.
*/
void setMaxLength(int length);
/**
* Get maximize length.
*@return Maximize length.
*/
int getMaxLength()const;
/**
* Return the total inputed characters.
*@return Total inputed character count.
*/
std::size_t getCharCount()const;
/**
* @brief Toggle password input mode.
*
* @param enable True if enable password input, false otherwise.
*/
void setPasswordEnabled(bool enable);
/**
* @brief Query whether password input mode is enabled or not.
*
* @return True if password input is enabled, false otherwise.
*/
bool isPasswordEnabled()const;
/**
* @brief Change password style text.
*
* @param styleText The styleText for password mask, the default value is "*".
*/
void setPasswordStyleText(const std::string& styleText);
/**
* @brief Set the password text content.
*
* @param text The content of password.
*/
void setPasswordText(const std::string& text);
/**
* @brief Toggle attach with IME.
*
* @param attach True if attach with IME, false otherwise.
*/
void setAttachWithIME(bool attach);
/**
* @brief Query whether the IME is attached or not.
*
* @return True if IME is attached, false otherwise.
*/
bool getAttachWithIME()const;
/**
* @brief Toggle detach with IME.
*
* @param detach True if detach with IME, false otherwise.
*/
void setDetachWithIME(bool detach);
/**
* @brief Query whether IME is detached or not.
*
* @return True if IME is detached, false otherwise.
*/
bool getDetachWithIME()const;
/**
* @brief Toggle enable text insert.
*
* @param insert True if enable insert text, false otherwise.
*/
void setInsertText(bool insert);
/**
* @brief Query whether insert text is enabled or not.
*
* @return True if insert text is enabled, false otherwise.
*/
bool getInsertText()const;
/**
* @brief Toggle enable delete backward.
*
* @param deleteBackward True if enable delete backward, false otherwise.
*/
void setDeleteBackward(bool deleteBackward);
/**
* @brief Query whether delete backward is enabled or not.
*
* @return True if delete backward is enabled, false otherwise.
*/
bool getDeleteBackward()const;
protected:
bool _maxLengthEnabled;
int _maxLength;
bool _attachWithIME;
bool _detachWithIME;
bool _insertText;
bool _deleteBackward;
};
/**
* @brief A widget which allows users to input text.
* The rendering of the input text are based on @see `TextFieldTTF'.
* If you want to use system control behavior, please use @see `EditBox` instead.
* @js NA
* @lua NA
*/
class CC_GUI_DLL TextField : public Widget
{
DECLARE_CLASS_GUI_INFO
public:
/**
* TextField event type.
*/
enum class EventType
{
ATTACH_WITH_IME,
DETACH_WITH_IME,
INSERT_TEXT,
DELETE_BACKWARD,
};
/**
* A callback which would be called when a TextField event happens.
*/
typedef std::function<void(Ref*, EventType)> ccTextFieldCallback;
/**
* @brief Default constructor.
*
*/
TextField();
/**
* @brief Default destructor.
*/
virtual ~TextField();
/**
* @brief Create an empty TextField.
*
* @return A TextField instance.
*/
static TextField* create();
/**
* @brief Create a TextField with a placeholder, a font name and a font size.
*
* @param placeholder The placeholder string.
* @param fontName The font name.
* @param fontSize The font size.
* @return A TextField instance.
*/
static TextField* create(const std::string& placeholder,
const std::string& fontName,
int fontSize);
/**
* @brief Set the touch size
* The touch size is used for @see `hitTest`.
* @param size A delimitation zone.
*/
void setTouchSize(const Size &size);
/**
* @brief Get current touch size of TextField.
*
* @return The TextField's touch size.
*/
Size getTouchSize()const;
/**
* @brief Toggle enable touch area.
*
* @param enable True if enable touch area, false otherwise.
*/
void setTouchAreaEnabled(bool enable);
virtual bool hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const override;
/**
* @brief Set placeholder of TextField.
*
* @param value The string value of placeholder.
*/
void setPlaceHolder(const std::string& value);
/**
* @brief Get the placeholder of TextField.
*
* @return A placeholder string.
*/
const std::string& getPlaceHolder()const;
/**
* @brief Query the placeholder string color.
*
* @return The color of placeholder.
*/
const Color4B& getPlaceHolderColor()const;
/**
* @brief Change the placeholder color.
*
* @param color A color value in `Color3B`.
*/
void setPlaceHolderColor(const Color3B& color);
/**
* @brief Change the placeholder color.
*
* @param color A color value in `Color4B`.
*/
void setPlaceHolderColor(const Color4B& color);
/**
* @brief Query the text string color.
*
* @return The color of the text.
*/
const Color4B& getTextColor()const;
/**
* @brief Change the text color.
*
* @param textColor The color value in `Color4B`.
*/
void setTextColor(const Color4B& textColor);
/**
* @brief Change font size of TextField.
*
* @param size The integer font size.
*/
void setFontSize(int size);
/**
* @brief Query the font size.
*
* @return The integer font size.
*/
int getFontSize()const;
/**
* @brief Change the font name of TextField.
*
* @param name The font name string.
*/
void setFontName(const std::string& name);
/**
* @brief Query the TextField's font name.
*
* @return The font name string.
*/
const std::string& getFontName()const;
/**
* @brief Detach the IME.
*/
virtual void didNotSelectSelf();
/**
*Change content of TextField.
*@param text A string content.
*/
void setString(const std::string& text);
/**
*Query the content of TextField.
*@return The string value of TextField.
*/
const std::string& getString()const;
virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override;
/**
* @brief Toggle maximize length enable
*
* @param enable True if enable maximize length, false otherwise.
*/
void setMaxLengthEnabled(bool enable);
/**
* @brief Query whether max length is enabled or not.
*
* @return True if maximize length is enabled, false otherwise.
*/
bool isMaxLengthEnabled()const;
/**
* @brief Change maximize input length limitation.
*
* @param length A character count in integer.
*/
void setMaxLength(int length);
/**
* @brief Query maximize input length of TextField.
*
* @return The integer value of maximize input length.
*/
int getMaxLength()const;
/**
* @brief Query the input string length.
*
* @return A integer length value.
*/
int getStringLength() const;
/**
* @brief Toggle enable password input mode.
*
* @param enable True if enable password input mode, false otherwise.
*/
void setPasswordEnabled(bool enable);
/**
* @brief Query whether password is enabled or not.
*
* @return True if password is enabled, false otherwise.
*/
bool isPasswordEnabled()const;
/**
* @brief Change password style text.
*
* @param styleText The styleText for password mask, the default value is "*".
*/
void setPasswordStyleText(const char* styleText);
/**
* @brief Query the password style text.
*
* @return A password style text.
*/
const char* getPasswordStyleText()const;
virtual void update(float dt) override;
/**
* @brief Query whether the IME is attached or not.
*
* @return True if IME is attached, false otherwise.
*/
bool getAttachWithIME()const;
/**
* @brief Toggle attach with IME.
*
* @param attach True if attach with IME, false otherwise.
*/
void setAttachWithIME(bool attach);
/**
* @brief Query whether IME is detached or not.
*
* @return True if IME is detached, false otherwise.
*/
bool getDetachWithIME()const;
/**
* @brief Toggle detach with IME.
*
* @param detach True if detach with IME, false otherwise.
*/
void setDetachWithIME(bool detach);
/**
* @brief Whether it is ready to get the inserted text or not.
*
* @return True if the insert text is ready, false otherwise.
*/
bool getInsertText()const;
/**
* @brief Toggle enable insert text mode
*
* @param insertText True if enable insert text, false otherwise.
*/
void setInsertText(bool insertText);
/**
* @brief Whether it is ready to delete backward in TextField.
*
* @return True is the delete backward is enabled, false otherwise.
*/
bool getDeleteBackward()const;
/**
* @brief Toggle enable delete backward mode.
*
* @param deleteBackward True is delete backward is enabled, false otherwise.
*/
void setDeleteBackward(bool deleteBackward);
/**
* Add a event listener to TextField, when some predefined event happens, the callback will be called.
*@param callback A callback function with type of `ccTextFieldCallback`.
*/
void addEventListener(const ccTextFieldCallback& callback);
/**
* Returns the "class name" of widget.
*/
virtual std::string getDescription() const override;
/**
* @brief Get the renderer size in auto mode.
*
* @return A delimitation zone.
*/
virtual Size getAutoRenderSize();
//override functions.
virtual Size getVirtualRendererSize() const override;
virtual Node* getVirtualRenderer() override;
virtual void onEnter() override;
/**
* @brief Attach the IME for inputing.
*
*/
void attachWithIME();
/**
* @brief Change the text area size.
*
* @param size A delimitation zone.
*/
void setTextAreaSize(const Size &size);
/**
* @brief Change horizontal text alignment.
*
* @param alignment A alignment arguments in @see `TextHAlignment`.
*/
void setTextHorizontalAlignment(TextHAlignment alignment);
/**
* @brief Inquire the horizontal alignment
*
* @return The horizontal alignment
*/
TextHAlignment getTextHorizontalAlignment() const;
/**
* @brief Change the vertical text alignment.
*
* @param alignment A alignment arguments in @see `TextVAlignment`.
*/
void setTextVerticalAlignment(TextVAlignment alignment);
/**
* @brief Inquire the horizontal alignment
*
* @return The horizontal alignment
*/
TextVAlignment getTextVerticalAlignment() const;
/**
* 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);
CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
protected:
virtual void initRenderer() override;
void attachWithIMEEvent();
void detachWithIMEEvent();
void insertTextEvent();
void deleteBackwardEvent();
virtual void onSizeChanged() override;
void textfieldRendererScaleChangedWithSize();
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void adaptRenderers() override;
protected:
UICCTextField* _textFieldRenderer;
float _touchWidth;
float _touchHeight;
bool _useTouchArea;
Ref* _textFieldEventListener;
ccTextFieldCallback _eventCallback;
bool _textFieldRendererAdaptDirty;
private:
enum class FontType
{
SYSTEM,
TTF,
BMFONT
};
std::string _fontName;
int _fontSize;
FontType _fontType;
};
}
// end of ui group
/// @}
NS_CC_END
#endif /* defined(__TextField__) */