feat: add central application state
This commit is contained in:
parent
0168601e70
commit
dcf9f09c88
8 changed files with 100 additions and 29 deletions
24
include/lardon3d/app_state.h
Normal file
24
include/lardon3d/app_state.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef LARDON3D_APP_STATE_H
|
||||
#define LARDON3D_APP_STATE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
LARDON3D_SCREEN_HOME = 0,
|
||||
LARDON3D_SCREEN_PROJECTS,
|
||||
LARDON3D_SCREEN_IMPORT,
|
||||
LARDON3D_SCREEN_VIEWER,
|
||||
LARDON3D_SCREEN_HELP
|
||||
} Lardon3DScreen;
|
||||
|
||||
typedef struct {
|
||||
Lardon3DScreen screen;
|
||||
bool running;
|
||||
bool project_loaded;
|
||||
char project_name[128];
|
||||
char status_message[256];
|
||||
} Lardon3DAppState;
|
||||
|
||||
void lardon3d_app_state_init(Lardon3DAppState *state);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
#ifndef LARDON3D_LAYOUT_H
|
||||
#define LARDON3D_LAYOUT_H
|
||||
|
||||
#include <lardon3d/tui.h>
|
||||
#include <lardon3d/app_state.h>
|
||||
|
||||
void lardon3d_layout_draw(Lardon3DScreen screen, int rows, int cols);
|
||||
void lardon3d_layout_draw(
|
||||
const Lardon3DAppState *state,
|
||||
int rows,
|
||||
int cols
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -3,16 +3,10 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
LARDON3D_SCREEN_HOME = 0,
|
||||
LARDON3D_SCREEN_PROJECTS,
|
||||
LARDON3D_SCREEN_IMPORT,
|
||||
LARDON3D_SCREEN_VIEWER,
|
||||
LARDON3D_SCREEN_HELP
|
||||
} Lardon3DScreen;
|
||||
#include <lardon3d/app_state.h>
|
||||
|
||||
bool lardon3d_tui_init(void);
|
||||
bool lardon3d_tui_run(void);
|
||||
bool lardon3d_tui_run(Lardon3DAppState *state);
|
||||
void lardon3d_tui_shutdown(void);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ executable(
|
|||
sources: [
|
||||
'src/main.c',
|
||||
'src/app.c',
|
||||
'src/app_state.c',
|
||||
'src/tui.c',
|
||||
'src/layout.c',
|
||||
],
|
||||
|
|
|
|||
|
|
@ -2,11 +2,15 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include <lardon3d/app.h>
|
||||
#include <lardon3d/app_state.h>
|
||||
#include <lardon3d/tui.h>
|
||||
|
||||
int
|
||||
lardon3d_app_run(void)
|
||||
{
|
||||
Lardon3DAppState state;
|
||||
lardon3d_app_state_init(&state);
|
||||
|
||||
if (!setlocale(LC_ALL, "")) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
|
@ -15,7 +19,7 @@ lardon3d_app_run(void)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
bool success = lardon3d_tui_run();
|
||||
bool success = lardon3d_tui_run(&state);
|
||||
lardon3d_tui_shutdown();
|
||||
|
||||
return success ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
|
|
|
|||
17
src/app_state.c
Normal file
17
src/app_state.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <lardon3d/app_state.h>
|
||||
|
||||
void
|
||||
lardon3d_app_state_init(Lardon3DAppState *state)
|
||||
{
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
*state = (Lardon3DAppState) {
|
||||
.screen = LARDON3D_SCREEN_HOME,
|
||||
.running = true,
|
||||
.project_loaded = false,
|
||||
.project_name = "",
|
||||
.status_message = "Bienvenue dans Lardon3D",
|
||||
};
|
||||
}
|
||||
31
src/layout.c
31
src/layout.c
|
|
@ -35,13 +35,17 @@ draw_too_small(int rows, int columns)
|
|||
static void
|
||||
draw_frame(int rows, int columns)
|
||||
{
|
||||
int journal_row = rows - 7;
|
||||
int footer_row = rows - 3;
|
||||
|
||||
(void)box(stdscr, 0, 0);
|
||||
(void)mvhline(2, 1, ACS_HLINE, columns - 2);
|
||||
(void)mvhline(journal_row, 1, ACS_HLINE, columns - 2);
|
||||
(void)mvhline(footer_row, 1, ACS_HLINE, columns - 2);
|
||||
(void)mvaddch(2, 0, ACS_LTEE);
|
||||
(void)mvaddch(2, columns - 1, ACS_RTEE);
|
||||
(void)mvaddch(journal_row, 0, ACS_LTEE);
|
||||
(void)mvaddch(journal_row, columns - 1, ACS_RTEE);
|
||||
(void)mvaddch(footer_row, 0, ACS_LTEE);
|
||||
(void)mvaddch(footer_row, columns - 1, ACS_RTEE);
|
||||
}
|
||||
|
|
@ -83,22 +87,39 @@ screen_texts(
|
|||
}
|
||||
|
||||
static void
|
||||
draw_content(Lardon3DScreen screen, int rows, int columns)
|
||||
draw_content(const Lardon3DAppState *state, int rows, int columns)
|
||||
{
|
||||
const char *title;
|
||||
const char *content;
|
||||
const char *footer;
|
||||
screen_texts(screen, &title, &content, &footer);
|
||||
screen_texts(state->screen, &title, &content, &footer);
|
||||
int title_length = (int)strlen(title);
|
||||
int content_length = (int)strlen(content);
|
||||
int journal_row = rows - 7;
|
||||
const char *project = state->project_loaded
|
||||
? state->project_name
|
||||
: "Aucun projet chargé.";
|
||||
|
||||
draw_text(1, (columns - title_length) / 2, title_length, title);
|
||||
draw_text(rows / 2, (columns - content_length) / 2, content_length, content);
|
||||
draw_text(3, 2, columns - 4, "Projet");
|
||||
draw_text(4, 4, columns - 6, project);
|
||||
draw_text(
|
||||
(3 + journal_row) / 2,
|
||||
(columns - content_length) / 2,
|
||||
content_length,
|
||||
content
|
||||
);
|
||||
draw_text(journal_row + 1, 2, columns - 4, "Journal");
|
||||
draw_text(journal_row + 2, 4, columns - 6, state->status_message);
|
||||
draw_text(rows - 2, 2, columns - 4, footer);
|
||||
}
|
||||
|
||||
void
|
||||
lardon3d_layout_draw(Lardon3DScreen screen, int rows, int columns)
|
||||
lardon3d_layout_draw(
|
||||
const Lardon3DAppState *state,
|
||||
int rows,
|
||||
int columns
|
||||
)
|
||||
{
|
||||
(void)erase();
|
||||
|
||||
|
|
@ -106,7 +127,7 @@ lardon3d_layout_draw(Lardon3DScreen screen, int rows, int columns)
|
|||
draw_too_small(rows, columns);
|
||||
} else {
|
||||
draw_frame(rows, columns);
|
||||
draw_content(screen, rows, columns);
|
||||
draw_content(state, rows, columns);
|
||||
}
|
||||
|
||||
(void)refresh();
|
||||
|
|
|
|||
32
src/tui.c
32
src/tui.c
|
|
@ -5,12 +5,12 @@
|
|||
#include <lardon3d/tui.h>
|
||||
|
||||
static void
|
||||
redraw(Lardon3DScreen screen)
|
||||
redraw(const Lardon3DAppState *state)
|
||||
{
|
||||
int rows;
|
||||
int columns;
|
||||
getmaxyx(stdscr, rows, columns);
|
||||
lardon3d_layout_draw(screen, rows, columns);
|
||||
lardon3d_layout_draw(state, rows, columns);
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
@ -30,16 +30,20 @@ lardon3d_tui_init(void)
|
|||
}
|
||||
|
||||
bool
|
||||
lardon3d_tui_run(void)
|
||||
lardon3d_tui_run(Lardon3DAppState *state)
|
||||
{
|
||||
Lardon3DScreen screen = LARDON3D_SCREEN_HOME;
|
||||
redraw(screen);
|
||||
if (!state) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
redraw(state);
|
||||
|
||||
while (state->running) {
|
||||
int key = getch();
|
||||
|
||||
if (key == 'q' || key == 'Q') {
|
||||
return true;
|
||||
state->running = false;
|
||||
continue;
|
||||
}
|
||||
if (key == ERR) {
|
||||
return false;
|
||||
|
|
@ -48,19 +52,19 @@ lardon3d_tui_run(void)
|
|||
bool should_redraw = true;
|
||||
switch (key) {
|
||||
case KEY_F(1):
|
||||
screen = LARDON3D_SCREEN_HELP;
|
||||
state->screen = LARDON3D_SCREEN_HELP;
|
||||
break;
|
||||
case KEY_F(2):
|
||||
screen = LARDON3D_SCREEN_PROJECTS;
|
||||
state->screen = LARDON3D_SCREEN_PROJECTS;
|
||||
break;
|
||||
case KEY_F(3):
|
||||
screen = LARDON3D_SCREEN_IMPORT;
|
||||
state->screen = LARDON3D_SCREEN_IMPORT;
|
||||
break;
|
||||
case KEY_F(4):
|
||||
screen = LARDON3D_SCREEN_VIEWER;
|
||||
state->screen = LARDON3D_SCREEN_VIEWER;
|
||||
break;
|
||||
case 27:
|
||||
screen = LARDON3D_SCREEN_HOME;
|
||||
state->screen = LARDON3D_SCREEN_HOME;
|
||||
break;
|
||||
case KEY_RESIZE:
|
||||
break;
|
||||
|
|
@ -70,9 +74,11 @@ lardon3d_tui_run(void)
|
|||
}
|
||||
|
||||
if (should_redraw) {
|
||||
redraw(screen);
|
||||
redraw(state);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
|||
Loading…
Reference in a new issue