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