move input, handle backspace, show cursor

This commit is contained in:
Luka Jankovic 2025-07-02 22:51:29 +02:00
parent d63cdb1c01
commit c72a215afb
4 changed files with 139 additions and 2 deletions

View file

@ -29,6 +29,7 @@ else()
endif()
set(SOURCES
kubo_input.c
kubo_command.c
kubo_bar.c
kubo_wall.c

View file

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

104
kubo_input.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#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;
}

26
kubo_input.h Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef KUBO_INPUT_H
#define KUBO_INPUT_H
#include "kubo_context.h"
void kubo_input_handle(struct kubo_context *context);
#endif