reworked context state handling
This commit is contained in:
parent
5eb4437ac4
commit
7528ed007e
4 changed files with 28 additions and 30 deletions
|
|
@ -34,13 +34,12 @@ void kubo_bar_render(struct kubo_context *context) {
|
||||||
int bar_text_x = 10;
|
int bar_text_x = 10;
|
||||||
|
|
||||||
DrawRectangle(0, bar_y, GetScreenWidth(), KUBO_BAR_HEIGHT, BLACK);
|
DrawRectangle(0, bar_y, GetScreenWidth(), KUBO_BAR_HEIGHT, BLACK);
|
||||||
bar_text_x =
|
bar_text_x = push_text(bar_text_x, bar_y, context->state.label,
|
||||||
push_text(bar_text_x, bar_y, kubo_context_get_label(context->state),
|
context->state.color);
|
||||||
kubo_context_get_color(context->state));
|
|
||||||
|
|
||||||
bar_text_x += 2 * KUBO_BAR_PADDING;
|
bar_text_x += 2 * KUBO_BAR_PADDING;
|
||||||
|
|
||||||
if (context->state == KUBO_CONTEXT_COMMAND) {
|
if (context->state.id == KUBO_CONTEXT_COMMAND) {
|
||||||
kubo_command_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2),
|
kubo_command_render(context, bar_text_x, bar_y + (KUBO_BAR_PADDING / 2),
|
||||||
KUBO_BAR_HEIGHT - KUBO_BAR_PADDING);
|
KUBO_BAR_HEIGHT - KUBO_BAR_PADDING);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,12 @@
|
||||||
|
|
||||||
#include "kubo_context.h"
|
#include "kubo_context.h"
|
||||||
|
|
||||||
|
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},
|
||||||
|
{.id = KUBO_CONTEXT_WALL_SELECT, .label = "Wall Select", .color = BLUE}};
|
||||||
|
|
||||||
void kubo_context_init(struct kubo_context *context) {
|
void kubo_context_init(struct kubo_context *context) {
|
||||||
if (!context) {
|
if (!context) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -28,7 +34,7 @@ void kubo_context_init(struct kubo_context *context) {
|
||||||
context->wall_select_index = 0;
|
context->wall_select_index = 0;
|
||||||
context->exit_pending = false;
|
context->exit_pending = false;
|
||||||
|
|
||||||
context->state = KUBO_CONTEXT_NORMAL;
|
context->state = kubo_context_states[KUBO_CONTEXT_NORMAL];
|
||||||
}
|
}
|
||||||
|
|
||||||
void kubo_context_cleanup(struct kubo_context *context) {
|
void kubo_context_cleanup(struct kubo_context *context) {
|
||||||
|
|
@ -39,9 +45,9 @@ 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);
|
kubo_char_arr_clear(&context->command);
|
||||||
context->state = state;
|
context->state = kubo_context_states[state];
|
||||||
|
|
||||||
switch (context->state) {
|
switch (context->state.id) {
|
||||||
case KUBO_CONTEXT_COMMAND:
|
case KUBO_CONTEXT_COMMAND:
|
||||||
kubo_char_arr_add(&context->command, ':');
|
kubo_char_arr_add(&context->command, ':');
|
||||||
break;
|
break;
|
||||||
|
|
@ -58,14 +64,6 @@ void kubo_context_set_state(struct kubo_context *context,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *kubo_context_get_label(enum kubo_context_state state) {
|
|
||||||
return kubo_context_labels[state];
|
|
||||||
}
|
|
||||||
|
|
||||||
Color kubo_context_get_color(enum kubo_context_state state) {
|
|
||||||
return kubo_context_colors[state];
|
|
||||||
}
|
|
||||||
|
|
||||||
void kubo_context_accept_cmd(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));
|
printf("accepting cmd %s\n", kubo_char_arr_build_str(&context->command));
|
||||||
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
||||||
|
|
@ -73,7 +71,7 @@ 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) {
|
||||||
|
|
||||||
if (context->state != KUBO_CONTEXT_WALL_NEW) {
|
if (context->state.id != KUBO_CONTEXT_WALL_NEW) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,7 +95,8 @@ 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) {
|
||||||
if (context->state != KUBO_CONTEXT_WALL_SELECT || !context->walls.count) {
|
if (context->state.id != KUBO_CONTEXT_WALL_SELECT ||
|
||||||
|
!context->walls.count) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +107,8 @@ void kubo_context_select_next_wall(struct kubo_context *context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void kubo_context_select_prev_wall(struct kubo_context *context) {
|
void kubo_context_select_prev_wall(struct kubo_context *context) {
|
||||||
if (context->state != KUBO_CONTEXT_WALL_SELECT || !context->walls.count) {
|
if (context->state.id != KUBO_CONTEXT_WALL_SELECT ||
|
||||||
|
!context->walls.count) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,15 +36,16 @@ enum kubo_context_state {
|
||||||
KUBO_CONTEXT_WALL_SELECT,
|
KUBO_CONTEXT_WALL_SELECT,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char *kubo_context_labels[] = {"Normal", "Command", "Wall New",
|
struct kubo_context_state_data {
|
||||||
"Wall Select"};
|
enum kubo_context_state id;
|
||||||
|
char *label;
|
||||||
static const Color kubo_context_colors[] = {BLACK, RED, GREEN, BLUE};
|
Color color;
|
||||||
|
};
|
||||||
|
|
||||||
struct kubo_context {
|
struct kubo_context {
|
||||||
bool exit_pending;
|
bool exit_pending;
|
||||||
struct kubo_wall_arr walls;
|
struct kubo_wall_arr walls;
|
||||||
enum kubo_context_state state;
|
struct kubo_context_state_data state;
|
||||||
|
|
||||||
// KUBO_CONTEXT_COMMAND
|
// KUBO_CONTEXT_COMMAND
|
||||||
struct kubo_char_arr command;
|
struct kubo_char_arr command;
|
||||||
|
|
@ -58,8 +59,6 @@ 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);
|
||||||
const char *kubo_context_get_label(enum kubo_context_state state);
|
|
||||||
Color kubo_context_get_color(enum kubo_context_state state);
|
|
||||||
|
|
||||||
void kubo_context_accept_cmd(struct kubo_context *context);
|
void kubo_context_accept_cmd(struct kubo_context *context);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ static void window_render(struct kubo_context *context) {
|
||||||
struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i);
|
struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i);
|
||||||
assert(wall != NULL);
|
assert(wall != NULL);
|
||||||
|
|
||||||
kubo_wall_render(wall, context->state == KUBO_CONTEXT_WALL_SELECT);
|
kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
kubo_bar_render(context);
|
kubo_bar_render(context);
|
||||||
|
|
@ -58,7 +58,7 @@ static void window_render(struct kubo_context *context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void window_left_mouse(struct kubo_context *context) {
|
static void window_left_mouse(struct kubo_context *context) {
|
||||||
switch (context->state) {
|
switch (context->state.id) {
|
||||||
case KUBO_CONTEXT_WALL_NEW:
|
case KUBO_CONTEXT_WALL_NEW:
|
||||||
new_wall_click(context);
|
new_wall_click(context);
|
||||||
break;
|
break;
|
||||||
|
|
@ -70,7 +70,7 @@ static void window_left_mouse(struct kubo_context *context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void window_right_mouse(struct kubo_context *context) {
|
static void window_right_mouse(struct kubo_context *context) {
|
||||||
switch (context->state) {
|
switch (context->state.id) {
|
||||||
case KUBO_CONTEXT_WALL_NEW:
|
case KUBO_CONTEXT_WALL_NEW:
|
||||||
new_wall_end(context);
|
new_wall_end(context);
|
||||||
break;
|
break;
|
||||||
|
|
@ -87,7 +87,7 @@ static void window_key_input(struct kubo_context *context) {
|
||||||
|
|
||||||
int key_code = GetKeyPressed();
|
int key_code = GetKeyPressed();
|
||||||
|
|
||||||
if (context->state == KUBO_CONTEXT_COMMAND) {
|
if (context->state.id == KUBO_CONTEXT_COMMAND) {
|
||||||
switch (key_code) {
|
switch (key_code) {
|
||||||
case KEY_ESCAPE:
|
case KEY_ESCAPE:
|
||||||
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
kubo_context_set_state(context, KUBO_CONTEXT_NORMAL);
|
||||||
|
|
@ -143,7 +143,7 @@ static void window_char_input(struct kubo_context *context) {
|
||||||
do {
|
do {
|
||||||
char_code = GetCharPressed();
|
char_code = GetCharPressed();
|
||||||
|
|
||||||
if (char_code && context->state == KUBO_CONTEXT_COMMAND) {
|
if (char_code && context->state.id == KUBO_CONTEXT_COMMAND) {
|
||||||
kubo_char_arr_add(&context->command, char_code);
|
kubo_char_arr_add(&context->command, char_code);
|
||||||
} else if (char_code == ':') {
|
} else if (char_code == ':') {
|
||||||
kubo_context_set_state(context, KUBO_CONTEXT_COMMAND);
|
kubo_context_set_state(context, KUBO_CONTEXT_COMMAND);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue