separate command handling
This commit is contained in:
parent
90db87bec9
commit
0be3768867
11 changed files with 143 additions and 52 deletions
|
|
@ -39,6 +39,7 @@ set(SOURCES
|
||||||
kubo_wall.c
|
kubo_wall.c
|
||||||
kubo_window.c
|
kubo_window.c
|
||||||
kubo_context.c
|
kubo_context.c
|
||||||
|
kubo_command.c
|
||||||
main.c
|
main.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,8 @@ void kubo_bar_render(struct kubo_context *context) {
|
||||||
bar_text_x += 2 * KUBO_BAR_PADDING;
|
bar_text_x += 2 * KUBO_BAR_PADDING;
|
||||||
|
|
||||||
if (context->state.id == KUBO_CONTEXT_COMMAND) {
|
if (context->state.id == KUBO_CONTEXT_COMMAND) {
|
||||||
kubo_command_bar_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2),
|
kubo_command_bar_render(bar_text_x, bar_y + (KUBO_BAR_PADDING / 2),
|
||||||
KUBO_BAR_HEIGHT - KUBO_BAR_PADDING);
|
KUBO_BAR_HEIGHT - KUBO_BAR_PADDING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
63
kubo_command.c
Normal file
63
kubo_command.c
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* 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_command.h"
|
||||||
|
|
||||||
|
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 struct kubo_char_arr command;
|
||||||
|
|
||||||
|
void kubo_command_init() {
|
||||||
|
kubo_char_arr_init(&command);
|
||||||
|
}
|
||||||
|
|
||||||
|
void kubo_command_cleanup() {
|
||||||
|
kubo_char_arr_free(&command);
|
||||||
|
}
|
||||||
|
|
||||||
|
void kubo_command_append_char(int c) { kubo_char_arr_add(&command, c); }
|
||||||
|
|
||||||
|
void kubo_command_pop() {
|
||||||
|
if (command.count > 1) {
|
||||||
|
kubo_char_arr_pop(&command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void kubo_command_accept_cmd(struct kubo_context *context) {
|
||||||
|
for (size_t i = 0; i < kubo_commands_size; i++) {
|
||||||
|
if (strcmp(kubo_commands[i].input, kubo_char_arr_build_str(&command)) ==
|
||||||
|
0) {
|
||||||
|
kubo_commands[i].function(context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *kubo_command_get_str() { return kubo_char_arr_build_str(&command); }
|
||||||
|
|
||||||
|
void kubo_command_clear() { kubo_char_arr_clear(&command); }
|
||||||
|
|
||||||
|
static inline void kubo_command_exit(struct kubo_context *context) {
|
||||||
|
context->exit_pending = true;
|
||||||
|
}
|
||||||
44
kubo_command.h
Normal file
44
kubo_command.h
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* 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_COMMAND_H
|
||||||
|
#define KUBO_COMMAND_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "kubo_char_arr.h"
|
||||||
|
#include "kubo_context.h"
|
||||||
|
|
||||||
|
typedef void (*kubo_command_function)(struct kubo_context *context);
|
||||||
|
|
||||||
|
struct kubo_command_data {
|
||||||
|
const char *input;
|
||||||
|
kubo_command_function function;
|
||||||
|
};
|
||||||
|
|
||||||
|
void kubo_command_init();
|
||||||
|
void kubo_command_cleanup();
|
||||||
|
|
||||||
|
void kubo_command_append_char(int c);
|
||||||
|
void kubo_command_pop();
|
||||||
|
void kubo_command_accept_cmd(struct kubo_context *context);
|
||||||
|
void kubo_command_clear();
|
||||||
|
char *kubo_command_get_str();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -18,9 +18,8 @@
|
||||||
|
|
||||||
#include "kubo_command_bar.h"
|
#include "kubo_command_bar.h"
|
||||||
|
|
||||||
void kubo_command_bar_render(struct kubo_context *context, int x, int y,
|
void kubo_command_bar_render(int x, int y, int font_size) {
|
||||||
int font_size) {
|
char *cmd = kubo_command_get_str();
|
||||||
char *cmd = kubo_char_arr_build_str(&context->command);
|
|
||||||
DrawText(cmd, x, y, font_size, WHITE);
|
DrawText(cmd, x, y, font_size, WHITE);
|
||||||
|
|
||||||
x += MeasureText(cmd, font_size);
|
x += MeasureText(cmd, font_size);
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,9 @@
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
|
#include "kubo_command.h"
|
||||||
#include "kubo_context.h"
|
#include "kubo_context.h"
|
||||||
|
|
||||||
void kubo_command_bar_render(struct kubo_context *context, int x, int y,
|
void kubo_command_bar_render(int x, int y, int font_size);
|
||||||
int font_size);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,6 @@
|
||||||
|
|
||||||
#include "kubo_context.h"
|
#include "kubo_context.h"
|
||||||
|
|
||||||
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[] = {
|
static const struct kubo_context_state_data kubo_context_states[] = {
|
||||||
{.id = KUBO_CONTEXT_NORMAL, .label = "Normal", .color = BLACK},
|
{.id = KUBO_CONTEXT_NORMAL, .label = "Normal", .color = BLACK},
|
||||||
{.id = KUBO_CONTEXT_COMMAND, .label = "Command", .color = RED},
|
{.id = KUBO_CONTEXT_COMMAND, .label = "Command", .color = RED},
|
||||||
|
|
@ -40,7 +32,6 @@ void kubo_context_init(struct kubo_context *context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
kubo_wall_arr_init(&context->walls);
|
kubo_wall_arr_init(&context->walls);
|
||||||
kubo_char_arr_init(&context->command);
|
|
||||||
context->wall_select_index = 0;
|
context->wall_select_index = 0;
|
||||||
context->exit_pending = false;
|
context->exit_pending = false;
|
||||||
|
|
||||||
|
|
@ -49,17 +40,14 @@ void kubo_context_init(struct kubo_context *context) {
|
||||||
|
|
||||||
void kubo_context_cleanup(struct kubo_context *context) {
|
void kubo_context_cleanup(struct kubo_context *context) {
|
||||||
kubo_wall_arr_free(&context->walls);
|
kubo_wall_arr_free(&context->walls);
|
||||||
kubo_char_arr_free(&context->command);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = kubo_context_states[state];
|
context->state = kubo_context_states[state];
|
||||||
|
|
||||||
switch (context->state.id) {
|
switch (context->state.id) {
|
||||||
case KUBO_CONTEXT_COMMAND:
|
case KUBO_CONTEXT_COMMAND:
|
||||||
kubo_char_arr_add(&context->command, ':');
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KUBO_CONTEXT_WALL_NEW:
|
case KUBO_CONTEXT_WALL_NEW:
|
||||||
|
|
@ -74,18 +62,6 @@ void kubo_context_set_state(struct kubo_context *context,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void kubo_context_accept_cmd(struct kubo_context *context) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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.id != KUBO_CONTEXT_WALL_NEW) {
|
if (context->state.id != KUBO_CONTEXT_WALL_NEW) {
|
||||||
|
|
@ -155,7 +131,3 @@ static inline void wall_select_refresh(struct kubo_context *context) {
|
||||||
: KUBO_WALL_DONE;
|
: KUBO_WALL_DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void kubo_command_exit(struct kubo_context *context) {
|
|
||||||
context->exit_pending = true;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "kubo_char_arr.h"
|
|
||||||
#include "kubo_dynarray.h"
|
#include "kubo_dynarray.h"
|
||||||
#include "kubo_wall.h"
|
#include "kubo_wall.h"
|
||||||
|
|
||||||
|
|
@ -50,20 +49,10 @@ struct kubo_context {
|
||||||
struct kubo_wall_arr walls;
|
struct kubo_wall_arr walls;
|
||||||
struct kubo_context_state_data state;
|
struct kubo_context_state_data state;
|
||||||
|
|
||||||
// KUBO_CONTEXT_COMMAND
|
|
||||||
struct kubo_char_arr command;
|
|
||||||
|
|
||||||
// KUBO_CONTEXT_WALL_SELECT
|
// KUBO_CONTEXT_WALL_SELECT
|
||||||
size_t wall_select_index;
|
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_init(struct kubo_context *context);
|
||||||
void kubo_context_cleanup(struct kubo_context *context);
|
void kubo_context_cleanup(struct kubo_context *context);
|
||||||
|
|
||||||
|
|
|
||||||
12
kubo_input.c
12
kubo_input.c
|
|
@ -76,10 +76,11 @@ static void char_input(struct kubo_context *context) {
|
||||||
int char_code;
|
int char_code;
|
||||||
do {
|
do {
|
||||||
char_code = GetCharPressed();
|
char_code = GetCharPressed();
|
||||||
|
|
||||||
if (char_code && context->state.id == KUBO_CONTEXT_COMMAND) {
|
if (char_code && context->state.id == KUBO_CONTEXT_COMMAND) {
|
||||||
kubo_char_arr_add(&context->command, char_code);
|
kubo_command_append_char(char_code);
|
||||||
} else if (char_code == ':') {
|
} else if (char_code == ':') {
|
||||||
|
kubo_command_clear();
|
||||||
|
kubo_command_append_char(':');
|
||||||
kubo_context_set_state(context, KUBO_CONTEXT_COMMAND);
|
kubo_context_set_state(context, KUBO_CONTEXT_COMMAND);
|
||||||
}
|
}
|
||||||
} while (char_code > 0);
|
} while (char_code > 0);
|
||||||
|
|
@ -92,13 +93,12 @@ static void handle_cmd_input(struct kubo_context *context) {
|
||||||
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
||||||
break;
|
break;
|
||||||
case KEY_ENTER:
|
case KEY_ENTER:
|
||||||
kubo_context_accept_cmd(context);
|
kubo_command_accept_cmd(context);
|
||||||
|
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
||||||
break;
|
break;
|
||||||
case KEY_DELETE:
|
case KEY_DELETE:
|
||||||
case KEY_BACKSPACE:
|
case KEY_BACKSPACE:
|
||||||
if (context->command.count > 1) {
|
kubo_command_pop();
|
||||||
kubo_char_arr_pop(&context->command);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#define KUBO_INPUT_H
|
#define KUBO_INPUT_H
|
||||||
|
|
||||||
#include "kubo_context.h"
|
#include "kubo_context.h"
|
||||||
|
#include "kubo_command.h"
|
||||||
|
|
||||||
void kubo_input_handle(struct kubo_context *context);
|
void kubo_input_handle(struct kubo_context *context);
|
||||||
|
|
||||||
|
|
|
||||||
22
main.c
22
main.c
|
|
@ -1,12 +1,33 @@
|
||||||
|
/*
|
||||||
|
* 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 <raylib.h>
|
#include <raylib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "kubo_command.h"
|
||||||
#include "kubo_context.h"
|
#include "kubo_context.h"
|
||||||
#include "kubo_window.h"
|
#include "kubo_window.h"
|
||||||
|
|
||||||
#include "kubo_file.h"
|
#include "kubo_file.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
kubo_command_init();
|
||||||
|
|
||||||
struct kubo_context context;
|
struct kubo_context context;
|
||||||
kubo_context_init(&context);
|
kubo_context_init(&context);
|
||||||
|
|
@ -23,6 +44,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
kubo_window_cleanup();
|
kubo_window_cleanup();
|
||||||
kubo_context_cleanup(&context);
|
kubo_context_cleanup(&context);
|
||||||
|
kubo_command_cleanup();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue