-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.cpp
227 lines (193 loc) · 3.91 KB
/
Button.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <algorithm>
#include <cstring>
#include <iostream>
#include "Button.h"
#include "Input.h"
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
// dummy function for callbacks
void dummyCallbackFunction(Button* object, void* data)
{
if (data && object)
{
}
/* dummy function does nothing */
}
Button::Button():
OnMouseOver(dummyCallbackFunction),
OnMouseOut(dummyCallbackFunction),
OnClick(dummyCallbackFunction),
data_(0),
image_(NULL),
caption_(NULL),
faceColor_(al_map_rgb(192,192,192)),
borderColor_(al_map_rgb(96,96,96)),
shadowColor_(al_map_rgb(0,0,0)),
textColor_(al_map_rgb(0,0,0)),
w_(128), h_(24),
x_(0), y_(0)
{}
Button::~Button()
{
if (caption_ != NULL)
{
delete [] caption_;
caption_ = NULL;
}
if (image_ != NULL)
{
al_destroy_bitmap(image_);
image_ = NULL;
}
}
void Button::Create()
{
if (caption_ != NULL)
{
ALLEGRO_FONT *font = al_load_bitmap_font("a4_font.tga");
if (!font)
std::cerr << "Font a4_font.tga not loaded correctly\n";
int width = al_get_text_width(font, caption_);
w_ = std::max(w_, width + 8);
h_ = std::max(h_, al_get_font_line_height(font) + 8);
image_ = al_create_bitmap(w_, h_);
al_set_target_bitmap(image_);
al_clear_to_color(faceColor_);
al_draw_rectangle(0, 0, w_-1, h_-1, borderColor_, 0);
al_draw_textf(font, textColor_,
w_ / 2 - width / 2,
h_ / 2 - al_get_font_line_height(font) / 2, 0, caption_);
}
}
void Button::Recreate()
{
if (image_ != NULL)
{
al_destroy_bitmap(image_);
image_ = NULL;
}
this->Create();
}
bool Button::Update()
{
std::pair<int,int> mouse = Input::GetInstance()->GetMouse();
if (mouse.first >= x_ && mouse.first <= x_ + w_ &&
mouse.second >= y_ && mouse.second <= y_ + h_)
{
this->OnMouseOver(this, data_);
if (Input::GetInstance()->IsMouseClicked())
{
this->OnClick(this, data_);
return true;
}
}
else
{
this->OnMouseOut(this, data_);
}
return false;
}
void Button::Render(ALLEGRO_BITMAP* destination)
{
if (image_ == NULL) return;
al_set_target_bitmap(destination);
al_draw_bitmap_region(image_, 0, 0, al_get_bitmap_width(image_),
al_get_bitmap_height(image_), x_, y_, 0);
}
void Button::SetCaption(const char* caption)
{
if (caption_ != NULL)
delete caption_;
caption_ = new char[strlen(caption)+1];
strcpy(caption_, caption);
}
void Button::SetSize(int width, int height)
{
// this function does not do anything if the button has already been created
if (image_ != NULL) return;
w_ = width;
h_ = height;
}
void Button::SetPosition(int x, int y)
{
x_ = x;
y_ = y;
}
void Button::SetPosition(ALLEGRO_BITMAP* anchorBitmap, int x, int y)
{
if (x < 0)
x_ = al_get_bitmap_width(anchorBitmap) - (w_ - x);
else
x_ = x;
if (y < 0)
y_ = al_get_bitmap_height(anchorBitmap) - (h_ - y);
else
y_ = y;
}
void Button::SetTextColor(ALLEGRO_COLOR color)
{
textColor_ = color;
}
void Button::SetShadowColor(ALLEGRO_COLOR color)
{
shadowColor_ = color;
}
void Button::SetBorderColor(ALLEGRO_COLOR color)
{
borderColor_ = color;
}
void Button::SetFaceColor(ALLEGRO_COLOR color)
{
faceColor_ = color;
}
void Button::CenterOn(ALLEGRO_BITMAP* surface)
{
if (surface == NULL) return; // do nothing if no surface exists
x_ = al_get_bitmap_width(surface) / 2 - w_ / 2;
y_ = al_get_bitmap_height(surface) / 2 - h_ / 2;
}
int Button::GetX()
{
return x_;
}
int Button::GetY()
{
return y_;
}
int Button::GetWidth()
{
return w_;
}
int Button::GetHeight()
{
return h_;
}
ALLEGRO_COLOR Button::GetTextColor()
{
return textColor_;
}
ALLEGRO_COLOR Button::GetShadowColor()
{
return shadowColor_;
}
ALLEGRO_COLOR Button::GetBorderColor()
{
return borderColor_;
}
ALLEGRO_COLOR Button::GetFaceColor()
{
return faceColor_;
}
char* Button::GetCaption()
{
return caption_;
}
void Button::SetMiscData(void* data)
{
data_ = data;
}
void* Button::GetMiscData()
{
return data_;
}