From 7528ed007eb99cbcc98d6b403e489889a2b0c626 Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Wed, 2 Jul 2025 18:45:55 +0200 Subject: [PATCH] reworked context state handling --- kubo_bar.c | 7 +++---- kubo_context.c | 28 ++++++++++++++-------------- kubo_context.h | 13 ++++++------- kubo_window.c | 10 +++++----- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/kubo_bar.c b/kubo_bar.c index 9ea994b..ec9f05a 100644 --- a/kubo_bar.c +++ b/kubo_bar.c @@ -34,13 +34,12 @@ void kubo_bar_render(struct kubo_context *context) { int bar_text_x = 10; DrawRectangle(0, bar_y, GetScreenWidth(), KUBO_BAR_HEIGHT, BLACK); - bar_text_x = - push_text(bar_text_x, bar_y, kubo_context_get_label(context->state), - kubo_context_get_color(context->state)); + bar_text_x = push_text(bar_text_x, bar_y, context->state.label, + context->state.color); bar_text_x += 2 * KUBO_BAR_PADDING; - if (context->state == KUBO_CONTEXT_COMMAND) { + if (context->state.id == KUBO_CONTEXT_COMMAND) { kubo_command_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2), KUBO_BAR_HEIGHT - KUBO_BAR_PADDING); } diff --git a/kubo_context.c b/kubo_context.c index 8a43b69..1596416 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -18,6 +18,12 @@ #include "kubo_context.h" +const struct kubo_context_state_data kubo_context_states[] = { + {.id = KUBO_CONTEXT_NORMAL, .label = "Normal", .color = BLACK}, + {.id = KUBO_CONTEXT_COMMAND, .label = "Command", .color = RED}, + {.id = KUBO_CONTEXT_WALL_NEW, .label = "Wall New", .color = GREEN}, + {.id = KUBO_CONTEXT_WALL_SELECT, .label = "Wall Select", .color = BLUE}}; + void kubo_context_init(struct kubo_context *context) { if (!context) { return; @@ -28,7 +34,7 @@ void kubo_context_init(struct kubo_context *context) { context->wall_select_index = 0; context->exit_pending = false; - context->state = KUBO_CONTEXT_NORMAL; + context->state = kubo_context_states[KUBO_CONTEXT_NORMAL]; } void kubo_context_cleanup(struct kubo_context *context) { @@ -39,9 +45,9 @@ void kubo_context_cleanup(struct kubo_context *context) { void kubo_context_set_state(struct kubo_context *context, enum kubo_context_state state) { kubo_char_arr_clear(&context->command); - context->state = state; + context->state = kubo_context_states[state]; - switch (context->state) { + switch (context->state.id) { case KUBO_CONTEXT_COMMAND: kubo_char_arr_add(&context->command, ':'); break; @@ -58,14 +64,6 @@ void kubo_context_set_state(struct kubo_context *context, } } -const char *kubo_context_get_label(enum kubo_context_state state) { - return kubo_context_labels[state]; -} - -Color kubo_context_get_color(enum kubo_context_state state) { - return kubo_context_colors[state]; -} - void kubo_context_accept_cmd(struct kubo_context *context) { printf("accepting cmd %s\n", kubo_char_arr_build_str(&context->command)); kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); @@ -73,7 +71,7 @@ void kubo_context_accept_cmd(struct kubo_context *context) { struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { - if (context->state != KUBO_CONTEXT_WALL_NEW) { + if (context->state.id != KUBO_CONTEXT_WALL_NEW) { return NULL; } @@ -97,7 +95,8 @@ struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { } void kubo_context_select_next_wall(struct kubo_context *context) { - if (context->state != KUBO_CONTEXT_WALL_SELECT || !context->walls.count) { + if (context->state.id != KUBO_CONTEXT_WALL_SELECT || + !context->walls.count) { return; } @@ -108,7 +107,8 @@ void kubo_context_select_next_wall(struct kubo_context *context) { } void kubo_context_select_prev_wall(struct kubo_context *context) { - if (context->state != KUBO_CONTEXT_WALL_SELECT || !context->walls.count) { + if (context->state.id != KUBO_CONTEXT_WALL_SELECT || + !context->walls.count) { return; } diff --git a/kubo_context.h b/kubo_context.h index 3db109a..3f78a62 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -36,15 +36,16 @@ enum kubo_context_state { KUBO_CONTEXT_WALL_SELECT, }; -static const char *kubo_context_labels[] = {"Normal", "Command", "Wall New", - "Wall Select"}; - -static const Color kubo_context_colors[] = {BLACK, RED, GREEN, BLUE}; +struct kubo_context_state_data { + enum kubo_context_state id; + char *label; + Color color; +}; struct kubo_context { bool exit_pending; struct kubo_wall_arr walls; - enum kubo_context_state state; + struct kubo_context_state_data state; // KUBO_CONTEXT_COMMAND struct kubo_char_arr command; @@ -58,8 +59,6 @@ void kubo_context_cleanup(struct kubo_context *context); void kubo_context_set_state(struct kubo_context *context, enum kubo_context_state state); -const char *kubo_context_get_label(enum kubo_context_state state); -Color kubo_context_get_color(enum kubo_context_state state); void kubo_context_accept_cmd(struct kubo_context *context); diff --git a/kubo_window.c b/kubo_window.c index 52033ef..7bb4792 100644 --- a/kubo_window.c +++ b/kubo_window.c @@ -49,7 +49,7 @@ static void window_render(struct kubo_context *context) { struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i); assert(wall != NULL); - kubo_wall_render(wall, context->state == KUBO_CONTEXT_WALL_SELECT); + kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT); } kubo_bar_render(context); @@ -58,7 +58,7 @@ static void window_render(struct kubo_context *context) { } static void window_left_mouse(struct kubo_context *context) { - switch (context->state) { + switch (context->state.id) { case KUBO_CONTEXT_WALL_NEW: new_wall_click(context); break; @@ -70,7 +70,7 @@ static void window_left_mouse(struct kubo_context *context) { } static void window_right_mouse(struct kubo_context *context) { - switch (context->state) { + switch (context->state.id) { case KUBO_CONTEXT_WALL_NEW: new_wall_end(context); break; @@ -87,7 +87,7 @@ static void window_key_input(struct kubo_context *context) { int key_code = GetKeyPressed(); - if (context->state == KUBO_CONTEXT_COMMAND) { + if (context->state.id == KUBO_CONTEXT_COMMAND) { switch (key_code) { case KEY_ESCAPE: kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); @@ -143,7 +143,7 @@ static void window_char_input(struct kubo_context *context) { do { char_code = GetCharPressed(); - if (char_code && context->state == KUBO_CONTEXT_COMMAND) { + if (char_code && context->state.id == KUBO_CONTEXT_COMMAND) { kubo_char_arr_add(&context->command, char_code); } else if (char_code == ':') { kubo_context_set_state(context, KUBO_CONTEXT_COMMAND);