reworked context state handling

This commit is contained in:
Luka Jankovic 2025-07-02 18:45:55 +02:00
parent 5eb4437ac4
commit 7528ed007e
4 changed files with 28 additions and 30 deletions

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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);

View file

@ -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);