-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUIButton.h
166 lines (131 loc) · 4.17 KB
/
GUIButton.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// GUIButton.h
// Deep
//
// Created by Nathan Daly on 9/18/12.
// Copyright (c) 2012 Lions Entertainment. All rights reserved.
//
#ifndef Deep_GUIButton_h
#define Deep_GUIButton_h
#include "GUIImageView.h"
#include "GUITextViews.h"
#include <iostream>
namespace GUI {
class QuitAction {
public:
void operator()() {
throw QuitAction();
}
};
class Button : public ImageView {
public:
Button()
: ImageView(GUIImage("GUIImages/button.bmp")),
is_pressed(false), is_hovered(false),
image(GUIImage("GUIImages/button.bmp")), hovered_image(GUIImage("GUIImages/button2.bmp")),
clicked_image(GUIImage("GUIImages/button3.bmp"))
{
SDL_Color clear = {0,0xff,0};
set_clear_color(clear);
}
protected:
// What to do when button is clicked
virtual void operation() {
// Default action is to do nothing.
}
virtual void set_image(const GUIImage& image_) {
image = image_;
if (!is_pressed && !is_hovered) draw_onto_self(image, DispPoint());
}
virtual void set_hovered_image(const GUIImage& image_) {
hovered_image = image_;
if (!is_pressed && is_hovered) draw_onto_self(hovered_image, DispPoint());
}
virtual void set_clicked_image(const GUIImage& image_) {
clicked_image = image_;
if (is_pressed && is_hovered) draw_onto_self(clicked_image, DispPoint());
}
// Returns true if the mouse_down is finished being handled.
// If returns false, handling will continue up the chain.
virtual bool handle_mouse_down(DispPoint coord) {
is_pressed = true;
draw_onto_self(clicked_image, DispPoint());
is_hovered = true;
capture_focus();
return true;
}
virtual bool handle_mouse_up(DispPoint coord) {
// Only perform event on mouse release.
lose_focus();
if (is_pressed) {
is_pressed = false;
draw_onto_self(image, DispPoint());
if (is_hovered) {
operation(); // Do what you were born to do!
}
}
if (is_hovered) {
is_hovered = false;
return true;
}
else return false;
}
virtual bool handle_mouse_motion(DispPoint coord, DispPoint rel_motion) {
// Only perform event on mouse release.
if (is_pressed && !is_hovered && rel_point_is_on_me(coord)) {
draw_onto_self(clicked_image, DispPoint());
is_hovered = true;
}
else if (!is_pressed && !is_hovered && rel_point_is_on_me(coord)) {
draw_onto_self(hovered_image, DispPoint());
is_hovered = true;
capture_focus();
}
else if (is_hovered && !rel_point_is_on_me(coord)) {
draw_onto_self(image, DispPoint());
is_hovered = false;
if (!is_pressed)
lose_focus();
}
return false;
}
private:
bool is_pressed, is_hovered;
GUIImage image, hovered_image, clicked_image;
};
class TextButton : public Button {
public:
TextButton(const std::string &button_text_ = "")
{
button_text = new TextView(get_w()-30, get_h());
button_text->set_text(button_text_);
button_text->set_text_size(16);
attach_subview(button_text, DispPoint(get_w()-button_text->get_w(), 0));
}
TextView* get_text_view() { return button_text; }
void set_text(const std::string &button_text_) { button_text->set_text(button_text_); }
public:
TextView *button_text;
};
class NoAction {
public:
void operator()() { }
};
template <typename Oper = NoAction>
class ActionButton : public TextButton {
public:
ActionButton(Oper oper = Oper(), const std::string &button_text_ = "")
:TextButton(button_text_), oper(oper)
{ }
virtual void operation() {
oper();
}
private:
Oper oper;
};
template <typename Oper>
TextButton* create_button(Oper oper, const std::string &button_text_ = "") {
return new ActionButton<Oper>(oper, button_text_);
}
} // namespace GUI
#endif