* Revert breaking changes from last revision
[supertux.git] / src / gui / button.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "gui/button.hpp"
18
19 #include "supertux/globals.hpp"
20 #include "video/drawing_context.hpp"
21
22 FontPtr Button::info_font;
23
24 Button::Button(SurfacePtr image_, std::string info_, SDLKey binding_) :
25   pos(),
26   size(),
27   image(),
28   binding(binding_),
29   id(),
30   state(),
31   info()
32 {
33   image = image_;
34   size = Vector(image->get_width(), image->get_height());
35   id = 0;
36   info = info_;
37 }
38
39 Button::Button(const Button& rhs) :
40   pos(rhs.pos),
41   size(rhs.size),
42   image(rhs.image),
43   binding(rhs.binding),
44   id(rhs.id),
45   state(rhs.state),
46   info(rhs.info)
47 {
48 }
49
50 Button::~Button()
51 {
52 }
53
54 Button&
55 Button::operator=(const Button& rhs)
56 {
57   if (this != &rhs)
58   {
59     pos = rhs.pos;
60     size = rhs.size;
61     image = rhs.image;
62     binding = rhs.binding;
63     id = rhs.id;
64     state = rhs.state;
65     info = rhs.info;
66   }
67   return *this;
68 }
69
70 void Button::draw(DrawingContext &context, bool selected)
71 {
72   if(selected)
73     context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
74   else
75     context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
76
77   Vector tanslation = -context.get_translation();
78   if(state == BT_SHOW_INFO)
79   {
80     Vector offset;
81     if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
82       offset = Vector(size.x, - 10);
83     else if(pos.x + tanslation.x < 100)
84       offset = Vector(size.x, 0);
85     else
86       offset = Vector(-30, -size.y/2);
87     context.draw_text(info_font, info, pos + offset, ALIGN_LEFT, LAYER_GUI+2);
88     if(binding != 0)
89       context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
90                         ")", pos + offset + Vector(0,12),
91                         ALIGN_LEFT,  LAYER_GUI+2);
92   }
93
94   context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
95 }
96
97 int Button::event(SDL_Event &event, int x_offset, int y_offset)
98 {
99   state = BT_NONE;
100   switch(event.type)
101   {
102     case SDL_MOUSEBUTTONDOWN:
103       if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
104          event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
105       {
106         if(event.button.button == SDL_BUTTON_RIGHT)
107           state = BT_SHOW_INFO;
108       }
109       break;
110     case SDL_MOUSEBUTTONUP:
111       if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
112          event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
113       {
114         if(event.button.button == SDL_BUTTON_LEFT)
115           state = BT_SELECTED;
116       }
117       break;
118     case SDL_KEYDOWN:        // key pressed
119       if(event.key.keysym.sym == binding)
120         state = BT_SELECTED;
121       break;
122     default:
123       break;
124   }
125   return state;
126 }
127
128 /* EOF */