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