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 |
|
---|
23 | #ifdef WINDOWS
|
---|
24 | al_draw_rectangle(1, 1, this->width, this->height, al_map_rgb(0, 255, 0), 1);
|
---|
25 | #else
|
---|
26 | al_draw_rectangle(1, 0, this->width, this->height-1, al_map_rgb(0, 255, 0), 1);
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
30 | al_draw_bitmap(bitmap, x, y, 0);
|
---|
31 | }
|
---|
32 |
|
---|
33 | bool Button::handleEvent(ALLEGRO_EVENT& e)
|
---|
34 | {
|
---|
35 | if (e.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
36 | if (e.mouse.button == 1) {
|
---|
37 | if (this->x < e.mouse.x && e.mouse.x < (this->x+this->width)
|
---|
38 | && this->y < e.mouse.y && e.mouse.y < (this->y+this->height))
|
---|
39 | {
|
---|
40 | this->callback();
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | return false;
|
---|
47 | }
|
---|