esc and entr handling in command mode

This commit is contained in:
Luka Jankovic 2025-07-02 18:00:40 +02:00
parent 5daf8b9dd3
commit 5eb4437ac4
6 changed files with 66 additions and 14 deletions

37
kubo_char_arr.h Normal file
View file

@ -0,0 +1,37 @@
/*
* 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_CHAR_ARR_H
#define KUBO_CHAR_ARR_H
#include <stdbool.h>
#include "kubo_dynarray.h"
KUBO_DYNARRAY_REGISTER(kubo_char_arr, char);
static inline char *kubo_char_arr_build_str(struct kubo_char_arr *arr) {
char *res = malloc(arr->count + 1);
for (int i = 0; i < arr->count; i++) {
res[i] = kubo_char_arr_get(arr, i);
}
res[arr->count] = '\0';
return res;
}
#endif

View file

@ -18,15 +18,6 @@
#include "kubo_command.h" #include "kubo_command.h"
static char *kubo_char_arr_build_str(struct kubo_char_arr *arr) {
char *res = malloc(arr->count + 1);
for (int i = 0; i < arr->count; i++) {
res[i] = kubo_char_arr_get(arr, i);
}
res[arr->count] = '\0';
return res;
}
void kubo_command_render(struct kubo_context *context, int x, int y, int font_size) { 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); DrawText(kubo_char_arr_build_str(&context->command), x, y, font_size, WHITE);
} }

View file

@ -38,6 +38,7 @@ void kubo_context_cleanup(struct kubo_context *context) {
void kubo_context_set_state(struct kubo_context *context, void kubo_context_set_state(struct kubo_context *context,
enum kubo_context_state state) { enum kubo_context_state state) {
kubo_char_arr_clear(&context->command);
context->state = state; context->state = state;
switch (context->state) { switch (context->state) {
@ -65,6 +66,11 @@ Color kubo_context_get_color(enum kubo_context_state state) {
return kubo_context_colors[state]; return kubo_context_colors[state];
} }
void kubo_context_accept_cmd(struct kubo_context *context) {
printf("accepting cmd %s\n", kubo_char_arr_build_str(&context->command));
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
}
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
if (context->state != KUBO_CONTEXT_WALL_NEW) { if (context->state != KUBO_CONTEXT_WALL_NEW) {

View file

@ -23,11 +23,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "kubo_char_arr.h"
#include "kubo_dynarray.h" #include "kubo_dynarray.h"
#include "kubo_wall.h" #include "kubo_wall.h"
KUBO_DYNARRAY_REGISTER(kubo_wall_arr, struct kubo_wall *); KUBO_DYNARRAY_REGISTER(kubo_wall_arr, struct kubo_wall *);
KUBO_DYNARRAY_REGISTER(kubo_char_arr, char);
enum kubo_context_state { enum kubo_context_state {
KUBO_CONTEXT_NORMAL, KUBO_CONTEXT_NORMAL,
@ -36,7 +36,8 @@ enum kubo_context_state {
KUBO_CONTEXT_WALL_SELECT, KUBO_CONTEXT_WALL_SELECT,
}; };
static const char *kubo_context_labels[] = {"Normal", "Command", "Wall New", "Wall Select"}; static const char *kubo_context_labels[] = {"Normal", "Command", "Wall New",
"Wall Select"};
static const Color kubo_context_colors[] = {BLACK, RED, GREEN, BLUE}; static const Color kubo_context_colors[] = {BLACK, RED, GREEN, BLUE};
@ -60,6 +61,8 @@ void kubo_context_set_state(struct kubo_context *context,
const char *kubo_context_get_label(enum kubo_context_state state); const char *kubo_context_get_label(enum kubo_context_state state);
Color kubo_context_get_color(enum kubo_context_state state); Color kubo_context_get_color(enum kubo_context_state state);
void kubo_context_accept_cmd(struct kubo_context *context);
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context); struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context);
void kubo_context_select_next_wall(struct kubo_context *context); void kubo_context_select_next_wall(struct kubo_context *context);

View file

@ -69,6 +69,11 @@
size_t index) { \ size_t index) { \
assert(index < arr->count); \ assert(index < arr->count); \
arr->data[index] = new_val; \ arr->data[index] = new_val; \
} \
\
static inline void name##_clear(struct name *arr) { \
free(arr->data); \
name##_init(arr); \
} }
#endif #endif

View file

@ -87,11 +87,21 @@ static void window_key_input(struct kubo_context *context) {
int key_code = GetKeyPressed(); int key_code = GetKeyPressed();
if (key_code == KEY_ESCAPE) { if (context->state == KUBO_CONTEXT_COMMAND) {
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); switch (key_code) {
case KEY_ESCAPE:
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
break;
case KEY_ENTER:
kubo_context_accept_cmd(context);
default:
break;
}
return;
} }
if (!key_code || context->state == KUBO_CONTEXT_COMMAND) { if (!key_code) {
return; return;
} }