- memleak fix and menu fix from MatzeB
[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.h"
25 #include "globals.h"
26 #include "button.h"
27
28 Timer Button::popup_timer;
29
30 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
31 {
32   popup_timer.init(false);
33
34   char filename[1024];
35
36   if(!icon_file.empty())
37     {
38       snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
39       if(!faccessible(filename))
40         snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
41     }
42   else
43     {
44       snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
45     }
46
47   if(mw != -1 || mh != -1)
48     {
49       icon = new Surface(filename,USE_ALPHA);
50       if(mw != -1)
51         icon->w = mw;
52       if(mh != -1)
53         icon->h = mh;
54
55       SDL_Rect dest;
56       dest.x = 0;
57       dest.y = 0;
58       dest.w = icon->w;
59       dest.h = icon->h;
60       SDL_SoftStretch(icon->impl->get_sdl_surface(), NULL,
61           icon->impl->get_sdl_surface(), &dest);
62     }
63   else
64     icon = new Surface(filename,USE_ALPHA);
65
66   info = ninfo;
67
68   shortcut = nshortcut;
69
70   rect.x = x;
71   rect.y = y;
72   rect.w = icon->w;
73   rect.h = icon->h;
74   tag = -1;
75   state = BUTTON_NONE;
76   show_info = false;
77   bkgd = NULL;
78 }
79
80 void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
81 {
82   char filename[1024];
83
84   if(!icon_file.empty())
85     {
86       snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
87       if(!faccessible(filename))
88         snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
89     }
90   else
91     {
92       snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
93     }
94
95   delete icon;
96   icon = new Surface(filename,USE_ALPHA);
97 }
98
99 void Button::draw()
100 {
101   if(state == BUTTON_HOVER)
102     if(!popup_timer.check())
103      show_info = true;
104
105   fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
106   fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
107   if(bkgd != NULL)
108     {
109       bkgd->draw(rect.x,rect.y);
110     }
111   icon->draw(rect.x,rect.y);
112   if(show_info)
113     {
114       char str[80];
115       int i = -32;
116
117       if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
118         i = rect.w + strlen(info.c_str()) * white_small_text->w;
119
120       if(!info.empty())
121         white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
122       sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
123       white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
124     }
125   if(state == BUTTON_PRESSED)
126     fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
127   else if(state == BUTTON_HOVER)
128     fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
129 }
130
131 Button::~Button()
132 {
133   delete icon;
134 }
135
136 void Button::event(SDL_Event &event)
137 {
138   SDLKey key = event.key.keysym.sym;
139
140   if(event.motion.x > rect.x && event.motion.x < rect.x + rect.w &&
141       event.motion.y > rect.y && event.motion.y < rect.y + rect.h)
142     {
143       if(event.type == SDL_MOUSEBUTTONDOWN)
144         {
145           if(event.button.button == SDL_BUTTON_LEFT)
146             {
147               state = BUTTON_PRESSED;
148             }
149           else
150             {
151               show_info = true;
152             }
153         }
154       else if(event.type == SDL_MOUSEBUTTONUP)
155         {
156           if(event.button.button == SDL_BUTTON_LEFT && state == BUTTON_PRESSED)
157             {
158               state = BUTTON_CLICKED;
159             }
160           else if(event.button.button != SDL_BUTTON_LEFT && state != BUTTON_PRESSED)
161             {
162               show_info = true;
163             }
164         }
165
166       if(state != BUTTON_PRESSED && state != BUTTON_CLICKED)
167         {
168           state = BUTTON_HOVER;
169           mouse_cursor->set_state(MC_LINK);
170         }
171     }
172   else if((event.type != SDL_KEYDOWN && event.type != SDL_KEYUP) || event.type == SDL_MOUSEMOTION)
173     {
174       state = BUTTON_NONE;
175       if(show_info)
176         {
177           show_info = false;
178         }
179     }
180
181   if(event.type == SDL_KEYDOWN)
182     {
183       if(key == shortcut)
184         state = BUTTON_PRESSED;
185     }
186   else if(event.type == SDL_KEYUP)
187     {
188       if(state == BUTTON_PRESSED && key == shortcut)
189         state = BUTTON_CLICKED;
190     }
191   else if(event.type == SDL_MOUSEMOTION)
192     {
193       popup_timer.start(1500);
194     
195       if(show_info)
196         {
197           show_info = false;
198         }
199     }
200     
201 }
202
203 int Button::get_state()
204 {
205   int rstate;
206   if(state == BUTTON_CLICKED)
207     {
208       rstate = state;
209       state = BUTTON_NONE;
210       return rstate;
211     }
212   else
213     {
214       return state;
215     }
216 }
217
218 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
219
220   bw = 32;
221   bh = 32;
222   rect.x = x;
223   rect.y = y;
224   rect.w = w;
225   rect.h = h;
226   hidden = false;
227 }
228
229 Button* ButtonPanel::event(SDL_Event& event)
230 {
231   if(!hidden)
232     {
233       for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
234         {
235           (*it)->event(event);
236           if((*it)->state != BUTTON_NONE)
237             return (*it);
238         }
239       return NULL;
240     }
241   else
242     {
243       return NULL;
244     }
245 }
246
247 ButtonPanel::~ButtonPanel()
248 {
249   for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
250     {
251       delete (*it);
252     }
253   item.clear();
254 }
255
256 void ButtonPanel::draw()
257 {
258
259   if(hidden == false)
260     {
261       fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
262       for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
263         {
264           (*it)->draw();
265         }
266     }
267 }
268
269 void ButtonPanel::additem(Button* pbutton, int tag)
270 {
271   int max_cols, row, col;
272
273   item.push_back(pbutton);
274
275   /* A button_panel takes control of the buttons it contains and arranges them */
276
277   max_cols = rect.w / bw;
278
279   row = (item.size()-1) / max_cols;
280   col = (item.size()-1) % max_cols;
281
282   item[item.size()-1]->rect.x = rect.x + col * bw;
283   item[item.size()-1]->rect.y = rect.y + row * bh;
284   item[item.size()-1]->tag = tag;
285
286 }
287