1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
class Button {
private:
//hold the offsets
SDL_Rect off;
//check whether or not to hide the button
bool bhide;
//colors for the button
MY_Rect inside,outside;
string color;
public:
Button(int, int, int, int);
void setOff(int, int, int, int);
void show();
bool isDown();
void hide() { bhide = true; }
void unHide(){ bhide = false; }
};
Button::Button(int x, int y, int w, int h){
//make sure the button doesn't auto-hide
bhide = false;
//set the offsets
off.x = x;
off.y = y;
off.w = w;
off.h = h;
//default color
color = "gray";
}
bool Button::isDown(){
//checks if the mouse is down
if( event.type == SDL_MOUSEBUTTONDOWN ){
int x = event.button.x;
int y = event.button.y;
if ((x > off.x ) && ( x < off.x + off.w ) && ( y > off.y ) && ( y < off.y + off.h)){
return true;
}
}
return false;
}
void Button::setOff(int h, int w, int x, int y){
off.h = h;
off.w = w;
off.x = x;
off.y = y;
}
void Button::show(){
if(!bhide){
//outline of the button
MY_Rect outside;
outside.x = off.x - 2;
outside.y = off.y - 2;
outside.w = off.w + 6;
outside.h = off.h + 6;
outside.fill("black");
//body of the button
inside.x = off.x;
inside.y = off.y;
inside.w = off.w;
inside.h = off.h;
inside.fill(color);
//check for hover
int x = event.button.x;
int y = event.button.y;
if ( ( x > off.x ) && ( x < off.x + off.w ) && ( y > off.y ) && ( y < off.y + off.h ) ) {
//set the inside of the button to a lighter color
inside.fill("light-" + color);
}else{
//set back to original color
inside.fill(color);
}
}
}
|