-
Notifications
You must be signed in to change notification settings - Fork 3
/
TextBox.cpp
132 lines (118 loc) · 2.47 KB
/
TextBox.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
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
#include "TextBox.h"
bool TextBox::isResourcesLoaded = false;
Font TextBox::textBoxfont;
Texture TextBox::textCursor;
void TextBox::loadResources()
{
textBoxfont.loadFromFile("resources\\fonts\\segoeuil.ttf");
textCursor.loadFromFile("resources\\images\\textCursor.png");
}
void TextBox::draw(RenderTarget & target, RenderStates states) const
{
Vector2f thisPos = getPosition();
Control::draw(target, states);
if (isTakingInput)
{
RectangleShape cover(getSize());
cover.setPosition(thisPos);
cover.setFillColor(Color(65,252,233,90));
target.draw(cover);
Text txt(input, textBoxfont, 15);
txt.setFillColor(Color::Black);
txt.setPosition(thisPos.x + 2, thisPos.y);
target.draw(txt);
if (cursorInterval.getElapsedTime().asMilliseconds() %2000 < 1000)
{
Sprite s(textCursor);
s.setPosition(thisPos.x + txt.getLocalBounds().width, thisPos.y);
target.draw(s);
}
}
else
{
Text txt((path == "") ? "This PC" : path, textBoxfont, 15);
txt.setFillColor(Color::Black);
txt.setPosition(thisPos.x + 2, thisPos.y);
target.draw(txt);
}
}
TextBox::TextBox()
{
setSize(Vector2f(100, 20));
if (!isResourcesLoaded)
{
loadResources();
isResourcesLoaded = true;
}
setBorderColor(Color::Black);
setBorderThickness(1);
setBackgroundColor(Color::White);
path = "";
input = "";
isTakingInput = false;
}
void TextBox::setPath(string str)
{
path = str;
}
string TextBox::getPath() const
{
return path;
}
bool TextBox::getIsTakingInput()
{
return isTakingInput;
}
void TextBox::setIsInputInProgress(bool i)
{
isTakingInput = i;
}
void TextBox::setInputString(string s)
{
input = s;
}
string TextBox::getInputString()
{
return input;
}
TextBox::~TextBox()
{
}
FloatRect TextBox::getGlobalBounds() const
{
return FloatRect(getPosition(), getSize());
}
Control * TextBox::handleEvent(Event event, Vector2f mousePos)
{
if (event.type == Event::EventType::TextEntered)
{
if (event.text.unicode != 13)
{
if (event.text.unicode == '\b' && input != "")
{
input.pop_back();
}
else if(event.text.unicode != '\b')
{
input.push_back(event.text.unicode);
}
}
else
{
isTakingInput = false;
}
}
else if (event.type == Event::MouseButtonPressed)
{
if (!contains(mousePos))
{
isTakingInput = false;
input = "";
}
}
return this;
}
Control * TextBox::clone() const
{
return new TextBox(*this);
}