source: network-game/client/Client/Button.cpp@ 1e250bf

Last change on this file since 1e250bf was 6319311, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

Some redfinition issues related to winsock2 are fixed and a few allegro rendering functions are now in GameRender

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include "Button.h"
2
3#include "../../common/Compiler.h"
4
5Button::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
12Button::~Button(void)
13{
14}
15
16void 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
35bool 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}
Note: See TracBrowser for help on using the repository browser.