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