-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
62 lines (52 loc) · 2.7 KB
/
Copy pathButton.cpp
File metadata and controls
62 lines (52 loc) · 2.7 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
//
// Created by jonng on 12/7/2020.
//
#include "Button.h"
Button::Button() : Button("Text",0.f,0.f,100.f,50.f){ }
Button::Button(string input, float x, float y, float width, float height) {
buttonBackground.setFillColor(sf::Color::White);
setSize({width,height});
setPosition({x,y});
theText.setFont(Fonts::getFont(Fonts::MILKY_NICE));
setString(input);
theText.setFillColor(Color::Black);
// theText.setCharacterSize(25);
// theText.setOrigin(theText.getGlobalBounds().width/2, theText.getGlobalBounds().height/2);
// theText.setPosition(buttonBackground.getGlobalBounds().left + buttonBackground.getGlobalBounds().width/2, buttonBackground.getGlobalBounds().top + buttonBackground.getGlobalBounds().height/2-5);
}
void Button::setPosition(sf::Vector2f position) {
buttonBackground.setPosition(position);
theText.setPosition({buttonBackground.getGlobalBounds().left+(buttonBackground.getGlobalBounds().width-theText.getGlobalBounds().width)/2, buttonBackground.getGlobalBounds().top+(buttonBackground.getGlobalBounds().height-theText.getGlobalBounds().height)/4});
}
void Button::draw (RenderTarget &window, RenderStates states) const {
window.draw(buttonBackground);
window.draw(theText);
}
FloatRect Button::getGlobalBounds() {
return buttonBackground.getGlobalBounds();
}
void Button::setString(std::string aString) {
theText.setString(aString);
setPosition(buttonBackground.getPosition());
// theText.setPosition({buttonBackground.getGlobalBounds().left+(buttonBackground.getGlobalBounds().width-theText.getGlobalBounds().width)/2, buttonBackground.getGlobalBounds().top+(buttonBackground.getGlobalBounds().height-theText.getGlobalBounds().height)/4});
}
void Button::setSize(sf::Vector2f size) {
buttonBackground.setSize(size);
theText.setCharacterSize((int)(buttonBackground.getSize().y*0.7));
setPosition(buttonBackground.getPosition());
// theText.setPosition({buttonBackground.getGlobalBounds().left+(buttonBackground.getGlobalBounds().width-theText.getGlobalBounds().width)/2, buttonBackground.getGlobalBounds().top+(buttonBackground.getGlobalBounds().height-theText.getGlobalBounds().height)/4});
}
void Button::addEventHandler(sf::RenderWindow &window, sf::Event event) {
if(MouseEvents<sf::RectangleShape>::mouseClicked(buttonBackground,window)) {
buttonBackground.setFillColor({100,100,255});
theText.setFillColor({200,200,255});
}
else if (MouseEvents<sf::RectangleShape>::hovered(buttonBackground,window)) {
buttonBackground.setFillColor({200,200,255});
theText.setFillColor({33,33,33});
}
else {
buttonBackground.setFillColor(sf::Color::White);
theText.setFillColor({33,33,33});
}
}