[5c95436] | 1 | #include "RadioButtonList.h"
|
---|
| 2 |
|
---|
| 3 | #include <string>
|
---|
| 4 | #include <cmath>
|
---|
| 5 |
|
---|
| 6 | using namespace std;
|
---|
| 7 |
|
---|
| 8 | RadioButtonList::RadioButtonList(int x, int y, string strLabel, ALLEGRO_FONT *font) :
|
---|
| 9 | GuiComponent(x, y, 300, 300, font)
|
---|
| 10 | {
|
---|
| 11 | this->strLabel = strLabel;
|
---|
| 12 | this->selectedButton = -1;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | RadioButtonList::~RadioButtonList(void)
|
---|
| 17 | {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | void RadioButtonList::draw(ALLEGRO_DISPLAY *display)
|
---|
| 21 | {
|
---|
| 22 | al_set_target_bitmap(bitmap);
|
---|
| 23 | al_clear_to_color(al_map_rgb(0, 0, 0));
|
---|
| 24 |
|
---|
| 25 | int fontHeight = al_get_font_line_height(font);
|
---|
| 26 |
|
---|
| 27 | al_draw_text(font, al_map_rgb(0, 255, 0), 0, 0, ALLEGRO_ALIGN_LEFT, this->strLabel.c_str());
|
---|
| 28 | for(unsigned int i=0; i<this->vctRadioButtons.size(); i++) {
|
---|
| 29 | al_draw_circle(12, 26+i*20, 8, al_map_rgb(0, 255, 0), 1);
|
---|
| 30 | if (i == this->selectedButton)
|
---|
| 31 | al_draw_filled_circle(12, 26+i*20, 6, al_map_rgb(0, 255, 0));
|
---|
| 32 | al_draw_text(font, al_map_rgb(0, 255, 0), 26, 20+i*20, ALLEGRO_ALIGN_LEFT, this->vctRadioButtons[i].c_str());
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
| 36 | al_draw_bitmap(bitmap, x, y, 0);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | bool RadioButtonList::handleEvent(ALLEGRO_EVENT& e)
|
---|
| 40 | {
|
---|
| 41 | int centerX, centerY;
|
---|
| 42 | centerX = x+12;
|
---|
| 43 |
|
---|
| 44 | if (e.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
| 45 | if (e.mouse.button == 1) {
|
---|
| 46 | for (int i=0; i<this->vctRadioButtons.size(); i++) {
|
---|
| 47 | centerY = y+26+i*20;
|
---|
| 48 |
|
---|
| 49 | if (sqrt(pow((float)(e.mouse.x-centerX), 2.0f)+pow((float)(e.mouse.y-centerY), 2.0f))< 8) {
|
---|
| 50 | this->selectedButton = i;
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | return false;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void RadioButtonList::addRadioButton(string s) {
|
---|
| 61 | this->vctRadioButtons.push_back(s);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | int RadioButtonList::getSelectedButton() {
|
---|
| 65 | return this->selectedButton;
|
---|
| 66 | }
|
---|