CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MIT 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 <cugl/input/CUInput.h>
38 #include <cugl/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 #pragma mark -
78 
109 class TextInput : public InputDevice {
110 public:
111 #pragma mark Listeners
112 
137  typedef std::function<void(const TextInputEvent& event, bool focus)> Listener;
138 
154  typedef std::function<bool(const std::string& value)> Validator;
155 
156 #pragma mark Values
157 protected:
159  std::string _buffer;
160 
162  bool _active;
164  bool _updated;
165 
167  Validator _validator;
169  std::unordered_map<Uint32, Listener> _listeners;
170 
171 #pragma mark -
172 #pragma mark Constructor
173 
179  TextInput();
180 
184  virtual ~TextInput() {}
185 
191  virtual void dispose() override;
192 
193 public:
194 #pragma mark -
195 #pragma mark Activation
196 
206  void begin();
207 
214  void end();
215 
224  bool isActive() const { return _active; }
225 
226 #pragma mark -
227 #pragma mark Data Access
228 
235  const std::string& getBuffer() const { return _buffer; }
236 
245  bool didUpdate() const { return _updated; }
246 
247 #pragma mark -
248 #pragma mark Listeners
249 
259  virtual bool requestFocus(Uint32 key) override;
260 
269  void setValidator(Validator validator);
270 
279  const Validator getValidator() const { return _validator; }
280 
288  bool isListener(Uint32 key) const;
289 
299  const Listener getListener(Uint32 key) const;
300 
312  bool addListener(Uint32 key, Listener listener);
313 
324  bool removeListener(Uint32 key);
325 
326 protected:
336  void validate(const std::string& value, const Timestamp& stamp);
337 
338 
339 #pragma mark -
340 #pragma mark Input Device
341 
347  virtual void clearState() override { _updated = false; }
348 
360  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
361 
372  virtual void queryEvents(std::vector<Uint32>& eventset) override;
373 
374  // Apparently friends are not inherited
375  friend class Input;
376 };
377 
378 }
379 
380 #endif /* __CU_TEXT_INPUT_H__ */
bool _updated
Definition: CUTextInput.h:164
void validate(const std::string &value, const Timestamp &stamp)
std::function< bool(const std::string &value)> Validator
Definition: CUTextInput.h:154
bool didUpdate() const
Definition: CUTextInput.h:245
Definition: CUInput.h:77
bool isListener(Uint32 key) const
Definition: CUTimestamp.h:61
Validator _validator
Definition: CUTextInput.h:167
virtual ~TextInput()
Definition: CUTextInput.h:184
bool removeListener(Uint32 key)
virtual bool requestFocus(Uint32 key) override
Timestamp timestamp
Definition: CUTextInput.h:54
std::string _buffer
Definition: CUTextInput.h:159
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:109
void setValidator(Validator validator)
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:235
std::unordered_map< Uint32, Listener > _listeners
Definition: CUTextInput.h:169
const Listener getListener(Uint32 key) const
bool isActive() const
Definition: CUTextInput.h:224
bool _active
Definition: CUTextInput.h:162
bool addListener(Uint32 key, Listener listener)
std::function< void(const TextInputEvent &event, bool focus)> Listener
Definition: CUTextInput.h:137
const Validator getValidator() const
Definition: CUTextInput.h:279
virtual void clearState() override
Definition: CUTextInput.h:347
Definition: CUAction.h:51
virtual void dispose() override
Definition: CUInput.h:298
TextInputEvent()
Definition: CUTextInput.h:63