-
Notifications
You must be signed in to change notification settings - Fork 27
/
text_input_handler.cpp
95 lines (88 loc) · 3.61 KB
/
text_input_handler.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
#include "text_input_handler.h"
#include "utf8_util.h"
void TextInputHandler::enable(std::string text, bool multiline) {
enabled = true;
this->multiline = multiline;
update(std::move(text));
}
void TextInputHandler::update(std::string text) {
currentText = std::move(text);
currentTextPosition = currentText.size();
currentTextPositionUTF = UTF8Util::getLength(currentText.c_str(), currentTextPosition);
currentTextCopyPosition = currentTextPosition;
}
void TextInputHandler::disable() {
currentText.clear();
currentTextPosition = 0;
currentTextPositionUTF = 0;
currentTextCopyPosition = 0;
enabled = false;
}
void TextInputHandler::onTextInput(std::string const &text) {
if (!enabled) {
textUpdateCallback(text);
return;
}
if (text.size() == 1 && text[0] == 8) { // backspace
if (currentTextPositionUTF <= 0)
return;
currentTextPositionUTF--;
auto deleteStart = currentTextPosition - 1;
while (deleteStart > 0 && (currentText[deleteStart] & 0b11000000) == 0b10000000)
deleteStart--;
currentText.erase(currentText.begin() + deleteStart, currentText.begin() + currentTextPosition);
currentTextPosition = deleteStart;
} else if (text.size() == 1 && text[0] == 127) { // delete key
if (currentTextPosition >= currentText.size())
return;
auto deleteEnd = currentTextPosition + 1;
while (deleteEnd < currentText.size() && (currentText[deleteEnd] & 0b11000000) == 0b10000000)
deleteEnd++;
currentText.erase(currentText.begin() + currentTextPosition, currentText.begin() + deleteEnd);
} else {
currentText.insert(currentText.begin() + currentTextPosition, text.begin(), text.end());
currentTextPosition += text.size();
currentTextPositionUTF += UTF8Util::getLength(text.c_str(), text.size());
}
textUpdateCallback(currentText);
currentTextCopyPosition = currentTextPosition;
}
void TextInputHandler::onKeyPressed(KeyCode key, KeyAction action) {
if (key == KeyCode::LEFT_SHIFT || key == KeyCode::RIGHT_SHIFT)
shiftPressed = (action != KeyAction::RELEASE);
if (action != KeyAction::PRESS && action != KeyAction::REPEAT)
return;
if (key == KeyCode::RIGHT) {
if (currentTextPosition >= currentText.size())
return;
currentTextPosition++;
while (currentTextPosition < currentText.size() &&
(currentText[currentTextPosition] & 0b11000000) == 0b10000000)
currentTextPosition++;
currentTextPositionUTF++;
} else if (key == KeyCode::LEFT) {
if (currentTextPosition <= 0)
return;
currentTextPosition--;
while (currentTextPosition > 0 && (currentText[currentTextPosition] & 0b11000000) == 0b10000000)
currentTextPosition--;
currentTextPositionUTF--;
} else if (key == KeyCode::HOME) {
currentTextPosition = 0;
currentTextPositionUTF = 0;
} else if (key == KeyCode::END) {
currentTextPosition = currentText.size();
currentTextPositionUTF = UTF8Util::getLength(currentText.c_str(), currentTextPosition);
}
if (!shiftPressed)
currentTextCopyPosition = currentTextPosition;
}
std::string TextInputHandler::getCopyText() const {
if (currentTextCopyPosition != currentTextPosition) {
size_t start = std::min(currentTextPosition, currentTextCopyPosition);
size_t end = std::max(currentTextPosition, currentTextCopyPosition);
return currentText.substr(start, end - start);
} else {
return currentText;
}
}