-
Notifications
You must be signed in to change notification settings - Fork 1
/
Textfield.cpp
executable file
·73 lines (65 loc) · 2.51 KB
/
Textfield.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
72
73
#include "Textfield.h"
Textfield::Textfield(sf::Font & font, sf::Vector2f & size, sf::Vector2f & padding, unsigned int letterSize) : sf::RectangleShape(size + padding), padding(padding) {
inputText = sf::Text(string, font, letterSize);
inputText.setOrigin(-padding.x, (float) letterSize / 2.0f);
inputText.setColor(sf::Color::Black);
cursor = sf::RectangleShape(sf::Vector2f(3.0f, (float) letterSize));
cursor.setOrigin(0.0f, (float) letterSize / 2.0f);
cursor.setFillColor(sf::Color::Black);
blinkCursorTimer.restart();
}
Textfield ::~Textfield() {
// nothing to delete as of yet ...
}
void Textfield::operator=(const Textfield & other) {
this->setSize(other.getSize());
this->setOrigin(other.getOrigin());
this->setFillColor(other.getFillColor());
this->setScale(other.getScale());
this->setPosition(other.getPosition());
string = other.string;
inputText = other.inputText;
cursor = other.cursor;
blinkCursorTimer = other.blinkCursorTimer;
hasFocus = other.hasFocus;
padding = other.padding;
}
void Textfield::draw(sf::RenderWindow & window) {
window.draw(*this);
inputText.setString(string);
inputText.setPosition(this->getGlobalBounds().left, this->getGlobalBounds().top + this->getGlobalBounds().height / 2.1f);
window.draw(inputText);
cursor.setPosition(inputText.getGlobalBounds().left + inputText.getGlobalBounds().width, this->getGlobalBounds().top + this->getGlobalBounds().height / 2.0f);
if (hasFocus && (int) blinkCursorTimer.getElapsedTime().asSeconds() % 2 == 0) {
window.draw(cursor);
}
}
void Textfield::handleEvent(sf::Event event) {
if (hasFocus && event.type == sf::Event::TextEntered) {
if ((char)event.text.unicode == 8) { // backspace
if (string.size() > 0) {
string.pop_back();
}
}
else if ((char)event.text.unicode >= 32 && (char)event.text.unicode < 127) { // everything else that can be displayed
if (cursor.getGlobalBounds().left + cursor.getGlobalBounds().width < this->getGlobalBounds().left + this->getGlobalBounds().width - 2.0f * padding.x) {
string.push_back((char)event.text.unicode);
}
}
blinkCursorTimer.restart();
}
else if (event.type == sf::Event::MouseButtonPressed) {
if (this->getGlobalBounds().contains((float) event.mouseButton.x, (float) event.mouseButton.y)) {
hasFocus = true;
}
else {
hasFocus = false;
}
}
}
void Textfield::setTextColor(sf::Color color) {
inputText.setColor(color);
}
void Textfield::setCursorColor(sf::Color color) {
cursor.setFillColor(color);
}