-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.cpp
More file actions
66 lines (58 loc) · 1.86 KB
/
Button.cpp
File metadata and controls
66 lines (58 loc) · 1.86 KB
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
#include "Button.h"
#include "GameEngine.h"
#include "Sfx/SoundPlayer.h"
Button::Button(const sf::Input &_steering,sf::Vector2f _position,sf::Vector2f _size,sf::Color _color,std::string _text)
: color(_color), position(_position), size(_size), steering(_steering), pressed(false)
{
font.LoadFromFile("Data/Fonts/8-BIT WONDER.ttf");
text.SetFont( font );
box = sf::Shape::Rectangle(0,0,size.x,size.y,color);
text.SetColor(sf::Color(255,255,255));
box.SetPosition(position);
text.SetText( _text );
text.SetSize( 12 );
//text.SetCenter(box.GetCenter());
text.SetCenter(text.GetRect().GetWidth() /2,text.GetRect().GetHeight()/2);
text.SetPosition(box.GetPosition().x + size.x/2,box.GetPosition().y + size.y/2);
//text.SetCenter((text.GetRect().GetWidth() - size.x)/2, (text.GetRect().GetHeight() + size.y)/2);
singlePressed = false;
pressed = false;
isMouseOn = false;
}
Button::~Button(void)
{
}
void Button::SetPosition(sf::Vector2f newPosition)
{
box.SetPosition(newPosition);
}
void Button::GetEvent()
{
box.SetColor(color);
if( GameEngine::getInstance()->getEvent().Type == sf::Event::MouseMoved )
isMouseOn = true;
if( ( (GameEngine::getInstance()->GetMouseCoords().x) < ( size.x + position.x) && (GameEngine::getInstance()->GetMouseCoords().x) > (position.x) )
&& ( (GameEngine::getInstance()->GetMouseCoords().y) < ( size.y + position.y) && (GameEngine::getInstance()->GetMouseCoords().y) > (position.y) ))
{
box.SetColor(sf::Color(255,255,255));
if( steering.IsMouseButtonDown(sf::Mouse::Left) && isMouseOn )
{
if( pressed == false )
singlePressed = true;
else
singlePressed = false;
pressed = true;
} else
{
pressed = false;
singlePressed = false;
}
}
if ( singlePressed )
SoundPlayer::getInstance()->Play(Snd::MenuSelect);
}
void Button::Display(sf::RenderWindow *window)
{
window->Draw( box );
window->Draw( text );
}