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