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/globals.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());
39 Button::Button(const Button& rhs) :
55 Button::operator=(const Button& rhs)
62 binding = rhs.binding;
70 void Button::draw(DrawingContext &context, bool selected)
73 context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
75 context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
77 Vector tanslation = -context.get_translation();
78 if(state == BT_SHOW_INFO)
81 if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
82 offset = Vector(size.x, - 10);
83 else if(pos.x + tanslation.x < 100)
84 offset = Vector(size.x, 0);
86 offset = Vector(-30, -size.y/2);
87 context.draw_text(info_font, info, pos + offset, ALIGN_LEFT, LAYER_GUI+2);
89 context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
90 ")", pos + offset + Vector(0,12),
91 ALIGN_LEFT, LAYER_GUI+2);
94 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
97 int Button::event(SDL_Event &event, int x_offset, int y_offset)
102 case SDL_MOUSEBUTTONDOWN:
103 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
104 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
106 if(event.button.button == SDL_BUTTON_RIGHT)
107 state = BT_SHOW_INFO;
110 case SDL_MOUSEBUTTONUP:
111 if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
112 event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
114 if(event.button.button == SDL_BUTTON_LEFT)
118 case SDL_KEYDOWN: // key pressed
119 if(event.key.keysym.sym == binding)