diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-02-16 16:31:08 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-02-16 16:31:08 -0500 |
| commit | 4ffae4aeec2e5cc708f3a13899ad6b280b25b83f (patch) | |
| tree | 32a94ebef53a1e31965fa5eeab8323114a0532d1 | |
| parent | 9bd3edfb51ac9b975bb02596eebafb2c9069aa36 (diff) | |
| download | volute-4ffae4aeec2e5cc708f3a13899ad6b280b25b83f.zip | |
number field widget
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | main.c | 34 | ||||
| -rw-r--r-- | ui.c | 8 | ||||
| -rw-r--r-- | ui.h | 5 | ||||
| -rw-r--r-- | widget.c | 28 | ||||
| -rw-r--r-- | widget.h | 8 |
6 files changed, 74 insertions, 13 deletions
@@ -14,7 +14,7 @@ else endif LDFLAGS += $(GLFLAG) -SRC = main.c microui.c renderer.c +SRC = main.c microui.c renderer.c widget.c ui.c OBJ = ${SRC:.c=.o} all: volute @@ -28,4 +28,4 @@ volute: ${OBJ} %.o: %.c ${CC} -c ${CFLAGS} $< -${SRC}: microui.h renderer.h atlas.inl +${SRC}: microui.h renderer.h atlas.inl widget.h ui.h @@ -5,6 +5,8 @@ #include <SDL2/SDL.h> #include "renderer.h" #include "microui.h" +#include "widget.h" +#include "ui.h" /* Constants. */ @@ -33,11 +35,11 @@ static const char key_map[256] = { static int text_width(mu_Font font, const char *text, int len); static int text_height(mu_Font font); -static void main_loop(mu_Context *ctx); +static void main_loop(mu_Context *ctx, UI *ui); static void handle_events(mu_Context *ctx); static void handle_event(SDL_Event e, mu_Context *ctx); -static void process_frame(mu_Context *ctx); -static void main_window(mu_Context *ctx); +static void process_frame(mu_Context *ctx, UI *ui); +static void main_window(mu_Context *ctx, UI *ui); static void render(mu_Context *ctx); static void render_command(mu_Command *cmd); @@ -63,7 +65,11 @@ main(void) { ctx.text_width = text_width; ctx.text_height = text_height; - main_loop(&ctx); + /* Init data structures. */ + static UI ui; + init_ui(&ui); + + main_loop(&ctx, &ui); return 0; } @@ -82,10 +88,10 @@ text_height(mu_Font font) { } static void -main_loop(mu_Context *ctx) { +main_loop(mu_Context *ctx, UI *ui) { for (;;) { handle_events(ctx); - process_frame(ctx); + process_frame(ctx, ui); render(ctx); } } @@ -135,14 +141,14 @@ handle_event(SDL_Event e, mu_Context *ctx) { } static void -process_frame(mu_Context *ctx) { +process_frame(mu_Context *ctx, UI *ui) { mu_begin(ctx); - main_window(ctx); + main_window(ctx, ui); mu_end(ctx); } static void -main_window(mu_Context *ctx) { +main_window(mu_Context *ctx, UI *ui) { int w, h; r_get_window_size(&w, &h); @@ -151,8 +157,14 @@ main_window(mu_Context *ctx) { } /* TODO */ mu_layout_row(ctx, 2, (int[]) {0, 0}, 0); - mu_label(ctx, "foo"); - mu_label(ctx, "bar"); + static double value = 0.0; + if (field(ctx, &ui->displacement) & MU_RES_CHANGE) { + /* TODO */ + value = ui->displacement.value; + } + static char buf[64]; + snprintf(buf, sizeof(buf), "%lf", value); + mu_label(ctx, buf); mu_end_window(ctx); } @@ -0,0 +1,8 @@ +#include "microui.h" +#include "widget.h" +#include "ui.h" + +void +init_ui(UI *ui) { + init_field(&ui->displacement); +} @@ -0,0 +1,5 @@ +typedef struct { + Field displacement; +} UI; + +void init_ui(UI *ui); diff --git a/widget.c b/widget.c new file mode 100644 index 0000000..8a77b8f --- /dev/null +++ b/widget.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#include "microui.h" +#include "widget.h" + +void +init_field(Field *f) { + f->buf[0] = '\0'; + f->value = 0.0; +} + +/* field draws a Field widget and updates its value. + * It returns MU_RES_CHANGE if the value has changed. */ +int +field(mu_Context *ctx, Field *f) { + double value; + int changed = 0; + if (mu_textbox(ctx, f->buf, sizeof(f->buf)) & MU_RES_CHANGE) { + if (sscanf(f->buf, "%lf", &value) == 1) { + f->value = value; + changed = 1; + } else if (f->buf[0] == '\0') { + f->value = 0.0; + changed = 1; + } + } + return changed ? MU_RES_CHANGE : 0; +} diff --git a/widget.h b/widget.h new file mode 100644 index 0000000..a0149e0 --- /dev/null +++ b/widget.h @@ -0,0 +1,8 @@ +/* Field is a floating point number input field. */ +typedef struct { + char buf[64]; + double value; +} Field; + +void init_field(Field *f); +int field(mu_Context *ctx, Field *f); |