4 // Copyright (C) 2005 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
27 #include "mousecursor.h"
28 #include "app/globals.h"
29 #include "video/font.h"
30 #include "video/surface.h"
32 using namespace SuperTux;
34 Font* Button::info_font = 0;
35 extern SDL_Surface* screen;
39 Button::Button(Surface* image_, std::string info_, SDLKey binding_)
43 size = Vector(image->w, image->h);
52 void Button::draw(DrawingContext &context, bool selected)
55 context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
57 context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
59 Vector tanslation = -context.get_translation();
60 if(state == BT_SHOW_INFO)
63 if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
64 offset = Vector(size.x, - 10);
65 else if(pos.x + tanslation.x < 100)
66 offset = Vector(size.x, 0);
68 offset = Vector(-30, -size.y/2);
69 context.draw_text(info_font, info, pos + offset, LEFT_ALLIGN, LAYER_GUI+2);
71 context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
72 ")", pos + offset + Vector(0,12),
73 LEFT_ALLIGN, LAYER_GUI+2);
76 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
79 int Button::event(SDL_Event &event, int x_offset, int y_offset)
84 case SDL_MOUSEBUTTONDOWN:
85 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
86 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
88 if(event.button.button == SDL_BUTTON_RIGHT)
92 case SDL_MOUSEBUTTONUP:
93 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
94 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
96 if(event.button.button == SDL_BUTTON_LEFT)
100 case SDL_KEYDOWN: // key pressed
101 if(event.key.keysym.sym == binding)
110 /* Group of buttons */
112 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_)
113 : pos(pos_), buttons_size(buttons_size_), buttons_box(buttons_box_)
117 button_selected = -1;
119 mouse_left_button = false;
123 ButtonGroup::~ButtonGroup()
127 void ButtonGroup::add_button(Button button, int id, bool select)
129 button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
130 button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
131 button.size = buttons_size;
134 button_selected = id;
136 buttons.push_back(button);
139 void ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
141 button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
142 button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
143 button1.size.x = button2.size.x = buttons_size.x;
144 button1.size.y = button2.size.y = buttons_size.y / 2;
145 button2.pos.y += buttons_size.y / 2;
150 buttons.push_back(button1);
151 buttons.push_back(button2);
154 void ButtonGroup::draw(DrawingContext &context)
156 context.draw_filled_rect(pos - Vector(12,4),
157 Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
158 Color (0,0,0, 128), LAYER_GUI-1);
160 context.push_transform();
161 context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
162 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
164 if(i->pos.y < row*buttons_size.y ||
165 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
168 i->draw(context, i->id == button_selected ? true : false);
170 context.pop_transform();
173 bool ButtonGroup::event(SDL_Event &event)
175 bool caught_event = false;
179 case SDL_MOUSEMOTION:
182 if(mouse_left_button)
184 pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/screen->w);
185 pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/screen->h);
188 if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
189 event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
192 case SDL_MOUSEBUTTONDOWN:
193 if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
194 buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
195 event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
200 if(event.button.button == SDL_BUTTON_WHEELUP)
206 else if(event.button.button == SDL_BUTTON_WHEELDOWN)
209 if(row > (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
210 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0))
211 row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
212 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0);
214 else if(event.button.button == SDL_BUTTON_LEFT)
215 mouse_left_button = true;
217 caught_event = false;
219 case SDL_MOUSEBUTTONUP:
220 mouse_left_button = false;
229 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
231 if(i->pos.y < row*buttons_size.y ||
232 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
235 if(i->event(event, (int)pos.x,
236 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
238 button_selected = i->id;
247 int ButtonGroup::selected_id()
249 return button_selected;
252 void ButtonGroup::set_unselected()
254 button_selected = -1;
257 bool ButtonGroup::is_hover()