Code: Select all
class Button
{
public:
Button( int x, int y, int w, int h, bool fOver, bool fDown );
bool Check( SDL_Event event, SDL_Surface *buffer );
void Load( SDL_Surface *image );
private:
void Draw( int x, int y, SDL_Surface *screen, SDL_Rect clip );
bool overImage;
bool downImage;
SDL_Surface *buttonImage;
SDL_Surface *currentImage;
int mouseX;
int mouseY;
SDL_Rect button;
SDL_Rect out;
SDL_Rect over;
SDL_Rect down;
SDL_Rect toggled;
};
Button::Button( int x, int y, int w, int h, bool fOver, bool fDown )
{
overImage = fOver;
downImage = fDown;
button.x = x;
button.y = y;
button.w = w;
button.h = h;
out.x = 0;
out.y = 0;
out.w = w;
out.h = h;
if( fOver == true )
{
over.x = w + 1;
over.y = 0;
over.w = w;
over.h = h;
}
if( fOver == false && fDown == true )
{
down.x = w + 1;
down.y = 0;
down.w = w;
down.h = h;
}
if( fOver == true && fDown == true )
{
down.x = (w * 2) + 1;
down.y = 0;
down.w = w;
down.h = h;
}
if( fOver == true && fDown == true )
{
toggled.x = (w * 3) + 1;
toggled.y = 0;
toggled.w = w;
toggled.h = h;
}
if( fOver == false && fDown == true || fOver == true && fDown == false )
{
toggled.x = (w * 2) + 1;
toggled.y = 0;
toggled.w = w;
toggled.h = h;
}
}
bool Button::Check( SDL_Event event, SDL_Surface *buffer )
{
SDL_GetMouseState( &mouseX, &mouseY );
if( mouseX > button.x && mouseX < ( button.x + button.w ) && mouseY < button.y && mouseY > ( button.y + button.h ) )
{
if( overImage )
{
Draw( button.x, button.y, buffer, over );
}
while( event.type == SDL_MOUSEBUTTONDOWN && downImage )
{
Draw( button.x, button.y, buffer, down );
}
if( mouseX > button.x && mouseX < ( button.x + button.w ) && mouseY < button.y && mouseY > ( button.y + button.h ) )
{
Draw( button.x, button.y, buffer, toggled );
return true;
}
}
else
{
Draw( button.x, button.y, buffer, out );
return false;
}
}
void Button::Draw( int x, int y, SDL_Surface *screen, SDL_Rect clip )
{
ApplySurface( x, y, buttonImage, screen, clip );
}
void Button::Load( SDL_Surface *image )
{
buttonImage = image;
}
It's so annoying cause I spent like an hour typing this shit up.
Any help would be SERIOUSLY appretiated.