-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.cpp
74 lines (66 loc) · 1.43 KB
/
Button.cpp
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
#include "Button.h"
//Class to simulate a button
//just draws and does some action when pressed
Button::Button(sf::Vector2i pPosition, sf::Vector2i pSize, sf::Texture *pTexture, BUTTONTYPE pButtonType) :
mSpriteSize(pSize),
mButtonType(pButtonType),
mSprite(*pTexture),
mPressed(false)
{
//set sprite position now instead of in draw because buttons dont move
mSprite.setPosition(pPosition.x, pPosition.y);
mSprite.scale(0.5, 0.5);
}
Button::~Button(void)
{
}
//do what the button is designed to do
void Button::ButtonEvent()
{
//rather than a class for each button that extends this class, we store button type and use
//a switch statement to determine what to do
switch(mButtonType)
{
//do stuff based on what button it is
case(bSTART):
if(mMenuState == mMAIN)
{
if(isPressed())
{
mGameState = gGAME;
}
PressButton();
}
break;
case(bCREDITS):
if(mMenuState == mMAIN)
{
if(isPressed())
{
mMenuState = mCREDITS;
}
PressButton();
}
break;
case(bBACK):
if(mMenuState == mCREDITS)
{
if(isPressed())
{
mMenuState = mMAIN;
}
PressButton();
}
break;
}
}
void Button::Draw(sf::RenderWindow *window)
{
mSprite.setTextureRect(sf::IntRect(mButtonType * mSpriteSize.x, mPressed * mSpriteSize.y, mSpriteSize.x, mSpriteSize.y));
window->draw(mSprite);
}
//switch between button up and button down basically just to animate it
inline void Button::PressButton()
{
mPressed = !mPressed;
}