-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBar.cpp
91 lines (80 loc) · 2.54 KB
/
TextBar.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Created by evgen on 18.05.2021.
//
#include "TextBar.h"
#include <iostream>
Interface::TextBar::TextBar(int x, int y, int max_size_of_text,
const sf::Texture& texture, const sf::Font& font):
Shape(x, y, texture.getSize().x, texture.getSize().y)
{
this->max_size_of_text = max_size_of_text;
sprite_box.setTexture(texture);
sprite_box.setOrigin((float)(Shape.x - Shape.mouse_x_min), (float)(Shape.y - Shape.mouse_y_min));
sprite_box.setPosition((float)Shape.x, (float)Shape.y);
is_active = false;
text = "";
visual_text.setString(text);
visual_text.setFont(font);
visual_text.setPosition((float)Shape.mouse_x_min, (float)Shape.mouse_y_min);
visual_text.setCharacterSize(35);
visual_text.setFillColor(sf::Color::Black);
visual_text.setStyle(sf::Text::Bold);
}
void Interface::TextBar::event_holder(const sf::Event& event) {
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
if (sprite_box.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
this->setActive();
else{
this->setPassive();
}
}
}
if (this->is_active) {
if (event.type == sf::Event::TextEntered) {
switch (event.text.unicode) {
case (0xD): {
setPassive();
return;
}
case (8): //Backspace
if (text.isEmpty()) return;
else {
text.erase((text.getSize() - 1), 1);
visual_text.setString(text);
return;
}
default : {
if (text.getSize() < max_size_of_text) {
text += (char) (event.text.unicode);
visual_text.setString(text);
}
return;
}
}
}
}
}
void Interface::TextBar::setActive(){
is_active = true;
sprite_box.setColor(sf::Color(128, 128, 128));
}
void Interface::TextBar::setPassive()
{
is_active = false;
sprite_box.setColor(sf::Color::White);
}
void Interface::TextBar::display(sf::RenderWindow& window) const
{
window.draw(sprite_box);
window.draw(visual_text);
}
void Interface::TextBar::setText(std::string&& str)
{
visual_text.setString(str);
}
std::string Interface::TextBar::returnText() {
return this->text;
}