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