Dragonfly  4.3
A text-based game engine
TextEntry.h
1 ///
2 /// A basic TextEntry
3 ///
4 /// Invokes callback() when clicked.
5 /// Changes to highlight color on mouse over.
6 ///
7 
8 #ifndef __TEXTENTRY_H__
9 #define __TEXTENTRY_H__
10 
11 // Engine includes.
12 #include "EventMouse.h"
13 #include "ViewObject.h"
14 
15 namespace df {
16 
17 /// Count of number of text entry modified attribute categories.
18 const int TATTR_COUNT = 6;
19 
20 /// Categories of text entry attributes that indicate modification.
22  TEXT,
23  LIMIT,
24  CURSOR,
25  CURSOR_CHAR,
26  BLINK_RATE,
27  NUMBERS_ONLY,
28 };
29 
30 class TextEntry : public ViewObject {
31 
32  private:
33  std::string m_text; ///< Text entered.
34  int m_limit; ///< Character limit in text.
35  int m_cursor; ///< Cursor location.
36  char m_cursor_char; ///< Cursor character.
37  int m_blink_rate; ///< Cursor blink rate.
38  bool m_numbers_only; ///< True if only numbers.
39  bool m_tmodified[TATTR_COUNT]; ///< Modified attribute since serialize().
40 
41  public:
42  TextEntry();
43 
44  /// Set text entered.
45  void setText(std::string new_text);
46 
47  /// Get text entered.
48  std::string getText() const;
49 
50  /// Handle "keyboard" events.
51  /// Return 0 if ignored, else 1.
52  int eventHandler(const Event *p_e);
53 
54  /// Called when TextEntry enter hit.
55  /// Must be defined by derived class.
56  virtual void callback() = 0;
57 
58  /// Set limit of number of characters allowed.
59  void setLimit(int new_limit);
60 
61  /// Get limit of number of characters allowed.
62  int getLimit() const;
63 
64  /// Set cursor to location.
65  void setCursor(int new_cursor);
66 
67  /// Get cursor location.
68  int getCursor() const;
69 
70  /// Set blink rate for cursor (in ticks).
71  void setBlinkRate(int new_blink_rate);
72 
73  /// Get blink rate for cursor (in ticks).
74  int getBlinkRate() const;
75 
76  /// Return true if only numbers can be entered.
77  bool numbersOnly() const;
78 
79  /// Set to allow only numbers to be entered.
80  void setNumbersOnly(bool new_numbers_only = true);
81 
82  /// Set cursor character.
83  void setCursorChar(char new_cursor_char);
84 
85  /// Get cursor character.
86  char getCursorChar() const;
87 
88  /// Draw viewstring + text entered.
89  virtual void draw();
90 
91  /// Serialize Button attributes to single string.
92  /// Only modified attributes (unless all is true).
93  virtual std::string serialize(bool all = false);
94 
95  /// Deserialize string to become attributes.
96  /// Rerturn 0 if no errors, else -1.
97  virtual int deserialize(std::string s);
98 
99  /// Return true if attribute modified since last serialize.
100  virtual bool isModified(enum TextEntryAttribute attribute) const;
101 
102  /// Return true if any attribute modified since last serialize.
103  virtual bool isModified() const;
104 };
105 
106 } // end of namespace df
107 #endif
Definition: ViewObject.h:45
TextEntryAttribute
Categories of text entry attributes that indicate modification.
Definition: TextEntry.h:21
Definition: TextEntry.h:30
void setNumbersOnly(bool new_numbers_only=true)
Set to allow only numbers to be entered.
void setBlinkRate(int new_blink_rate)
Set blink rate for cursor (in ticks).
int getCursor() const
Get cursor location.
char m_cursor_char
Cursor character.
Definition: TextEntry.h:36
void setText(std::string new_text)
Set text entered.
Definition: Event.h:15
A 2-d bounding box.
Definition: Box.h:10
bool m_numbers_only
True if only numbers.
Definition: TextEntry.h:38
int m_blink_rate
Cursor blink rate.
Definition: TextEntry.h:37
int eventHandler(const Event *p_e)
Handle "keyboard" events.
virtual std::string serialize(bool all=false)
Serialize Button attributes to single string.
char getCursorChar() const
Get cursor character.
const int TATTR_COUNT
Count of number of text entry modified attribute categories.
Definition: TextEntry.h:18
int getBlinkRate() const
Get blink rate for cursor (in ticks).
bool numbersOnly() const
Return true if only numbers can be entered.
bool m_tmodified[TATTR_COUNT]
Modified attribute since serialize().
Definition: TextEntry.h:39
virtual int deserialize(std::string s)
Deserialize string to become attributes.
void setCursorChar(char new_cursor_char)
Set cursor character.
void setCursor(int new_cursor)
Set cursor to location.
int getLimit() const
Get limit of number of characters allowed.
int m_limit
Character limit in text.
Definition: TextEntry.h:34
void setLimit(int new_limit)
Set limit of number of characters allowed.
virtual void draw()
Draw viewstring + text entered.
std::string m_text
Text entered.
Definition: TextEntry.h:33
virtual void callback()=0
Called when TextEntry enter hit.
std::string getText() const
Get text entered.
int m_cursor
Cursor location.
Definition: TextEntry.h:35
virtual bool isModified() const
Return true if any attribute modified since last serialize.