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