Dragonfly 4.20
A text-based game engine
TextBox.h
1///
2/// A basic TextBox, with word wrap and scrolling.
3///
4
5#ifndef __TEXTBOX_H__
6#define __TEXTBOX_H__
7
8// Engine includes.
9#include "Event.h"
10#include "ViewObject.h"
11
12namespace df {
13
14/// Categories of TextBox attributes that indicate modification.
15enum class TextBoxAttribute : unsigned int {
16 TEXT = 1 << (ViewObjectAttributeMax + 0), // 01
17 APPEARANCE = 1 << (ViewObjectAttributeMax + 1), // 10
18};
19const int TextBoxAttributeMax = ViewObjectAttributeMax + 2;
20
21class TextBox : public ViewObject {
22
23 private:
24 std::string m_text; ///< Text in box.
25 Vector m_size; ///< Width (x), height (y), in spaces.
26 bool m_padding; ///< True to pad rows from side bars.
27 bool m_word_wrap; ///< True to wrap lines at spaces.
28
29 public:
30 TextBox();
31
32 /// Set text, overwritting any current text.
33 void setText(std::string new_text);
34
35 /// Append text, adding to any current text.
36 void addText(std::string new_text);
37
38 /// Get text.
39 std::string getText() const;
40
41 /// Remove all text.
42 void clearText();
43
44 /// Get size.
45 Vector getSize() const;
46
47 /// Set size.
48 void setSize(df::Vector new_size);
49
50 /// Get padding.
51 bool getPadding() const;
52
53 /// Set padding.
54 void setPadding(bool new_padding);
55
56 /// Get word wrap.
57 bool getWordWrap() const;
58
59 /// Set word wrap.
60 void setWordWrap(bool new_word_wrap);
61
62 /// Draw box and text.
63 virtual int draw() override;
64
65 /// Serialize attributes to stream.
66 /// Can specify individual attribute(s) to force (modified or not).
67 /// Default is only modified attributes.
68 /// Clears modified bits for attributes serialized.
69 /// Return 0 if ok, else -1.
70 virtual int serialize(std::stringstream *p_ss, unsigned int attr=0) override;
71
72 /// Deserialize stream to attributes and apply.
73 /// p_ss - incoming stream to deserialize.
74 /// p_a - outgoing bitmask of attributes modified (NULL means no outgoing).
75 /// Return 0 if ok, else -1.
76 virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL) override;
77};
78
79} // end of namespace df
80#endif
Definition: TextBox.h:21
void clearText()
Remove all text.
bool getWordWrap() const
Get word wrap.
std::string getText() const
Get text.
void setPadding(bool new_padding)
Set padding.
Vector getSize() const
Get size.
void setSize(df::Vector new_size)
Set size.
bool m_word_wrap
True to wrap lines at spaces.
Definition: TextBox.h:27
void setText(std::string new_text)
Set text, overwritting any current text.
void setWordWrap(bool new_word_wrap)
Set word wrap.
std::string m_text
Text in box.
Definition: TextBox.h:24
bool m_padding
True to pad rows from side bars.
Definition: TextBox.h:26
Vector m_size
Width (x), height (y), in spaces.
Definition: TextBox.h:25
void addText(std::string new_text)
Append text, adding to any current text.
bool getPadding() const
Get padding.
virtual int draw() override
Draw box and text.
virtual int serialize(std::stringstream *p_ss, unsigned int attr=0) override
Serialize attributes to stream.
virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL) override
Deserialize stream to attributes and apply.
Definition: Vector.h:12
Definition: ViewObject.h:39
An animation for a sprite.
Definition: Animation.h:15
TextBoxAttribute
Categories of TextBox attributes that indicate modification.
Definition: TextBox.h:15