-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextbox.h
More file actions
111 lines (100 loc) · 2.18 KB
/
Textbox.h
File metadata and controls
111 lines (100 loc) · 2.18 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
#pragma once
//textbox class constructed using TermSpar YouTube channel
#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>
#define DELETE_KEY 8
#define ENTER_KEY 13
#define ESCAPE_Key 27
class Textbox {
public:
Textbox() {
}//constructor
Textbox(int size, sf::Color color, bool sel) {
textbox.setCharacterSize(size);
textbox.setFillColor(color);
isSelected = sel;
if (sel) {
textbox.setString("-");
}
else {
textbox.setString("");
}
}
void setFont(sf::Font& font) {
textbox.setFont(font);
}
void setPosition(sf::Vector2f pos) {
textbox.setPosition(pos);
}
void setLimit(bool ToF) {
hasLimit = ToF;
}
void setLimit(bool ToF, int lim) {
hasLimit = ToF;
limit = lim;
}
void setSelected(bool sel) {
isSelected = sel;
if (!sel) {
std::string t = text.str();
std::string newT = "";
for (int i = 0; i < t.length() - 1; i++) {
newT += t[i];
}
textbox.setString(newT);
}
}
std::string getText() {
return text.str();
}
void drawTo(sf::RenderWindow& window) {
window.draw(textbox);
}
void typedOn(sf::Event input) {
if (isSelected) {
int charTyped = input.text.unicode;
if (charTyped < 128) {
if (hasLimit) {
if (text.str().length() <= limit) {
inputLogic(charTyped);
}
else if (text.str().length() > limit && charTyped == DELETE_KEY) {
deleteLastChar();
}
}
}
else {
inputLogic(charTyped);
}
}
}
private:
sf::Text textbox;
std::ostringstream text;
bool isSelected = false;
bool hasLimit = false;
int limit;
void inputLogic(int charTyped) {
if (charTyped != DELETE_KEY && charTyped != ENTER_KEY && charTyped != ESCAPE_Key) {
text << static_cast<char>(charTyped);
}
else if (charTyped == DELETE_KEY) {
if (text.str().length() > 0) {
deleteLastChar();
}
textbox.setString(text.str() + "_");
}
//else if () {}
}
void deleteLastChar() {
std::string t = text.str();
std::string newT = "";
for (int i = 0; i < t.length() - 1; i++) {
newT += t[i];
}
text.str("");
text << newT;
textbox.setString(text.str());
}
};