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