/
input.cpp
186 lines (154 loc) · 3.33 KB
/
input.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
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
181
182
183
184
185
186
#include "input.h"
#include "utils.h"
#include "define.h"
#include "window.h"
Input::Input(Window & window):
Label(window, "")
{
textColor(DEFAULT_COLOR_TEXT);
bgColor(DEFAULT_COLOR_INPUT_BG);
position(0, 0);
size(DEFAULT_INPUT_WIDTH, DEFAULT_INPUT_HEIGHT);
showBg(true);
layout(LayoutLeft, LayoutMiddle);
padding(1, 3, 2, 2);
_charLimit = 256;
_bGrating = false;
_asciiOnly = false;
_passwdOnly = false;
_passwdMaskChar = DEFAULT_PASSWORD_CHAR;
SDL_SetTextInputRect(&_rc);
}
void Input::passwdOnly(bool value, const std::string &sign, const std::string &mask)
{
_passwdOnly = true;
if (_passwdOnly)
{
_asciiOnly = true;
_validPasswdSign = sign;
_passwdMaskChar = mask;
}
else
{
_asciiOnly = false;
_passwdMaskChar = DEFAULT_PASSWORD_CHAR;
_validPasswdSign.clear();
}
}
bool Input::isValidPasswd(const std::string &s)
{
for (auto c : s)
{
auto it = _validPasswdSign.find(c);
if (!isalnum(c) && it == std::string::npos)
{
return false;
}
}
return true;
}
std::string Input::password()
{
return _passwd;
}
void Input::clear()
{
_text.clear();
_passwd.clear();
}
bool Input::event(SDL_Event & e)
{
switch (e.type)
{
case SDL_TEXTINPUT:
{
if (_asciiOnly && !testAscii(e.text.text))
break;
size_t before = _text.size();
if (!_passwdOnly)
{
_text += e.text.text;
}
else
{
if (!isValidPasswd(e.text.text))
break;
_passwd += e.text.text;
while (_passwd.size() > _charLimit)
u8Pop(_passwd);
_text += _passwdMaskChar;
}
while (_text.size() > _charLimit)
u8Pop(_text);
//std::string input;
//u8ToGbk(!_passwdOnly ? _text : _passwd, input);
//printf("_text=%s\n", input.c_str());
//printf("u8Len=%zu\n", u8Len(_text));
break;
}
case APP_TIME:
{
_bGrating = !_bGrating;
break;
}
case SDL_KEYUP:
{
if (e.key.state == SDL_RELEASED && e.key.keysym.sym == SDLK_BACKSPACE && !_text.empty())
{
size_t before = _text.size();
u8Pop(_text);
if (_passwdOnly)
u8Pop(_passwd);
}
}
default:
break;
}
return true;
}
bool Input::setViewPort(const TextRenderInfo &info, SDL_Rect &rcText, SDL_Rect &rcViewPort)
{
if (_text.empty())
return false;
memcpy(&rcViewPort, &rcText, sizeof(SDL_Rect));
rcViewPort.x = 0;
rcViewPort.y = 0;
SDL_Rect rcPading;
getPaddingRect(rcPading);
if (rcText.w <= rcPading.w)
return true;
rcViewPort.w = rcText.w;
rcViewPort.h = rcText.h;
rcViewPort.x = rcText.w - rcPading.w;
rcViewPort.w = rcPading.w;
rcText.w = rcPading.w;
return true;
}
bool Input::draw(SDL_Renderer * renderer)
{
//static clock_t clock = std::clock();
Label::draw(renderer);
SDL_Rect rcText = { 0, 0, 0, 0 };
if (!_text.empty())
{
memcpy(&rcText, &getTextRect(), sizeof(rcText));
}
SDL_Rect rcPading;
getPaddingRect(rcPading);
SDL_Rect dstText = { rcPading.x, rcPading.y, rcText.w, rcText.h };
if (_bGrating)
{
int x = rcPading.x + dstText.w + 1; // +1 for grating
SDL_SetRenderDrawColor(renderer, 0xcc, 0xcc, 0xcc, 0xff);
SDL_RenderDrawLine(renderer, x, rcPading.y, x, rcPading.y + rcPading.h);
}
return true;
}
void Input::charLimit(size_t n)
{
_charLimit = n;
}
void Input::asciiOnly(bool value)
{
_asciiOnly = value;
}