forked from dgcor/DGEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputText.cpp
More file actions
executable file
·180 lines (170 loc) · 3.56 KB
/
Copy pathInputText.cpp
File metadata and controls
executable file
·180 lines (170 loc) · 3.56 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "InputText.h"
#include "Game.h"
#include "Utils/Utils.h"
std::shared_ptr<Action> InputText::getAction(uint16_t nameHash16) const noexcept
{
switch (nameHash16)
{
case str2int16("change"):
return changeAction;
case str2int16("click"):
case str2int16("enter"):
return enterAction;
case str2int16("minLength"):
return minLengthAction;
default:
return nullptr;
}
}
bool InputText::setAction(uint16_t nameHash16, const std::shared_ptr<Action>& action) noexcept
{
switch (nameHash16)
{
case str2int16("change"):
changeAction = action;
break;
case str2int16("click"):
case str2int16("enter"):
enterAction = action;
break;
case str2int16("minLength"):
minLengthAction = action;
break;
default:
return false;
}
return true;
}
void InputText::setRegex(const std::string& regex_)
{
regex.reset();
if (regex_.size() > 0)
{
try
{
regex = std::make_optional<std::regex>(regex_, std::regex::ECMAScript);
}
catch (std::exception&) {}
}
}
bool InputText::isValidMin(const std::string& str) const noexcept
{
if (str.size() < minLength)
{
return false;
}
if (minValue.has_value() == true)
{
if (std::holds_alternative<std::string>(*minValue) == true)
{
return std::get<std::string>(*minValue) <= str;
}
else if (std::holds_alternative<int64_t>(*minValue) == true)
{
return std::get<int64_t>(*minValue) <= Utils::strtoll(str);
}
else if (std::holds_alternative<double>(*minValue) == true)
{
return std::get<double>(*minValue) <= Utils::strtod(str);
}
}
return true;
}
bool InputText::isValidMax(const std::string& str) const noexcept
{
if (maxLength > 0 && str.size() > maxLength)
{
return false;
}
if (maxValue.has_value() == true)
{
if (std::holds_alternative<std::string>(*maxValue) == true)
{
return std::get<std::string>(*maxValue) >= str;
}
else if (std::holds_alternative<int64_t>(*maxValue) == true)
{
return std::get<int64_t>(*maxValue) >= Utils::strtoll(str);
}
else if (std::holds_alternative<double>(*maxValue) == true)
{
return std::get<double>(*maxValue) >= Utils::strtod(str);
}
}
return true;
}
void InputText::click(Game& game)
{
if (isValidMin(text->getText()) == false)
{
if (minLengthAction != nullptr)
{
game.Events().addBack(minLengthAction);
}
return;
}
if (enterAction != nullptr)
{
game.Events().addBack(enterAction);
}
}
void InputText::update(Game& game)
{
if (text->Visible() == false)
{
return;
}
if (game.wasTextEntered() == true &&
game.TextEntered().unicode < 256 &&
game.TextEntered().unicode != 0)
{
while (true)
{
auto ch = static_cast<char>(game.TextEntered().unicode);
auto txt = text->getText();
if (ch == 8 && txt.size() > 0) // backspace
{
txt.pop_back();
text->setText(txt);
}
else if (ch < 0 || ch >= 32)
{
txt.push_back(ch);
if (isValidMax(txt) == false)
{
break;
}
if (regex.has_value() == true &&
std::regex_match(txt, *regex) == false)
{
break;
}
text->setText(txt);
}
triggerOnChange = true;
break;
}
}
Text::update(game);
}
bool InputText::getProperty(const std::string_view prop, Variable& var) const
{
if (prop.size() <= 1)
{
return false;
}
auto props = Utils::splitStringIn2(prop, '.');
auto propHash = str2int16(props.first);
switch (propHash)
{
case str2int16("number"):
var = Variable((int64_t)std::strtoll(text->getText().c_str(), nullptr, 10));
break;
case str2int16("double"):
var = Variable((int64_t)std::strtod(text->getText().c_str(), nullptr));
break;
default:
return text->getProperty(prop, var);
}
return true;
}