From c72a215afba37d7d43415aa811ac84fe1c8817cc Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Wed, 2 Jul 2025 22:51:29 +0200 Subject: [PATCH] move input, handle backspace, show cursor --- CMakeLists.txt | 1 + kubo_command.c | 10 ++++- kubo_input.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ kubo_input.h | 26 +++++++++++++ 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 kubo_input.c create mode 100644 kubo_input.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f6eb3a6..ee30305 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ else() endif() set(SOURCES + kubo_input.c kubo_command.c kubo_bar.c kubo_wall.c diff --git a/kubo_command.c b/kubo_command.c index 6478942..a1f2eb9 100644 --- a/kubo_command.c +++ b/kubo_command.c @@ -18,6 +18,12 @@ #include "kubo_command.h" -void kubo_command_render(struct kubo_context *context, int x, int y, int font_size) { - DrawText(kubo_char_arr_build_str(&context->command), x, y, font_size, WHITE); +void kubo_command_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); + + x += MeasureText(cmd, font_size); + x += 2; + DrawRectangle(x, y, MeasureText("x", font_size), font_size, WHITE); } diff --git a/kubo_input.c b/kubo_input.c new file mode 100644 index 0000000..106a13a --- /dev/null +++ b/kubo_input.c @@ -0,0 +1,104 @@ +/* + * 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_input.h" + +static void key_input(struct kubo_context *context); +static void char_input(struct kubo_context *context); + +static void handle_cmd_input(struct kubo_context *context); + +void kubo_input_handle(struct kubo_context *context) { + char_input(context); + + if (context->state.id == KUBO_CONTEXT_COMMAND) { + handle_cmd_input(context); + } + + key_input(context); +} + +static void key_input(struct kubo_context *context) { + int key_code = GetKeyPressed(); + + switch (key_code) { + case KEY_Q: + kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); + break; + + case KEY_W: + kubo_context_set_state(context, KUBO_CONTEXT_WALL_NEW); + break; + + case KEY_S: + kubo_context_set_state(context, KUBO_CONTEXT_WALL_SELECT); + break; + + case KEY_RIGHT: + case KEY_UP: + case KEY_L: + case KEY_K: + kubo_context_select_next_wall(context); + break; + + case KEY_LEFT: + case KEY_DOWN: + case KEY_J: + case KEY_H: + kubo_context_select_prev_wall(context); + break; + + default: + break; + } +} + +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); + } else if (char_code == ':') { + kubo_context_set_state(context, KUBO_CONTEXT_COMMAND); + } + } while (char_code > 0); +} + +static void handle_cmd_input(struct kubo_context *context) { + int key_code = GetKeyPressed(); + switch (key_code) { + case KEY_ESCAPE: + kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); + break; + case KEY_ENTER: + kubo_context_accept_cmd(context); + break; + case KEY_DELETE: + case KEY_BACKSPACE: + if (context->command.count > 1) { + kubo_char_arr_pop(&context->command); + } + break; + default: + break; + } + + return; +} diff --git a/kubo_input.h b/kubo_input.h new file mode 100644 index 0000000..c44459a --- /dev/null +++ b/kubo_input.h @@ -0,0 +1,26 @@ +/* + * 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_INPUT_H +#define KUBO_INPUT_H + +#include "kubo_context.h" + +void kubo_input_handle(struct kubo_context *context); + +#endif