-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextBox.h
118 lines (93 loc) · 2.3 KB
/
TextBox.h
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
#ifndef TEXTBOX_H_
#define TEXTBOX_H_
#include <SFML/Graphics.hpp>
#include <string>
class TextBox : public sf::Transformable
{
public:
TextBox(sf::Vector2f pos, sf::Font& font, sf::Vector2f size)
{
setPosition(pos);
sf::Vector2f outerSize = size;
outerSize.x += 10;
outerSize.y += 10;
outer.setSize(outerSize);
outer.setOrigin(outerSize/2.0f);
outer.setFillColor(sf::Color(160, 82, 45));
outer.setOutlineColor(sf::Color::Black);
outer.setOutlineThickness(2);
inner.setSize(size);
inner.setOrigin(size/2.0f);
inner.setFillColor(sf::Color::White);
cursor.setSize(sf::Vector2f(3, size.y));
cursor.setOrigin(1.5, size.y/2);
cursor.setFillColor(sf::Color::Black);
cursor.setPosition(-size.x/2, 0);
text.setFont(font);
text.setColor(sf::Color::Black);
text.setCharacterSize(20);
text.setOrigin(0, size.y/2);
text.setPosition(-size.x/2, 0);
cursorHidden = false;
retpressed = false;
}
bool update(sf::Event event)
{
retpressed = false;
if(event.key.code == sf::Keyboard::Return)
{
retpressed = true;
return true;
}
if(event.type == sf::Event::TextEntered)
{
if(event.text.unicode == 8) //backspace
{
realText = realText.substr(0, realText.size()-1);
return false;
}
realText += (char)event.text.unicode;
}
return false;
}
bool draw(sf::RenderWindow& window,
sf::RenderStates states = sf::RenderStates::Default) //returns true on enter
{
text.setString(realText);
cursor.setPosition(-inner.getSize().x/2 + text.getGlobalBounds().width, 0);
if(cursorTime.getElapsedTime().asSeconds() >= 0.5)
{
if(cursorHidden)
cursor.setFillColor(sf::Color::Black);
else
cursor.setFillColor(sf::Color::Transparent);
cursorHidden = !cursorHidden;
cursorTime.restart();
}
states.transform *= getTransform();
window.draw(outer, states);
window.draw(inner, states);
window.draw(text, states);
window.draw(cursor, states);
return retpressed;
}
std::string getString()
{
return text.getString();
}
void clear()
{
realText = "";
cursor.setPosition(-inner.getSize().x/2, 0);
text.setString("");
}
sf::RectangleShape outer;
sf::RectangleShape inner;
sf::RectangleShape cursor;
bool cursorHidden;
sf::Clock cursorTime;
sf::Text text;
std::string realText;
bool retpressed;
};
#endif /* TEXTBOX_H_ */