diff --git a/CMakeLists.txt b/CMakeLists.txt index c3c2532..a7726b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ set(SOURCES kubo_wall.c kubo_window.c kubo_context.c + kubo_command.c main.c ) diff --git a/kubo_bar.c b/kubo_bar.c index dab27c0..5cee631 100644 --- a/kubo_bar.c +++ b/kubo_bar.c @@ -31,8 +31,8 @@ void kubo_bar_render(struct kubo_context *context) { bar_text_x += 2 * KUBO_BAR_PADDING; if (context->state.id == KUBO_CONTEXT_COMMAND) { - kubo_command_bar_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2), - KUBO_BAR_HEIGHT - KUBO_BAR_PADDING); + kubo_command_bar_render(bar_text_x, bar_y + (KUBO_BAR_PADDING / 2), + KUBO_BAR_HEIGHT - KUBO_BAR_PADDING); } } diff --git a/kubo_command.c b/kubo_command.c new file mode 100644 index 0000000..51be490 --- /dev/null +++ b/kubo_command.c @@ -0,0 +1,63 @@ +/* + * Copyright Luka Jankovic 2025 + * + * This file is part of Kubo. + * + * Kubo is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Kubo is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Kubo. If not, see . + */ + +#include "kubo_command.h" + +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 struct kubo_char_arr command; + +void kubo_command_init() { + kubo_char_arr_init(&command); +} + +void kubo_command_cleanup() { + kubo_char_arr_free(&command); +} + +void kubo_command_append_char(int c) { kubo_char_arr_add(&command, c); } + +void kubo_command_pop() { + if (command.count > 1) { + kubo_char_arr_pop(&command); + } +} + +void kubo_command_accept_cmd(struct kubo_context *context) { + for (size_t i = 0; i < kubo_commands_size; i++) { + if (strcmp(kubo_commands[i].input, kubo_char_arr_build_str(&command)) == + 0) { + kubo_commands[i].function(context); + return; + } + } +} + +char *kubo_command_get_str() { return kubo_char_arr_build_str(&command); } + +void kubo_command_clear() { kubo_char_arr_clear(&command); } + +static inline void kubo_command_exit(struct kubo_context *context) { + context->exit_pending = true; +} diff --git a/kubo_command.h b/kubo_command.h new file mode 100644 index 0000000..3e8d896 --- /dev/null +++ b/kubo_command.h @@ -0,0 +1,44 @@ +/* + * Copyright Luka Jankovic 2025 + * + * This file is part of Kubo. + * + * Kubo is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Kubo is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Kubo. If not, see . + */ + +#ifndef KUBO_COMMAND_H +#define KUBO_COMMAND_H + +#include +#include + +#include "kubo_char_arr.h" +#include "kubo_context.h" + +typedef void (*kubo_command_function)(struct kubo_context *context); + +struct kubo_command_data { + const char *input; + kubo_command_function function; +}; + +void kubo_command_init(); +void kubo_command_cleanup(); + +void kubo_command_append_char(int c); +void kubo_command_pop(); +void kubo_command_accept_cmd(struct kubo_context *context); +void kubo_command_clear(); +char *kubo_command_get_str(); + +#endif diff --git a/kubo_command_bar.c b/kubo_command_bar.c index a7e4714..bdbe317 100644 --- a/kubo_command_bar.c +++ b/kubo_command_bar.c @@ -18,9 +18,8 @@ #include "kubo_command_bar.h" -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); +void kubo_command_bar_render(int x, int y, int font_size) { + char *cmd = kubo_command_get_str(); DrawText(cmd, x, y, font_size, WHITE); x += MeasureText(cmd, font_size); diff --git a/kubo_command_bar.h b/kubo_command_bar.h index 058f2bf..c1df3aa 100644 --- a/kubo_command_bar.h +++ b/kubo_command_bar.h @@ -21,9 +21,9 @@ #include +#include "kubo_command.h" #include "kubo_context.h" -void kubo_command_bar_render(struct kubo_context *context, int x, int y, - int font_size); +void kubo_command_bar_render(int x, int y, int font_size); #endif diff --git a/kubo_context.c b/kubo_context.c index a9b93e9..42ce2ed 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -18,14 +18,6 @@ #include "kubo_context.h" -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}, @@ -40,7 +32,6 @@ void kubo_context_init(struct kubo_context *context) { } kubo_wall_arr_init(&context->walls); - kubo_char_arr_init(&context->command); context->wall_select_index = 0; context->exit_pending = false; @@ -49,17 +40,14 @@ void kubo_context_init(struct kubo_context *context) { void kubo_context_cleanup(struct kubo_context *context) { kubo_wall_arr_free(&context->walls); - kubo_char_arr_free(&context->command); } void kubo_context_set_state(struct kubo_context *context, enum kubo_context_state state) { - kubo_char_arr_clear(&context->command); context->state = kubo_context_states[state]; switch (context->state.id) { case KUBO_CONTEXT_COMMAND: - kubo_char_arr_add(&context->command, ':'); break; case KUBO_CONTEXT_WALL_NEW: @@ -74,18 +62,6 @@ void kubo_context_set_state(struct kubo_context *context, } } -void kubo_context_accept_cmd(struct kubo_context *context) { - 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); -} - struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { if (context->state.id != KUBO_CONTEXT_WALL_NEW) { @@ -155,7 +131,3 @@ 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 56f42a0..ffc6318 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -24,7 +24,6 @@ #include #include -#include "kubo_char_arr.h" #include "kubo_dynarray.h" #include "kubo_wall.h" @@ -50,20 +49,10 @@ struct kubo_context { struct kubo_wall_arr walls; struct kubo_context_state_data state; - // KUBO_CONTEXT_COMMAND - struct kubo_char_arr command; - // KUBO_CONTEXT_WALL_SELECT 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); diff --git a/kubo_input.c b/kubo_input.c index 1b6b413..5387429 100644 --- a/kubo_input.c +++ b/kubo_input.c @@ -76,10 +76,11 @@ static void char_input(struct kubo_context *context) { int char_code; do { char_code = GetCharPressed(); - if (char_code && context->state.id == KUBO_CONTEXT_COMMAND) { - kubo_char_arr_add(&context->command, char_code); + kubo_command_append_char(char_code); } else if (char_code == ':') { + kubo_command_clear(); + kubo_command_append_char(':'); kubo_context_set_state(context, KUBO_CONTEXT_COMMAND); } } while (char_code > 0); @@ -92,13 +93,12 @@ static void handle_cmd_input(struct kubo_context *context) { kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); break; case KEY_ENTER: - kubo_context_accept_cmd(context); + kubo_command_accept_cmd(context); + kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); break; case KEY_DELETE: case KEY_BACKSPACE: - if (context->command.count > 1) { - kubo_char_arr_pop(&context->command); - } + kubo_command_pop(); break; default: break; diff --git a/kubo_input.h b/kubo_input.h index c44459a..0ec3c06 100644 --- a/kubo_input.h +++ b/kubo_input.h @@ -20,6 +20,7 @@ #define KUBO_INPUT_H #include "kubo_context.h" +#include "kubo_command.h" void kubo_input_handle(struct kubo_context *context); diff --git a/main.c b/main.c index 4eeac79..42c14dd 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,33 @@ +/* + * Copyright Luka Jankovic 2025 + * + * This file is part of Kubo. + * + * Kubo is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Kubo is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Kubo. If not, see . + */ + #include #include +#include "kubo_command.h" #include "kubo_context.h" #include "kubo_window.h" #include "kubo_file.h" int main(int argc, char *argv[]) { + + kubo_command_init(); struct kubo_context context; kubo_context_init(&context); @@ -23,6 +44,7 @@ int main(int argc, char *argv[]) { kubo_window_cleanup(); kubo_context_cleanup(&context); + kubo_command_cleanup(); return 0; }