#include <button_group.hpp>
Public Member Functions | |
ButtonGroup (Vector pos_, Vector size_, Vector button_box_) | |
~ButtonGroup () | |
void | draw (DrawingContext &context) |
bool | event (SDL_Event &event) |
void | add_button (Button button, int id, bool select=false) |
void | add_pair_of_buttons (Button button1, int id1, Button button2, int id2) |
int | selected_id () |
void | set_unselected () |
bool | is_hover () |
Private Types | |
typedef std::vector< Button > | Buttons |
Private Member Functions | |
ButtonGroup (const ButtonGroup &) | |
ButtonGroup & | operator= (const ButtonGroup &) |
Private Attributes | |
Vector | pos |
Vector | buttons_size |
Vector | buttons_box |
Buttons | buttons |
int | button_selected |
int | row |
bool | mouse_hover |
bool | mouse_left_button |
int | buttons_pair_nb |
Definition at line 29 of file button_group.hpp.
typedef std::vector<Button> ButtonGroup::Buttons [private] |
Definition at line 46 of file button_group.hpp.
Definition at line 22 of file button_group.cpp.
References button_selected, buttons, buttons_pair_nb, mouse_hover, mouse_left_button, and row.
00022 : 00023 pos(pos_), 00024 buttons_size(buttons_size_), 00025 buttons_box(buttons_box_), 00026 buttons(), 00027 button_selected(), 00028 row(), 00029 mouse_hover(), 00030 mouse_left_button(), 00031 buttons_pair_nb() 00032 { 00033 buttons.clear(); 00034 row = 0; 00035 button_selected = -1; 00036 mouse_hover = false; 00037 mouse_left_button = false; 00038 buttons_pair_nb = 0; 00039 }
ButtonGroup::~ButtonGroup | ( | ) |
ButtonGroup::ButtonGroup | ( | const ButtonGroup & | ) | [private] |
void ButtonGroup::draw | ( | DrawingContext & | context | ) |
Definition at line 75 of file button_group.cpp.
References button_selected, buttons, buttons_box, buttons_size, DrawingContext::draw_filled_rect(), LAYER_GUI, DrawingContext::pop_transform(), pos, DrawingContext::push_transform(), row, DrawingContext::set_translation(), Vector::x, and Vector::y.
00076 { 00077 context.draw_filled_rect(pos - Vector(12,4), 00078 Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8), 00079 Color (0,0,0, 128), LAYER_GUI-1); 00080 00081 context.push_transform(); 00082 context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row)); 00083 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i) 00084 { 00085 if(i->pos.y < row*buttons_size.y || 00086 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y) 00087 continue; 00088 00089 i->draw(context, i->id == button_selected); 00090 } 00091 context.pop_transform(); 00092 }
bool ButtonGroup::event | ( | SDL_Event & | event | ) |
Definition at line 95 of file button_group.cpp.
References BT_SELECTED, button_selected, buttons, buttons_box, buttons_pair_nb, buttons_size, g_screen, mouse_hover, mouse_left_button, pos, row, SCREEN_HEIGHT, SCREEN_WIDTH, Vector::x, and Vector::y.
00096 { 00097 bool caught_event = false; 00098 00099 switch(event.type) 00100 { 00101 case SDL_MOUSEMOTION: 00102 mouse_hover = false; 00103 00104 if(mouse_left_button) 00105 { 00106 pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/g_screen->w); 00107 pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/g_screen->h); 00108 caught_event = true; 00109 } 00110 if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x && 00111 event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y) 00112 mouse_hover = true; 00113 break; 00114 case SDL_MOUSEBUTTONDOWN: 00115 if(event.button.x < pos.x-12 || event.button.x > pos.x+16 + 00116 buttons_box.x*buttons_size.x || event.button.y < pos.y-4 || 00117 event.button.y > pos.y+8 + buttons_box.y*buttons_size.y) 00118 break; 00119 00120 caught_event = true; 00121 00122 if(event.button.button == SDL_BUTTON_WHEELUP) 00123 { 00124 row--; 00125 if(row < 0) 00126 row = 0; 00127 } 00128 else if(event.button.button == SDL_BUTTON_WHEELDOWN) 00129 { 00130 row++; 00131 if(row > (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y + 00132 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0)) 00133 row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y + 00134 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0); 00135 } 00136 else if(event.button.button == SDL_BUTTON_LEFT) 00137 mouse_left_button = true; 00138 else 00139 caught_event = false; 00140 break; 00141 case SDL_MOUSEBUTTONUP: 00142 mouse_left_button = false; 00143 break; 00144 default: 00145 break; 00146 } 00147 00148 if(caught_event) 00149 return true; 00150 00151 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i) 00152 { 00153 if(i->pos.y < row*buttons_size.y || 00154 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y) 00155 continue; 00156 00157 if(i->event(event, (int)pos.x, 00158 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED) 00159 { 00160 button_selected = i->id; 00161 caught_event = true; 00162 break; 00163 } 00164 } 00165 00166 return caught_event; 00167 }
void ButtonGroup::add_button | ( | Button | button, | |
int | id, | |||
bool | select = false | |||
) |
Definition at line 46 of file button_group.cpp.
References button_selected, buttons, buttons_box, buttons_pair_nb, buttons_size, Button::id, Button::pos, Button::size, Vector::x, and Vector::y.
00047 { 00048 button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x; 00049 button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y; 00050 button.size = buttons_size; 00051 button.id = id; 00052 if(select) 00053 button_selected = id; 00054 00055 buttons.push_back(button); 00056 }
Definition at line 59 of file button_group.cpp.
References buttons, buttons_box, buttons_pair_nb, buttons_size, Button::id, Button::pos, Button::size, Vector::x, and Vector::y.
00060 { 00061 button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x; 00062 button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y; 00063 button1.size.x = button2.size.x = buttons_size.x; 00064 button1.size.y = button2.size.y = buttons_size.y / 2; 00065 button2.pos.y += buttons_size.y / 2; 00066 button1.id = id1; 00067 button2.id = id2; 00068 00069 buttons_pair_nb++; 00070 buttons.push_back(button1); 00071 buttons.push_back(button2); 00072 }
int ButtonGroup::selected_id | ( | ) |
Definition at line 170 of file button_group.cpp.
References button_selected.
00171 { 00172 return button_selected; 00173 }
void ButtonGroup::set_unselected | ( | ) |
Definition at line 176 of file button_group.cpp.
References button_selected.
00177 { 00178 button_selected = -1; 00179 }
bool ButtonGroup::is_hover | ( | ) |
Definition at line 182 of file button_group.cpp.
References mouse_hover.
00183 { 00184 return mouse_hover; 00185 }
ButtonGroup& ButtonGroup::operator= | ( | const ButtonGroup & | ) | [private] |
Vector ButtonGroup::pos [private] |
Vector ButtonGroup::buttons_size [private] |
Definition at line 49 of file button_group.hpp.
Referenced by add_button(), add_pair_of_buttons(), draw(), and event().
Vector ButtonGroup::buttons_box [private] |
Definition at line 50 of file button_group.hpp.
Referenced by add_button(), add_pair_of_buttons(), draw(), and event().
Buttons ButtonGroup::buttons [private] |
Definition at line 51 of file button_group.hpp.
Referenced by add_button(), add_pair_of_buttons(), ButtonGroup(), draw(), and event().
int ButtonGroup::button_selected [private] |
Definition at line 53 of file button_group.hpp.
Referenced by add_button(), ButtonGroup(), draw(), event(), selected_id(), and set_unselected().
int ButtonGroup::row [private] |
bool ButtonGroup::mouse_hover [private] |
Definition at line 55 of file button_group.hpp.
Referenced by ButtonGroup(), event(), and is_hover().
bool ButtonGroup::mouse_left_button [private] |
int ButtonGroup::buttons_pair_nb [private] |
Definition at line 58 of file button_group.hpp.
Referenced by add_button(), add_pair_of_buttons(), ButtonGroup(), and event().