kubo/kubo_input.c

104 lines
2.6 KiB
C

/*
* 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;
}