Dragonfly 4.19
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
15namespace df {
16
17/// Count of number of text entry modified attribute categories.
18const int TATTR_COUNT = 8;
19
20/// Categories of text entry attributes that indicate modification.
22 ALL_TEXT,
23 MODIFIED_TEXT,
24 TEXT,
25 LIMIT,
26 CURSOR,
27 CURSOR_CHAR,
28 BLINK_RATE,
29 NUMBERS_ONLY,
30};
31
32class TextEntry : public ViewObject {
33
34 private:
35 std::string m_text; ///< Text entered.
36 int m_limit; ///< Character limit in text.
37 int m_cursor; ///< Cursor location.
38 char m_cursor_char; ///< Cursor character.
39 int m_blink_rate; ///< Cursor blink rate.
40 bool m_numbers_only; ///< True if only numbers.
41 bool m_tmodified[TATTR_COUNT]; ///< Modified attribute since serialize().
42
43 public:
44 TextEntry();
45
46 /// Set text entered.
47 void setText(std::string new_text);
48
49 /// Get text entered.
50 std::string getText() const;
51
52 /// Handle "keyboard" events.
53 /// Return 0 if ignored, else 1.
54 int eventHandler(const Event *p_e) override;
55
56 /// Called when TextEntry enter hit.
57 /// Must be defined by derived class.
58 virtual void callback() = 0;
59
60 /// Set limit of number of characters allowed.
61 void setLimit(int new_limit);
62
63 /// Get limit of number of characters allowed.
64 int getLimit() const;
65
66 /// Set cursor to location.
67 void setCursor(int new_cursor);
68
69 /// Get cursor location.
70 int getCursor() const;
71
72 /// Set blink rate for cursor (in ticks).
73 void setBlinkRate(int new_blink_rate);
74
75 /// Get blink rate for cursor (in ticks).
76 int getBlinkRate() const;
77
78 /// Return true if only numbers can be entered.
79 bool numbersOnly() const;
80
81 /// Set to allow only numbers to be entered.
82 void setNumbersOnly(bool new_numbers_only = true);
83
84 /// Set cursor character.
85 void setCursorChar(char new_cursor_char);
86
87 /// Get cursor character.
88 char getCursorChar() const;
89
90 /// Draw viewstring + text entered.
91 virtual int draw() override;
92
93 /// Serialize TextEntry attributes to single string.
94 /// Can specify attribute to serialize (default "" means all modified).
95 /// If attr is "ALL", serialize all attributes (modified or not).
96 /// Clear m_tmodified[] array for attributes serialized.
97 virtual std::string serialize(std::string attr="") override;
98
99 /// Deserialize string to become attributes.
100 /// Rerturn 0 if no errors, else -1.
101 virtual int deserialize(std::string s) override;
102
103 /// Return true if attribute modified since last serialize.
104 virtual bool isModified(enum TextEntryAttribute attribute) const;
105
106 /// Return true if any attribute modified since last serialize.
107 virtual bool isModified() const override;
108};
109
110} // end of namespace df
111#endif
Definition: Event.h:15
Definition: TextEntry.h:32
int m_blink_rate
Cursor blink rate.
Definition: TextEntry.h:39
int getBlinkRate() const
Get blink rate for cursor (in ticks).
bool m_tmodified[TATTR_COUNT]
Modified attribute since serialize().
Definition: TextEntry.h:41
virtual void callback()=0
Called when TextEntry enter hit.
virtual bool isModified(enum TextEntryAttribute attribute) const
Return true if attribute modified since last serialize.
std::string getText() const
Get text entered.
void setLimit(int new_limit)
Set limit of number of characters allowed.
char m_cursor_char
Cursor character.
Definition: TextEntry.h:38
int m_limit
Character limit in text.
Definition: TextEntry.h:36
int eventHandler(const Event *p_e) override
Handle "keyboard" events.
char getCursorChar() const
Get cursor character.
virtual int draw() override
Draw viewstring + text entered.
virtual int deserialize(std::string s) override
Deserialize string to become attributes.
void setNumbersOnly(bool new_numbers_only=true)
Set to allow only numbers to be entered.
int getLimit() const
Get limit of number of characters allowed.
virtual bool isModified() const override
Return true if any attribute modified since last serialize.
bool numbersOnly() const
Return true if only numbers can be entered.
void setCursor(int new_cursor)
Set cursor to location.
virtual std::string serialize(std::string attr="") override
Serialize TextEntry attributes to single string.
std::string m_text
Text entered.
Definition: TextEntry.h:35
int m_cursor
Cursor location.
Definition: TextEntry.h:37
void setText(std::string new_text)
Set text entered.
void setCursorChar(char new_cursor_char)
Set cursor character.
int getCursor() const
Get cursor location.
void setBlinkRate(int new_blink_rate)
Set blink rate for cursor (in ticks).
bool m_numbers_only
True if only numbers.
Definition: TextEntry.h:40
Definition: ViewObject.h:45
An animation for a sprite.
Definition: Animation.h:15
TextEntryAttribute
Categories of text entry attributes that indicate modification.
Definition: TextEntry.h:21
const int TATTR_COUNT
Count of number of text entry modified attribute categories.
Definition: TextEntry.h:18