diff options
Diffstat (limited to 'src/synth_gui.c')
| -rw-r--r-- | src/synth_gui.c | 206 |
1 files changed, 205 insertions, 1 deletions
diff --git a/src/synth_gui.c b/src/synth_gui.c index 62a6ab7..3fa2536 100644 --- a/src/synth_gui.c +++ b/src/synth_gui.c @@ -3,6 +3,7 @@ #include <portaudio.h> #define RAYGUI_IMPLEMENTATION #include "raygui.h" +#include "os.h" //#include "raylib.h" void @@ -895,6 +896,205 @@ draw_audiomidisetup(synth_t *synth) } } +char** split_string(const char* str, char delimiter, int* itemCount) { + if (!str || !itemCount) { + if (itemCount) *itemCount = 0; + return NULL; + } + + // First pass: count delimiters to determine array size + int count = 1; + for (int i = 0; str[i]; i++) { + if (str[i] == delimiter) { + count++; + } + } + + // Allocate array of string pointers + char** result = malloc(count * sizeof(char*)); + if (!result) { + *itemCount = 0; + return NULL; + } + + // Create a working copy of the string + char* str_copy = malloc(strlen(str) + 1); + if (!str_copy) { + free(result); + *itemCount = 0; + return NULL; + } + strcpy(str_copy, str); + + // Second pass: split the string + int index = 0; + char* token = str_copy; + char* next_delim; + + while ((next_delim = strchr(token, delimiter)) != NULL) { + *next_delim = '\0'; // Replace delimiter with null terminator + + // Allocate memory for this substring and copy it + result[index] = malloc(strlen(token) + 1); + if (result[index]) { + strcpy(result[index], token); + } + index++; + token = next_delim + 1; // Move to next token + } + + // Handle the last token (after the last delimiter or the whole string if no delimiters) + result[index] = malloc(strlen(token) + 1); + if (result[index]) { + strcpy(result[index], token); + } + + free(str_copy); + *itemCount = count; + return result; +} + +// Function to free the memory allocated by split_string +void free_split_result(char** result, int itemCount) { + if (result) { + for (int i = 0; i < itemCount; i++) { + free(result[i]); + } + free(result); + } +} + +int +gui_string_spinner(Rectangle rect, char * text, int * index) +{ + int itemCount = 0; + char **items = NULL; + int change = 0; + + float m = GetMouseWheelMove(); + int x = 0; + if (m < 0) x = -1; + else if (m > 0) x = 1; + Vector2 p = GetMousePosition(); + + if (text != NULL) items = split_string(text, ';', &itemCount); + + if (CheckCollisionPointRec(p, rect)) { + if (x > 0) { + if (IsKeyDown(KEY_LEFT_SHIFT)) { + (*index)+=10; + } else { + (*index)++; + } + if (*index >= itemCount) { + *index = itemCount - 1; // set to itemCound to loop + } + change = 1; + } else if (x < 0) { + if (IsKeyDown(KEY_LEFT_SHIFT)) { + (*index)-=10; + } else { + (*index)--; + } + if (*index <= -1) { + *index = 0; // set to itemCound to loop + } + change = 1; + } + } + + if (GuiButton((Rectangle){rect.x, rect.y, rect.height, rect.height}, "<")) { + (*index)--; + if (*index <= -1) { + *index = 0; // set to itemCound to loop + } + change = 1; + } + float box_x = rect.x + (rect.height + 1); + float box_width = rect.width - (rect.height + 1) - (rect.height + 1); + DrawRectangleLines(box_x, rect.y, box_width, rect.height, WHITE); + + int text_size = 10; + + char buf[1024] = ""; + + sprintf(buf, "%d: %s", *index, items[*index]); + DrawText(buf, + box_x + box_width/2 - (float)MeasureText(buf, text_size) / 2, + rect.y + rect.height / 2 - (float)text_size / 2, + text_size, + WHITE); + + if (GuiButton((Rectangle){rect.x + (rect.height + 1) + rect.width - (rect.height + 1) - (rect.height + 1), rect.y, rect.height, rect.height}, ">")) { + (*index)++; + if (*index >= itemCount) { + *index = itemCount - 1; // set to itemCound to loop + } + change = 1; + } + + free_split_result(items, itemCount); + + return change; +} + +int prev_active = -1; + +int sound_file_picker_initialized = 0; +char * wavs = NULL; +char * wavs_full = NULL; + +void +draw_sound_file_picker(Rectangle rect) +{ + static int scroll_index = 0; // Current scroll position + static int active_item = 0; // Currently selected item (-1 means none selected) + + if (!sound_file_picker_initialized) { + wavs = scan_for_wav_pretty("/home/grm/code/synth-project-b/waves/Free Wavetables[2048]"); + //wavs[strlen(wavs) - 1] = '\0'; // remove last ; + wavs_full = scan_for_wav("/home/grm/code/synth-project-b/waves/Free Wavetables[2048]"); + //wavs_full[strlen(wavs_full) - 1] = '\0'; + /* printf("strlen wavs: %ld\n", strlen(wavs)); */ + /* printf("strlen wavs_full: %ld\n", strlen(wavs_full)); */ + sound_file_picker_initialized = 1; + } + + /* char aaa[10000] = ""; */ + /* char *t = aaa; */ + /* int i = 0; */ + /* for (int j =0; j < 1000; j++) { */ + /* sprintf(t, "%d;", i++); */ + /* t += strlen(t); */ + /* } */ + /* aaa[strlen(aaa) - 1] = '\0'; */ + + if (gui_string_spinner(rect, wavs, &scroll_index)) { + char path[1024] = ""; + get_nth_entry(wavs_full, scroll_index, path); + printf("changing wavetable to: %s\n", path); + osc_sound_change_wavetable(path); + } + prev_active = active_item; + //active_item = GuiListView(rect, aaa, &scroll_index, active_item); + /* if (prev_active != active_item && active_item != -1) { */ + /* char path[1024] = ""; */ + /* get_nth_entry(wavs_full, active_item, path); */ + /* printf("changing wavetable to: %s\n", path); */ + /* osc_sound_change_wavetable(path); */ + /* // sound changed */ + /* } */ + /* if (GuiDropdownBox((Rectangle){WIDTH - 300 - 50, */ + /* 12 + 24 + 6, 300, */ + /* 24}, */ + /* wavs, &wav_pick, edit_wav)) { */ + /* edit_wav = !edit_wav; */ + /* } */ + //free(wavs); + //free(wavs_full); + +} + void draw_main(synth_t *synth) { @@ -985,6 +1185,10 @@ draw_main(synth_t *synth) // signals draw_signals(synth, 20, 390, WIDTH - 2*20, 200); + + if (synth->geni == 4) + draw_sound_file_picker((Rectangle){ WIDTH / 2.0 - 128, 150 + 122, 256 + 60, 24 }); + //draw_signals(synth, 300, 390, WIDTH - 2*300, 200); //DrawText("THE SYNTH!!!!!!!!!!!!!!!!!!1", WIDTH / 2 - 100, 50, 20, LIGHTGRAY); @@ -1049,7 +1253,7 @@ rayrun(synth_t *synth){ //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(BLACK); - + if (synth->gui.screen == SCREEN_MAIN) draw_main(synth); else if (synth->gui.screen == SCREEN_AUDIOMIDISETUP) |
