commit cb22e8c5355b5677d096ffc9e089e2b738a4ecb6
parent 433500bc3be3fd9a234aea976b465203a43d9a33
Author: hhvn <dev@hhvn.uk>
Date: Thu, 15 Sep 2022 21:57:57 +0100
Functions for generic text input
Diffstat:
4 files changed, 35 insertions(+), 12 deletions(-)
diff --git a/src/main.h b/src/main.h
@@ -34,6 +34,9 @@ int strprefix(char *str, char *prefix);
char * strsuffix(char *str, char *suffix);
int strlistpos(char *str, char **list, size_t len);
float strnum(char *str);
+void edittrunc(wchar_t *str, int *len, int *cur);
+void editrm(wchar_t *str, int *len, int *cur);
+void editins(wchar_t *str, int *len, int *cur, int size, wchar_t c);
/* ui.c */
#define VIEWS_MAX_WIDTH (UI_VIEW_LAST*100)
diff --git a/src/str.c b/src/str.c
@@ -3,6 +3,7 @@
#include <stdarg.h>
#include <string.h>
#include <limits.h>
+#include <wchar.h>
#include <errno.h>
#include <math.h>
#include "main.h"
@@ -175,3 +176,26 @@ strnum(char *str) {
if (!str) return 0;
return strtof(str, NULL);
}
+
+void
+edittrunc(wchar_t *str, int *len, int *cur) {
+ *len = *cur = 0;
+ str[0] = '\0';
+}
+
+void
+editrm(wchar_t *str, int *len, int *cur) {
+ wmemmove(str + *cur - 1, str + *cur, *len - *cur);
+ str[--(*len)] = '\0';
+ (*cur)--;
+}
+
+void
+editins(wchar_t *str, int *len, int *cur, int size, wchar_t c) {
+ if (*len >= size)
+ return;
+ wmemmove(str + *cur + 1, str + *cur, *len - *cur);
+ str[*cur] = c;
+ str[++(*len)] = '\0';
+ (*cur)++;
+}
diff --git a/src/ui.c b/src/ui.c
@@ -349,24 +349,16 @@ ui_keyboard_handle(void) {
if (IsKeyPressed(KEY_ENTER) && in->onenter) {
wcstombs(in->str, in->wstr, INPUT_MAX);
- if (in->onenter(in)) {
- in->len = in->cur = 0;
- in->wstr[0] = '\0';
- }
+ if (in->onenter(in))
+ edittrunc(in->wstr, &in->len, &in->cur);
} else if (ui_keyboard_check(KEY_BACKSPACE, &fcount) && in->len && in->cur) {
- wmemmove(in->wstr + in->cur - 1,
- in->wstr + in->cur, in->len - in->cur);
- in->wstr[--in->len] = '\0';
- in->cur--;
+ editrm(in->wstr, &in->len, &in->cur);
} else if (ui_keyboard_check(KEY_LEFT, &fcount) && in->cur) {
in->cur--;
} else if (ui_keyboard_check(KEY_RIGHT, &fcount) && in->cur != in->len) {
in->cur++;
} else if (c && in->len < INPUT_MAX) {
- wmemmove(in->wstr + in->cur + 1, in->wstr + in->cur, in->len - in->cur);
- in->wstr[in->cur] = c;
- in->wstr[++in->len] = '\0';
- in->cur++;
+ editins(in->wstr, &in->len, &in->cur, INPUT_MAX, c);
}
}
diff --git a/src/ui/main.c b/src/ui/main.c
@@ -51,6 +51,8 @@ View_main view_main = {
.sys = NULL,
};
+Input test = { 0 };
+
Vector2
kmtopx(Vector2 km) {
return (Vector2) {
@@ -290,4 +292,6 @@ ui_draw_view_main(void) {
ui_draw_checkbox(x, y += FONT_SIZE*1.5, &view_main.infobox.orbit.comet);
ui_draw_checkbox(x, y += FONT_SIZE*1.5, &view_main.infobox.comettail);
pane_end();
+
+ ui_draw_input(500, 500, 150, &test);
}