From acc33aa47e133e7734d17d3289ee1ffd1997913d Mon Sep 17 00:00:00 2001 From: Hielke Morsink Date: Tue, 26 Dec 2017 15:36:22 +0100 Subject: [PATCH] Remove duplicated code in text window The two functions for opening were almost identical. The only difference was that one was passed a string ID and the other a raw string. The one taking the string ID now converts it to a raw string, and then calls the other functions. This also makes the utf8 string const and replaces some C-string code with String::Set. --- src/openrct2-ui/windows/TextInput.cpp | 98 ++++++--------------------- src/openrct2-ui/windows/Window.h | 2 +- 2 files changed, 23 insertions(+), 77 deletions(-) diff --git a/src/openrct2-ui/windows/TextInput.cpp b/src/openrct2-ui/windows/TextInput.cpp index f6df561f71..54bdab8586 100644 --- a/src/openrct2-ui/windows/TextInput.cpp +++ b/src/openrct2-ui/windows/TextInput.cpp @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include #include #include @@ -91,27 +93,34 @@ static rct_window_event_list window_text_input_events = { }; static rct_string_id input_text_description; -static char text_input[TEXT_INPUT_SIZE] = { 0 }; +static utf8 text_input[TEXT_INPUT_SIZE] = { 0 }; static rct_windowclass calling_class = 0; static rct_windownumber calling_number = 0; static sint32 calling_widget = 0; static sint32 _maxInputLength; void window_text_input_open(rct_window* call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description, rct_string_id existing_text, uintptr_t existing_args, sint32 maxLength) +{ + // Get the raw string + utf8 buffer[Util::CountOf(text_input)]; + if (existing_text != STR_NONE) + format_string(buffer, maxLength, existing_text, &existing_args); + + utf8_remove_format_codes(buffer, false); + window_text_input_raw_open(call_w, call_widget, title, description, buffer, maxLength); +} + +void window_text_input_raw_open(rct_window* call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description, const_utf8string existing_text, sint32 maxLength) { _maxInputLength = maxLength; window_close_by_class(WC_TEXTINPUT); - // Clear the text input buffer - memset(text_input, 0, maxLength); - - // Enter in the text input buffer any existing - // text. - if (existing_text != STR_NONE) - format_string(text_input, maxLength, existing_text, &existing_args); - - utf8_remove_format_codes(text_input, false); + // Set the input text + if (existing_text != nullptr) + String::Set(text_input, sizeof(text_input), existing_text); + else + String::Set(text_input, sizeof(text_input), ""); // This is the text displayed above the input box input_text_description = description; @@ -122,8 +131,7 @@ void window_text_input_open(rct_window* call_w, rct_widgetindex call_widget, rct sint32 no_lines = 0, font_height = 0; - // String length needs to add 12 either side of box - // +13 for cursor when max length. + // String length needs to add 12 either side of box +13 for cursor when max length. gfx_wrap_string(wrapped_string, WW - (24 + 13), &no_lines, &font_height); sint32 height = no_lines * 10 + WH; @@ -138,73 +146,11 @@ void window_text_input_open(rct_window* call_w, rct_widgetindex call_widget, rct ); w->widgets = window_text_input_widgets; - w->enabled_widgets = (1 << WIDX_CLOSE) | (1<enabled_widgets = (1ULL << WIDX_CLOSE) | (1ULL << WIDX_CANCEL) | (1ULL << WIDX_OKAY); window_text_input_widgets[WIDX_TITLE].text = title; - // Save calling window details so that the information - // can be passed back to the correct window & widget - calling_class = call_w->classification; - calling_number = call_w->number; - calling_widget = call_widget; - - gTextInput = context_start_text_input(text_input, maxLength); - - window_init_scroll_widgets(w); - w->colours[0] = call_w->colours[0]; - w->colours[1] = call_w->colours[1]; - w->colours[2] = call_w->colours[2]; -} - -void window_text_input_raw_open(rct_window* call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description, utf8string existing_text, sint32 maxLength) -{ - _maxInputLength = maxLength; - - window_close_by_class(WC_TEXTINPUT); - - // Clear the text input buffer - memset(text_input, 0, maxLength); - - // Enter in the text input buffer any existing - // text. - if (existing_text != nullptr) - safe_strcpy(text_input, existing_text, maxLength); - - // In order to prevent strings that exceed the maxLength - // from crashing the game. - text_input[maxLength - 1] = '\0'; - - // This is the text displayed above the input box - input_text_description = description; - - // Work out the existing size of the window - char wrapped_string[TEXT_INPUT_SIZE]; - safe_strcpy(wrapped_string, text_input, TEXT_INPUT_SIZE); - - sint32 no_lines = 0, font_height = 0; - - // String length needs to add 12 either side of box - // +13 for cursor when max length. - gfx_wrap_string(wrapped_string, WW - (24 + 13), &no_lines, &font_height); - - sint32 height = no_lines * 10 + WH; - - // Window will be in the centre of the screen - rct_window* w = window_create_centred( - WW, - height, - &window_text_input_events, - WC_TEXTINPUT, - WF_STICK_TO_FRONT - ); - - w->widgets = window_text_input_widgets; - w->enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_CANCEL) | (1 << WIDX_OKAY); - - window_text_input_widgets[WIDX_TITLE].text = title; - - // Save calling window details so that the information - // can be passed back to the correct window & widget + // Save calling window details so that the information can be passed back to the correct window & widget calling_class = call_w->classification; calling_number = call_w->number; calling_widget = call_widget; diff --git a/src/openrct2-ui/windows/Window.h b/src/openrct2-ui/windows/Window.h index e8e4bee43c..121dd4d5ec 100644 --- a/src/openrct2-ui/windows/Window.h +++ b/src/openrct2-ui/windows/Window.h @@ -131,7 +131,7 @@ void window_network_status_close(); void window_text_input_key(rct_window * w, char keychar); void window_text_input_open(rct_window * call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description, rct_string_id existing_text, uintptr_t existing_args, sint32 maxLength); -void window_text_input_raw_open(rct_window * call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description, utf8string existing_text, sint32 maxLength); +void window_text_input_raw_open(rct_window * call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description, const_utf8string existing_text, sint32 maxLength); rct_window * window_object_load_error_open(utf8 * path, size_t numMissingObjects, const rct_object_entry * missingObjects);