-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTyping.cpp
130 lines (107 loc) · 3.78 KB
/
Typing.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
//
// Created by Brandon Hargitay on 2/27/23.
//
#include "Typing.h"
void Typing::draw(sf::RenderTarget &target, sf::RenderStates states) const {
states.transform *= getTransform();
for(auto& letter : textInput){
target.draw(letter,states);
}
}
Typing::Typing() {
letter.setFont(Font::getFont());
letter.setFillColor(sf::Color::White);
letter.setCharacterSize(40);
}
void Typing::addEventHandler(sf::RenderWindow &window, const sf::Event &event) {
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Z && event.key.control) {
if (!History::isEmpty()) {
applySnapshot(History::pop());
return;
}
}
if (event.type == sf::Event::TextEntered) {
History::push(getSnapshot());
if (event.text.unicode >= 32 && event.text.unicode <= 126) {
letter.setString(static_cast<char>(event.text.unicode));
textInput.push_back(letter);
setCharacterPosition();
}
else if (event.text.unicode == 8) {
if (!textInput.empty()) {
textInput.pop_back();
}
}
}
colorMatchingWords();
}
void Typing::setCharacterPosition() {
if (textInput.size() == 1) {
textInput.back().setPosition(getPosition());
}
else {
float xpos = std::prev(textInput.end(), 2)->getPosition().x
+ std::prev(textInput.end(), 2)->getGlobalBounds().width
+ std::prev(textInput.end(), 2)->getLetterSpacing();
textInput.back().setPosition(xpos, getPosition().y );
}
}
float Typing::getLastX() {
if (!textInput.empty()) {
sf::Text lastText = textInput.back();
return lastText.getPosition().x + lastText.getGlobalBounds().width + lastText.getLetterSpacing();
}
}
float Typing::getY() {
return getPosition().y;
}
void Typing::findAndColorWord(const std::string& word, const sf::Color& color) {
std::string currentWord;
int wordLength = word.length();
for (auto it = textInput.begin(); it != textInput.end(); ++it) {
char currentChar = it->getString()[0];
currentWord.push_back(currentChar);
if (currentWord.size() >= wordLength) {
if (currentWord.substr(currentWord.size() - wordLength, wordLength) == word) {
for (int i = 0; i < wordLength; ++i) {
std::prev(it, wordLength - i - 1)->setFillColor(color);
}
}
}
}
}
void Typing::colorNumbers() {
for (auto it = textInput.begin(); it != textInput.end(); ++it) {
char currentChar = it->getString()[0];
// Check if the character is a digit and set its color to red
if (currentChar >= '0' && currentChar <= '9') {
it->setFillColor(sf::Color::Red);
} else {
it->setFillColor(sf::Color::White);
}
}
}
void Typing::colorMatchingWords() {
colorNumbers(); // Add this line to handle coloring numbers
colorOperators(); // Add this line to handle coloring operators
findAndColorWord("int", sf::Color::Blue);
findAndColorWord("float", sf::Color::Red);
findAndColorWord("char", sf::Color::Yellow);
findAndColorWord("double", sf::Color::Green);
}
void Typing::colorOperators() {
std::string operators = "+-*/%=&|<>!^?";
for (auto it = textInput.begin(); it != textInput.end(); ++it) {
char currentChar = it->getString()[0];
// Check if the character is an operator and set its color to green
if (operators.find(currentChar) != std::string::npos) {
it->setFillColor(sf::Color::Green);
}
}
}
Snapshot Typing::getSnapshot() {
return Snapshot(textInput);
}
void Typing::applySnapshot(const Snapshot &snapshot) {
textInput = snapshot.textData;
}