src/gui/button.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Simple button
00020  *****************************************************************************/
00021 
00022 #include "button.h"
00023 #include "../tool/resource_manager.h"
00024 #include "../graphic/sprite.h"
00025 #include "../include/app.h"
00026 
00027 Button::Button (const Rectanglei &rect, const Profile *res_profile,
00028                 const std::string& resource_id, bool _img_scale) : Widget(rect)
00029 {
00030   image = resource_manager.LoadSprite(res_profile,resource_id);
00031   image->cache.EnableLastFrameCache();
00032 
00033   // image scalling or not
00034   img_scale = _img_scale;
00035   
00036   if (img_scale)
00037     image->ScaleSize(rect.GetSize());
00038 }
00039 
00040 Button::Button (const Point2i &m_position, const Profile *res_profile, 
00041                 const std::string& resource_id, bool _img_scale)
00042 {
00043   image = resource_manager.LoadSprite(res_profile, resource_id);
00044   position = m_position;
00045   size = image->GetSize();
00046 
00047   // image scalling on resize
00048   img_scale = _img_scale;
00049 }
00050 
00051 Button::~Button()
00052 {
00053   delete image;
00054 }
00055 
00056 void Button::Draw(const Point2i &mousePosition, Surface& surf) const
00057 {
00058   uint frame = Contains(mousePosition)?1:0;
00059 
00060   image->SetCurrentFrame(frame);
00061 
00062   if (img_scale) {
00063     // image scalling : easy to place image
00064     image->Blit(surf, position);
00065   } else {
00066     // centering image
00067     Point2i pos = position;
00068 
00069     pos.x += (GetSizeX()/2) - (image->GetWidth()/2);
00070     pos.y += (GetSizeY()/2) - (image->GetHeight()/2);
00071 
00072     image->Blit(surf, pos);
00073   }
00074 }
00075 
00076 void Button::SetSizePosition(const Rectanglei &rect)
00077 {
00078   StdSetSizePosition(rect);
00079 
00080   if (img_scale)
00081     image->ScaleSize(size);
00082 }

Generated on Mon Jan 1 13:10:57 2007 for Wormux by  doxygen 1.4.7