From f93aeb33c6cfb7b0dec103f48389aea8207b709e Mon Sep 17 00:00:00 2001 From: Duncan Frost Date: Mon, 22 Dec 2014 10:40:00 +0000 Subject: [PATCH] Added multi line text input --- src/windows/text_input.c | 90 ++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 22 deletions(-) diff --git a/src/windows/text_input.c b/src/windows/text_input.c index 3ab2861575..9b65d0f603 100644 --- a/src/windows/text_input.c +++ b/src/windows/text_input.c @@ -35,6 +35,8 @@ #define WW 250 #define WH 90 +#define MAX_LINE_LENGTH 32 + enum WINDOW_TEXT_INPUT_WIDGET_IDX { WIDX_BACKGROUND, WIDX_TITLE, @@ -59,6 +61,7 @@ static void window_text_input_paint(); static void window_text_input_text(int key, rct_window* w); static void window_text_input_update(rct_window* w); static void window_text_input_close(); +static void window_text_input_invalidate(); //0x9A3F7C static void* window_text_input_events[] = { @@ -87,7 +90,7 @@ static void* window_text_input_events[] = { window_text_input_emptysub, window_text_input_emptysub, window_text_input_emptysub, - window_text_input_emptysub, + window_text_input_invalidate, window_text_input_paint, window_text_input_emptysub }; @@ -103,14 +106,17 @@ void window_text_input_open(rct_window* call_w, int call_widget, rct_string_id t { _maxInputLength = maxLength; + int no_lines = (_maxInputLength - 1) / MAX_LINE_LENGTH; + int height = WH + no_lines * 10; + window_close_by_class(WC_TEXTINPUT); // Window will be in the center of the screen rct_window* w = window_create( (RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, sint16) / 2) - WW / 2, - (RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, sint16) / 2) - WH / 2, + (RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, sint16) / 2) - height / 2, WW, - WH, + height, (uint32*)window_text_input_events, WC_TEXTINPUT, 0 @@ -189,34 +195,60 @@ static void window_text_input_paint(){ int y = w->y + 25; + int no_lines = (_maxInputLength - 1) / MAX_LINE_LENGTH; + int font_height = 0; + int height = WH + no_lines * 10; + gfx_draw_string_centred(dpi, input_text_description, w->x + WW / 2, y, w->colours[1], 0); y += 25; - gfx_fill_rect_inset(dpi, w->x + 10, y, w->x + WW - 10, y + 12, w->colours[1], 0x60); + gfx_fill_rect_inset(dpi, w->x + 10, y, w->x + WW - 10, y + 10 * (no_lines + 1) + 2, w->colours[1], 0x60); y += 1; - gfx_draw_string(dpi, text_input, w->colours[1], w->x + 12, y); - // Make a copy of the string for measuring the width. - char temp_string[512] = { 0 }; - memcpy(temp_string, text_input, gTextInputCursorPosition); - - int x = w->x + 13 + gfx_get_string_width(temp_string); + char wrapped_string[512]; + strcpy(wrapped_string, text_input); - int width = 6; - if ((uint32)gTextInputCursorPosition < strlen(text_input)){ - // Make a new 1 character wide string for measuring the width - // of the character that the cursor is under. - temp_string[1] = '\0'; - temp_string[0] = text_input[gTextInputCursorPosition]; - width = max(gfx_get_string_width(temp_string) - 2, 4); - } + gfx_wrap_string(wrapped_string, WW - 24, &no_lines, &font_height); - // Draw the cursor - y += 9; - if (w->frame_no > 15){ - gfx_fill_rect(dpi, x, y, x + width, y, w->colours[1]); + char* wrap_pointer = wrapped_string; + int char_count = 0; + uint8 cur_drawn = 0; + + for (int line = 0; line <= no_lines; ++line){ + gfx_draw_string(dpi, wrap_pointer, w->colours[1], w->x + 12, y); + + int string_length = get_string_length(wrap_pointer); + + if (!cur_drawn && (gTextInputCursorPosition <= char_count + string_length)){ + // Make a copy of the string for measuring the width. + char temp_string[512] = { 0 }; + memcpy(temp_string, wrap_pointer, gTextInputCursorPosition - char_count); + int cur_x = w->x + 13 + gfx_get_string_width(temp_string); + + int width = 6; + if ((uint32)gTextInputCursorPosition < strlen(text_input)){ + // Make a new 1 character wide string for measuring the width + // of the character that the cursor is under. + temp_string[1] = '\0'; + temp_string[0] = text_input[gTextInputCursorPosition]; + width = max(gfx_get_string_width(temp_string) - 2, 4); + } + + if (w->frame_no > 15){ + gfx_fill_rect(dpi, cur_x, y + 9, cur_x + width, y + 9, w->colours[1]); + } + + cur_drawn++; + } + + wrap_pointer += string_length + 1; + + if (text_input[char_count + string_length] == ' ')char_count++; + char_count += string_length; + + y += 10; } } @@ -259,4 +291,18 @@ static void window_text_input_close(){ // Make sure that we take it out of the text input // mode otherwise problems may occur. osinterface_stop_text_input(); +} + +static void window_text_input_invalidate(){ + int no_lines = (_maxInputLength - 1) / MAX_LINE_LENGTH; + + int height = WH + no_lines * 10; + + window_text_input_widgets[WIDX_OKAY].top = height - 21; + window_text_input_widgets[WIDX_OKAY].bottom = height - 10; + + window_text_input_widgets[WIDX_CANCEL].top = height - 21; + window_text_input_widgets[WIDX_CANCEL].bottom = height - 10; + + window_text_input_widgets[WIDX_BACKGROUND].bottom = height - 1; } \ No newline at end of file