4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "mousecursor.hpp"
28 #include "video/font.hpp"
29 #include "video/surface.hpp"
31 Font* Button::info_font = 0;
32 extern SDL_Surface* screen;
36 Button::Button(Surface* image_, std::string info_, SDLKey binding_)
40 size = Vector(image->get_width(), image->get_height());
49 void Button::draw(DrawingContext &context, bool selected)
52 context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
54 context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
56 Vector tanslation = -context.get_translation();
57 if(state == BT_SHOW_INFO)
60 if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
61 offset = Vector(size.x, - 10);
62 else if(pos.x + tanslation.x < 100)
63 offset = Vector(size.x, 0);
65 offset = Vector(-30, -size.y/2);
66 context.draw_text(info_font, info, pos + offset, LEFT_ALLIGN, LAYER_GUI+2);
68 context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
69 ")", pos + offset + Vector(0,12),
70 LEFT_ALLIGN, LAYER_GUI+2);
73 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
76 int Button::event(SDL_Event &event, int x_offset, int y_offset)
81 case SDL_MOUSEBUTTONDOWN:
82 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
83 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
85 if(event.button.button == SDL_BUTTON_RIGHT)
89 case SDL_MOUSEBUTTONUP:
90 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
91 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
93 if(event.button.button == SDL_BUTTON_LEFT)
97 case SDL_KEYDOWN: // key pressed
98 if(event.key.keysym.sym == binding)
107 /* Group of buttons */
109 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_)
110 : pos(pos_), buttons_size(buttons_size_), buttons_box(buttons_box_)
114 button_selected = -1;
116 mouse_left_button = false;
120 ButtonGroup::~ButtonGroup()
124 void ButtonGroup::add_button(Button button, int id, bool select)
126 button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
127 button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
128 button.size = buttons_size;
131 button_selected = id;
133 buttons.push_back(button);
136 void ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
138 button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
139 button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
140 button1.size.x = button2.size.x = buttons_size.x;
141 button1.size.y = button2.size.y = buttons_size.y / 2;
142 button2.pos.y += buttons_size.y / 2;
147 buttons.push_back(button1);
148 buttons.push_back(button2);
151 void ButtonGroup::draw(DrawingContext &context)
153 context.draw_filled_rect(pos - Vector(12,4),
154 Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
155 Color (0,0,0, 128), LAYER_GUI-1);
157 context.push_transform();
158 context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
159 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
161 if(i->pos.y < row*buttons_size.y ||
162 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
165 i->draw(context, i->id == button_selected ? true : false);
167 context.pop_transform();
170 bool ButtonGroup::event(SDL_Event &event)
172 bool caught_event = false;
176 case SDL_MOUSEMOTION:
179 if(mouse_left_button)
181 pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/screen->w);
182 pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/screen->h);
185 if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
186 event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
189 case SDL_MOUSEBUTTONDOWN:
190 if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
191 buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
192 event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
197 if(event.button.button == SDL_BUTTON_WHEELUP)
203 else if(event.button.button == SDL_BUTTON_WHEELDOWN)
206 if(row > (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
207 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0))
208 row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
209 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0);
211 else if(event.button.button == SDL_BUTTON_LEFT)
212 mouse_left_button = true;
214 caught_event = false;
216 case SDL_MOUSEBUTTONUP:
217 mouse_left_button = false;
226 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
228 if(i->pos.y < row*buttons_size.y ||
229 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
232 if(i->event(event, (int)pos.x,
233 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
235 button_selected = i->id;
244 int ButtonGroup::selected_id()
246 return button_selected;
249 void ButtonGroup::set_unselected()
251 button_selected = -1;
254 bool ButtonGroup::is_hover()