From 5579d66080ce9edfff4d4b9008da73820b994044 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Mon, 20 Dec 2021 12:02:39 +0100 Subject: [PATCH] Refactoring the form logic. --- src/game/map_editor.c | 73 ++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/src/game/map_editor.c b/src/game/map_editor.c index 59ffe5a..c369d5a 100644 --- a/src/game/map_editor.c +++ b/src/game/map_editor.c @@ -42,6 +42,8 @@ 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 () { @@ -58,7 +60,6 @@ map_editor_loop () { MapEditorStatus *status = malloc (sizeof *status); status->selected_option = 0; status->exit = false; - status->first_render_map_creation_form = true; status->current_form = SELECT_MAP; status->form_creation = NULL; status->fields = NULL; @@ -112,22 +113,27 @@ map_editor_handle_input_map_editor (MapEditorStatus *status) { static void map_editor_handle_input (MapEditorStatus *status) { - if (status->current_form == SELECT_MAP) { - map_editor_handle_input_select_map (status); - return; - } - if (status->current_form == MAP_EDITOR) { - map_editor_handle_input_map_editor (status); - return; + switch (status->current_form) { + case SELECT_MAP: + map_editor_handle_input_select_map (status); + break; + case MAP_EDITOR: + map_editor_handle_input_map_editor (status); + 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 map_editor_render_map_creation_form (MapEditorStatus *status) { if (status->first_render_map_creation_form) { + erase (); status->fields = malloc (sizeof *(status->fields) * 3); - FORM *form; - int ch; 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); @@ -137,35 +143,16 @@ map_editor_render_map_creation_form (MapEditorStatus *status) { 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)); - form = new_form(status->fields); - post_form(form); + 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."); - refresh (); - while((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; - default: - form_driver(form, ch); - 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); } @@ -176,9 +163,31 @@ map_editor_render (MapEditorStatus *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: + 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; + default: + form_driver(form, ch); + break; + } +}