-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClickableText.cpp
71 lines (60 loc) · 1.49 KB
/
ClickableText.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
#include "stdafx.h"
#include "ClickableText.h"
#include "Resources.h"
#include "MyAlgorithms.h"
#include "Debugger.h"
ClickableText::ClickableText(const std::string& message, std::function<void()> func, int x, int y) : text(message, Resources::Instance()->FontBank("Arial"), 30), OnClick(func), isShowing(true), isMousedOver(false)
{
SetPosition(x,y);
}
bool ClickableText::HandleEvents(const sf::Event& someEvent)
{
if(isShowing)
{
if(someEvent.type==sf::Event::MouseButtonPressed && MyAlgorithms::IsWithin(sf::Mouse::getPosition(Game::Instance()->mainWindow_), text))
{
OnClick();
return true;
}
if(MyAlgorithms::IsWithin(sf::Mouse::getPosition(Game::Instance()->mainWindow_), text)) //if your mouse is over the button
{
if(!isMousedOver) //and you weren't already moused over
{
text.setColor(sf::Color::Red);
isMousedOver=true;
}
}
else if(isMousedOver) //if you were moused over
{
text.setColor(sf::Color::White);
isMousedOver=false;
}
return false;
}
}
void ClickableText::Update()
{
//isMousedOver ? text.setColor(sf::Color::Red) : text.setColor(sf::Color::White);
}
void ClickableText::Draw()
{
if(isShowing)
Game::Instance()->mainWindow_.draw(text);
}
//centers to position
void ClickableText::SetPosition(float x, float y)
{
text.setPosition(x-text.getGlobalBounds().width/2,y);
}
void ClickableText::MouseOver(bool b)
{
isMousedOver=b;
}
void ClickableText::Show(bool b)
{
isShowing=b;
}
sf::Text& ClickableText::GetText()
{
return text;
}