ntrop

2D binary entropy visualization inspired by ..cantor.dust..
git clone https://git.sinitax.com/sinitax/ntrop
Log | Files | Refs | sfeed.txt

commit 1b41ae91d6016ae2031aa17cfd1a50cb16727421
parent f165df94468786319400344fa7fd813e8e1899a7
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 13 Feb 2023 01:44:43 +0100

Improve user input handling with vargs key_press_hold

Diffstat:
Mntrop.c | 132++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 90 insertions(+), 42 deletions(-)

diff --git a/ntrop.c b/ntrop.c @@ -13,6 +13,8 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) > (b) ? (b) : (a)) +enum { PRESS = 1, HOLD }; + const char *file_path; uint8_t *file_data; ssize_t data_len; @@ -162,19 +164,67 @@ init_entropy_colors(Color *data_colors) free(counts); } -bool -key_press_hold(int key) +int +key_press_hold(int cnt, ...) { - if (!IsKeyDown(key)) { - hold_times[key] = 0; - return false; + va_list ap; + int i, key; + + va_start(ap, cnt); + for (i = 0; i < cnt; i++) { + key = va_arg(ap, int); + + if (!IsKeyDown(key)) { + hold_times[key] = 0; + continue; + } + + if (IsKeyPressed(key)) + return PRESS; + + hold_times[key] += 1; + if (hold_times[key] > 10) + return HOLD; } - if (IsKeyPressed(key)) - return true; - hold_times[key] += 1; + return 0; +} + +void +update_data_window_start(void) +{ + ssize_t len; - return hold_times[key] > 10; + len = data_len - data_window_len; + if (len % data_width) + len += data_width - (len % data_width); + data_window_start = MAX(0, MIN(len, data_window_start)); +} + +void +update_data_window_len(void) +{ + update_data_window_start(); + data_window_len = MIN(data_len - data_window_start, data_window_len); + data_window_len = MAX(0, data_window_len); + data_height = data_window_len / data_width; + + zoom_y = (data_height - 1.F * window_height / zoom) / 2.F; +} + +void +update_data_width(void) +{ + + data_width = MAX(1, data_width); + update_data_window_len(); + + if (show_bar) { + zoom_x = - 1.F * bar_width / zoom + - (1.F * (window_width - bar_width) / zoom - data_width) / 2.F; + } else { + zoom_x = -(1.F * window_width / zoom - data_width) / 2.F; + } } void @@ -188,6 +238,7 @@ vis(void) ssize_t data_x, data_y; ssize_t data_pos; ssize_t x, y; + int b; data_colors = value_colors; show_value = true; @@ -236,8 +287,7 @@ vis(void) } if (IsKeyPressed(KEY_G)) zoom = 2; - zoom_x = (data_width - 1.F * window_width / zoom) / 2.F; - zoom_y = (data_height - 1.F * window_height / zoom) / 2.F; + update_data_width(); ImageResize(&window_image, window_width, window_height); if (window_init_frames) window_init_frames -= 1; @@ -268,6 +318,7 @@ vis(void) data_window_start = bar_start + (mouse_y * bar_width + mouse_x) * bar_zoom; data_window_start = MAX(0, MIN(data_len - data_window_len, data_window_start - data_window_len / 2)); + update_data_window_start(); } else if (drag) { zoom_x = drag_zoom_x - (mouse_x - drag_mouse_x) * 1.F / zoom; zoom_y = drag_zoom_y - (mouse_y - drag_mouse_y) * 1.F / zoom; @@ -291,51 +342,48 @@ vis(void) show_pos ^= IsKeyPressed(KEY_T); show_bar ^= IsKeyPressed(KEY_B); - if (IsKeyDown(KEY_LEFT_CONTROL)) { - if (key_press_hold(KEY_LEFT) || key_press_hold(KEY_A)) { - data_width -= IsKeyDown(KEY_LEFT_SHIFT) ? 5 : 1; - } else if (key_press_hold(KEY_RIGHT) || key_press_hold(KEY_D)) { - data_width += IsKeyDown(KEY_LEFT_SHIFT) ? 5 : 1; - } - data_width = MAX(1, data_width); - } else if (IsKeyDown(KEY_LEFT_SHIFT)) { - if (key_press_hold(KEY_LEFT) || key_press_hold(KEY_A)) { - data_window_start += 1; - } else if (key_press_hold(KEY_RIGHT) || key_press_hold(KEY_D)) { - data_window_start -= 1; + if (IsKeyDown(KEY_LEFT_SHIFT)) { + if ((b = key_press_hold(2, KEY_LEFT, KEY_A))) { + data_width -= IsKeyDown(KEY_LEFT_ALT) ? 5 : 1; + update_data_width(); + } else if ((b = key_press_hold(2, KEY_RIGHT, KEY_D))) { + data_width += IsKeyDown(KEY_LEFT_ALT) ? 5 : 1; + update_data_width(); } } else if (IsKeyDown(KEY_LEFT_ALT)) { - if (key_press_hold(KEY_LEFT) || key_press_hold(KEY_A)) { - data_window_start -= data_width * 8; - } else if (key_press_hold(KEY_RIGHT) || key_press_hold(KEY_D)) { - data_window_start += data_width * 8; + if ((b = key_press_hold(2, KEY_LEFT, KEY_A))) { + data_window_start -= data_width + * (b == HOLD ? 8 : 1); + update_data_window_start(); + } else if ((b = key_press_hold(2, KEY_RIGHT, KEY_D))) { + data_window_start += data_width + * (b == HOLD ? 8 : 1); + update_data_window_start(); } - if (key_press_hold(KEY_UP) || key_press_hold(KEY_W)) { - data_window_len -= data_width * 8; - } else if (key_press_hold(KEY_DOWN) || key_press_hold(KEY_S)) { - data_window_len += data_width * 8; + if ((b = key_press_hold(2, KEY_UP, KEY_W))) { + data_window_len -= data_width + * (b == HOLD ? 8 : 1); + update_data_window_len(); + } else if ((b = key_press_hold(2, KEY_DOWN, KEY_S))) { + data_window_len += data_width + * (b == HOLD ? 8 : 1); + update_data_window_len(); } } else { - if (key_press_hold(KEY_LEFT) || key_press_hold(KEY_A)) { + if (key_press_hold(2, KEY_LEFT, KEY_A)) { zoom_x -= 20.F / zoom; - } else if (key_press_hold(KEY_RIGHT) || key_press_hold(KEY_D)) { + } else if (key_press_hold(2, KEY_RIGHT, KEY_D)) { zoom_x += 20.F / zoom; } - if (key_press_hold(KEY_UP) || key_press_hold(KEY_W)) { + if (key_press_hold(2, KEY_UP, KEY_W)) { zoom_y -= 20.F / zoom; - } else if (key_press_hold(KEY_DOWN) || key_press_hold(KEY_S)) { + } else if (key_press_hold(2, KEY_DOWN, KEY_S)) { zoom_y += 20.F / zoom; } } - len = data_len - data_window_len; - if (len % data_width) - len += data_width - (len % data_width); - data_window_start = MAX(0, MIN(len, data_window_start)); - data_window_len = MAX(0, MIN(len, data_window_len)); - data_height = data_window_len / data_width; - + if (IsKeyPressed(KEY_P)) { show_value = !show_value; if (show_value)