From 78128222e9e281a35f6483bf6bc4edf301ba8a5b Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Thu, 30 Mar 2023 01:28:49 +0200 Subject: [PATCH] Make textbuttons() work for transparent text boxes Misa asked me if this should only work for non-transparent textboxes, and it shouldn't - that was kind of an oversight. To make it work for transparent textboxes as well, I made a little restructuring to avoid duplicating the code - fill_buttons() is now called textbox_line(), and it replaces the direct accessing of the textbox lines in the printing loops. The code that checks the width of the textbox does not need to be copied, since the text box is naturally not drawn for transparent text boxes. --- desktop_version/src/Graphics.cpp | 45 ++++++++++++++++++-------------- desktop_version/src/Graphics.h | 1 + 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index 9e310795..8e260878 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -765,8 +765,23 @@ void Graphics::drawtile3( int x, int y, int t, int off, int height_subtract /*= draw_texture_part(grphx.im_tiles3, x, y, x2, y2, 8, 8 - height_subtract, 1, 1); } -static void fill_buttons(char* buffer, const size_t buffer_len, const char* line) -{ +const char* Graphics::textbox_line( + char* buffer, + const size_t buffer_len, + const size_t textbox_i, + const size_t line_i +) { + /* Gets a line in a textbox, accounting for filling button placeholders like {b_map}. + * Takes a buffer as an argument, but DOESN'T ALWAYS write to that buffer. + * Always use the return value! ^^ + * Does not check boundaries. */ + + const char* line = textboxes[textbox_i].lines[line_i].c_str(); + if (!textboxes[textbox_i].fill_buttons) + { + return line; + } + vformat_buf(buffer, buffer_len, line, "b_act:but," @@ -780,6 +795,7 @@ static void fill_buttons(char* buffer, const size_t buffer_len, const char* line vformat_button(ActionSet_InGame, Action_InGame_Restart), vformat_button(ActionSet_InGame, Action_InGame_Esc) ); + return buffer; } void Graphics::drawgui(void) @@ -787,7 +803,6 @@ void Graphics::drawgui(void) int text_sign; int crew_yp; int crew_sprite; - size_t i; if (flipmode) { @@ -803,7 +818,7 @@ void Graphics::drawgui(void) } //Draw all the textboxes to the screen - for (i = 0; i max) { max = len; @@ -881,21 +896,11 @@ void Graphics::drawgui(void) for (j = 0; j < textboxes[i].lines.size(); j++) { - const char* line = textboxes[i].lines[j].c_str(); - char buffer[SCREEN_WIDTH_CHARS + 1]; - - if (textboxes[i].fill_buttons) - { - // Fill button placeholders like {b_map} in dialogue text. - fill_buttons(buffer, sizeof(buffer), line); - line = buffer; - } - font::print( textboxes[i].print_flags | PR_BRIGHTNESS(tl_lerp*255) | PR_CJK_LOW, textboxes[i].xp + 8, yp + text_yoff + text_sign * (j * font_height), - line, + textbox_line(buffer, sizeof(buffer), i, j), textboxes[i].r, textboxes[i].g, textboxes[i].b ); } diff --git a/desktop_version/src/Graphics.h b/desktop_version/src/Graphics.h index 035a45d4..f689e39a 100644 --- a/desktop_version/src/Graphics.h +++ b/desktop_version/src/Graphics.h @@ -160,6 +160,7 @@ public: void draw_grid_tile(SDL_Texture* texture, int t, int x, int y, int width, int height, SDL_Color color); void updatetextboxes(void); + const char* textbox_line(char* buffer, size_t buffer_len, size_t textbox_i, size_t line_i); void drawgui(void); void draw_sprite(int x, int y, int t, int r, int g, int b);