1 | #include "Button.h"
|
---|
2 |
|
---|
3 | Button::Button(int x, int y, int width, int height, ALLEGRO_FONT *font, string str, void (*callback)()) :
|
---|
4 | GuiComponent(x, y, width, height, font)
|
---|
5 | {
|
---|
6 | this->str = str;
|
---|
7 | this->callback = callback;
|
---|
8 | }
|
---|
9 |
|
---|
10 | Button::~Button(void)
|
---|
11 | {
|
---|
12 | }
|
---|
13 |
|
---|
14 | void Button::draw(ALLEGRO_DISPLAY *display)
|
---|
15 | {
|
---|
16 | al_set_target_bitmap(bitmap);
|
---|
17 | al_clear_to_color(al_map_rgb(0, 0, 0));
|
---|
18 |
|
---|
19 | int fontHeight = al_get_font_line_height(font);
|
---|
20 |
|
---|
21 | al_draw_text(font, al_map_rgb(0, 255, 0), this->width/2, (this->height-fontHeight)/2, ALLEGRO_ALIGN_CENTRE, str.c_str());
|
---|
22 | al_draw_rectangle(1, 1, this->width, this->height, al_map_rgb(0, 255, 0), 1);
|
---|
23 |
|
---|
24 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
25 | al_draw_bitmap(bitmap, x, y, 0);
|
---|
26 | }
|
---|
27 |
|
---|
28 | bool Button::handleEvent(ALLEGRO_EVENT& e)
|
---|
29 | {
|
---|
30 | if (e.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
31 | if (e.mouse.button == 1) {
|
---|
32 | if (this->x < e.mouse.x && e.mouse.x < (this->x+this->width)
|
---|
33 | && this->y < e.mouse.y && e.mouse.y < (this->y+this->height))
|
---|
34 | {
|
---|
35 | this->callback();
|
---|
36 | return true;
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | return false;
|
---|
42 | }
|
---|