aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--main.c34
-rw-r--r--ui.c8
-rw-r--r--ui.h5
-rw-r--r--widget.c28
-rw-r--r--widget.h8
6 files changed, 74 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index a075288..11a5ef4 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/main.c b/main.c
index d36d9be..fb0770a 100644
--- a/main.c
+++ b/main.c
@@ -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);
}
diff --git a/ui.c b/ui.c
new file mode 100644
index 0000000..f44e89d
--- /dev/null
+++ b/ui.c
@@ -0,0 +1,8 @@
+#include "microui.h"
+#include "widget.h"
+#include "ui.h"
+
+void
+init_ui(UI *ui) {
+ init_field(&ui->displacement);
+}
diff --git a/ui.h b/ui.h
new file mode 100644
index 0000000..9fa170f
--- /dev/null
+++ b/ui.h
@@ -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);