parse command input

This commit is contained in:
Luka Jankovic 2025-07-02 23:21:26 +02:00
parent 9911031f98
commit b7b8bd8b1e
7 changed files with 39 additions and 13 deletions

View file

@ -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

View file

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

View file

@ -25,7 +25,7 @@
#include <raylib.h>
#include "kubo_context.h"
#include "kubo_command.h"
#include "kubo_command_bar.h"
void kubo_bar_render(struct kubo_context *context);

View file

@ -16,10 +16,10 @@
* Kubo. If not, see <https://www.gnu.org/licenses/>.
*/
#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);

View file

@ -16,14 +16,14 @@
* Kubo. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef KUBO_COMMAND_H
#define KUBO_COMMAND_H
#ifndef KUBO_COMMAND_BAR_H
#define KUBO_COMMAND_BAR_H
#include <raylib.h>
#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

View file

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

View file

@ -22,6 +22,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);