174 lines
5.8 KiB
C
174 lines
5.8 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <l3tde/game/map_editor.h>
|
|
#include <l3tde/game/map_editor/creation_form.h>
|
|
|
|
#include <l3tde/overworld.h>
|
|
|
|
#include <l3tde/util/string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#define BACKSPACE 127
|
|
#define KEY_TAB '\t'
|
|
|
|
static void
|
|
map_editor_creation_form_move_to_button_accept (MapEditorStatus *status);
|
|
static void
|
|
map_editor_creation_form_move_to_field (MapEditorStatus *status, int move_to);
|
|
static FIELD *
|
|
map_editor_get_last_field (FIELD **fields);
|
|
|
|
typedef enum {
|
|
MAP_NAME_FIELD,
|
|
MAP_DESCRIPTION_FIELD,
|
|
X_FIELD,
|
|
Y_FIELD,
|
|
N_FIELDS
|
|
} FieldsFormCreation;
|
|
|
|
void
|
|
map_editor_creation_form_handle_input (MapEditorStatus *status) {
|
|
FORM *form = status->form_creation;
|
|
int ch;
|
|
ch = getch ();
|
|
switch (ch) {
|
|
case BACKSPACE:
|
|
form_driver (form, REQ_DEL_PREV);
|
|
break;
|
|
case KEY_LEFT:
|
|
form_driver (form, REQ_PREV_CHAR);
|
|
break;
|
|
case KEY_RIGHT:
|
|
form_driver (form, REQ_NEXT_CHAR);
|
|
break;
|
|
case KEY_INTRO:
|
|
if (status->is_button_accept_selected) {
|
|
L3TDEOverworldPtr overworld = l3tde_overworld_create ();
|
|
const char *title = field_buffer (status->fields[MAP_NAME_FIELD], 0);
|
|
const char *description = field_buffer (status->fields[MAP_DESCRIPTION_FIELD], 0);
|
|
const char *x_str = field_buffer (status->fields[X_FIELD], 0);
|
|
const char *y_str = field_buffer (status->fields[Y_FIELD], 0);
|
|
if (!x_str || !y_str) {
|
|
break;
|
|
}
|
|
errno = 0;
|
|
size_t x = strtoll (x_str, NULL, 10);
|
|
if (errno == EINVAL || errno == ERANGE) {
|
|
mvprintw (11, 0, "X is not a number.");
|
|
break;
|
|
}
|
|
errno = 0;
|
|
size_t y = strtoll (y_str, NULL, 10);
|
|
if (errno == EINVAL || errno == ERANGE) {
|
|
mvprintw (11, 0, "Y is not a number.");
|
|
break;
|
|
}
|
|
status->current_form = MAP_EDITOR;
|
|
status->overworld = overworld;
|
|
L3TDEMapHeaderPtr header = l3tde_map_header_create (title,
|
|
strlen (title) + 1, description,
|
|
strlen (description) + 1);
|
|
L3TDEMapNodePtr **nodes = malloc (sizeof *nodes * x);
|
|
for (int i = 0; i < x; i++) {
|
|
nodes[i] = malloc (sizeof **nodes * y);
|
|
for (int j = 0; j < y; j++) {
|
|
nodes[i][j] = l3tde_map_node_create (overworld->terrains[0], 0);
|
|
}
|
|
}
|
|
L3TDEMapPtr map = l3tde_map_create (header, nodes, x, y,
|
|
NULL, 0);
|
|
L3TDEGamePtr game = l3tde_game_create (map, 0, 0);
|
|
status->game = game;
|
|
break;
|
|
}
|
|
case KEY_TAB:
|
|
case KEY_DOWN:
|
|
if (!status->is_button_accept_selected
|
|
&& map_editor_get_last_field (status->fields)
|
|
== current_field (status->form_creation)) {
|
|
map_editor_creation_form_move_to_button_accept (status);
|
|
} else {
|
|
map_editor_creation_form_move_to_field (status, REQ_NEXT_FIELD);
|
|
}
|
|
break;
|
|
case KEY_UP:
|
|
if (!status->is_button_accept_selected
|
|
&& status->fields[0]
|
|
== current_field (status->form_creation)) {
|
|
map_editor_creation_form_move_to_button_accept (status);
|
|
} else {
|
|
map_editor_creation_form_move_to_field (status, REQ_PREV_FIELD);
|
|
}
|
|
break;
|
|
case ctrl ('c'):
|
|
status->exit = 1;
|
|
break;
|
|
case 'q':
|
|
if (status->is_button_accept_selected) {
|
|
status->exit = 1;
|
|
break;
|
|
}
|
|
default:
|
|
form_driver (form, ch);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void
|
|
map_editor_creation_form_render (MapEditorStatus *status) {
|
|
if (status->first_render_map_creation_form) {
|
|
erase ();
|
|
status->fields = malloc (sizeof *(status->fields) * N_FIELDS + 1);
|
|
curs_set (1);
|
|
status->fields[MAP_NAME_FIELD] = new_field (1, 10, 1, 0, 50, 0);
|
|
status->fields[MAP_DESCRIPTION_FIELD] = new_field (1, 10, 3, 0, 50, 0);
|
|
status->fields[X_FIELD] = new_field (1, 10, 5, 0, 50, 0);
|
|
status->fields[Y_FIELD] = new_field (1, 10, 7, 0, 50, 0);
|
|
status->fields[N_FIELDS] = NULL;
|
|
init_pair (1, COLOR_WHITE, COLOR_BLUE);
|
|
for (int i = 0; i < N_FIELDS; i++) {
|
|
set_field_fore (status->fields[i], COLOR_PAIR (1));
|
|
set_field_back (status->fields[i], COLOR_PAIR (1));
|
|
}
|
|
status->form_creation = new_form (status->fields);
|
|
post_form (status->form_creation);
|
|
status->first_render_map_creation_form = false;
|
|
mvprintw (0, 0, "Map name.");
|
|
mvprintw (2, 0, "Map description.");
|
|
mvprintw (4, 0, "Set the x max value.");
|
|
mvprintw (6, 0, "Set the y max value.");
|
|
attron (COLOR_PAIR (1));
|
|
mvprintw (9, 0, "Accept");
|
|
attroff (COLOR_PAIR (1));
|
|
}
|
|
}
|
|
|
|
|
|
static void
|
|
map_editor_creation_form_move_to_field (MapEditorStatus *status, int move_to) {
|
|
FORM *form = status->form_creation;
|
|
|
|
status->is_button_accept_selected = false;
|
|
form_driver (form, move_to);
|
|
}
|
|
|
|
|
|
static void
|
|
map_editor_creation_form_move_to_button_accept (MapEditorStatus *status) {
|
|
status->is_button_accept_selected = true;
|
|
wmove (stdscr, 9, 0);
|
|
}
|
|
|
|
static FIELD *
|
|
map_editor_get_last_field (FIELD **fields) {
|
|
FIELD *last_field = NULL;
|
|
size_t i = 0;
|
|
do {
|
|
last_field = fields[i];
|
|
i++;
|
|
} while (fields[i] != NULL);
|
|
return last_field;
|
|
}
|