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
25 #include "screen/screen.h"
26 #include "screen/drawing_context.h"
31 Timer Button::popup_timer;
33 Button::Button(Surface* button_image, const std::string& ninfo,
34 SDLKey nshortcut, int x, int y, int mw, int mh)
36 popup_timer.init(false);
39 icon.push_back(button_image);
54 Button::Button(const std::string& imagefilename, const std::string& ninfo,
55 SDLKey nshortcut, int x, int y, int mw, int mh)
57 popup_timer.init(false);
59 add_icon(imagefilename, mw, mh);
74 void Button::add_icon(const std::string& icon_file, int mw, int mh)
78 if(!icon_file.empty())
80 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
81 if(!faccessible(filename))
82 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
86 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
89 if(mw != -1 || mh != -1)
91 icon.push_back(new Surface(filename,USE_ALPHA));
92 icon.back()->resize(mw,mh);
95 icon.push_back(new Surface(filename,USE_ALPHA));
99 void Button::draw(DrawingContext& context)
101 if(state == BUTTON_HOVER)
102 if(!popup_timer.check())
105 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
106 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
108 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
109 context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI);
114 viewport.set_translation(Vector(rect.x, rect.y));
115 drawable->draw(viewport, 0);
123 if(0 > rect.x - white_small_text->get_text_width(info))
124 i = rect.w + (int)white_small_text->get_text_width(info);
127 context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI);
128 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
129 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);
131 if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
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);
139 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
142 // FIXME TODO XXX: commenting this out fixes the leveleditor quit crash
143 // probably should be deleted somehow, though
147 void Button::event(SDL_Event &event)
149 if(state == BUTTON_DEACTIVE)
152 SDLKey key = event.key.keysym.sym;
154 if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
156 if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
157 event.button.y < rect.y || event.button.y >= rect.y + rect.h)
160 if(event.button.button == SDL_BUTTON_RIGHT)
165 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
167 state = BUTTON_WHEELUP;
170 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
172 state = BUTTON_WHEELDOWN;
176 if(event.button.button == SDL_BUTTON_LEFT)
177 if(event.type == SDL_MOUSEBUTTONDOWN)
178 state = BUTTON_PRESSED;
180 state = BUTTON_CLICKED;
182 else if(event.type == SDL_MOUSEMOTION)
184 if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
185 event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
191 state = BUTTON_HOVER;
192 popup_timer.start(1500);
200 else if(event.type == SDL_KEYDOWN)
203 state = BUTTON_PRESSED;
205 else if(event.type == SDL_KEYUP)
207 if(state == BUTTON_PRESSED && key == shortcut)
208 state = BUTTON_CLICKED;
212 int Button::get_state()
219 case BUTTON_WHEELDOWN:
228 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
240 Button* ButtonPanel::event(SDL_Event& event)
245 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
248 if((*it)->state != BUTTON_NONE)
250 if(hlast && (*it)->state == BUTTON_CLICKED)
263 ButtonPanel::~ButtonPanel()
265 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
272 void ButtonPanel::draw(DrawingContext& context)
277 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
278 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
280 (*it)->draw(context);
281 if(hlast && it == last_clicked)
283 fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
289 void ButtonPanel::additem(Button* pbutton, int tag)
291 int max_cols, row, col;
293 item.push_back(pbutton);
295 /* A button_panel takes control of the buttons it contains and arranges them */
297 max_cols = rect.w / bw;
299 row = (item.size()-1) / max_cols;
300 col = (item.size()-1) % max_cols;
302 item[item.size()-1]->rect.x = rect.x + col * bw;
303 item[item.size()-1]->rect.y = rect.y + row * bh;
304 item[item.size()-1]->tag = tag;
308 void ButtonPanel::set_button_size(int w, int h)
314 Button* ButtonPanel::manipulate_button(int i)
316 if(int(item.size())-1 < i)
317 return item[item.size()-1];
322 void ButtonPanel::highlight_last(bool b)