CUGL 1.3
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 
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 
313  bool addListener(Uint32 key, Listener listener);
314 
325  bool removeListener(Uint32 key);
326 
327 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__ */
cugl::TextInput::_active
bool _active
Definition: CUTextInput.h:162
cugl::TextInput::begin
void begin()
cugl::TextInput::isActive
bool isActive() const
Definition: CUTextInput.h:224
cugl::TextInputEvent::added
std::string added
Definition: CUTextInput.h:58
cugl::TextInput::didUpdate
bool didUpdate() const
Definition: CUTextInput.h:245
cugl::TextInput::_buffer
std::string _buffer
Definition: CUTextInput.h:159
cugl::TextInput::_listeners
std::unordered_map< Uint32, Listener > _listeners
Definition: CUTextInput.h:169
cugl::TextInput::_validator
Validator _validator
Definition: CUTextInput.h:167
cugl::TextInput::validate
void validate(const std::string &value, const Timestamp &stamp)
cugl::TextInput::TextInput
TextInput()
cugl::Timestamp
Definition: CUTimestamp.h:61
cugl::TextInput::getBuffer
const std::string & getBuffer() const
Definition: CUTextInput.h:235
cugl::TextInput::updateState
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
cugl::TextInput::removeListener
bool removeListener(Uint32 key)
cugl::TextInput::clearState
virtual void clearState() override
Definition: CUTextInput.h:349
cugl::TextInputEvent::buffer
std::string buffer
Definition: CUTextInput.h:56
cugl::TextInput::addListener
bool addListener(Uint32 key, Listener listener)
cugl::TextInput::queryEvents
virtual void queryEvents(std::vector< Uint32 > &eventset) override
cugl::TextInput::dispose
virtual void dispose() override
cugl::TextInputEvent::timestamp
Timestamp timestamp
Definition: CUTextInput.h:54
cugl::TextInput::getValidator
const Validator getValidator() const
Definition: CUTextInput.h:279
cugl::TextInput::end
void end()
cugl::TextInputEvent::TextInputEvent
TextInputEvent()
Definition: CUTextInput.h:63
cugl::TextInputEvent::TextInputEvent
TextInputEvent(const std::string &text, const std::string &suffix, const Timestamp &stamp)
Definition: CUTextInput.h:72
cugl::TextInput::Validator
std::function< bool(const std::string &value)> Validator
Definition: CUTextInput.h:154
cugl::TextInput::setValidator
void setValidator(Validator validator)
cugl::TextInput::Listener
std::function< void(const TextInputEvent &event, bool focus)> Listener
Definition: CUTextInput.h:137
cugl::TextInput::requestFocus
virtual bool requestFocus(Uint32 key) override
cugl::Input
Definition: CUInput.h:77
cugl::TextInput::getListener
const Listener getListener(Uint32 key) const
cugl::TextInput::~TextInput
virtual ~TextInput()
Definition: CUTextInput.h:184
cugl::InputDevice
Definition: CUInput.h:298
cugl::TextInput::_updated
bool _updated
Definition: CUTextInput.h:164
cugl::TextInputEvent
Definition: CUTextInput.h:51
cugl::TextInput::isListener
bool isListener(Uint32 key) const
cugl::TextInput
Definition: CUTextInput.h:109