Dragonfly 4.20
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/// Categories of button attributes that indicate modification.
18enum class ButtonAttribute : unsigned int {
19 APPEARANCE = 1 << (ViewObjectAttributeMax + 0), // 1
20};
21const int ButtonAttributeMax = ViewObjectAttributeMax + 1;
22
23class Button : public ViewObject {
24
25 private:
26 Color m_highlight_color; ///< Color when highlighted.
27 Color m_default_color; ///< Color when not highlighted.
28
29 public:
30 Button();
31
32 /// Handle "mouse" events.
33 /// Return 0 if ignored, else 1.
34 int eventHandler(const Event *p_e) override;
35
36 /// Set highlight (when mouse over) color for Button.
37 void setHighlightColor(Color new_highlight_color);
38
39 /// Get highlight (when mouse over) color for Button.
41
42 /// Set color of Button.
43 void setDefaultColor(Color new_default_color);
44
45 /// Get color of Button
47
48 /// Return true if mouse over Button, else false.
49 bool mouseOverButton(const EventMouse *p_e) const;
50
51 /// Called when Button clicked.
52 /// Must be defined by derived class.
53 virtual void callback() = 0;
54
55 /// Serialize attributes to stream.
56 /// Can specify individual attribute(s) to force (modified or not).
57 /// Default is only modified attributes.
58 /// Clears modified bits for attributes serialized.
59 /// Return 0 if ok, else -1.
60 virtual int serialize(std::stringstream *p_ss, unsigned int attr=0) override;
61
62 /// Deserialize stream to attributes and apply.
63 /// p_ss - incoming stream to deserialize.
64 /// p_a - outgoing bitmask of attributes modified (NULL means no outgoing).
65 /// Return 0 if ok, else -1.
66 virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL) override;
67};
68
69} // end of namespace df
70#endif
Definition: Button.h:23
virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL) override
Deserialize stream to attributes and apply.
bool mouseOverButton(const EventMouse *p_e) const
Return true if mouse over Button, else false.
virtual void callback()=0
Called when Button clicked.
int eventHandler(const Event *p_e) override
Handle "mouse" events.
void setDefaultColor(Color new_default_color)
Set color of Button.
virtual int serialize(std::stringstream *p_ss, unsigned int attr=0) override
Serialize attributes to stream.
Color m_highlight_color
Color when highlighted.
Definition: Button.h:26
Color m_default_color
Color when not highlighted.
Definition: Button.h:27
void setHighlightColor(Color new_highlight_color)
Set highlight (when mouse over) color for Button.
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:39
An animation for a sprite.
Definition: Animation.h:15
ButtonAttribute
Categories of button attributes that indicate modification.
Definition: Button.h:18
Color
Colors Dragonfly recognizes.
Definition: Color.h:11