src/gui/button_group.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "gui/button_group.hpp"
00018 
00019 #include "supertux/globals.hpp"
00020 #include "video/drawing_context.hpp"
00021 
00022 ButtonGroup::ButtonGroup(Vector pos_, Vector buttons_size_, Vector buttons_box_) :
00023   pos(pos_), 
00024   buttons_size(buttons_size_), 
00025   buttons_box(buttons_box_),
00026   buttons(),
00027   button_selected(),
00028   row(),
00029   mouse_hover(),
00030   mouse_left_button(),
00031   buttons_pair_nb()
00032 {
00033   buttons.clear();
00034   row = 0;
00035   button_selected = -1;
00036   mouse_hover = false;
00037   mouse_left_button = false;
00038   buttons_pair_nb = 0;
00039 }
00040 
00041 ButtonGroup::~ButtonGroup()
00042 {
00043 }
00044 
00045 void
00046 ButtonGroup::add_button(Button button, int id, bool select)
00047 {
00048   button.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
00049   button.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
00050   button.size = buttons_size;
00051   button.id = id;
00052   if(select)
00053     button_selected = id;
00054 
00055   buttons.push_back(button);
00056 }
00057 
00058 void
00059 ButtonGroup::add_pair_of_buttons(Button button1, int id1, Button button2, int id2)
00060 {
00061   button1.pos.x = button2.pos.x = ((buttons.size()-buttons_pair_nb) % (int)buttons_box.x) * buttons_size.x;
00062   button1.pos.y = button2.pos.y = ((int)((buttons.size()-buttons_pair_nb) / buttons_box.x)) * buttons_size.y;
00063   button1.size.x = button2.size.x = buttons_size.x;
00064   button1.size.y = button2.size.y = buttons_size.y / 2;
00065   button2.pos.y += buttons_size.y / 2;
00066   button1.id = id1;
00067   button2.id = id2;
00068 
00069   buttons_pair_nb++;
00070   buttons.push_back(button1);
00071   buttons.push_back(button2);
00072 }
00073 
00074 void
00075 ButtonGroup::draw(DrawingContext &context)
00076 {
00077   context.draw_filled_rect(pos - Vector(12,4),
00078                            Vector(buttons_size.x*buttons_box.x + 16, buttons_size.y*buttons_box.y + 8),
00079                            Color (0,0,0, 128), LAYER_GUI-1);
00080 
00081   context.push_transform();
00082   context.set_translation(Vector(-pos.x, -pos.y + buttons_size.y*row));
00083   for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
00084   {
00085     if(i->pos.y < row*buttons_size.y ||
00086        i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
00087       continue;
00088 
00089     i->draw(context, i->id == button_selected);
00090   }
00091   context.pop_transform();
00092 }
00093 
00094 bool
00095 ButtonGroup::event(SDL_Event &event)
00096 {
00097   bool caught_event = false;
00098 
00099   switch(event.type)
00100   {
00101     case SDL_MOUSEMOTION:
00102       mouse_hover = false;
00103 
00104       if(mouse_left_button)
00105       {
00106         pos.x += int(event.motion.xrel * float(SCREEN_WIDTH)/g_screen->w);
00107         pos.y += int(event.motion.yrel * float(SCREEN_HEIGHT)/g_screen->h);
00108         caught_event = true;
00109       }
00110       if(event.button.x > pos.x-12 && event.button.x < pos.x+16 + buttons_box.x*buttons_size.x &&
00111          event.button.y > pos.y-4 && event.button.y < pos.y+8 + buttons_box.y*buttons_size.y)
00112         mouse_hover = true;
00113       break;
00114     case SDL_MOUSEBUTTONDOWN:
00115       if(event.button.x < pos.x-12 || event.button.x > pos.x+16 +
00116          buttons_box.x*buttons_size.x || event.button.y < pos.y-4 ||
00117          event.button.y > pos.y+8 + buttons_box.y*buttons_size.y)
00118         break;
00119 
00120       caught_event = true;
00121 
00122       if(event.button.button == SDL_BUTTON_WHEELUP)
00123       {
00124         row--;
00125         if(row < 0)
00126           row = 0;
00127       }
00128       else if(event.button.button == SDL_BUTTON_WHEELDOWN)
00129       {
00130         row++;
00131         if(row > (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
00132            ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0))
00133           row = (int)((buttons.size()-buttons_pair_nb)/buttons_box.x) - (int)buttons_box.y +
00134             ((int)(buttons.size()-buttons_pair_nb)%(int)buttons_box.x != 0 ? 1 : 0);
00135       }
00136       else if(event.button.button == SDL_BUTTON_LEFT)
00137         mouse_left_button = true;
00138       else
00139         caught_event = false;
00140       break;
00141     case SDL_MOUSEBUTTONUP:
00142       mouse_left_button = false;
00143       break;
00144     default:
00145       break;
00146   }
00147 
00148   if(caught_event)
00149     return true;
00150 
00151   for(Buttons::iterator i = buttons.begin(); i != buttons.end(); ++i)
00152   {
00153     if(i->pos.y < row*buttons_size.y ||
00154        i->pos.y + i->size.y > (row + buttons_box.y) * buttons_size.y)
00155       continue;
00156 
00157     if(i->event(event, (int)pos.x,
00158                 (int)pos.y - row*(int)buttons_size.y) == BT_SELECTED)
00159     {
00160       button_selected = i->id;
00161       caught_event = true;
00162       break;
00163     }
00164   }
00165 
00166   return caught_event;
00167 }
00168 
00169 int
00170 ButtonGroup::selected_id()
00171 {
00172   return button_selected;
00173 }
00174 
00175 void
00176 ButtonGroup::set_unselected()
00177 {
00178   button_selected = -1;
00179 }
00180 
00181 bool
00182 ButtonGroup::is_hover()
00183 {
00184   return mouse_hover;
00185 }
00186 
00187 /* EOF */

Generated on Mon Jun 9 03:38:18 2014 for SuperTux by  doxygen 1.5.1