CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 
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 
429  bool deactivate(bool dispose=false);
430 
436  bool isActive() const { return _active; }
437 
450  bool requestFocus();
451 
464  bool releaseFocus();
465 
471  bool hasFocus() const { return _focused; }
472 
473 
474 protected:
475 #pragma mark -
476 #pragma mark Rendering
477 
496  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
497 
498 
499 #pragma mark -
500 #pragma mark Internal Helpers
501 
511  bool validate(const std::string& value);
512 
521  void updateInput(const TextInputEvent& event, bool focus);
522 
532  void updateKey(const KeyEvent& event, bool focus, bool down);
533 
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__ */
cugl::TextField::_cursor
Rect _cursor
Definition: CUTextField.h:109
cugl::TextField::alloc
static std::shared_ptr< TextField > alloc(const std::string &text, const std::shared_ptr< Font > &font)
Definition: CUTextField.h:210
cugl::TextField::requestFocus
bool requestFocus()
cugl::TextField::_active
bool _active
Definition: CUTextField.h:119
cugl::TextField::setExitListener
void setExitListener(Listener listener)
Definition: CUTextField.h:352
cugl::TextField::setText
virtual void setText(const std::string &text, bool resize=false) override
cugl::TextField::_backDown
bool _backDown
Definition: CUTextField.h:137
cugl::TextField::releaseFocus
bool releaseFocus()
cugl::TextField::TextField
TextField()
cugl::TextField::hasFocus
bool hasFocus() const
Definition: CUTextField.h:471
cugl::TextField
Definition: CUTextField.h:81
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::TextField::validate
bool validate(const std::string &value)
cugl::TextField::hasExitListener
bool hasExitListener() const
Definition: CUTextField.h:328
cugl::Label
Definition: CULabel.h:74
cugl::TextField::_inputKey
Uint32 _inputKey
Definition: CUTextField.h:125
cugl::TextField::_exitListener
Listener _exitListener
Definition: CUTextField.h:130
cugl::TextField::Listener
std::function< void(const std::string &name, const std::string &current)> Listener
Definition: CUTextField.h:103
cugl::TextField::activate
bool activate(Uint32 key)
cugl::TextField::skipWord
int skipWord(bool forward)
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::TextField::dispose
virtual void dispose() override
cugl::TextField::_textLength
float _textLength
Definition: CUTextField.h:115
cugl::KeyEvent
Definition: CUKeyboard.h:293
cugl::TextField::hasTypeListener
bool hasTypeListener() const
Definition: CUTextField.h:281
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::TextField::_cursorBlink
int _cursorBlink
Definition: CUTextField.h:111
cugl::TextField::setTypeListener
void setTypeListener(Listener listener)
Definition: CUTextField.h:305
cugl::TextField::updateCursor
void updateCursor()
cugl::TextField::alloc
static std::shared_ptr< TextField > alloc(const Size &size, const std::shared_ptr< Font > &font)
Definition: CUTextField.h:188
cugl::TextField::getExitListener
const Listener getExitListener() const
Definition: CUTextField.h:340
cugl::TextField::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
cugl::TextField::isActive
bool isActive() const
Definition: CUTextField.h:436
cugl::TextField::removeTypeListener
bool removeTypeListener()
cugl::TextField::deleteOne
void deleteOne()
cugl::TextField::_backCount
Uint32 _backCount
Definition: CUTextField.h:139
cugl::TextField::~TextField
~TextField()
Definition: CUTextField.h:161
cugl::TextField::_cursorIndex
int _cursorIndex
Definition: CUTextField.h:113
cugl::Vec2
Definition: CUVec2.h:61
cugl::TextField::_metaDown
bool _metaDown
Definition: CUTextField.h:135
cugl::TextField::_mouse
bool _mouse
Definition: CUTextField.h:123
cugl::TextField::deactivate
bool deactivate(bool dispose=false)
cugl::TextField::removeExitListener
bool removeExitListener()
cugl::TextField::_focused
bool _focused
Definition: CUTextField.h:121
cugl::TextField::updatePress
void updatePress(Vec2 pos, bool focus)
cugl::TextField::_altDown
bool _altDown
Definition: CUTextField.h:133
cugl::TextField::updateKey
void updateKey(const KeyEvent &event, bool focus, bool down)
cugl::TextInputEvent
Definition: CUTextInput.h:51
cugl::TextField::deleteMany
bool deleteMany(Uint32 counter)
cugl::TextField::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUTextField.h:262
cugl::TextField::updateInput
void updateInput(const TextInputEvent &event, bool focus)
cugl::TextField::alloc
static std::shared_ptr< TextField > alloc(const char *text, const std::shared_ptr< Font > &font)
Definition: CUTextField.h:232
cugl::TextField::getTypeListener
const Listener getTypeListener() const
Definition: CUTextField.h:293
cugl::TextField::_typeListener
Listener _typeListener
Definition: CUTextField.h:128