diff --git a/CMakeLists.txt b/CMakeLists.txt index b6a534f..4ed5bf4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ endif() set(SOURCES kubo_input.c - kubo_command.c + kubo_command_bar.c kubo_bar.c kubo_wall.c kubo_window.c diff --git a/kubo_bar.c b/kubo_bar.c index c269d54..dab27c0 100644 --- a/kubo_bar.c +++ b/kubo_bar.c @@ -31,7 +31,7 @@ void kubo_bar_render(struct kubo_context *context) { bar_text_x += 2 * KUBO_BAR_PADDING; if (context->state.id == KUBO_CONTEXT_COMMAND) { - kubo_command_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2), + kubo_command_bar_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2), KUBO_BAR_HEIGHT - KUBO_BAR_PADDING); } } @@ -45,4 +45,3 @@ static inline int push_text(int x, int y, const char *text, Color bg_color) { DrawText(text, x, y + (KUBO_BAR_PADDING / 2), font_size, WHITE); return x + text_width; } - diff --git a/kubo_bar.h b/kubo_bar.h index 9b0ee83..13836cd 100644 --- a/kubo_bar.h +++ b/kubo_bar.h @@ -25,7 +25,7 @@ #include #include "kubo_context.h" -#include "kubo_command.h" +#include "kubo_command_bar.h" void kubo_bar_render(struct kubo_context *context); diff --git a/kubo_command.c b/kubo_command_bar.c similarity index 86% rename from kubo_command.c rename to kubo_command_bar.c index a1f2eb9..a7e4714 100644 --- a/kubo_command.c +++ b/kubo_command_bar.c @@ -16,10 +16,10 @@ * Kubo. If not, see . */ -#include "kubo_command.h" +#include "kubo_command_bar.h" -void kubo_command_render(struct kubo_context *context, int x, int y, - int font_size) { +void kubo_command_bar_render(struct kubo_context *context, int x, int y, + int font_size) { char *cmd = kubo_char_arr_build_str(&context->command); DrawText(cmd, x, y, font_size, WHITE); diff --git a/kubo_command.h b/kubo_command_bar.h similarity index 81% rename from kubo_command.h rename to kubo_command_bar.h index 99a1362..058f2bf 100644 --- a/kubo_command.h +++ b/kubo_command_bar.h @@ -16,14 +16,14 @@ * Kubo. If not, see . */ -#ifndef KUBO_COMMAND_H -#define KUBO_COMMAND_H +#ifndef KUBO_COMMAND_BAR_H +#define KUBO_COMMAND_BAR_H #include #include "kubo_context.h" -void kubo_command_render(struct kubo_context *context, int x, int y, - int font_size); +void kubo_command_bar_render(struct kubo_context *context, int x, int y, + int font_size); #endif diff --git a/kubo_context.c b/kubo_context.c index ca2e290..c45c666 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -18,7 +18,15 @@ #include "kubo_context.h" -const struct kubo_context_state_data kubo_context_states[] = { +static inline void kubo_command_exit(struct kubo_context *context); + +static const struct kubo_command_data kubo_commands[] = { + {":q", kubo_command_exit}}; + +static const size_t kubo_commands_size = + sizeof(kubo_commands) / sizeof(kubo_commands[0]); + +static 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}, @@ -67,7 +75,14 @@ void kubo_context_set_state(struct kubo_context *context, } void kubo_context_accept_cmd(struct kubo_context *context) { - printf("accepting cmd %s\n", kubo_char_arr_build_str(&context->command)); + for (size_t i = 0; i < kubo_commands_size; i++) { + if (strcmp(kubo_commands[i].input, + kubo_char_arr_build_str(&context->command)) == 0) { + kubo_commands[i].function(context); + return; + } + } + kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); } @@ -129,3 +144,7 @@ static inline void wall_select_refresh(struct kubo_context *context) { : KUBO_WALL_DONE; } } + +static inline void kubo_command_exit(struct kubo_context *context) { + context->exit_pending = true; +} diff --git a/kubo_context.h b/kubo_context.h index e495f38..27c095b 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "kubo_char_arr.h" #include "kubo_dynarray.h" @@ -54,6 +55,13 @@ struct kubo_context { size_t wall_select_index; }; +typedef void (*kubo_command_function)(struct kubo_context *context); + +struct kubo_command_data { + const char *input; + kubo_command_function function; +}; + void kubo_context_init(struct kubo_context *context); void kubo_context_cleanup(struct kubo_context *context);