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