133 lines
3.8 KiB
C
133 lines
3.8 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_context.h"
|
|
|
|
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},
|
|
{.id = KUBO_CONTEXT_WALL_SELECT, .label = "Wall Select", .color = BLUE}};
|
|
|
|
static inline void wall_select_refresh(struct kubo_context *context);
|
|
|
|
void kubo_context_init(struct kubo_context *context) {
|
|
if (!context) {
|
|
return;
|
|
}
|
|
|
|
kubo_wall_arr_init(&context->walls);
|
|
context->wall_select_index = 0;
|
|
context->exit_pending = false;
|
|
|
|
context->state = kubo_context_states[KUBO_CONTEXT_NORMAL];
|
|
}
|
|
|
|
void kubo_context_cleanup(struct kubo_context *context) {
|
|
kubo_wall_arr_free(&context->walls);
|
|
}
|
|
|
|
void kubo_context_set_state(struct kubo_context *context,
|
|
enum kubo_context_state state) {
|
|
context->state = kubo_context_states[state];
|
|
|
|
switch (context->state.id) {
|
|
case KUBO_CONTEXT_COMMAND:
|
|
break;
|
|
|
|
case KUBO_CONTEXT_WALL_NEW:
|
|
break;
|
|
|
|
case KUBO_CONTEXT_WALL_SELECT:
|
|
wall_select_refresh(context);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
|
|
|
|
if (context->state.id != KUBO_CONTEXT_WALL_NEW) {
|
|
return NULL;
|
|
}
|
|
|
|
if (context->walls.count > 0) {
|
|
struct kubo_wall *last_wall =
|
|
kubo_wall_arr_get(&context->walls, context->walls.count - 1);
|
|
switch (last_wall->state) {
|
|
case KUBO_WALL_INIT:
|
|
case KUBO_WALL_DRAWING:
|
|
return last_wall;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
struct kubo_wall *wall = malloc(sizeof(struct kubo_wall));
|
|
kubo_wall_init(wall);
|
|
kubo_wall_arr_add(&context->walls, wall);
|
|
|
|
return wall;
|
|
}
|
|
|
|
void kubo_context_delete_wall(struct kubo_context *context) {
|
|
if (context->state.id != KUBO_CONTEXT_WALL_SELECT ||
|
|
!context->walls.count) {
|
|
return;
|
|
}
|
|
|
|
kubo_wall_arr_del(&context->walls, context->wall_select_index);
|
|
kubo_context_select_next_wall(context);
|
|
}
|
|
|
|
void kubo_context_select_next_wall(struct kubo_context *context) {
|
|
if (context->state.id != KUBO_CONTEXT_WALL_SELECT ||
|
|
!context->walls.count) {
|
|
return;
|
|
}
|
|
|
|
context->wall_select_index =
|
|
(context->wall_select_index + 1) % context->walls.count;
|
|
|
|
wall_select_refresh(context);
|
|
}
|
|
|
|
void kubo_context_select_prev_wall(struct kubo_context *context) {
|
|
if (context->state.id != KUBO_CONTEXT_WALL_SELECT ||
|
|
!context->walls.count) {
|
|
return;
|
|
}
|
|
|
|
size_t count = context->walls.count;
|
|
|
|
context->wall_select_index =
|
|
(context->wall_select_index + count - 1) % count;
|
|
|
|
wall_select_refresh(context);
|
|
}
|
|
|
|
static inline void wall_select_refresh(struct kubo_context *context) {
|
|
for (size_t i = 0; i < context->walls.count; i++) {
|
|
struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i);
|
|
wall->state = i == context->wall_select_index ? KUBO_WALL_SELECTED
|
|
: KUBO_WALL_DONE;
|
|
}
|
|
}
|