Button Class Reference

#include <button.hpp>

List of all members.

Public Member Functions

 Button (SurfacePtr image_, std::string info_, SDLKey binding_)
 Button (const Button &rhs)
 ~Button ()
Buttonoperator= (const Button &rhs)
void draw (DrawingContext &context, bool selected)
int event (SDL_Event &event, int x_offset=0, int y_offset=0)

Static Public Attributes

static FontPtr info_font

Private Attributes

Vector pos
Vector size
SurfacePtr image
SDLKey binding
int id
int state
std::string info

Friends

class ButtonGroup


Detailed Description

Definition at line 38 of file button.hpp.


Constructor & Destructor Documentation

Button::Button ( SurfacePtr  image_,
std::string  info_,
SDLKey  binding_ 
)

Definition at line 24 of file button.cpp.

References image, info, and size.

00024                                                                   :
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 }

Button::Button ( const Button rhs  ) 

Definition at line 39 of file button.cpp.

00039                                 :
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 }

Button::~Button (  ) 

Definition at line 50 of file button.cpp.

00051 {
00052 }


Member Function Documentation

Button & Button::operator= ( const Button rhs  ) 

Definition at line 55 of file button.cpp.

References binding, id, image, info, pos, size, and state.

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 }

void Button::draw ( DrawingContext context,
bool  selected 
)

Definition at line 70 of file button.cpp.

References ALIGN_LEFT, binding, BT_SHOW_INFO, DrawingContext::draw_filled_rect(), DrawingContext::draw_surface_part(), DrawingContext::draw_text(), DrawingContext::get_translation(), image, info, info_font, LAYER_GUI, pos, SCREEN_HEIGHT, size, state, Vector::x, and Vector::y.

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 }

int Button::event ( SDL_Event &  event,
int  x_offset = 0,
int  y_offset = 0 
)

Definition at line 97 of file button.cpp.

References binding, BT_NONE, BT_SELECTED, BT_SHOW_INFO, pos, size, state, Vector::x, and Vector::y.

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 }


Friends And Related Function Documentation

friend class ButtonGroup [friend]

Definition at line 53 of file button.hpp.


Member Data Documentation

FontPtr Button::info_font [static]

Definition at line 50 of file button.hpp.

Referenced by draw().

Vector Button::pos [private]

Definition at line 56 of file button.hpp.

Referenced by ButtonGroup::add_button(), ButtonGroup::add_pair_of_buttons(), draw(), event(), and operator=().

Vector Button::size [private]

Definition at line 57 of file button.hpp.

Referenced by ButtonGroup::add_button(), ButtonGroup::add_pair_of_buttons(), Button(), draw(), event(), and operator=().

SurfacePtr Button::image [private]

Definition at line 59 of file button.hpp.

Referenced by Button(), draw(), and operator=().

SDLKey Button::binding [private]

Definition at line 60 of file button.hpp.

Referenced by draw(), event(), and operator=().

int Button::id [private]

Definition at line 62 of file button.hpp.

Referenced by ButtonGroup::add_button(), ButtonGroup::add_pair_of_buttons(), and operator=().

int Button::state [private]

Definition at line 63 of file button.hpp.

Referenced by draw(), event(), and operator=().

std::string Button::info [private]

Definition at line 64 of file button.hpp.

Referenced by Button(), draw(), and operator=().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:29 2014 for SuperTux by  doxygen 1.5.1