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
28 Timer Button::popup_timer;
30 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
32 popup_timer.init(false);
34 add_icon(icon_file,mw,mh);
50 void Button::add_icon(std::string icon_file, int mw, int mh)
54 if(!icon_file.empty())
56 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
57 if(!faccessible(filename))
58 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
62 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
65 if(mw != -1 || mh != -1)
67 icon.push_back(new Surface(filename,USE_ALPHA));
68 icon.back()->resize(mw,mh);
71 icon.push_back(new Surface(filename,USE_ALPHA));
77 if(state == BUTTON_HOVER)
78 if(!popup_timer.check())
81 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
82 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
84 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
85 (*it)->draw(rect.x,rect.y);
87 if(game_object != NULL)
89 game_object->draw_on_screen(rect.x,rect.y);
97 if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
98 i = rect.w + strlen(info.c_str()) * white_small_text->w;
101 white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
102 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
103 white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
105 if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
106 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
107 else if(state == BUTTON_HOVER)
108 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
113 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
119 void Button::event(SDL_Event &event)
121 if(state == BUTTON_DEACTIVE)
124 SDLKey key = event.key.keysym.sym;
126 if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
128 if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
129 event.button.y < rect.y || event.button.y >= rect.y + rect.h)
132 if(event.button.button == SDL_BUTTON_RIGHT)
137 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
139 state = BUTTON_WHEELUP;
142 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
144 state = BUTTON_WHEELDOWN;
148 if(event.button.button == SDL_BUTTON_LEFT)
149 if(event.type == SDL_MOUSEBUTTONDOWN)
150 state = BUTTON_PRESSED;
152 state = BUTTON_CLICKED;
154 else if(event.type == SDL_MOUSEMOTION)
156 if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
157 event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
163 state = BUTTON_HOVER;
164 popup_timer.start(1500);
172 else if(event.type == SDL_KEYDOWN)
175 state = BUTTON_PRESSED;
177 else if(event.type == SDL_KEYUP)
179 if(state == BUTTON_PRESSED && key == shortcut)
180 state = BUTTON_CLICKED;
184 int Button::get_state()
191 case BUTTON_WHEELDOWN:
200 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
212 Button* ButtonPanel::event(SDL_Event& event)
217 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
220 if((*it)->state != BUTTON_NONE)
222 if(hlast && (*it)->state == BUTTON_CLICKED)
235 ButtonPanel::~ButtonPanel()
237 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
244 void ButtonPanel::draw()
249 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
250 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
253 if(hlast && it == last_clicked)
255 fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
261 void ButtonPanel::additem(Button* pbutton, int tag)
263 int max_cols, row, col;
265 item.push_back(pbutton);
267 /* A button_panel takes control of the buttons it contains and arranges them */
269 max_cols = rect.w / bw;
271 row = (item.size()-1) / max_cols;
272 col = (item.size()-1) % max_cols;
274 item[item.size()-1]->rect.x = rect.x + col * bw;
275 item[item.size()-1]->rect.y = rect.y + row * bh;
276 item[item.size()-1]->tag = tag;
280 void ButtonPanel::set_button_size(int w, int h)
286 Button* ButtonPanel::manipulate_button(int i)
288 if(int(item.size())-1 < i)
289 return item[item.size()-1];
294 void ButtonPanel::highlight_last(bool b)