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