From bb13b4a409578cf1e9fbca727196f88249fc4b0b Mon Sep 17 00:00:00 2001 From: grm Date: Sun, 19 Oct 2025 12:44:29 +0300 Subject: Add y-divider, generic vbar and fix fill math --- src/synth_gui.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 17 deletions(-) (limited to 'src/synth_gui.c') diff --git a/src/synth_gui.c b/src/synth_gui.c index 3fa2536..f96cccb 100644 --- a/src/synth_gui.c +++ b/src/synth_gui.c @@ -285,7 +285,7 @@ draw_wave(synth_t *synth, int x, int y, int width, int height) col = MAGENTA; else col = WHITE; - DrawPixel(i + x , y + height / 2 + (int)floor(50.0 * synth->viz.wave_viz_buffer[ii + j]), col); + DrawPixel(i + x , y + height / 2 + (int)floor(50.0 * synth->viz.wave_viz_buffer[ii + j] * synth->viz.y_divider), col); } } /* for (int j = 0; j < 100; j++) { */ @@ -551,13 +551,105 @@ draw_filter_adsr_graph(synth_t * synth, int x, int y, int width, int height) } } +float +map(float value, float start1, float end1, float start2, float end2) { + return start2 + (end2 - start2) * ((value - start1) / (end1 - start1)); +} + + +/** + * This uses the FNV-1a hash algorithm, which provides better distribution and + * handles any integer values without collisions from the combining operation + * itself. + */ +uint +rect_checksum(int x, int y, int width, int height) { + unsigned int hash = 2166136261u; // FNV offset basis + hash = (hash ^ x) * 16777619u; + hash = (hash ^ y) * 16777619u; + hash = (hash ^ width) * 16777619u; + hash = (hash ^ height) * 16777619u; + return hash; +} + +uint rect_id_pressed = 0; + +float +generic_vbar(float val, float def, float min, float max, int x, int y, int width, int height) { + + Vector2 p = GetMousePosition(); + uint checksum = rect_checksum(x, y, width, height); + + if (CheckCollisionPointRec(p, (Rectangle){x, y, width, height})) { + if (IsMouseButtonPressed(0)) { + rect_id_pressed = checksum; + } + if (IsMouseButtonPressed(1)) { + val = def; + } + } + if (IsMouseButtonDown(0) && rect_id_pressed == checksum) { + if (p.y < y) { + val = max; + } else if (p.y >= y + height) { + val = min; + } else { + val = min + (max - min) * (1.0f - ((p.y - y) / (float)height)); + } + } + if (IsMouseButtonReleased(0) && rect_id_pressed == checksum) { + rect_id_pressed = 0; + } + + int fill_height = map(val, min, max, 0, height - 2); + + DrawRectangleLines(x, y, width, height, WHITE); + DrawRectangle(x + 1, y + height - fill_height - 1, width - 2, fill_height, Fade(MAROON, 0.7f)); + + return val; +} + void draw_signals(synth_t * synth, int x, int y, int width, int height) { DrawRectangleLines(x, y, width, height, WHITE); - if (synth->viz.wave_enabled || synth->viz.spectrum_enabled) - synth->viz.rate_divider = GuiSlider((Rectangle){ x + (width / 2.0) / 2, y - 12, width / 2.0, 12 }, "", NULL, synth->viz.rate_divider , 1, 150); + if (synth->viz.wave_enabled || synth->viz.spectrum_enabled) { + synth->viz.rate_divider = + GuiSlider((Rectangle){x + (width / 2.0) / 2, y - 12, width / 2.0, 12}, + "", NULL, synth->viz.rate_divider, 1, 150); + synth->viz.y_divider = generic_vbar(synth->viz.y_divider, 1.0, 0.5, 3.0, x - 10, y, 10, height); + /* /\* GuiSlider((Rectangle){x - 10, y, 10, height}, "", NULL, *\/ */ + /* /\* synth->viz.y_divider, 0.5, 3); *\/ */ + + /* Vector2 p = GetMousePosition(); */ + + /* if (CheckCollisionPointRec(p, (Rectangle){x - 10, y, 10, height})) { */ + /* if (IsMouseButtonPressed(0)) { */ + /* is_y_divider_pressed = 1; */ + /* } */ + /* } */ + /* float min = 0.5; */ + /* float max = 3; */ + /* if (IsMouseButtonDown(0) && is_y_divider_pressed) { */ + /* if (p.y < y) { */ + /* synth->viz.y_divider = max; */ + /* } else if (p.y >= y + height) { */ + /* synth->viz.y_divider = min; */ + /* } else { */ + /* synth->viz.y_divider = min + max - (min + (max - min)) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0)); */ + /* } */ + /* } */ + /* if (IsMouseButtonReleased(0) && is_y_divider_pressed) { */ + /* is_y_divider_pressed = 0; */ + /* } */ + + /* int fill_height = map(synth->viz.y_divider, min, max, 0, height - 2); */ + + /* DrawRectangleLines(x - 10, y, 10, height, WHITE); */ + /* DrawRectangle(x - 10 + 1, y + height - fill_height - 1, 10 - 2, fill_height, Fade(MAROON, 0.7f)); */ + + } int viz_size = width * synth->viz.rate_divider; @@ -645,11 +737,6 @@ draw_cc_circle(cc_t * cc, int x, int y, int width, int height) { } -float -map(float value, float start1, float end1, float start2, float end2) { - return start2 + (end2 - start2) * ((value - start1) / (end1 - start1)); -} - #define CC_SET(cc, value) cc->mod = value - cc->target #include "web.h" void @@ -665,14 +752,11 @@ draw_cc_hbar(cc_t * cc, int x, int y, int width, int height) { } if (IsMouseButtonDown(0) && flag == cc->name) { if (p.x < x) { - //cc->target = cc->min; cc_set(cc, cc->min); } else if (p.x >= x + width) { - //cc->target = cc->max; cc_set(cc, cc->max); } else { - //cc->target = (cc->min + (cc->max - cc->min) * ((((p.x - x) * (float)1/width) - 0) / (1 - 0))); - cc_set(cc, (cc->min + (cc->max - cc->min) * ((((p.x - x) * (float)1/width) - 0) / (1 - 0)))); + cc_set(cc, cc->min + (cc->max - cc->min) * ((p.x - x) / (float)width)); } char tmp[128] = ""; sprintf(tmp, "%f", cc->value); @@ -699,7 +783,6 @@ draw_cc_hbar(cc_t * cc, int x, int y, int width, int height) { void draw_cc_vbar(cc_t * cc, int x, int y, int width, int height) { char buf1[32], buf2[32]; - //int current = height * (cc->target - cc->min) / (cc->max - cc->min) + cc->min - 1; Vector2 p = GetMousePosition(); @@ -710,14 +793,11 @@ draw_cc_vbar(cc_t * cc, int x, int y, int width, int height) { } if (IsMouseButtonDown(0) && flag == cc->name) { if (p.y < y) { - //cc->target = cc->max; cc_set(cc, cc->max); } else if (p.y >= y + height) { - //cc->target = cc->min; cc_set(cc, cc->min); } else { - //cc->target = cc->min + cc->max - (cc->min + (cc->max - cc->min) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0))); - cc_set(cc, (cc->min + cc->max - (cc->min + (cc->max - cc->min) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0))))); + cc_set(cc, cc->min + (cc->max - cc->min) * (1.0f - ((p.y - y) / (float)height))); } } if (IsMouseButtonReleased(0) && flag == cc->name) { -- cgit v1.2.3