4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
24 #include "../app/setup.h"
25 #include "../video/screen.h"
26 #include "../video/drawing_context.h"
27 #include "../app/globals.h"
28 #include "../gui/button.h"
30 using namespace SuperTux;
32 Timer Button::popup_timer;
33 Font* Button::info_font = 0;
35 Button::Button(Surface* button_image, const std::string& ninfo,
36 SDLKey nshortcut, int x, int y, int mw, int mh)
38 popup_timer.init(false);
41 icon.push_back(button_image);
56 Button::Button(const std::string& imagefilename, const std::string& ninfo,
57 SDLKey nshortcut, int x, int y, int mw, int mh)
59 popup_timer.init(false);
61 add_icon(imagefilename, mw, mh);
76 void Button::add_icon(const std::string& icon_file, int mw, int mh)
80 if(!icon_file.empty())
82 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
83 if(!FileSystem::faccessible(filename))
84 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
88 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
91 if(mw != -1 || mh != -1)
93 icon.push_back(new Surface(filename,true));
94 icon.back()->resize(mw,mh);
97 icon.push_back(new Surface(filename,true));
101 void Button::draw(DrawingContext& context)
103 if(state == BUTTON_HOVER)
104 if(!popup_timer.check())
107 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
108 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
110 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
111 context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI);
116 viewport.set_translation(Vector(rect.x, rect.y));
117 drawable->draw(viewport, 0);
125 if(0 > rect.x - info_font->get_text_width(info))
126 i = rect.w + (int)info_font->get_text_width(info);
129 context.draw_text(info_font, info, Vector(i + rect.x - info_font->get_text_width(info), rect.y), LAYER_GUI);
130 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
131 context.draw_text(info_font, str, Vector(i + rect.x - info_font->get_text_width(str), rect.y + info_font->get_height()+2), LAYER_GUI);
133 if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
134 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
135 else if(state == BUTTON_HOVER)
136 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
141 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
144 // FIXME TODO XXX: commenting this out fixes the leveleditor quit crash
145 // probably should be deleted somehow, though
149 void Button::event(SDL_Event &event)
151 if(state == BUTTON_DEACTIVE)
154 SDLKey key = event.key.keysym.sym;
156 if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
158 if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
159 event.button.y < rect.y || event.button.y >= rect.y + rect.h)
162 if(event.button.button == SDL_BUTTON_RIGHT)
167 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
169 state = BUTTON_WHEELUP;
172 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
174 state = BUTTON_WHEELDOWN;
178 if(event.button.button == SDL_BUTTON_LEFT)
179 if(event.type == SDL_MOUSEBUTTONDOWN)
180 state = BUTTON_PRESSED;
182 state = BUTTON_CLICKED;
184 else if(event.type == SDL_MOUSEMOTION)
186 if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
187 event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
193 state = BUTTON_HOVER;
194 popup_timer.start(1500);
202 else if(event.type == SDL_KEYDOWN)
205 state = BUTTON_PRESSED;
207 else if(event.type == SDL_KEYUP)
209 if(state == BUTTON_PRESSED && key == shortcut)
210 state = BUTTON_CLICKED;
214 int Button::get_state()
221 case BUTTON_WHEELDOWN:
230 ButtonPanel::ButtonPanel(const SDL_Rect& rect)
232 ButtonPanel(rect.x, rect.y, rect.w, rect.h);
235 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
247 Button* ButtonPanel::event(SDL_Event& event)
252 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
255 if((*it)->state != BUTTON_NONE)
257 if(hlast && (*it)->state == BUTTON_CLICKED)
270 ButtonPanel::~ButtonPanel()
272 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
279 void ButtonPanel::draw(DrawingContext& context)
284 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
285 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
287 (*it)->draw(context);
288 if(hlast && it == last_clicked)
290 fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
296 void ButtonPanel::additem(Button* pbutton, int tag)
298 int max_cols, row, col;
300 item.push_back(pbutton);
302 /* A button_panel takes control of the buttons it contains and arranges them */
304 max_cols = rect.w / bw;
306 row = (item.size()-1) / max_cols;
307 col = (item.size()-1) % max_cols;
309 item[item.size()-1]->rect.x = rect.x + col * bw;
310 item[item.size()-1]->rect.y = rect.y + row * bh;
311 item[item.size()-1]->tag = tag;
315 void ButtonPanel::set_button_size(int w, int h)
321 Button* ButtonPanel::manipulate_button(int i)
323 if(int(item.size())-1 < i)
324 return item[item.size()-1];
329 void ButtonPanel::highlight_last(bool b)
334 void ButtonPanel::set_last_clicked(unsigned int last)
338 if(item.size() >= last)
340 last_clicked = item.begin() + last;