2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "gui/button.hpp"
19 #include "supertux/main.hpp"
20 #include "video/drawing_context.hpp"
22 Font* Button::info_font = 0;
24 Button::Button(Surface* image_, std::string info_, SDLKey binding_) :
34 size = Vector(image->get_width(), image->get_height());
43 void Button::draw(DrawingContext &context, bool selected)
46 context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
48 context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
50 Vector tanslation = -context.get_translation();
51 if(state == BT_SHOW_INFO)
54 if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
55 offset = Vector(size.x, - 10);
56 else if(pos.x + tanslation.x < 100)
57 offset = Vector(size.x, 0);
59 offset = Vector(-30, -size.y/2);
60 context.draw_text(info_font, info, pos + offset, ALIGN_LEFT, LAYER_GUI+2);
62 context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
63 ")", pos + offset + Vector(0,12),
64 ALIGN_LEFT, LAYER_GUI+2);
67 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
70 int Button::event(SDL_Event &event, int x_offset, int y_offset)
75 case SDL_MOUSEBUTTONDOWN:
76 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
77 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
79 if(event.button.button == SDL_BUTTON_RIGHT)
83 case SDL_MOUSEBUTTONUP:
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_LEFT)
91 case SDL_KEYDOWN: // key pressed
92 if(event.key.keysym.sym == binding)