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 "screen/screen.h"
25 #include "screen/drawing_context.h"
30 Timer Button::popup_timer;
32 Button::Button(Surface* button_image, const std::string& ninfo,
33 SDLKey nshortcut, int x, int y, int mw, int mh)
35 popup_timer.init(false);
38 icon.push_back(button_image);
53 Button::Button(const std::string& imagefilename, const std::string& ninfo,
54 SDLKey nshortcut, int x, int y, int mw, int mh)
56 popup_timer.init(false);
58 add_icon(imagefilename, mw, mh);
73 void Button::add_icon(const std::string& icon_file, int mw, int mh)
77 if(!icon_file.empty())
79 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
80 if(!faccessible(filename))
81 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
85 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
88 if(mw != -1 || mh != -1)
90 icon.push_back(new Surface(filename,USE_ALPHA));
91 icon.back()->resize(mw,mh);
94 icon.push_back(new Surface(filename,USE_ALPHA));
98 void Button::draw(DrawingContext& context)
100 if(state == BUTTON_HOVER)
101 if(!popup_timer.check())
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);
107 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
108 context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI);
113 viewport.set_translation(Vector(rect.x, rect.y));
114 drawable->draw(viewport, 0);
122 if(0 > rect.x - white_small_text->get_text_width(info))
123 i = rect.w + (int)white_small_text->get_text_width(info);
126 context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI);
127 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
128 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);
130 if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
131 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
132 else if(state == BUTTON_HOVER)
133 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
138 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
141 // FIXME TODO XXX: commenting this out fixes the leveleditor quit crash
142 // probably should be deleted somehow, though
146 void Button::event(SDL_Event &event)
148 if(state == BUTTON_DEACTIVE)
151 SDLKey key = event.key.keysym.sym;
153 if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
155 if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
156 event.button.y < rect.y || event.button.y >= rect.y + rect.h)
159 if(event.button.button == SDL_BUTTON_RIGHT)
164 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
166 state = BUTTON_WHEELUP;
169 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
171 state = BUTTON_WHEELDOWN;
175 if(event.button.button == SDL_BUTTON_LEFT)
176 if(event.type == SDL_MOUSEBUTTONDOWN)
177 state = BUTTON_PRESSED;
179 state = BUTTON_CLICKED;
181 else if(event.type == SDL_MOUSEMOTION)
183 if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
184 event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
190 state = BUTTON_HOVER;
191 popup_timer.start(1500);
199 else if(event.type == SDL_KEYDOWN)
202 state = BUTTON_PRESSED;
204 else if(event.type == SDL_KEYUP)
206 if(state == BUTTON_PRESSED && key == shortcut)
207 state = BUTTON_CLICKED;
211 int Button::get_state()
218 case BUTTON_WHEELDOWN:
227 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
239 Button* ButtonPanel::event(SDL_Event& event)
244 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
247 if((*it)->state != BUTTON_NONE)
249 if(hlast && (*it)->state == BUTTON_CLICKED)
262 ButtonPanel::~ButtonPanel()
264 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
271 void ButtonPanel::draw(DrawingContext& context)
276 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
277 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
279 (*it)->draw(context);
280 if(hlast && it == last_clicked)
282 fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
288 void ButtonPanel::additem(Button* pbutton, int tag)
290 int max_cols, row, col;
292 item.push_back(pbutton);
294 /* A button_panel takes control of the buttons it contains and arranges them */
296 max_cols = rect.w / bw;
298 row = (item.size()-1) / max_cols;
299 col = (item.size()-1) % max_cols;
301 item[item.size()-1]->rect.x = rect.x + col * bw;
302 item[item.size()-1]->rect.y = rect.y + row * bh;
303 item[item.size()-1]->tag = tag;
307 void ButtonPanel::set_button_size(int w, int h)
313 Button* ButtonPanel::manipulate_button(int i)
315 if(int(item.size())-1 < i)
316 return item[item.size()-1];
321 void ButtonPanel::highlight_last(bool b)