src/gui/button.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.hpp"
00018 
00019 #include "supertux/globals.hpp"
00020 #include "video/drawing_context.hpp"
00021 
00022 FontPtr Button::info_font;
00023 
00024 Button::Button(SurfacePtr image_, std::string info_, SDLKey binding_) :
00025   pos(),
00026   size(),
00027   image(),
00028   binding(binding_),
00029   id(),
00030   state(),
00031   info()
00032 {
00033   image = image_;
00034   size = Vector(image->get_width(), image->get_height());
00035   id = 0;
00036   info = info_;
00037 }
00038 
00039 Button::Button(const Button& rhs) :
00040   pos(rhs.pos),
00041   size(rhs.size),
00042   image(rhs.image),
00043   binding(rhs.binding),
00044   id(rhs.id),
00045   state(rhs.state),
00046   info(rhs.info)
00047 {
00048 }
00049 
00050 Button::~Button()
00051 {
00052 }
00053 
00054 Button&
00055 Button::operator=(const Button& rhs)
00056 {
00057   if (this != &rhs)
00058   {
00059     pos = rhs.pos;
00060     size = rhs.size;
00061     image = rhs.image;
00062     binding = rhs.binding;
00063     id = rhs.id;
00064     state = rhs.state;
00065     info = rhs.info;
00066   }
00067   return *this;
00068 }
00069 
00070 void Button::draw(DrawingContext &context, bool selected)
00071 {
00072   if(selected)
00073     context.draw_filled_rect(pos, size, Color (200,240,220), LAYER_GUI);
00074   else
00075     context.draw_filled_rect(pos, size, Color (200,200,220), LAYER_GUI);
00076 
00077   Vector tanslation = -context.get_translation();
00078   if(state == BT_SHOW_INFO)
00079   {
00080     Vector offset;
00081     if(pos.x + tanslation.x < 100 && pos.y + tanslation.y > SCREEN_HEIGHT - 20)
00082       offset = Vector(size.x, - 10);
00083     else if(pos.x + tanslation.x < 100)
00084       offset = Vector(size.x, 0);
00085     else
00086       offset = Vector(-30, -size.y/2);
00087     context.draw_text(info_font, info, pos + offset, ALIGN_LEFT, LAYER_GUI+2);
00088     if(binding != 0)
00089       context.draw_text(info_font, "(" + std::string(SDL_GetKeyName(binding)) +
00090                         ")", pos + offset + Vector(0,12),
00091                         ALIGN_LEFT,  LAYER_GUI+2);
00092   }
00093 
00094   context.draw_surface_part(image, Vector(0,0), size, pos, LAYER_GUI+1);
00095 }
00096 
00097 int Button::event(SDL_Event &event, int x_offset, int y_offset)
00098 {
00099   state = BT_NONE;
00100   switch(event.type)
00101   {
00102     case SDL_MOUSEBUTTONDOWN:
00103       if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
00104          event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
00105       {
00106         if(event.button.button == SDL_BUTTON_RIGHT)
00107           state = BT_SHOW_INFO;
00108       }
00109       break;
00110     case SDL_MOUSEBUTTONUP:
00111       if(event.button.x > pos.x + x_offset && event.button.x < pos.x + x_offset + size.x &&
00112          event.button.y > pos.y + y_offset && event.button.y < pos.y + y_offset + size.y)
00113       {
00114         if(event.button.button == SDL_BUTTON_LEFT)
00115           state = BT_SELECTED;
00116       }
00117       break;
00118     case SDL_KEYDOWN:        // key pressed
00119       if(event.key.keysym.sym == binding)
00120         state = BT_SELECTED;
00121       break;
00122     default:
00123       break;
00124   }
00125   return state;
00126 }
00127 
00128 /* EOF */

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