CUGL
Cornell University Game Library
CUTextInput.h
1 //
2 // CUTextInput.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class is an object-oriented interface to the SDL text input system.
6 // We have tried to keep this class as minimal as possible to make it as
7 // flexible as possible.
8 //
9 // This class is a singleton and should never be allocated directly. It
10 // should only be accessed via the Input dispatcher.
11 //
12 // CUGL zlib License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 7/8/16
33 
34 #ifndef __CU_TEXT_INPUT_H__
35 #define __CU_TEXT_INPUT_H__
36 
37 #include "CUInput.h"
38 #include "../util/CUTimestamp.h"
39 #include <functional>
40 
41 namespace cugl {
42 
43 #pragma mark TextInputEvent
44 
52 public:
56  std::string buffer;
58  std::string added;
59 
64 
72  TextInputEvent(const std::string& text, const std::string& suffix, const Timestamp& stamp) {
73  buffer = text; added = suffix; timestamp = stamp;
74  }
75 };
76 
77 
78 #pragma mark -
79 #pragma mark TextInputListener
80 
105 typedef std::function<void(const TextInputEvent& event, bool focus)> TextInputListener;
106 
107 #pragma mark TextInputValidator
108 
123 typedef std::function<bool(const std::string& value)> TextInputValidator;
124 
125 #pragma mark -
126 
157 class TextInput : public InputDevice {
158 #pragma mark Values
159 protected:
161  std::string _buffer;
162 
164  bool _active;
166  bool _updated;
167 
169  TextInputValidator _validator;
171  std::unordered_map<Uint32, TextInputListener> _listeners;
172 
173 #pragma mark -
174 #pragma mark Constructor
175 
181  TextInput();
182 
186  virtual ~TextInput() {}
187 
193  virtual void dispose() override;
194 
195 public:
196 #pragma mark -
197 #pragma mark Activation
198 
208  void begin();
209 
216  void end();
217 
226  bool isActive() const { return _active; }
227 
228 #pragma mark -
229 #pragma mark Data Access
230 
237  const std::string& getBuffer() const { return _buffer; }
238 
247  bool didUpdate() const { return _updated; }
248 
249 #pragma mark -
250 #pragma mark Listeners
251 
261  virtual bool requestFocus(Uint32 key) override;
262 
271  void setValidator(TextInputValidator validator);
272 
281  const TextInputValidator getValidator() const { return _validator; }
282 
290  bool isListener(Uint32 key) const;
291 
301  const TextInputListener getListener(Uint32 key) const;
302 
314  bool addListener(Uint32 key, TextInputListener listener);
315 
326  bool removeListener(Uint32 key);
327 
328 protected:
338  void validate(const std::string& value, const Timestamp& stamp);
339 
340 
341 #pragma mark -
342 #pragma mark Input Device
343 
349  virtual void clearState() override { _updated = false; }
350 
362  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
363 
374  virtual void queryEvents(std::vector<Uint32>& eventset) override;
375 
376  // Apparently friends are not inherited
377  friend class Input;
378 };
379 
380 }
381 
382 #endif /* __CU_TEXT_INPUT_H__ */
bool _updated
Definition: CUTextInput.h:166
void validate(const std::string &value, const Timestamp &stamp)
std::function< bool(const std::string &value)> TextInputValidator
Definition: CUTextInput.h:123
bool didUpdate() const
Definition: CUTextInput.h:247
std::unordered_map< Uint32, TextInputListener > _listeners
Definition: CUTextInput.h:171
Definition: CUInput.h:77
bool isListener(Uint32 key) const
Definition: CUTimestamp.h:61
const TextInputListener getListener(Uint32 key) const
virtual ~TextInput()
Definition: CUTextInput.h:186
const TextInputValidator getValidator() const
Definition: CUTextInput.h:281
bool removeListener(Uint32 key)
virtual bool requestFocus(Uint32 key) override
Timestamp timestamp
Definition: CUTextInput.h:54
std::string _buffer
Definition: CUTextInput.h:161
TextInputEvent(const std::string &text, const std::string &suffix, const Timestamp &stamp)
Definition: CUTextInput.h:72
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
std::string buffer
Definition: CUTextInput.h:56
Definition: CUTextInput.h:157
virtual void queryEvents(std::vector< Uint32 > &eventset) override
std::string added
Definition: CUTextInput.h:58
Definition: CUTextInput.h:51
const std::string & getBuffer() const
Definition: CUTextInput.h:237
bool addListener(Uint32 key, TextInputListener listener)
bool isActive() const
Definition: CUTextInput.h:226
bool _active
Definition: CUTextInput.h:164
TextInputValidator _validator
Definition: CUTextInput.h:169
virtual void clearState() override
Definition: CUTextInput.h:349
void setValidator(TextInputValidator validator)
std::function< void(const TextInputEvent &event, bool focus)> TextInputListener
Definition: CUTextInput.h:105
Definition: CUAnimationNode.h:52
virtual void dispose() override
Definition: CUInput.h:298
TextInputEvent()
Definition: CUTextInput.h:63