Refactoring the form logic.

This commit is contained in:
sergiotarxz 2021-12-20 12:02:39 +01:00
parent 984c4c5efc
commit 5579d66080
1 changed files with 41 additions and 32 deletions

View File

@ -42,6 +42,8 @@ static void
map_editor_print_main_menu (MapEditorStatus *status); map_editor_print_main_menu (MapEditorStatus *status);
static void static void
map_editor_handle_input (MapEditorStatus *status); map_editor_handle_input (MapEditorStatus *status);
static void
map_editor_handle_input_creation_form (MapEditorStatus *status);
void void
map_editor_init () { map_editor_init () {
@ -58,7 +60,6 @@ map_editor_loop () {
MapEditorStatus *status = malloc (sizeof *status); MapEditorStatus *status = malloc (sizeof *status);
status->selected_option = 0; status->selected_option = 0;
status->exit = false; status->exit = false;
status->first_render_map_creation_form = true;
status->current_form = SELECT_MAP; status->current_form = SELECT_MAP;
status->form_creation = NULL; status->form_creation = NULL;
status->fields = NULL; status->fields = NULL;
@ -112,22 +113,27 @@ map_editor_handle_input_map_editor (MapEditorStatus *status) {
static void static void
map_editor_handle_input (MapEditorStatus *status) { map_editor_handle_input (MapEditorStatus *status) {
if (status->current_form == SELECT_MAP) { switch (status->current_form) {
case SELECT_MAP:
map_editor_handle_input_select_map (status); map_editor_handle_input_select_map (status);
return; break;
} case MAP_EDITOR:
if (status->current_form == MAP_EDITOR) {
map_editor_handle_input_map_editor (status); map_editor_handle_input_map_editor (status);
return; break;
case MAP_CREATION_FORM:
map_editor_handle_input_creation_form (status);
break;
default:
fprintf (stderr, "Unable to handle input creation form\n");
break;
} }
} }
static void static void
map_editor_render_map_creation_form (MapEditorStatus *status) { map_editor_render_map_creation_form (MapEditorStatus *status) {
if (status->first_render_map_creation_form) { if (status->first_render_map_creation_form) {
erase ();
status->fields = malloc (sizeof *(status->fields) * 3); status->fields = malloc (sizeof *(status->fields) * 3);
FORM *form;
int ch;
curs_set (1); curs_set (1);
status->fields[0] = new_field (1, 10, 1, 0, 50, 0); status->fields[0] = new_field (1, 10, 1, 0, 50, 0);
status->fields[1] = new_field (1, 10, 3, 0, 50, 0); status->fields[1] = new_field (1, 10, 3, 0, 50, 0);
@ -137,14 +143,40 @@ map_editor_render_map_creation_form (MapEditorStatus *status) {
set_field_back (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_fore (status->fields[1], COLOR_PAIR(1));
set_field_back (status->fields[1], COLOR_PAIR(1)); set_field_back (status->fields[1], COLOR_PAIR(1));
form = new_form(status->fields); status->form_creation = new_form(status->fields);
post_form(form); post_form (status->form_creation);
status->first_render_map_creation_form = false;
mvprintw (0, 0, "Set the x max value."); mvprintw (0, 0, "Set the x max value.");
mvprintw (2, 0, "Set the y max value."); mvprintw (2, 0, "Set the y max value.");
refresh (); }
while((ch = getch())) }
{ switch(ch)
{ static void
map_editor_render (MapEditorStatus *status) {
if (status->current_form == SELECT_MAP) {
map_editor_print_main_menu (status);
}
if (status->current_form == MAP_CREATION_FORM) {
map_editor_render_map_creation_form (status);
}
}
static void
map_editor_print_main_menu (MapEditorStatus *status) {
erase ();
for (int i = 0; i < N_OPTIONS; i++) {
if (status->selected_option == i) attron (A_REVERSE);
mvprintw(i, 0, "%s", OPTIONS_STR[i]);
if (status->selected_option == i) attroff (A_REVERSE);
}
}
static void
map_editor_handle_input_creation_form (MapEditorStatus *status) {
FORM *form = status->form_creation;
int ch;
ch = getch();
switch(ch) {
case BACKSPACE: case BACKSPACE:
form_driver(form, REQ_DEL_PREV); form_driver(form, REQ_DEL_PREV);
break; break;
@ -158,27 +190,4 @@ map_editor_render_map_creation_form (MapEditorStatus *status) {
form_driver(form, ch); form_driver(form, ch);
break; break;
} }
}
status->first_render_map_creation_form = false;
}
}
static void
map_editor_render (MapEditorStatus *status) {
erase ();
if (status->current_form == SELECT_MAP) {
map_editor_print_main_menu (status);
}
if (status->current_form == MAP_CREATION_FORM) {
map_editor_render_map_creation_form (status);
}
}
static void
map_editor_print_main_menu (MapEditorStatus *status) {
for (int i = 0; i < N_OPTIONS; i++) {
if (status->selected_option == i) attron (A_REVERSE);
mvprintw(i, 0, "%s", OPTIONS_STR[i]);
if (status->selected_option == i) attroff (A_REVERSE);
}
} }