1
2 /* Battle Tanks Game
3 * Copyright (C) 2006-2009 Battle Tanks team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Additional rights can be granted beyond the GNU General Public License
22 * on the terms provided in the Exception. If you modify this file,
23 * you may extend this exception to your version of the file,
24 * but you are not obligated to do so. If you do not wish to provide this
25 * exception without modification, you must delete this exception statement
26 * from your version and license this file solely under the GPL without exception.
27 */
28 #include "button.h"
29 #include "sdlx/font.h"
30 #include "resource_manager.h"
31
Button(const std::string & font,const std::string & label)32 Button::Button(const std::string &font, const std::string &label) : _font(ResourceManager->loadFont(font, true)), _label(label) {
33 _w = _font->render(NULL, 0, 0, label);
34 _background.init("menu/background_box.png", _w + 24, _font->get_height() + 8);
35 }
36
37
render(sdlx::Surface & surface,int x,int y) const38 void Button::render(sdlx::Surface& surface, int x, int y) const {
39 _background.render(surface, x, y);
40
41 _font->render(surface, x + (_background.w - _w) / 2, y + (_background.h - _font->get_height()) / 2, _label);
42 }
43
onMouse(const int button,const bool pressed,const int x,const int y)44 bool Button::onMouse(const int button, const bool pressed, const int x, const int y) {
45 if (pressed)
46 return true;
47 invalidate(true);
48 return true;
49 }
50
get_size(int & w,int & h) const51 void Button::get_size(int &w, int &h) const {
52 w = _background.w;
53 h = _background.h;
54 }
55
on_mouse_enter(bool enter)56 void Button::on_mouse_enter(bool enter) {
57 if (enter && _background.get_background() == "menu/background_box.png") {
58 _background.set_background("menu/background_box_dark.png");
59 } else if (!enter && _background.get_background() != "menu/background_box.png") {
60 _background.set_background("menu/background_box.png");
61 }
62 }
63