aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-03-01 12:27:50 -0500
committerSam Anthony <sam@samanthony.xyz>2025-03-01 12:27:50 -0500
commit8cd4a9079a3a4343c2e2fa34d41901569541ec98 (patch)
treeb5c94d79823c73eab85e86bc16ae8940b6b51145
parent89fd08ec37e6b3d5191d20c2e3d219579255d934 (diff)
downloadvolute-8cd4a9079a3a4343c2e2fa34d41901569541ec98.zip
reformat displacement when unit changes
-rw-r--r--main.c6
-rw-r--r--ui.c15
-rw-r--r--ui.h1
-rw-r--r--widget.c8
-rw-r--r--widget.h2
5 files changed, 31 insertions, 1 deletions
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)] = {
@@ -66,6 +69,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;
PressureMaker convert;
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
@@ -33,10 +33,17 @@ w_field(mu_Context *ctx, w_Field *f) {
}
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;