23 #include "shared/shared.h"
24 #include "common/common.h"
25 #include "common/field.h"
27 #include "client/keys.h"
28 #include "client/video.h"
29 #include "refresh/refresh.h"
36 void IF_Init(inputField_t *field,
size_t visibleChars,
size_t maxChars)
38 memset(field, 0,
sizeof(*field));
40 if (maxChars >=
sizeof(field->text)) {
41 maxChars =
sizeof(field->text) - 1;
43 if (visibleChars > maxChars) {
44 visibleChars = maxChars;
47 field->maxChars = maxChars;
48 field->visibleChars = visibleChars;
58 memset(field->text, 0,
sizeof(field->text));
69 if (field->maxChars && text) {
70 size_t len =
Q_strlcpy(field->text, text, field->maxChars + 1);
71 field->cursorPos = len >= field->maxChars ? field->maxChars - 1 : len;
85 qboolean IF_KeyEvent(inputField_t *field,
int key)
87 if (!field->maxChars) {
90 if (field->cursorPos >= field->maxChars) {
91 Com_Error(ERR_FATAL,
"%s: bad cursorPos", __func__);
95 if (field->text[field->cursorPos]) {
96 memmove(field->text + field->cursorPos,
97 field->text + field->cursorPos + 1,
98 sizeof(field->text) - field->cursorPos);
103 if (key == K_BACKSPACE || (key ==
'h' &&
Key_IsDown(K_CTRL))) {
104 if (field->cursorPos > 0) {
105 memmove(field->text + field->cursorPos - 1,
106 field->text + field->cursorPos,
107 sizeof(field->text) - field->cursorPos);
114 size_t oldpos = field->cursorPos;
117 while (field->cursorPos > 0 && field->text[field->cursorPos] <= 32) {
122 while (field->cursorPos > 0 && field->text[field->cursorPos - 1] > 32) {
125 memmove(field->text + field->cursorPos, field->text + oldpos,
126 sizeof(field->text) - oldpos);
131 memmove(field->text, field->text + field->cursorPos,
132 sizeof(field->text) - field->cursorPos);
133 field->cursorPos = 0;
138 field->text[field->cursorPos] = 0;
147 if (key == K_LEFTARROW || (key ==
'b' &&
Key_IsDown(K_CTRL))) {
148 if (field->cursorPos > 0) {
154 if (key == K_RIGHTARROW || (key ==
'f' &&
Key_IsDown(K_CTRL))) {
155 if (field->text[field->cursorPos]) {
162 if (field->cursorPos > 0 && field->text[field->cursorPos - 1] <= 32) {
165 while (field->cursorPos > 0 && field->text[field->cursorPos] <= 32) {
168 while (field->cursorPos > 0 && field->text[field->cursorPos - 1] > 32) {
175 while (field->text[field->cursorPos] && field->text[field->cursorPos] <= 32) {
178 while (field->text[field->cursorPos] > 32) {
184 if (key == K_HOME || (key ==
'a' &&
Key_IsDown(K_CTRL))) {
185 field->cursorPos = 0;
189 if (key == K_END || (key ==
'e' &&
Key_IsDown(K_CTRL))) {
190 field->cursorPos = strlen(field->text);
202 if (field->cursorPos >= field->maxChars) {
203 field->cursorPos = field->maxChars - 1;
214 qboolean IF_CharEvent(inputField_t *field,
int key)
216 if (!field->maxChars) {
219 if (field->cursorPos >= field->maxChars) {
220 Com_Error(ERR_FATAL,
"%s: bad cursorPos", __func__);
223 if (key < 32 || key > 127) {
227 if (field->cursorPos == field->maxChars - 1) {
229 field->text[field->cursorPos] = key;
235 field->text[field->cursorPos++] = key;
240 memmove(field->text + field->cursorPos + 1,
241 field->text + field->cursorPos,
242 sizeof(field->text) - field->cursorPos - 1);
243 field->text[field->cursorPos++] = key;
256 int IF_Draw(inputField_t *field,
int x,
int y,
int flags, qhandle_t font)
258 char *text = field->text;
259 size_t cursorPos = field->cursorPos;
263 if (!field->maxChars || !field->visibleChars) {
267 if (cursorPos >= field->maxChars) {
268 Com_Error(ERR_FATAL,
"%s: bad cursorPos", __func__);
272 if (cursorPos >= field->visibleChars) {
273 cursorPos = field->visibleChars - 1;
274 offset = field->cursorPos - cursorPos;
278 ret =
R_DrawString(x, y, flags, field->visibleChars, text + offset, font);
280 if (flags & UI_DRAWCURSOR) {
284 R_DrawChar(x + cursorPos * CHAR_WIDTH, y, flags,
c, font);