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;
34 Button::Button(Surface* button_image, const std::string& ninfo,
35 SDLKey nshortcut, int x, int y, int mw, int mh)
37 popup_timer.init(false);
40 icon.push_back(button_image);
55 Button::Button(const std::string& imagefilename, const std::string& ninfo,
56 SDLKey nshortcut, int x, int y, int mw, int mh)
58 popup_timer.init(false);
60 add_icon(imagefilename, mw, mh);
75 void Button::add_icon(const std::string& icon_file, int mw, int mh)
79 if(!icon_file.empty())
81 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
82 if(!FileSystem::faccessible(filename))
83 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
87 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
90 if(mw != -1 || mh != -1)
92 icon.push_back(new Surface(filename,true));
93 icon.back()->resize(mw,mh);
96 icon.push_back(new Surface(filename,true));
100 void Button::draw(DrawingContext& context)
102 if(state == BUTTON_HOVER)
103 if(!popup_timer.check())
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);
109 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
110 context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI);
115 viewport.set_translation(Vector(rect.x, rect.y));
116 drawable->draw(viewport, 0);
124 if(0 > rect.x - white_small_text->get_text_width(info))
125 i = rect.w + (int)white_small_text->get_text_width(info);
128 context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI);
129 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
130 context.draw_text(white_small_text, str, Vector(i + rect.x - white_small_text->get_text_width(str), rect.y + white_small_text->get_height()+2), LAYER_GUI);
132 if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
133 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
134 else if(state == BUTTON_HOVER)
135 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
140 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
143 // FIXME TODO XXX: commenting this out fixes the leveleditor quit crash
144 // probably should be deleted somehow, though
148 void Button::event(SDL_Event &event)
150 if(state == BUTTON_DEACTIVE)
153 SDLKey key = event.key.keysym.sym;
155 if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
157 if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
158 event.button.y < rect.y || event.button.y >= rect.y + rect.h)
161 if(event.button.button == SDL_BUTTON_RIGHT)
166 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
168 state = BUTTON_WHEELUP;
171 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
173 state = BUTTON_WHEELDOWN;
177 if(event.button.button == SDL_BUTTON_LEFT)
178 if(event.type == SDL_MOUSEBUTTONDOWN)
179 state = BUTTON_PRESSED;
181 state = BUTTON_CLICKED;
183 else if(event.type == SDL_MOUSEMOTION)
185 if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
186 event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
192 state = BUTTON_HOVER;
193 popup_timer.start(1500);
201 else if(event.type == SDL_KEYDOWN)
204 state = BUTTON_PRESSED;
206 else if(event.type == SDL_KEYUP)
208 if(state == BUTTON_PRESSED && key == shortcut)
209 state = BUTTON_CLICKED;
213 int Button::get_state()
220 case BUTTON_WHEELDOWN:
229 ButtonPanel::ButtonPanel(const SDL_Rect& rect)
231 ButtonPanel(rect.x, rect.y, rect.w, rect.h);
234 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
246 Button* ButtonPanel::event(SDL_Event& event)
251 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
254 if((*it)->state != BUTTON_NONE)
256 if(hlast && (*it)->state == BUTTON_CLICKED)
269 ButtonPanel::~ButtonPanel()
271 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
278 void ButtonPanel::draw(DrawingContext& context)
283 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
284 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
286 (*it)->draw(context);
287 if(hlast && it == last_clicked)
289 fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
295 void ButtonPanel::additem(Button* pbutton, int tag)
297 int max_cols, row, col;
299 item.push_back(pbutton);
301 /* A button_panel takes control of the buttons it contains and arranges them */
303 max_cols = rect.w / bw;
305 row = (item.size()-1) / max_cols;
306 col = (item.size()-1) % max_cols;
308 item[item.size()-1]->rect.x = rect.x + col * bw;
309 item[item.size()-1]->rect.y = rect.y + row * bh;
310 item[item.size()-1]->tag = tag;
314 void ButtonPanel::set_button_size(int w, int h)
320 Button* ButtonPanel::manipulate_button(int i)
322 if(int(item.size())-1 < i)
323 return item[item.size()-1];
328 void ButtonPanel::highlight_last(bool b)
333 void ButtonPanel::set_last_clicked(unsigned int last)
337 if(item.size() >= last)
339 last_clicked = item.begin() + last;