Dragonfly 4.19
A text-based game engine
Button.h
1///
2/// A basic button
3///
4/// Invokes callback() when clicked.
5/// Changes to highlight color on mouse over.
6///
7
8#ifndef __BUTTON_H__
9#define __BUTTON_H__
10
11// Engine includes.
12#include "EventMouse.h"
13#include "ViewObject.h"
14
15namespace df {
16
17/// Count of number of button modified attribute categories.
18const int BATTR_COUNT = 4;
19
20/// Categories of button attributes that indicate modification.
22 ALL_BUTTON,
23 MODIFIED_BUTTON,
24 HIGHLIGHT_COLOR,
25 DEFAULT_COLOR,
26};
27
28class Button : public ViewObject {
29
30 private:
31 Color m_highlight_color; ///< Color when highlighted.
32 Color m_default_color; ///< Color when not highlighted.
33 bool m_bmodified[BATTR_COUNT]; ///< Modified attribute since serialize().
34
35 public:
36 Button();
37
38 /// Handle "mouse" events.
39 /// Return 0 if ignored, else 1.
40 int eventHandler(const Event *p_e) override;
41
42 /// Set highlight (when mouse over) color for Button.
43 void setHighlightColor(Color new_highlight_color);
44
45 /// Get highlight (when mouse over) color for Button.
47
48 /// Set color of Button.
49 void setDefaultColor(Color new_default_color);
50
51 /// Get color of Button
53
54 /// Return true if mouse over Button, else false.
55 bool mouseOverButton(const EventMouse *p_e) const;
56
57 /// Called when Button clicked.
58 /// Must be defined by derived class.
59 virtual void callback() = 0;
60
61 /// Serialize Button attributes to single string.
62 /// Can specify attribute to serialize (default "" means all modified).
63 /// If attr is "ALL", serialize all attributes (modified or not).
64 /// Clear m_bmodified[] array for attributes serialized.
65 virtual std::string serialize(std::string attr="") override;
66
67 /// Deserialize string to become attributes.
68 /// Rerturn 0 if no errors, else -1.
69 virtual int deserialize(std::string s) override;
70
71 /// Return true if attribute modified since last serialize.
72 virtual bool isModified(enum ButtonAttribute attribute) const;
73
74 /// Return true if any attribute modified since last serialize.
75 virtual bool isModified() const override;
76};
77
78} // end of namespace df
79#endif
Definition: Button.h:28
virtual int deserialize(std::string s) override
Deserialize string to become attributes.
bool mouseOverButton(const EventMouse *p_e) const
Return true if mouse over Button, else false.
virtual bool isModified() const override
Return true if any attribute modified since last serialize.
virtual void callback()=0
Called when Button clicked.
virtual std::string serialize(std::string attr="") override
Serialize Button attributes to single string.
virtual bool isModified(enum ButtonAttribute attribute) const
Return true if attribute modified since last serialize.
int eventHandler(const Event *p_e) override
Handle "mouse" events.
void setDefaultColor(Color new_default_color)
Set color of Button.
Color m_highlight_color
Color when highlighted.
Definition: Button.h:31
Color m_default_color
Color when not highlighted.
Definition: Button.h:32
void setHighlightColor(Color new_highlight_color)
Set highlight (when mouse over) color for Button.
bool m_bmodified[BATTR_COUNT]
Modified attribute since serialize().
Definition: Button.h:33
Color getDefaultColor() const
Get color of Button.
Color getHighlightColor() const
Get highlight (when mouse over) color for Button.
Definition: EventMouse.h:33
Definition: Event.h:15
Definition: ViewObject.h:45
An animation for a sprite.
Definition: Animation.h:15
Color
Colors Dragonfly recognizes.
Definition: Color.h:11
const int BATTR_COUNT
Count of number of button modified attribute categories.
Definition: Button.h:18
ButtonAttribute
Categories of button attributes that indicate modification.
Definition: Button.h:21