Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / gui / button_group.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_group.hpp"
18
19 #include "supertux/main.hpp"
20 #include "video/drawing_context.hpp"
21
22 extern SDL_Surface* g_screen;
23
24 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_) :
25   pos(pos_), 
26   buttons_size(buttons_size_), 
27   buttons_box(buttons_box_)
28 {
29   buttons.clear();
30   row = 0;
31   button_selected = -1;
32   mouse_hover = false;
33   mouse_left_button = false;
34   buttons_pair_nb = 0;
35 }
36
37 ButtonGroup::~ButtonGroup()
38 {
39 }
40
41 void
42 ButtonGroup::add_button(Button button, int id, bool select)
43 {
44   button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
45   button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
46   button.size = buttons_size;
47   button.id = id;
48   if(select)
49     button_selected = id;
50
51   buttons.push_back(button);
52 }
53
54 void
55 ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
56 {
57   button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
58   button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
59   button1.size.x = button2.size.x = buttons_size.x;
60   button1.size.y = button2.size.y = buttons_size.y / 2;
61   button2.pos.y += buttons_size.y / 2;
62   button1.id = id1;
63   button2.id = id2;
64
65   buttons_pair_nb++;
66   buttons.push_back(button1);
67   buttons.push_back(button2);
68 }
69
70 void
71 ButtonGroup::draw(DrawingContext &context)
72 {
73   context.draw_filled_rect(pos - Vector(12,4),
74                            Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
75                            Color (0,0,0, 128), LAYER_GUI-1);
76
77   context.push_transform();
78   context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
79   for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
80   {
81     if(i->pos.y < row*buttons_size.y ||
82        i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
83       continue;
84
85     i->draw(context, i->id == button_selected);
86   }
87   context.pop_transform();
88 }
89
90 bool
91 ButtonGroup::event(SDL_Event &event)
92 {
93   bool caught_event = false;
94
95   switch(event.type)
96   {
97     case SDL_MOUSEMOTION:
98       mouse_hover = false;
99
100       if(mouse_left_button)
101       {
102         pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/g_screen->w);
103         pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/g_screen->h);
104         caught_event = true;
105       }
106       if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
107          event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
108         mouse_hover = true;
109       break;
110     case SDL_MOUSEBUTTONDOWN:
111       if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
112          buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
113          event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
114         break;
115
116       caught_event = true;
117
118       if(event.button.button == SDL_BUTTON_WHEELUP)
119       {
120         row--;
121         if(row < 0)
122           row = 0;
123       }
124       else if(event.button.button == SDL_BUTTON_WHEELDOWN)
125       {
126         row++;
127         if(row > (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
128            ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0))
129           row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
130             ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0);
131       }
132       else if(event.button.button == SDL_BUTTON_LEFT)
133         mouse_left_button = true;
134       else
135         caught_event = false;
136       break;
137     case SDL_MOUSEBUTTONUP:
138       mouse_left_button = false;
139       break;
140     default:
141       break;
142   }
143
144   if(caught_event)
145     return true;
146
147   for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
148   {
149     if(i->pos.y < row*buttons_size.y ||
150        i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
151       continue;
152
153     if(i->event(event, (int)pos.x,
154                 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
155     {
156       button_selected = i->id;
157       caught_event = true;
158       break;
159     }
160   }
161
162   return caught_event;
163 }
164
165 int
166 ButtonGroup::selected_id()
167 {
168   return button_selected;
169 }
170
171 void
172 ButtonGroup::set_unselected()
173 {
174   button_selected = -1;
175 }
176
177 bool
178 ButtonGroup::is_hover()
179 {
180   return mouse_hover;
181 }
182
183 /* EOF */