From 8cd4a9079a3a4343c2e2fa34d41901569541ec98 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 1 Mar 2025 12:27:50 -0500 Subject: reformat displacement when unit changes --- main.c | 6 +++++- ui.c | 15 +++++++++++++++ ui.h | 1 + widget.c | 8 ++++++++ widget.h | 2 ++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 2710bfa..9783fd1 100644 --- a/main.c +++ b/main.c @@ -157,7 +157,11 @@ displacement_row(mu_Context *ctx, UI *ui) { set_displacement(ui); set_all_volume_flow_rate(ui); } - w_select(ctx, &ui->displacement_unit); + if (w_select(ctx, &ui->displacement_unit) & MU_RES_CHANGE) { + printf("displacement unit changed\n"); + fflush(stdout); + set_displacement_unit(ui); + } } static void diff --git a/ui.c b/ui.c index 3fca1ae..216a10b 100644 --- a/ui.c +++ b/ui.c @@ -16,6 +16,9 @@ static const char *const volume_units[] = {"cc", "l", "ci"}; static const VolumeMaker volume_makers[nelem(volume_units)] = { cubic_centimetre, litre, cubic_inch, }; +static const VolumeReader volume_readers[nelem(volume_units)] = { + as_cubic_centimetre, as_litre, as_cubic_inch, +}; static const char *const pressure_units[] = {"mbar", "kPa", "bar", "psi"}; static const PressureMaker pressure_makers[nelem(pressure_units)] = { @@ -65,6 +68,18 @@ set_displacement(UI *ui) { } } +void +set_displacement_unit(UI *ui) { + VolumeMaker maker; + Volume disp; + VolumeReader reader; + + maker = volume_makers[ui->displacement_unit.oldidx]; + disp = maker(ui->displacement.value); + reader = volume_readers[ui->displacement_unit.idx]; + w_set_field(&ui->displacement, reader(disp)); +} + void set_map(UI *ui, int idx) { int unit_idx; diff --git a/ui.h b/ui.h index f1c4137..1238325 100644 --- a/ui.h +++ b/ui.h @@ -21,6 +21,7 @@ typedef struct { void init_ui(UI *ui); void set_displacement(UI *ui); +void set_displacement_unit(UI* ui); void set_map(UI *ui, int idx); void set_ve(UI *ui, int idx); void set_volume_flow_rate(UI *ui, int idx); diff --git a/widget.c b/widget.c index cc9d894..46c7b07 100644 --- a/widget.c +++ b/widget.c @@ -32,11 +32,18 @@ w_field(mu_Context *ctx, w_Field *f) { return changed ? MU_RES_CHANGE : 0; } +void +w_set_field(w_Field *f, double val) { + f->value = val; + snprintf(f->buf, sizeof(f->buf), "%lf", val); +} + void w_init_select(w_Select *select, int nopts, const char *const opts[]) { select->nopts = nopts; select->opts = opts; select->idx = 0; + select->oldidx = 0; select ->active = 0; } @@ -65,6 +72,7 @@ w_select(mu_Context *ctx, w_Select *select) { res = MU_RES_ACTIVE; for (i = 0; i < select->nopts; i++) { if (mu_button(ctx, select->opts[i])) { + select->oldidx = select->idx; select->idx = i; res |= MU_RES_CHANGE; select->active = 0; diff --git a/widget.h b/widget.h index 546ed1c..bc6a375 100644 --- a/widget.h +++ b/widget.h @@ -9,12 +9,14 @@ typedef struct { void w_init_field(w_Field *f); int w_field(mu_Context *ctx, w_Field *f); +void w_set_field(w_Field *f, double v); typedef struct { int nopts; const char *const *opts; int idx; /* index of selected option. */ + int oldidx; /* index of previously selected option. */ int active; } w_Select; -- cgit v1.2.3