From 2eeb52c125842593fd8340af8deb268f286e1ce6 Mon Sep 17 00:00:00 2001 From: cancel Date: Tue, 4 Dec 2018 15:22:33 +0900 Subject: [PATCH] Add start of piano input --- cli_main.c | 2 +- sim.c | 6 ++++-- sim.h | 14 +++++++++++++- tui_main.c | 46 +++++++++++++++++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/cli_main.c b/cli_main.c index f8b6acf..116e151 100644 --- a/cli_main.c +++ b/cli_main.c @@ -104,7 +104,7 @@ int main(int argc, char** argv) { Usz max_ticks = (Usz)ticks; for (Usz i = 0; i < max_ticks; ++i) { orca_run(field.buffer, markmap_r.buffer, field.height, field.width, i, - &bank); + &bank, ORCA_PIANO_BITS_NONE); } markmap_reusable_deinit(&markmap_r); bank_deinit(&bank); diff --git a/sim.c b/sim.c index 4b1efce..1ddf1e0 100644 --- a/sim.c +++ b/sim.c @@ -825,7 +825,8 @@ BEGIN_DUAL_PHASE_1(variable) return; } Glyph result = bank_params->vars_slots[var_idx]; - if (result == '.') return; + if (result == '.') + return; POKE(1, 0, result); STUN(1, 0); END_PHASE @@ -932,7 +933,8 @@ static void sim_phase_1(Gbuffer gbuf, Mbuffer mbuf, Usz height, Usz width, } void orca_run(Gbuffer gbuf, Mbuffer mbuf, Usz height, Usz width, - Usz tick_number, Bank* bank) { + Usz tick_number, Bank* bank, Piano_bits piano_bits) { + (void)piano_bits; Glyph vars_slots[('Z' - 'A' + 1) + ('z' - 'a' + 1)]; memset(vars_slots, '.', sizeof(vars_slots)); mbuffer_clear(mbuf, height, width); diff --git a/sim.h b/sim.h index 2d3ba40..89a3428 100644 --- a/sim.h +++ b/sim.h @@ -3,5 +3,17 @@ #include "base.h" #include "mark.h" +#define ORCA_PIANO_KEYS_COUNT ((size_t)(('9' - '0') + 1 + ('z' - 'a') + 1)) +#define ORCA_PIANO_BITS_NONE UINT64_C(0) +typedef U64 Piano_bits; + +static inline Piano_bits piano_bits_of(Glyph g) { + if (g >= '0' && g <= '9') + return UINT64_C(1) << (U64)((Usz)('9' - g)); + if (g >= 'a' && g <= 'z') + return UINT64_C(1) << (U64)(((Usz)('z' - g)) + ((Usz)('9' - '0')) + 1); + return UINT64_C(0); +} + void orca_run(Gbuffer gbuf, Mbuffer markmap, Usz height, Usz width, - Usz tick_number, Bank* bank); + Usz tick_number, Bank* bank, Piano_bits piano_bits); diff --git a/tui_main.c b/tui_main.c index e57d2e5..3bf8016 100644 --- a/tui_main.c +++ b/tui_main.c @@ -355,6 +355,11 @@ void tui_resize_grid(Field* field, Markmap_reusable* markmap, Isz delta_h, enum { Argopt_margins = UCHAR_MAX + 1 }; +typedef enum { + Tui_input_mode_normal = 0, + Tui_input_mode_piano = 1, +} Tui_input_mode; + int main(int argc, char** argv) { static struct option tui_options[] = { {"margins", required_argument, 0, Argopt_margins}, @@ -491,6 +496,7 @@ int main(int argc, char** argv) { Tui_cursor tui_cursor; tui_cursor_init(&tui_cursor); + Tui_input_mode input_mode = Tui_input_mode_normal; Usz tick_num = 0; Usz ruler_spacing_y = 8; Usz ruler_spacing_x = 8; @@ -516,7 +522,7 @@ int main(int argc, char** argv) { field_resize_raw_if_necessary(&scratch_field, field.height, field.width); field_copy(&field, &scratch_field); orca_run(field.buffer, markmap_r.buffer, field.height, field.width, - tick_num, &bank); + tick_num, &bank, ORCA_PIANO_BITS_NONE); field_copy(&scratch_field, &field); needs_remarking = false; } @@ -559,6 +565,7 @@ int main(int argc, char** argv) { } wrefresh(cont_win); + Piano_bits piano_bits = ORCA_PIANO_BITS_NONE; int key; // ncurses gives us ERR if there was no user input. We'll sleep for 0 // seconds, so that we'll yield CPU time to the OS instead of looping as @@ -632,23 +639,40 @@ int main(int argc, char** argv) { tui_resize_grid(&field, &markmap_r, 1, 0, tick_num, &scratch_field, &undo_hist, &tui_cursor, &needs_remarking); break; + case '\\': + if (input_mode == Tui_input_mode_piano) { + input_mode = Tui_input_mode_normal; + } else { + input_mode = Tui_input_mode_piano; + } + break; case ' ': undo_history_push(&undo_hist, &field, tick_num); orca_run(field.buffer, markmap_r.buffer, field.height, field.width, - tick_num, &bank); + tick_num, &bank, piano_bits); ++tick_num; needs_remarking = true; break; default: - if (key >= '!' && key <= '~') { - undo_history_push(&undo_hist, &field, tick_num); - gbuffer_poke(field.buffer, field.height, field.width, tui_cursor.y, - tui_cursor.x, (char)key); - // Indicate we want the next simulation step to be run predictavely, so - // that we can use the reulsting mark buffer for UI visualization. This - // is "expensive", so it could be skipped for non-interactive input in - // situations where max throughput is necessary. - needs_remarking = true; + switch (input_mode) { + case Tui_input_mode_normal: { + if (key >= '!' && key <= '~') { + undo_history_push(&undo_hist, &field, tick_num); + gbuffer_poke(field.buffer, field.height, field.width, tui_cursor.y, + tui_cursor.x, (char)key); + // Indicate we want the next simulation step to be run predictavely, + // so that we can use the reulsting mark buffer for UI visualization. + // This is "expensive", so it could be skipped for non-interactive + // input in situations where max throughput is necessary. + needs_remarking = true; + } + } break; + case Tui_input_mode_piano: { + if (key >= '!' && key <= '~') { + Piano_bits added_bits = piano_bits_of((Glyph)key); + piano_bits |= added_bits; + } + } break; } #if 0 else {