Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / gui / button.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "gui/button.hpp"
18
19 #include "supertux/main.hpp"
20 #include "video/drawing_context.hpp"
21
22 Font* Button::info_font = 0;
23
24 Button::Button(Surface* image_, std::string info_, SDLKey binding_) :
25   pos(),
26   size(),
27   image(),
28   binding(binding_),
29   id(),
30   state(),
31   info()
32 {
33   image = image_;
34   size = Vector(image->get_width(), image->get_height());
35   id = 0;
36   info = info_;
37 }
38
39 Button::~Button()
40 {
41 }
42
43 void Button::draw(DrawingContext &context, bool selected)
44 {
45   if(selected)
46     context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
47   else
48     context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
49
50   Vector tanslation = -context.get_translation();
51   if(state == BT_SHOW_INFO)
52   {
53     Vector offset;
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);
58     else
59       offset = Vector(-30, -size.y/2);
60     context.draw_text(info_font, info, pos + offset, ALIGN_LEFT, LAYER_GUI+2);
61     if(binding != 0)
62       context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
63                         ")", pos + offset + Vector(0,12),
64                         ALIGN_LEFT,  LAYER_GUI+2);
65   }
66
67   context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
68 }
69
70 int Button::event(SDL_Event &event, int x_offset, int y_offset)
71 {
72   state = BT_NONE;
73   switch(event.type)
74   {
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)
78       {
79         if(event.button.button == SDL_BUTTON_RIGHT)
80           state = BT_SHOW_INFO;
81       }
82       break;
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)
86       {
87         if(event.button.button == SDL_BUTTON_LEFT)
88           state = BT_SELECTED;
89       }
90       break;
91     case SDL_KEYDOWN:        // key pressed
92       if(event.key.keysym.sym == binding)
93         state = BT_SELECTED;
94       break;
95     default:
96       break;
97   }
98   return state;
99 }
100
101 /* EOF */