Dragonfly 4.24
A text-based game engine
TextEntry.h
1///
2/// A basic text entry.
3///
4/// Invokes callback() when enter pressed.
5///
6
7#ifndef __TEXTENTRY_H__
8#define __TEXTENTRY_H__
9
10// Engine includes.
11#include "EventMouse.h"
12#include "ViewObject.h"
13
14namespace df {
15
16/// Categories of TextEntry attributes that indicate modification.
17enum class TextEntryAttribute : unsigned int {
18 TEXT = 1 << (ViewObjectAttributeMax + 0), // 01
19 APPEARANCE = 1 << (ViewObjectAttributeMax + 1), // 10
20};
21const int TextEntryAttributeMax = ViewObjectAttributeMax + 2;
22
23class TextEntry : public ViewObject {
24
25 private:
26 std::string m_text; ///< Text entered.
27 int m_limit; ///< Character limit in text.
28 int m_cursor; ///< Cursor location.
29 char m_cursor_char; ///< Cursor character.
30 int m_blink_rate; ///< Cursor blink rate.
31 bool m_numbers_only; ///< True if only numbers.
32 bool m_shift_down; ///< True if shift key pressed.
33
34 public:
35 TextEntry();
36
37 /// Clear text entry.
38 void clearText();
39
40 /// Set text, increasing limit if needed.
41 void setText(std::string new_text);
42
43 /// Get text entered.
44 std::string getText() const;
45
46 /// Handle "keyboard" events.
47 /// Return 0 if ignored, else 1.
48 int eventHandler(const Event *p_e) override;
49
50 /// Called when TextEntry enter hit.
51 /// Must be defined by derived class.
52 virtual void callback() = 0;
53
54 /// Set limit of number of characters allowed.
55 void setLimit(int new_limit);
56
57 /// Get limit of number of characters allowed.
58 int getLimit() const;
59
60 /// Set cursor to location.
61 void setCursor(int new_cursor);
62
63 /// Get cursor location.
64 int getCursor() const;
65
66 /// Set blink rate for cursor (in ticks).
67 void setBlinkRate(int new_blink_rate);
68
69 /// Get blink rate for cursor (in ticks).
70 int getBlinkRate() const;
71
72 /// Return true if only numbers can be entered.
73 bool numbersOnly() const;
74
75 /// Set to allow only numbers to be entered.
76 void setNumbersOnly(bool new_numbers_only = true);
77
78 /// Set cursor character.
79 void setCursorChar(char new_cursor_char);
80
81 /// Get cursor character.
82 char getCursorChar() const;
83
84 /// Draw viewstring + text entered.
85 virtual int draw() override;
86
87 /// Serialize attributes to stream.
88 /// Can specify individual attribute(s) to force (modified or not).
89 /// Default is only modified attributes.
90 /// Clears modified bits for attributes serialized.
91 /// Return 0 if ok, else -1.
92 virtual int serialize(std::stringstream *p_ss, unsigned int attr=0) override;
93
94 /// Deserialize stream to attributes and apply.
95 /// p_ss - incoming stream to deserialize.
96 /// p_a - outgoing bitmask of attributes modified (NULL means no outgoing).
97 /// Return 0 if ok, else -1.
98 virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL) override;
99};
100
101} // end of namespace df
102#endif
Definition: Event.h:15
Definition: TextEntry.h:23
int m_blink_rate
Cursor blink rate.
Definition: TextEntry.h:30
virtual int serialize(std::stringstream *p_ss, unsigned int attr=0) override
Serialize attributes to stream.
int getBlinkRate() const
Get blink rate for cursor (in ticks).
void clearText()
Clear text entry.
virtual void callback()=0
Called when TextEntry enter hit.
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:29
int m_limit
Character limit in text.
Definition: TextEntry.h:27
int eventHandler(const Event *p_e) override
Handle "keyboard" events.
char getCursorChar() const
Get cursor character.
virtual int draw() override
Draw viewstring + text entered.
bool m_shift_down
True if shift key pressed.
Definition: TextEntry.h:32
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 int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL) override
Deserialize stream to attributes and apply.
bool numbersOnly() const
Return true if only numbers can be entered.
void setCursor(int new_cursor)
Set cursor to location.
std::string m_text
Text entered.
Definition: TextEntry.h:26
int m_cursor
Cursor location.
Definition: TextEntry.h:28
void setText(std::string new_text)
Set text, increasing limit if needed.
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:31
Definition: ViewObject.h:39
An animation for a sprite.
Definition: Animation.h:15
TextEntryAttribute
Categories of TextEntry attributes that indicate modification.
Definition: TextEntry.h:17