Made the SuperTux library again a bit more selfstanding.
[supertux.git] / lib / gui / button.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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 <cstring>
22 #include <cstdlib>
23
24 #include "../app/setup.h"
25 #include "../video/screen.h"
26 #include "../video/drawing_context.h"
27 #include "../app/globals.h"
28 #include "../gui/button.h"
29
30 using namespace SuperTux;
31
32 Timer Button::popup_timer;
33 Font* Button::info_font = 0;
34
35 Button::Button(Surface* button_image, const std::string& ninfo,
36                SDLKey nshortcut, int x, int y, int mw, int mh)
37 {
38   popup_timer.init(false);
39
40   if(button_image)
41     icon.push_back(button_image);
42
43   info = ninfo;
44
45   shortcut = nshortcut;
46
47   rect.x = x;
48   rect.y = y;
49   rect.w = icon[0]->w;
50   rect.h = icon[0]->h;
51   tag = -1;
52   state = BUTTON_NONE;
53   show_info = false;
54 }
55
56 Button::Button(const std::string& imagefilename, const std::string& ninfo,
57                SDLKey nshortcut, int x, int y, int mw, int mh)
58 {
59   popup_timer.init(false);
60
61   add_icon(imagefilename, mw, mh);
62
63   info = ninfo;
64
65   shortcut = nshortcut;
66
67   rect.x = x;
68   rect.y = y;
69   rect.w = icon[0]->w;
70   rect.h = icon[0]->h;
71   tag = -1;
72   state = BUTTON_NONE;
73   show_info = false;
74 }
75
76 void Button::add_icon(const std::string& icon_file, int mw, int mh)
77 {
78   char filename[1024];
79
80   if(!icon_file.empty())
81     {
82       snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
83       if(!FileSystem::faccessible(filename))
84         snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
85     }
86   else
87     {
88       snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
89     }
90
91   if(mw != -1 || mh != -1)
92     {
93       icon.push_back(new Surface(filename,true));
94       icon.back()->resize(mw,mh);
95     }
96   else
97     icon.push_back(new Surface(filename,true));
98
99 }
100
101 void Button::draw(DrawingContext& context)
102 {
103   if(state == BUTTON_HOVER)
104     if(!popup_timer.check())
105       show_info = true;
106
107   fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
108   fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
109
110   for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
111     context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI);
112
113   /*  if(drawable)
114     {
115       Camera viewport;
116       viewport.set_translation(Vector(rect.x, rect.y));
117       drawable->draw(viewport, 0);
118     }*/
119
120   if(show_info)
121     {
122       char str[80];
123       int i = -32;
124
125       if(0 > rect.x - info_font->get_text_width(info))
126         i = rect.w + (int)info_font->get_text_width(info);
127
128       if(!info.empty())
129         context.draw_text(info_font, info, Vector(i + rect.x - info_font->get_text_width(info), rect.y), LAYER_GUI);
130       sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
131       context.draw_text(info_font, str, Vector(i + rect.x -  info_font->get_text_width(str), rect.y + info_font->get_height()+2), LAYER_GUI);
132     }
133   if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
134     fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
135   else if(state == BUTTON_HOVER)
136     fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
137 }
138
139 Button::~Button()
140 {
141   for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
142     delete (*it);
143   icon.clear();
144   // FIXME TODO XXX: commenting this out fixes the leveleditor quit crash
145   //   probably should be deleted somehow, though
146   //delete drawable;
147 }
148
149 void Button::event(SDL_Event &event)
150 {
151   if(state == BUTTON_DEACTIVE)
152     return;
153
154   SDLKey key = event.key.keysym.sym;
155
156   if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
157     {
158       if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
159           event.button.y < rect.y || event.button.y >= rect.y + rect.h)
160         return;
161
162       if(event.button.button == SDL_BUTTON_RIGHT)
163         {
164           show_info = true;
165           return;
166         }
167       else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
168         {
169           state = BUTTON_WHEELUP;
170           return;
171         }
172       else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
173         {
174           state = BUTTON_WHEELDOWN;
175           return;
176         }
177
178       if(event.button.button == SDL_BUTTON_LEFT)
179         if(event.type == SDL_MOUSEBUTTONDOWN)
180           state = BUTTON_PRESSED;
181         else
182           state = BUTTON_CLICKED;
183     }
184   else if(event.type == SDL_MOUSEMOTION)
185     {
186       if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
187           event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
188         {
189           state = BUTTON_NONE;
190         }
191       else
192         {
193           state = BUTTON_HOVER;
194           popup_timer.start(1500);
195         }
196
197       if(show_info)
198         {
199           show_info = false;
200         }
201     }
202   else if(event.type == SDL_KEYDOWN)
203     {
204       if(key == shortcut)
205         state = BUTTON_PRESSED;
206     }
207   else if(event.type == SDL_KEYUP)
208     {
209       if(state == BUTTON_PRESSED && key == shortcut)
210         state = BUTTON_CLICKED;
211     }
212 }
213
214 int Button::get_state()
215 {
216   int rstate;
217   switch(state)
218     {
219     case BUTTON_CLICKED:
220     case BUTTON_WHEELUP:
221     case BUTTON_WHEELDOWN:
222       rstate = state;
223       state = BUTTON_NONE;
224       return rstate;
225     default:
226       return state;
227     }
228 }
229
230 ButtonPanel::ButtonPanel(const SDL_Rect& rect)
231 {
232   ButtonPanel(rect.x, rect.y, rect.w, rect.h);
233 }
234
235 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
236 {
237   bw = 32;
238   bh = 32;
239   rect.x = x;
240   rect.y = y;
241   rect.w = w;
242   rect.h = h;
243   hidden = false;
244   hlast = false;
245 }
246
247 Button* ButtonPanel::event(SDL_Event& event)
248 {
249   if(!hidden)
250     {
251       Button* ret = NULL;
252       for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
253         {
254           (*it)->event(event);
255           if((*it)->state != BUTTON_NONE)
256             {
257               if(hlast && (*it)->state == BUTTON_CLICKED)
258                 last_clicked = it;
259               ret = (*it);
260             }
261         }
262       return ret;
263     }
264   else
265     {
266       return NULL;
267     }
268 }
269
270 ButtonPanel::~ButtonPanel()
271 {
272   for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
273     {
274       delete (*it);
275     }
276   item.clear();
277 }
278
279 void ButtonPanel::draw(DrawingContext& context)
280 {
281
282   if(hidden == false)
283     {
284       fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
285       for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
286         {
287           (*it)->draw(context);
288           if(hlast && it == last_clicked)
289             {
290               fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
291             }
292         }
293     }
294 }
295
296 void ButtonPanel::additem(Button* pbutton, int tag)
297 {
298   int max_cols, row, col;
299
300   item.push_back(pbutton);
301
302   /* A button_panel takes control of the buttons it contains and arranges them */
303
304   max_cols = rect.w / bw;
305
306   row = (item.size()-1) / max_cols;
307   col = (item.size()-1) % max_cols;
308
309   item[item.size()-1]->rect.x = rect.x + col * bw;
310   item[item.size()-1]->rect.y = rect.y + row * bh;
311   item[item.size()-1]->tag = tag;
312
313 }
314
315 void ButtonPanel::set_button_size(int w, int h)
316 {
317   bw = w;
318   bh = h;
319 }
320
321 Button* ButtonPanel::manipulate_button(int i)
322 {
323   if(int(item.size())-1 < i)
324     return item[item.size()-1];
325   else
326     return item[i];
327 }
328
329 void ButtonPanel::highlight_last(bool b)
330 {
331   hlast = b;
332 }
333
334 void ButtonPanel::set_last_clicked(unsigned int last)
335 {
336   if(hlast)
337     {
338       if(item.size() >= last)
339         {
340           last_clicked = item.begin() + last;
341         }
342     }
343 }