1 /***************************************************************************
2 button.cpp - graphical buttons
5 copyright : (C) 2004 by Ricardo Cruz
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
24 #include "mousecursor.h"
25 #include "app/globals.h"
26 #include "video/font.h"
28 using namespace SuperTux;
30 Font* Button::info_font = 0;
34 Button::Button(Surface* image_, std::string info_, SDLKey binding_)
38 size = Vector(image->w, image->h);
47 void Button::draw(DrawingContext &context, bool selected)
50 context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
52 context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
54 Vector tanslation = -context.get_translation();
55 if(state == BT_SHOW_INFO)
58 if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
59 offset = Vector(size.x, - 10);
60 else if(pos.x + tanslation.x < 100)
61 offset = Vector(size.x, 0);
63 offset = Vector(-30, -size.y/2);
64 context.draw_text(info_font, info, pos + offset, LEFT_ALLIGN, LAYER_GUI+2);
66 context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
67 ")", pos + offset + Vector(0,12),
68 LEFT_ALLIGN, LAYER_GUI+2);
71 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
74 int Button::event(SDL_Event &event, int x_offset, int y_offset)
79 case SDL_MOUSEBUTTONDOWN:
80 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
81 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
83 if(event.button.button == SDL_BUTTON_RIGHT)
87 case SDL_MOUSEBUTTONUP:
88 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
89 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
91 if(event.button.button == SDL_BUTTON_LEFT)
95 case SDL_KEYDOWN: // key pressed
96 if(event.key.keysym.sym == binding)
105 /* Group of buttons */
107 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_)
108 : pos(pos_), buttons_size(buttons_size_), buttons_box(buttons_box_)
112 button_selected = -1;
114 mouse_left_button = false;
118 ButtonGroup::~ButtonGroup()
122 void ButtonGroup::add_button(Button button, int id, bool select)
124 button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
125 button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
126 button.size = buttons_size;
129 button_selected = id;
131 buttons.push_back(button);
134 void ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
136 button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
137 button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
138 button1.size.x = button2.size.x = buttons_size.x;
139 button1.size.y = button2.size.y = buttons_size.y / 2;
140 button2.pos.y += buttons_size.y / 2;
145 buttons.push_back(button1);
146 buttons.push_back(button2);
149 void ButtonGroup::draw(DrawingContext &context)
151 context.draw_filled_rect(pos - Vector(12,4),
152 Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
153 Color (0,0,0, 128), LAYER_GUI-1);
155 context.push_transform();
156 context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
157 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
159 if(i->pos.y < row*buttons_size.y ||
160 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
163 i->draw(context, i->id == button_selected ? true : false);
165 context.pop_transform();
168 bool ButtonGroup::event(SDL_Event &event)
170 bool caught_event = false;
174 case SDL_MOUSEMOTION:
177 if(mouse_left_button)
179 pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/screen->w);
180 pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/screen->h);
183 if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
184 event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
187 case SDL_MOUSEBUTTONDOWN:
188 if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
189 buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
190 event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
195 if(event.button.button == SDL_BUTTON_WHEELUP)
201 else if(event.button.button == SDL_BUTTON_WHEELDOWN)
204 if(row > (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
205 ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0))
206 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);
209 else if(event.button.button == SDL_BUTTON_LEFT)
210 mouse_left_button = true;
212 caught_event = false;
214 case SDL_MOUSEBUTTONUP:
215 mouse_left_button = false;
224 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
226 if(i->pos.y < row*buttons_size.y ||
227 i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
230 if(i->event(event, (int)pos.x,
231 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
233 button_selected = i->id;
242 int ButtonGroup::selected_id()
244 return button_selected;
247 void ButtonGroup::set_unselected()
249 button_selected = -1;
252 bool ButtonGroup::is_hover()