-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextField.cpp
More file actions
117 lines (90 loc) · 2.19 KB
/
TextField.cpp
File metadata and controls
117 lines (90 loc) · 2.19 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
#include <string>
#include <SFML/Graphics.hpp>
#include "TextField.h"
TextField::TextField() {
active = false;
box.setFillColor(sf::Color::White);
box.setOutlineThickness(3);
renderPlaceholder = false;
placeholder = "";
txt.setColor(sf::Color::Black);
size = GUI_TEXT_MAX;
length = 0;
}
void TextField::setPosition(sf::Vector2f vec) {
box.setPosition(vec);
txt.setPosition(vec+sf::Vector2f(5,5));
}
void TextField::input(sf::Event ev) {
if (ev.type == sf::Event::MouseButtonReleased) {
sf::Vector2f pos(ev.mouseButton.x, ev.mouseButton.y);
if (box.getGlobalBounds().contains(pos)) {
setActive(true);
} else {
setActive(false);
}
}
if (ev.type == sf::Event::TextEntered && active) {
sf::String str = txt.getString();
if (ev.text.unicode == GUI_TEXT_BACKSPACE) {
if (str.getSize() > 0) {
length--;
str = str.substring(0, str.getSize()-1);
}
} else if (ev.text.unicode == GUI_TEXT_ESCAPE){
setActive(false);
} else {
sf::String sfstr = "";
sfstr += ev.text.unicode;
str += sfstr.toAnsiString();
}
if (str.getSize() == size) return;
txt.setString(str);
length++;
}
}
void TextField::setFont(sf::Font& f) {
txt.setFont(f);
txt.setCharacterSize(24);
box.setSize(sf::Vector2f((txt.getCharacterSize()*(size/2+1))+10, txt.getCharacterSize()+10));
}
const sf::String& TextField::getText(){
return txt.getString();
}
void TextField::render(sf::RenderWindow& window) {
window.draw(box);
window.draw(txt);
}
void TextField::setActive(bool arg) {
active = arg;
if (active) {
box.setOutlineColor(sf::Color::Red);
} else {
box.setOutlineColor(sf::Color::White);
}
if (renderPlaceholder && arg) {
renderPlaceholder = false;
txt.setString("");
txt.setColor(sf::Color::Black);
}
}
void TextField::setPlaceholder(std::string str) {
placeholder = str;
renderPlaceholder = true;
txt.setColor(GUI_TEXT_GRAY);
txt.setString(placeholder);
setActive(false);
}
void TextField::setLength(int arg) {
size = arg;
box.setSize(sf::Vector2f((txt.getCharacterSize()*(size/2+1))+10, txt.getCharacterSize()+10));
}
int TextField::getTextLength() {
return length;
}
bool TextField::isActive() {
return active;
}
void TextField::open() {
setActive(true);
}