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