-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.h
More file actions
153 lines (147 loc) · 4.55 KB
/
Button.h
File metadata and controls
153 lines (147 loc) · 4.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
class Button
{
private:
sf::RectangleShape button;
sf::CircleShape circle1;
sf::CircleShape circle2;
sf::Text text;
//dimensions i.e height and width of buttons
int btnWidth;
int btnHeight;
bool circle;
public:
//Default constructor
Button():btnHeight(250),btnWidth(60)//setting default height and width
{
circle = false;
}
//parametrized constructor setting the text,size,position and font of button
Button(std::string btnText, sf::Vector2f buttonSize, sf::Vector2f buttonPos,sf::Font& font,bool circle)
{
this->circle = circle;
set(btnText, buttonSize,buttonPos,font,circle);
}
//graphics for setting the header i.e "FAST EXPRESS"
void Header(std::string btnText, sf::Vector2f buttonSize, sf::Vector2f buttonPos, sf::Font& font,bool circle)
{
this->circle = circle;
btnWidth = buttonSize.x;
btnHeight = buttonSize.y;
button.setPosition(buttonPos);
//Set Rectangle
button.setSize(buttonSize);
button.setFillColor(sf::Color::Black);
circle1.setFillColor(sf::Color::Black);
circle2.setFillColor(sf::Color::Black);
text.setString(btnText);
text.setColor(sf::Color::Red);
text.setCharacterSize(70);
text.setStyle(sf::Text::Style::Italic);
text.setOutlineColor(sf::Color::Yellow);
text.setOutlineThickness(2);
//Set Text
setFont(font);
setText(buttonPos);
//Setting circles on both ends
circle1.setRadius(btnHeight / 2);
circle1.setPosition(btnWidth - 340, buttonPos.y);
circle2.setRadius(btnHeight / 2);
circle2.setPosition(btnWidth + 90, buttonPos.y);
}
// set function for graphics of buttons i.e their font, color, size and texts
void set(std::string btnText,sf::Vector2f buttonSize,sf::Vector2f buttonPos, sf::Font& font,bool circle)
{
this->circle = circle;
btnWidth = buttonSize.x;
btnHeight = buttonSize.y;
button.setPosition(buttonPos);
//Set Rectangle
button.setSize(buttonSize);
button.setFillColor(sf::Color::Black);
//Setting Text
text.setString(btnText);
text.setColor(sf::Color::Red);
text.setCharacterSize(25);
setFont(font);
setText(buttonPos);
//Setting Circle
setCircle(buttonPos);
}
//set function for curve at each end of buttons
void setCircle(sf::Vector2f buttonPos)
{
circle1.setFillColor(sf::Color::Black);
circle2.setFillColor(sf::Color::Black);
circle1.setOutlineThickness(2);
circle1.setOutlineColor(sf::Color::Black);
circle1.setRadius(btnHeight/2);
circle1.setPosition(buttonPos.x-28, buttonPos.y);
circle2.setRadius(btnHeight / 2);
circle2.setPosition(buttonPos.x+180, buttonPos.y);
}
void setOutlineColor(sf::Color color)//setter for outline color
{
circle1.setOutlineColor(color);
}
void setSize(sf::Vector2f buttonSize)//setter for size
{
btnHeight = buttonSize.y;
btnWidth = buttonSize.x;
button.setSize(buttonSize);
circle1.setRadius(btnWidth / 2);
circle2.setRadius(btnWidth / 2);
}
void setFont(sf::Font& fonts)//setter for font
{
text.setFont(fonts);
}
void setBackColor(sf::Color color)//setter for main back color
{
button.setFillColor(color);
}
void setTextColor(sf::Color color)//setter for text color of button
{
text.setColor(color);
}
void setText(sf::Vector2f point)//setter for position of text in a button
{
float xPos = point.x;
float yPos = point.y;
text.setPosition(xPos, yPos);
}
void setPosition(sf::Vector2f point)//setter for position of button in the console
{
button.setPosition(point);
// Logic to place text in the middle of the button by getting its boundary coordinates
float xPos = (point.x + btnWidth / 2) - (text.getLocalBounds().width / 2);
float yPos = (point.y + btnHeight / 2.2) - (text.getLocalBounds().height / 2);
text.setPosition(xPos, yPos);
}
void drawTo(sf::RenderWindow& window)
{
if (circle==true)
{
window.draw(circle1);
window.draw(circle2);
}
window.draw(button);
window.draw(text);
}
//Check if the mouse is within the boundary of the button i.e if mouse is clicking ON the button
bool isMouseOver(sf::RenderWindow& window)
{
int mouseX = sf::Mouse::getPosition(window).x;
int mouseY = sf::Mouse::getPosition(window).y;
int btnPosX = button.getPosition().x;
int btnPosY = button.getPosition().y;
int btnxPosWidth = button.getPosition().x + btnWidth;
int btnyPosHeight = button.getPosition().y + btnHeight;
if (mouseX < btnxPosWidth && mouseX > btnPosX&& mouseY < btnyPosHeight && mouseY > btnPosY) {
return true;
}
return false;
}
};