Dragonfly  4.3
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 
15 namespace df {
16 
17 /// Count of number of button modified attribute categories.
18 const int BATTR_COUNT = 2;
19 
20 /// Categories of button attributes that indicate modification.
22  HIGHLIGHT_COLOR,
23  DEFAULT_COLOR,
24 };
25 
26 class Button : public ViewObject {
27 
28  private:
29  Color m_highlight_color; ///< Color when highlighted.
30  Color m_default_color; ///< Color when not highlighted.
31  bool m_bmodified[BATTR_COUNT]; ///< Modified attribute since serialize().
32 
33  public:
34  Button();
35 
36  /// Handle "mouse" events.
37  /// Return 0 if ignored, else 1.
38  int eventHandler(const Event *p_e);
39 
40  /// Set highlight (when mouse over) color for Button.
41  void setHighlightColor(Color new_highlight_color);
42 
43  /// Get highlight (when mouse over) color for Button.
44  Color getHighlightColor() const;
45 
46  /// Set color of Button.
47  void setDefaultColor(Color new_default_color);
48 
49  /// Get color of Button
50  Color getDefaultColor() const;
51 
52  /// Return true if mouse over Button, else false.
53  bool mouseOverButton(const EventMouse *p_e) const;
54 
55  /// Called when Button clicked.
56  /// Must be defined by derived class.
57  virtual void callback() = 0;
58 
59  /// Serialize Button attributes to single string.
60  /// Only modified attributes (unless all is true).
61  virtual std::string serialize(bool all = false);
62 
63  /// Deserialize string to become attributes.
64  /// Rerturn 0 if no errors, else -1.
65  virtual int deserialize(std::string s);
66 
67  /// Return true if attribute modified since last serialize.
68  virtual bool isModified(enum ButtonAttribute attribute) const;
69 
70  /// Return true if any attribute modified since last serialize.
71  virtual bool isModified() const;
72 };
73 
74 } // end of namespace df
75 #endif
Definition: ViewObject.h:45
void setDefaultColor(Color new_default_color)
Set color of Button.
void setHighlightColor(Color new_highlight_color)
Set highlight (when mouse over) color for Button.
Color getHighlightColor() const
Get highlight (when mouse over) color for Button.
int eventHandler(const Event *p_e)
Handle "mouse" events.
Definition: Event.h:15
A 2-d bounding box.
Definition: Box.h:10
ButtonAttribute
Categories of button attributes that indicate modification.
Definition: Button.h:21
Color m_default_color
Color when not highlighted.
Definition: Button.h:30
Color m_highlight_color
Color when highlighted.
Definition: Button.h:29
bool mouseOverButton(const EventMouse *p_e) const
Return true if mouse over Button, else false.
virtual int deserialize(std::string s)
Deserialize string to become attributes.
Color getDefaultColor() const
Get color of Button.
bool m_bmodified[BATTR_COUNT]
Modified attribute since serialize().
Definition: Button.h:31
Definition: Button.h:26
Color
Colors Dragonfly recognizes.
Definition: Color.h:11
virtual std::string serialize(bool all=false)
Serialize Button attributes to single string.
Definition: EventMouse.h:33
virtual void callback()=0
Called when Button clicked.
virtual bool isModified() const
Return true if any attribute modified since last serialize.
const int BATTR_COUNT
Count of number of button modified attribute categories.
Definition: Button.h:18