CUGL 1.2
Cornell University Game Library
CUTextField.h
1 //
2 // CUTextField.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a single line text field. It is useful
6 // for providing input forms for your application, such as saved games or
7 // player settings. Because it is only a single line, it is a subclass of
8 // label. A multiline text input would be a TextArea, and that is not
9 // currently supported.
10 //
11 // To make use of a TextField, BOTH Keyboard and TextInput input devices
12 // must be activated. In particular, TextInput allows the support of
13 // virtual keyboards on mobile devices.
14 //
15 // This class uses our standard shared-pointer architecture.
16 //
17 // 1. The constructor does not perform any initialization; it just sets all
18 // attributes to their defaults.
19 //
20 // 2. All initialization takes place via init methods, which can fail if an
21 // object is initialized more than once.
22 //
23 // 3. All allocation takes place via static constructors which return a shared
24 // pointer.
25 //
26 // CUGL MIT License:
27 // This software is provided 'as-is', without any express or implied
28 // warranty. In no event will the authors be held liable for any damages
29 // arising from the use of this software.
30 //
31 // Permission is granted to anyone to use this software for any purpose,
32 // including commercial applications, and to alter it and redistribute it
33 // freely, subject to the following restrictions:
34 //
35 // 1. The origin of this software must not be misrepresented; you must not
36 // claim that you wrote the original software. If you use this software
37 // in a product, an acknowledgment in the product documentation would be
38 // appreciated but is not required.
39 //
40 // 2. Altered source versions must be plainly marked as such, and must not
41 // be misrepresented as being the original software.
42 //
43 // 3. This notice may not be removed or altered from any source distribution.
44 //
45 // Author: Walker White and Enze Zhou
46 // Version: 1/8/18
47 //
48 #ifndef __CU_TEXT_FIELD_H__
49 #define __CU_TEXT_FIELD_H__
50 
51 #include <cugl/2d/CULabel.h>
52 #include <cugl/input/CUTextInput.h>
53 #include <cugl/input/CUKeyboard.h>
54 #include <string>
55 
56 namespace cugl {
57 
58 #pragma mark -
59 #pragma mark TextField
60 
81 class TextField : public Label {
82 public:
83 #pragma mark TextFieldListener
84 
103  typedef std::function<void (const std::string& name, const std::string& current)> Listener;
104 
105 protected:
106 #pragma mark -
107 #pragma mark Values
108 
115  float _textLength;
116 
117 
119  bool _active;
121  bool _focused;
123  bool _mouse;
125  Uint32 _inputKey;
126 
128  Listener _typeListener;
130  Listener _exitListener;
131 
133  bool _altDown;
135  bool _metaDown;
137  bool _backDown;
139  Uint32 _backCount;
140 
141 
142 public:
143 #pragma mark -
144 #pragma mark Constructors
145 
153  TextField();
154 
162 
172  virtual void dispose() override;
173 
174 
175 #pragma mark -
176 #pragma mark Static Constructors
177 
188  static std::shared_ptr<TextField> alloc(const Size& size, const std::shared_ptr<Font>& font) {
189  std::shared_ptr<TextField> result = std::make_shared<TextField>();
190  return (result->Label::init(size,font) ? result : nullptr);
191  }
192 
210  static std::shared_ptr<TextField> alloc(const std::string& text, const std::shared_ptr<Font>& font) {
211  std::shared_ptr<TextField> result = std::make_shared<TextField>();
212  return (result->initWithText(text,font) ? result : nullptr);
213  }
214 
232  static std::shared_ptr<TextField> alloc(const char* text, const std::shared_ptr<Font>& font) {
233  std::shared_ptr<TextField> result = std::make_shared<TextField>();
234  return (result->initWithText(text,font) ? result : nullptr);
235  }
236 
262  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
263  const std::shared_ptr<JsonValue> data) {
264  std::shared_ptr<TextField> node = std::make_shared<TextField>();
265  if (!node->initWithData(loader,data)) { node = nullptr; }
266  return std::dynamic_pointer_cast<Node>(node);
267  }
268 
269 
270 #pragma mark -
271 #pragma mark Listener
272 
281  bool hasTypeListener() const { return _typeListener != nullptr; }
282 
293  const Listener getTypeListener() const { return _typeListener; }
294 
305  void setTypeListener(Listener listener) { _typeListener = listener; }
306 
317  bool removeTypeListener();
318 
328  bool hasExitListener() const { return _exitListener != nullptr; }
329 
340  const Listener getExitListener() const { return _exitListener; }
341 
352  void setExitListener(Listener listener) { _exitListener = listener; }
353 
364  bool removeExitListener();
365 
366 
367 #pragma mark -
368 #pragma mark Editing
369 
388  virtual void setText(const std::string& text, bool resize=false) override;
389 
412  bool activate(Uint32 key);
413 
430  bool deactivate(bool dispose=false);
431 
437  bool isActive() const { return _active; }
438 
451  bool requestFocus();
452 
465  bool releaseFocus();
466 
472  bool hasFocus() const { return _focused; }
473 
474 
475 protected:
476 #pragma mark -
477 #pragma mark Rendering
478 
497  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
498 
499 
500 #pragma mark -
501 #pragma mark Internal Helpers
502 
512  bool validate(const std::string& value);
513 
522  void updateInput(const TextInputEvent& event, bool focus);
523 
533  void updateKey(const KeyEvent& Event, bool focus, bool down);
534 
544  void updatePress(Vec2 pos, bool focus);
545 
552  void updateCursor();
553 
565  int skipWord(bool forward);
566 
573  void deleteOne();
574 
588  bool deleteMany(Uint32 counter);
589 
590 };
591 
592 }
593 
594 #endif /* __CU_TEXT_FIELD_H__ */
Definition: CUSize.h:57
bool _altDown
Definition: CUTextField.h:133
bool deactivate(bool dispose=false)
std::function< void(const std::string &name, const std::string &current)> Listener
Definition: CUTextField.h:103
Definition: CUVec2.h:61
Uint32 _backCount
Definition: CUTextField.h:139
void setTypeListener(Listener listener)
Definition: CUTextField.h:305
float _textLength
Definition: CUTextField.h:115
void updateKey(const KeyEvent &Event, bool focus, bool down)
Definition: CUNode.h:92
void updateInput(const TextInputEvent &event, bool focus)
const Listener getExitListener() const
Definition: CUTextField.h:340
Uint32 _inputKey
Definition: CUTextField.h:125
bool _active
Definition: CUTextField.h:119
bool _focused
Definition: CUTextField.h:121
bool hasTypeListener() const
Definition: CUTextField.h:281
static std::shared_ptr< TextField > alloc(const std::string &text, const std::shared_ptr< Font > &font)
Definition: CUTextField.h:210
bool hasFocus() const
Definition: CUTextField.h:472
bool validate(const std::string &value)
bool _backDown
Definition: CUTextField.h:137
bool activate(Uint32 key)
void updatePress(Vec2 pos, bool focus)
bool hasExitListener() const
Definition: CUTextField.h:328
Definition: CUTextInput.h:51
bool removeTypeListener()
Listener _typeListener
Definition: CUTextField.h:128
Listener _exitListener
Definition: CUTextField.h:130
bool deleteMany(Uint32 counter)
bool isActive() const
Definition: CUTextField.h:437
int _cursorBlink
Definition: CUTextField.h:111
Definition: CUTextField.h:81
Definition: CUKeyboard.h:289
Definition: CURect.h:45
bool _metaDown
Definition: CUTextField.h:135
Definition: CUSceneLoader.h:77
void setExitListener(Listener listener)
Definition: CUTextField.h:352
Rect _cursor
Definition: CUTextField.h:109
virtual void dispose() override
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUTextField.h:262
Definition: CUColor4.h:1084
const Listener getTypeListener() const
Definition: CUTextField.h:293
static std::shared_ptr< TextField > alloc(const char *text, const std::shared_ptr< Font > &font)
Definition: CUTextField.h:232
bool _mouse
Definition: CUTextField.h:123
Definition: CULabel.h:74
Definition: CUAction.h:51
Definition: CUMat4.h:83
int skipWord(bool forward)
~TextField()
Definition: CUTextField.h:161
int _cursorIndex
Definition: CUTextField.h:113
bool removeExitListener()
virtual void setText(const std::string &text, bool resize=false) override
static std::shared_ptr< TextField > alloc(const Size &size, const std::shared_ptr< Font > &font)
Definition: CUTextField.h:188