148ac84a603be9ae743ddd1b3abe8ca10f442f24
[supertux.git] / src / gui / button.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <SDL.h>
23 #include <iostream>
24
25 #include "main.hpp"
26 #include "button.hpp"
27 #include "mousecursor.hpp"
28 #include "video/font.hpp"
29 #include "video/surface.hpp"
30
31 Font* Button::info_font = 0;
32 extern SDL_Surface* screen;
33
34 /* Buttons */
35
36 Button::Button(Surface* image_, std::string info_, SDLKey binding_)
37   : binding(binding_)
38 {
39   image = image_;
40   size = Vector(image->get_width(), image->get_height());
41   id = 0;
42   info = info_;
43 }
44
45 Button::~Button()
46 {
47 }
48
49 void Button::draw(DrawingContext &context, bool selected)
50 {
51 if(selected)
52   context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
53 else
54   context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
55
56 Vector tanslation = -context.get_translation();
57 if(state == BT_SHOW_INFO)
58   {
59   Vector offset;
60   if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
61     offset = Vector(size.x, - 10);
62   else if(pos.x + tanslation.x < 100)
63     offset = Vector(size.x, 0);
64   else 
65     offset = Vector(-30, -size.y/2);
66   context.draw_text(info_font, info, pos + offset, LEFT_ALLIGN, LAYER_GUI+2);
67   if(binding != 0)
68     context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
69                                  ")", pos + offset + Vector(0,12),
70                                  LEFT_ALLIGN,  LAYER_GUI+2);
71   }
72
73 context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
74 }
75
76 int Button::event(SDL_Event &event, int x_offset, int y_offset)
77 {
78 state = BT_NONE;
79 switch(event.type)
80   {
81   case SDL_MOUSEBUTTONDOWN:
82     if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
83        event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
84       {
85       if(event.button.button == SDL_BUTTON_RIGHT)
86         state = BT_SHOW_INFO;
87       }
88     break;
89   case SDL_MOUSEBUTTONUP:
90     if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
91        event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
92       {
93       if(event.button.button == SDL_BUTTON_LEFT)
94         state = BT_SELECTED;
95       }
96     break;
97   case SDL_KEYDOWN:     // key pressed
98     if(event.key.keysym.sym == binding)
99       state = BT_SELECTED;
100     break;
101   default:
102     break;
103   }
104 return state;
105 }
106
107 /* Group of buttons */
108
109 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_)
110   : pos(pos_), buttons_size(buttons_size_), buttons_box(buttons_box_)
111 {
112 buttons.clear();
113 row = 0;
114 button_selected = -1;
115 mouse_hover = false;
116 mouse_left_button = false;
117 buttons_pair_nb = 0;
118 }
119
120 ButtonGroup::~ButtonGroup()
121 {
122 }
123
124 void ButtonGroup::add_button(Button button, int id, bool select)
125 {
126 button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
127 button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
128 button.size = buttons_size;
129 button.id = id;
130 if(select)
131   button_selected = id;
132
133 buttons.push_back(button);
134 }
135
136 void ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
137 {
138 button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
139 button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
140 button1.size.x = button2.size.x = buttons_size.x;
141 button1.size.y = button2.size.y = buttons_size.y / 2;
142 button2.pos.y += buttons_size.y / 2;
143 button1.id = id1;
144 button2.id = id2;
145
146 buttons_pair_nb++;
147 buttons.push_back(button1);
148 buttons.push_back(button2);
149 }
150
151 void ButtonGroup::draw(DrawingContext &context)
152 {
153 context.draw_filled_rect(pos - Vector(12,4),
154         Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
155         Color (0,0,0, 128), LAYER_GUI-1);
156
157 context.push_transform();
158 context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
159 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
160   {
161   if(i->pos.y < row*buttons_size.y ||
162       i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
163     continue;
164
165   i->draw(context, i->id == button_selected ? true : false);
166   }
167 context.pop_transform();
168 }
169
170 bool ButtonGroup::event(SDL_Event &event)
171 {
172 bool caught_event = false;
173
174 switch(event.type)
175   {
176   case SDL_MOUSEMOTION:
177     mouse_hover = false;
178
179     if(mouse_left_button)
180       {
181       pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/screen->w); 
182       pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/screen->h);
183       caught_event = true;
184       }
185     if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
186        event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
187       mouse_hover = true;
188     break;
189   case SDL_MOUSEBUTTONDOWN:
190     if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
191         buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
192         event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
193       break;
194
195     caught_event = true;
196
197     if(event.button.button == SDL_BUTTON_WHEELUP)
198       {
199       row--;
200       if(row < 0)
201         row = 0;
202       }
203     else if(event.button.button == SDL_BUTTON_WHEELDOWN)
204       {
205       row++;
206       if(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))
208         row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
209               ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0);
210       }
211     else if(event.button.button == SDL_BUTTON_LEFT)
212       mouse_left_button = true;
213     else
214       caught_event = false;
215     break;
216   case SDL_MOUSEBUTTONUP:
217     mouse_left_button = false;
218     break;
219   default:
220     break;
221   }
222
223 if(caught_event)
224   return true;
225
226 for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
227   {
228   if(i->pos.y < row*buttons_size.y ||
229       i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
230     continue;
231
232   if(i->event(event, (int)pos.x,
233                      (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
234     {
235     button_selected = i->id;
236     caught_event = true;
237     break;
238     }
239   }
240
241 return caught_event;
242 }
243
244 int ButtonGroup::selected_id()
245 {
246 return button_selected;
247 }
248
249 void ButtonGroup::set_unselected()
250 {
251 button_selected = -1;
252 }
253
254 bool ButtonGroup::is_hover()
255 {
256 return mouse_hover;
257 }