Refactoring creation form to other file.
This commit is contained in:
parent
cfb5dd00f3
commit
d18b0185aa
|
@ -1,3 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <form.h>
|
||||
|
||||
#define ctrl(x) ((x) & 0x1f)
|
||||
|
||||
typedef enum {
|
||||
SELECT_MAP,
|
||||
MAP_CREATION_FORM,
|
||||
MAP_SELECTION_FORM,
|
||||
MAP_EDITOR,
|
||||
N_FORMS
|
||||
} MapEditorForms;
|
||||
|
||||
typedef enum {
|
||||
CREATE_NEW_MAP,
|
||||
OPEN_MAP,
|
||||
N_OPTIONS
|
||||
} MapEditorInitialFormOptions;
|
||||
|
||||
typedef struct {
|
||||
MapEditorInitialFormOptions selected_option;
|
||||
MapEditorForms current_form;
|
||||
bool exit;
|
||||
bool first_render_map_creation_form;
|
||||
bool is_button_accept_selected;
|
||||
FIELD **fields;
|
||||
FORM *form_creation;
|
||||
} MapEditorStatus;
|
||||
|
||||
void
|
||||
map_editor_init ();
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
void
|
||||
map_editor_creation_form_handle_input (MapEditorStatus *status);
|
||||
|
||||
void
|
||||
map_editor_creation_form_render (MapEditorStatus *status);
|
|
@ -12,6 +12,7 @@ sources = [
|
|||
'src/overworld.c',
|
||||
'src/game.c',
|
||||
'src/game/map_editor.c',
|
||||
'src/game/map_editor/creation_form.c',
|
||||
'src/map.c',
|
||||
'src/map/warp.c',
|
||||
'src/map/node.c',
|
||||
|
|
|
@ -5,35 +5,9 @@
|
|||
#include <form.h>
|
||||
|
||||
#include <l3tde/game/map_editor.h>
|
||||
#include <l3tde/game/map_editor/creation_form.h>
|
||||
|
||||
#define BACKSPACE 127
|
||||
#define KEY_INTRO '\n'
|
||||
#define KEY_TAB '\t'
|
||||
#define ctrl(x) ((x) & 0x1f)
|
||||
|
||||
typedef enum {
|
||||
SELECT_MAP,
|
||||
MAP_CREATION_FORM,
|
||||
MAP_SELECTION_FORM,
|
||||
MAP_EDITOR,
|
||||
N_FORMS
|
||||
} MapEditorForms;
|
||||
|
||||
typedef enum {
|
||||
CREATE_NEW_MAP,
|
||||
OPEN_MAP,
|
||||
N_OPTIONS
|
||||
} MapEditorInitialFormOptions;
|
||||
|
||||
typedef struct {
|
||||
MapEditorInitialFormOptions selected_option;
|
||||
MapEditorForms current_form;
|
||||
bool exit;
|
||||
bool first_render_map_creation_form;
|
||||
FIELD **fields;
|
||||
FORM *form_creation;
|
||||
} MapEditorStatus;
|
||||
|
||||
const char *OPTIONS_STR[N_OPTIONS] = {
|
||||
"Create a new map.",
|
||||
"Open an existing map."
|
||||
|
@ -45,8 +19,6 @@ static void
|
|||
map_editor_print_main_menu (MapEditorStatus *status);
|
||||
static void
|
||||
map_editor_handle_input (MapEditorStatus *status);
|
||||
static void
|
||||
map_editor_handle_input_creation_form (MapEditorStatus *status);
|
||||
|
||||
void
|
||||
map_editor_init () {
|
||||
|
@ -81,6 +53,7 @@ map_editor_handle_input_select_map (MapEditorStatus *status) {
|
|||
case KEY_INTRO:
|
||||
if (status->selected_option == CREATE_NEW_MAP) {
|
||||
status->first_render_map_creation_form = true;
|
||||
status->is_button_accept_selected = false;
|
||||
status->current_form = MAP_CREATION_FORM;
|
||||
}
|
||||
break;
|
||||
|
@ -126,7 +99,7 @@ map_editor_handle_input (MapEditorStatus *status) {
|
|||
map_editor_handle_input_map_editor (status);
|
||||
break;
|
||||
case MAP_CREATION_FORM:
|
||||
map_editor_handle_input_creation_form (status);
|
||||
map_editor_creation_form_handle_input (status);
|
||||
break;
|
||||
default:
|
||||
fprintf (stderr, "Unable to handle input creation form\n");
|
||||
|
@ -134,41 +107,8 @@ map_editor_handle_input (MapEditorStatus *status) {
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
map_editor_render_map_creation_form (MapEditorStatus *status) {
|
||||
if (status->first_render_map_creation_form) {
|
||||
erase ();
|
||||
status->fields = malloc (sizeof *(status->fields) * 3);
|
||||
curs_set (1);
|
||||
status->fields[0] = new_field (1, 10, 1, 0, 50, 0);
|
||||
status->fields[1] = new_field (1, 10, 3, 0, 50, 0);
|
||||
status->fields[2] = NULL;
|
||||
init_pair (1, COLOR_WHITE, COLOR_BLUE);
|
||||
set_field_fore (status->fields[0], COLOR_PAIR (1));
|
||||
set_field_back (status->fields[0], COLOR_PAIR (1));
|
||||
set_field_fore (status->fields[1], COLOR_PAIR (1));
|
||||
set_field_back (status->fields[1], 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, "Set the x max value.");
|
||||
mvprintw (2, 0, "Set the y max value.");
|
||||
attron (COLOR_PAIR (1));
|
||||
mvprintw (5, 0, "Accept");
|
||||
attroff (COLOR_PAIR (1));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
map_editor_render (MapEditorStatus *status) {
|
||||
|
@ -176,7 +116,7 @@ map_editor_render (MapEditorStatus *status) {
|
|||
map_editor_print_main_menu (status);
|
||||
}
|
||||
if (status->current_form == MAP_CREATION_FORM) {
|
||||
map_editor_render_map_creation_form (status);
|
||||
map_editor_creation_form_render (status);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,40 +130,3 @@ map_editor_print_main_menu (MapEditorStatus *status) {
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
map_editor_handle_input_creation_form (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_DOWN:
|
||||
if (map_editor_get_last_field (status->fields)
|
||||
== current_field (status->form_creation)) {
|
||||
wmove (stdscr, 5, 0);
|
||||
} else {
|
||||
form_driver (form, REQ_NEXT_FIELD);
|
||||
}
|
||||
break;
|
||||
case KEY_TAB:
|
||||
form_driver (form, REQ_NEXT_FIELD);
|
||||
break;
|
||||
case KEY_UP:
|
||||
form_driver (form, REQ_PREV_FIELD);
|
||||
break;
|
||||
case ctrl ('c'):
|
||||
status->exit = 1;
|
||||
break;
|
||||
default:
|
||||
form_driver (form, ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include <l3tde/game/map_editor.h>
|
||||
#include <l3tde/game/map_editor/creation_form.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);
|
||||
|
||||
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_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) * 3);
|
||||
curs_set (1);
|
||||
status->fields[0] = new_field (1, 10, 1, 0, 50, 0);
|
||||
status->fields[1] = new_field (1, 10, 3, 0, 50, 0);
|
||||
status->fields[2] = NULL;
|
||||
init_pair (1, COLOR_WHITE, COLOR_BLUE);
|
||||
set_field_fore (status->fields[0], COLOR_PAIR (1));
|
||||
set_field_back (status->fields[0], COLOR_PAIR (1));
|
||||
set_field_fore (status->fields[1], COLOR_PAIR (1));
|
||||
set_field_back (status->fields[1], 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, "Set the x max value.");
|
||||
mvprintw (2, 0, "Set the y max value.");
|
||||
attron (COLOR_PAIR (1));
|
||||
mvprintw (5, 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, 5, 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;
|
||||
}
|
Loading…
Reference in New Issue