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.
28 #include "mousecursor.hpp"
29 #include "video/font.hpp"
30 #include "video/surface.hpp"
31 #include "video/drawing_context.hpp"
33 Font* Button::info_font = 0;
34 extern SDL_Surface* screen;
38 Button::Button(Surface* image_, std::string info_, SDLKey binding_)
42 size = Vector(image->get_width(), image->get_height());
51 void Button::draw(DrawingContext &context, bool selected)
54 context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
56 context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
58 Vector tanslation = -context.get_translation();
59 if(state == BT_SHOW_INFO)
62 if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
63 offset = Vector(size.x, - 10);
64 else if(pos.x + tanslation.x < 100)
65 offset = Vector(size.x, 0);
67 offset = Vector(-30, -size.y/2);
68 context.draw_text(info_font, info, pos + offset, ALIGN_LEFT, LAYER_GUI+2);
70 context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
71 ")", pos + offset + Vector(0,12),
72 ALIGN_LEFT, LAYER_GUI+2);
75 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
78 int Button::event(SDL_Event &event, int x_offset, int y_offset)
83 case SDL_MOUSEBUTTONDOWN:
84 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
85 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
87 if(event.button.button == SDL_BUTTON_RIGHT)
91 case SDL_MOUSEBUTTONUP:
92 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
93 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
95 if(event.button.button == SDL_BUTTON_LEFT)
99 case SDL_KEYDOWN: // key pressed
100 if(event.key.keysym.sym == binding)
109 /* Group of buttons */
111 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_)
112 : pos(pos_), buttons_size(buttons_size_), buttons_box(buttons_box_)
116 button_selected = -1;
118 mouse_left_button = false;
122 ButtonGroup::~ButtonGroup()
126 void ButtonGroup::add_button(Button button, int id, bool select)
128 button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
129 button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
130 button.size = buttons_size;
133 button_selected = id;
135 buttons.push_back(button);
138 void ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
140 button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
141 button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
142 button1.size.x = button2.size.x = buttons_size.x;
143 button1.size.y = button2.size.y = buttons_size.y / 2;
144 button2.pos.y += buttons_size.y / 2;
149 buttons.push_back(button1);
150 buttons.push_back(button2);
153 void ButtonGroup::draw(DrawingContext &context)
155 context.draw_filled_rect(pos - Vector(12,4),
156 Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
157 Color (0,0,0, 128), LAYER_GUI-1);
159 context.push_transform();
160 context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
161 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
163 if(i->pos.y < row*buttons_size.y ||
164 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
167 i->draw(context, i->id == button_selected);
169 context.pop_transform();
172 bool ButtonGroup::event(SDL_Event &event)
174 bool caught_event = false;
178 case SDL_MOUSEMOTION:
181 if(mouse_left_button)
183 pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/screen->w);
184 pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/screen->h);
187 if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
188 event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
191 case SDL_MOUSEBUTTONDOWN:
192 if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
193 buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
194 event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
199 if(event.button.button == SDL_BUTTON_WHEELUP)
205 else if(event.button.button == SDL_BUTTON_WHEELDOWN)
208 if(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))
210 row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
211 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0);
213 else if(event.button.button == SDL_BUTTON_LEFT)
214 mouse_left_button = true;
216 caught_event = false;
218 case SDL_MOUSEBUTTONUP:
219 mouse_left_button = false;
228 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
230 if(i->pos.y < row*buttons_size.y ||
231 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
234 if(i->event(event, (int)pos.x,
235 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
237 button_selected = i->id;
246 int ButtonGroup::selected_id()
248 return button_selected;
251 void ButtonGroup::set_unselected()
253 button_selected = -1;
256 bool ButtonGroup::is_hover()