From 0a96e12306434b5365f9c956ad71cc4958b3ee97 Mon Sep 17 00:00:00 2001 From: Ivan Mogilko Date: Sun, 12 Jan 2025 00:45:15 +0300 Subject: [PATCH] Engine: combined few display_main params into DisplayTextLooks struct Added DisplayTextShrink enum for the "allow shrink" parameter. --- Engine/ac/character.cpp | 16 +++++++------- Engine/ac/display.cpp | 39 +++++++++++++++++----------------- Engine/ac/display.h | 32 +++++++++++++++++++++++----- Engine/ac/global_character.cpp | 8 +++---- Engine/ac/overlay.cpp | 15 +++++++------ Engine/ac/overlay.h | 2 +- 6 files changed, 67 insertions(+), 45 deletions(-) diff --git a/Engine/ac/character.cpp b/Engine/ac/character.cpp index b6e54af0b2f..33327f4bedd 100644 --- a/Engine/ac/character.cpp +++ b/Engine/ac/character.cpp @@ -2603,7 +2603,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i const color_t text_color = speakingChar->talkcolor; Rect ui_view = play.GetUIViewport(); - int allowShrink = 0; + DisplayTextShrink allow_shrink = kDisplayTextShrink_None; int bwidth = widd; if (bwidth < 0) bwidth = ui_view.GetWidth()/2 + ui_view.GetWidth()/4; @@ -2617,7 +2617,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i if (useview == 0) useview = -1; // speech bubble can shrink to fit - allowShrink = 1; + allow_shrink = kDisplayTextShrink_Left; if (speakingChar->room != displayed_room) { // not in room, centre it xx = -1; @@ -2633,7 +2633,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i set_our_eip(1500); if (game.options[OPT_SPEECHTYPE] == 0) - allowShrink = 1; + allow_shrink = kDisplayTextShrink_Left; // If has a valid speech view, and idle anim in progress for the character, then stop it if (useview >= 0) @@ -2881,7 +2881,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i } // allow the text box to be shrunk to fit the text - allowShrink = 1; + allow_shrink = kDisplayTextShrink_Left; // if the portrait's on the right, swap it round if (portrait_on_right) { @@ -2905,7 +2905,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i tdxp = xx; } tdxp += get_textwindow_border_width(play.speech_textwindow_gui) / 2; - allowShrink = 2; + allow_shrink = kDisplayTextShrink_Right; } if (game.options[OPT_SPEECHTYPE] == 3) overlay_x = 0; @@ -2971,7 +2971,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i } } else { - allowShrink = 1; + allow_shrink = kDisplayTextShrink_Left; } // it wants the centred position, so make it so @@ -2980,14 +2980,14 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i // if they used DisplaySpeechAt, then use the supplied width if ((widd > 0) && (isThought == 0)) - allowShrink = 0; + allow_shrink = kDisplayTextShrink_None; if (isThought) char_thinking = aschar; set_our_eip(155); display_main(tdxp, tdyp, bwidth, texx, nullptr, kDisplayText_Speech, 0 /* no overid */, - disp_style, FONT_SPEECH, text_color, isThought, allowShrink, -1 /* don't autoplace */); + DisplayTextLooks(disp_style, isThought, allow_shrink), FONT_SPEECH, text_color, -1 /* don't autoplace */); set_our_eip(156); if ((play.in_conversation > 0) && (game.options[OPT_SPEECHTYPE] == 3)) closeupface = nullptr; diff --git a/Engine/ac/display.cpp b/Engine/ac/display.cpp index fb5e7bca0dd..e8ff08ebe82 100644 --- a/Engine/ac/display.cpp +++ b/Engine/ac/display.cpp @@ -139,16 +139,16 @@ class DisplayMessageState : public GameState // Generates a textual image and returns a disposable bitmap -Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t text_color, int isThought, - int &xx, int &yy, int &adjustedXX, int &adjustedYY, int wii, int usingfont, int allowShrink, +Bitmap *create_textual_image(const char *text, const DisplayTextLooks &look, color_t text_color, + int &xx, int &yy, int &adjustedXX, int &adjustedYY, int wii, int usingfont, const TopBarSettings *topbar) { // // Configure the textual image // - const bool use_speech_textwindow = (style == kDisplayTextStyle_TextWindow) && (game.options[OPT_SPEECHTYPE] >= 2); - const bool use_thought_gui = (isThought) && (game.options[OPT_THOUGHTGUI] > 0); + const bool use_speech_textwindow = (look.Style == kDisplayTextStyle_TextWindow) && (game.options[OPT_SPEECHTYPE] >= 2); + const bool use_thought_gui = (look.AsThought) && (game.options[OPT_THOUGHTGUI] > 0); int usingGui = -1; if (use_speech_textwindow) @@ -185,7 +185,7 @@ Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t t yy = ui_view.GetHeight() / 2 - disp.FullTextHeight / 2 - padding; } // speech, so it wants to be above the character's head - else if (style == kDisplayTextStyle_Overchar) { + else if (look.Style == kDisplayTextStyle_Overchar) { yy -= disp.FullTextHeight; if (yy < 5) yy = 5; yy = adjust_y_for_guis(yy); @@ -195,11 +195,11 @@ Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t t // shrink the width of the dialog box to fit the text int oldWid = wii; // If it's not speech, or a shrink is allowed, then shrink it - if ((style == kDisplayTextStyle_MessageBox) || (allowShrink > 0)) + if ((look.Style == kDisplayTextStyle_MessageBox) || (look.AllowShrink > 0)) wii = longestline + paddingDoubledScaled; // shift the dialog box right to align it, if necessary - if ((allowShrink == 2) && (xx >= 0)) + if ((look.AllowShrink == 2) && (xx >= 0)) xx += (oldWid - wii); } @@ -237,12 +237,13 @@ Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t t if ((strlen(todis) < 1) || (strcmp(todis, " ") == 0) || (wii == 0)) return text_window_ds; - if (style != kDisplayTextStyle_MessageBox) + if (look.Style != kDisplayTextStyle_MessageBox) { // Textual overlay purposed for character speech int ttxleft = 0, ttxtop = paddingScaled, oriwid = wii - padding * 2; int drawBackground = 0; + DisplayTextLooks fix_look = look; if (use_speech_textwindow) { drawBackground = 1; @@ -250,7 +251,7 @@ Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t t else if (use_thought_gui) { // make it treat it as drawing inside a window now - style = kDisplayTextStyle_TextWindow; + fix_look.Style = kDisplayTextStyle_TextWindow; drawBackground = 1; } @@ -262,10 +263,10 @@ Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t t } // Assign final text color, either use passed parameter, or TextWindow property - if (style == kDisplayTextStyle_TextWindow) + if (fix_look.Style == kDisplayTextStyle_TextWindow) { if ((usingGui >= 0) && - ((game.options[OPT_SPEECHTYPE] >= 2) || (isThought))) + ((game.options[OPT_SPEECHTYPE] >= 2) || (fix_look.AsThought))) text_color = text_window_ds->GetCompatibleColor(guis[usingGui].FgColor); else text_color = text_window_ds->GetCompatibleColor(text_color); @@ -280,7 +281,7 @@ Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t t { int ttyp = ttxtop + i * disp.Linespacing; // if it's inside a text box then don't centre the text - if (style == kDisplayTextStyle_TextWindow) + if (fix_look.Style == kDisplayTextStyle_TextWindow) { wouttext_aligned(text_window_ds, ttxleft, ttyp, oriwid, usingfont, text_color, Lines[i].GetCStr(), play.text_align); } @@ -378,8 +379,8 @@ bool display_check_user_input(int skip) // pass blocking=2 to create permanent overlay ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, const TopBarSettings *topbar, DisplayTextType disp_type, int over_id, - DisplayTextStyle style, int usingfont, color_t text_color, - int isThought, int allowShrink, int autoplace_at_char, bool roomlayer) + const DisplayTextLooks &look, int usingfont, color_t text_color, + int autoplace_at_char, bool roomlayer) { // // Prepare for the message display @@ -402,7 +403,7 @@ ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, disp_type = kDisplayText_Speech; } - if ((style == kDisplayTextStyle_Overchar) && (disp_type < kDisplayText_NormalOverlay)) + if ((look.Style == kDisplayTextStyle_Overchar) && (disp_type < kDisplayText_NormalOverlay)) { // update the all_buttons_disabled variable in advance // of the adjust_x/y_for_guis calls @@ -439,8 +440,8 @@ ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, } int adjustedXX, adjustedYY; - Bitmap *text_window_ds = create_textual_image(text, style, text_color, isThought, - xx, yy, adjustedXX, adjustedYY, wii, usingfont, allowShrink, topbar); + Bitmap *text_window_ds = create_textual_image(text, look, text_color, + xx, yy, adjustedXX, adjustedYY, wii, usingfont, topbar); size_t nse = add_screen_overlay(roomlayer, xx, yy, over_id, text_window_ds, adjustedXX - xx, adjustedYY - yy); auto *over = get_overlay(nse); // FIXME: optimize return value @@ -488,8 +489,8 @@ void display_at(int xx, int yy, int wii, const char *text, const TopBarSettings try_auto_play_speech(text, text, play.narrator_speech); display_main(xx, yy, wii, text, topbar, kDisplayText_MessageBox, 0 /* no overid */, - kDisplayTextStyle_MessageBox, FONT_NORMAL, 0, - 0 /* not thought */, 0 /* no shrink */, -1 /* no autoplace */, false /* screen layer */); + DisplayTextLooks(kDisplayTextStyle_MessageBox), FONT_NORMAL, 0, + -1 /* no autoplace */, false /* screen layer */); // Stop any blocking voice-over, if was started by this function if (play.IsBlockingVoiceSpeech()) diff --git a/Engine/ac/display.h b/Engine/ac/display.h index 8339efcf3b1..bc6df8f5e65 100644 --- a/Engine/ac/display.h +++ b/Engine/ac/display.h @@ -40,6 +40,29 @@ enum DisplayTextStyle kDisplayTextStyle_Overchar, // display text above a character }; +// Whether displayed text is allowed to be shrinked +enum DisplayTextShrink +{ + kDisplayTextShrink_None, + kDisplayTextShrink_Left, + kDisplayTextShrink_Right +}; + +struct DisplayTextLooks +{ + DisplayTextStyle Style = kDisplayTextStyle_TextWindow; + // Is this a character's thought; FIXME: merge with Style? + bool AsThought = false; + // Allow to make the resulting overlay smaller than requested + DisplayTextShrink AllowShrink = kDisplayTextShrink_None; + + DisplayTextLooks() = default; + DisplayTextLooks(DisplayTextStyle style) + : Style(style) {} + DisplayTextLooks(DisplayTextStyle style, bool as_thought, DisplayTextShrink allow_shrink) + : Style(style), AsThought(as_thought), AllowShrink(allow_shrink) {} +}; + struct TopBarSettings { Common::String Text; @@ -68,8 +91,8 @@ struct ScreenOverlay; // see _display_main's comment below for parameters description. // NOTE: this function treats text as-is, not doing any processing over it. // TODO: refactor this collection of args into 1-2 structs with params. -Common::Bitmap *create_textual_image(const char *text, DisplayTextStyle style, color_t text_color, int isThought, - int &xx, int &yy, int &adjustedXX, int &adjustedYY, int wii, int usingfont, int allowShrink, +Common::Bitmap *create_textual_image(const char *text, const DisplayTextLooks &look, color_t text_color, + int &xx, int &yy, int &adjustedXX, int &adjustedYY, int wii, int usingfont, const TopBarSettings *topbar); // Creates a textual overlay using the given parameters; // * disp_type tells the wanted type of overlay and behavior (blocking, etc); @@ -78,15 +101,14 @@ Common::Bitmap *create_textual_image(const char *text, DisplayTextStyle style, c // FIXME: find a way to not pass over_id at all, this logic is bad // * style - more specific desired look (use text window, etc); // * pass yy = -1 to find Y co-ord automatically; -// * allowShrink = 0 for none, 1 for leftwards, 2 for rightwards. // autoplace_at_char - tells whether overlay should autoposition itself whenever game updates. // NOTE: this function treats the text as-is; it assumes that any processing // (translation, parsing voice token) was done prior to its call. // TODO: refactor this collection of args into few args + 1-2 structs with extended params. ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, const TopBarSettings *topbar, DisplayTextType disp_type, int over_id, - DisplayTextStyle style, int usingfont, color_t text_color, - int isThought, int allowShrink, int autoplace_at_char = -1, bool roomlayer = false); + const DisplayTextLooks &look, int usingfont, color_t text_color, + int autoplace_at_char = -1, bool roomlayer = false); // Displays a standard blocking message box at a given position void display_at(int xx, int yy, int wii, const char *text, const TopBarSettings *topbar); // Cleans up display message state diff --git a/Engine/ac/global_character.cpp b/Engine/ac/global_character.cpp index 5cea5f8e00b..974a69d49d9 100644 --- a/Engine/ac/global_character.cpp +++ b/Engine/ac/global_character.cpp @@ -230,11 +230,9 @@ void DisplaySpeechAt (int xx, int yy, int wii, int aschar, const char*spch) { // [DEPRECATED] left only for use in Display, replace/merge with modern function static int CreateTextOverlay(int xx, int yy, int wii, int fontid, int text_color, const char* text, int over_type, DisplayTextStyle style, int speech_for_char) { - int allowShrink = 0; - if (speech_for_char >= 0) // allow DisplaySpeechBackground to be shrunk - allowShrink = 1; - - auto *over = Overlay_CreateTextCore(false, xx, yy, wii, fontid, text_color, text, over_type, style, allowShrink, speech_for_char); + // allow DisplaySpeechBackground to be shrunk + DisplayTextShrink allow_shrink = (speech_for_char >= 0) ? kDisplayTextShrink_Left : kDisplayTextShrink_None; + auto *over = Overlay_CreateTextCore(false, xx, yy, wii, fontid, text_color, text, over_type, style, allow_shrink, speech_for_char); return over ? over->type : 0; } diff --git a/Engine/ac/overlay.cpp b/Engine/ac/overlay.cpp index 379166be6fd..49899068120 100644 --- a/Engine/ac/overlay.cpp +++ b/Engine/ac/overlay.cpp @@ -76,7 +76,7 @@ void Overlay_SetText(ScreenOverlay &over, int x, int y, int width, int fontid, i // TODO: find a nice way to refactor and share these code pieces // from CreateTextOverlay // allow DisplaySpeechBackground to be shrunk - int allow_shrink = over.IsAutoPosition() ? 1 : 0; + DisplayTextShrink allow_shrink = over.IsAutoPosition() ? kDisplayTextShrink_Left : kDisplayTextShrink_None; // from Overlay_CreateTextCore if (width < 8) width = play.GetUIViewport().GetWidth() / 2; @@ -88,9 +88,9 @@ void Overlay_SetText(ScreenOverlay &over, int x, int y, int width, int fontid, i // Recreate overlay image int dummy_x = x, dummy_y = y, adj_x = x, adj_y = y; - Bitmap *image = create_textual_image(draw_text, kDisplayTextStyle_TextWindow, text_color, - 0 /* not thought */, dummy_x, dummy_y, adj_x, adj_y, - width, fontid, allow_shrink, nullptr); + Bitmap *image = create_textual_image(draw_text, + DisplayTextLooks(kDisplayTextStyle_TextWindow, false /* not thought */, allow_shrink), + text_color, dummy_x, dummy_y, adj_x, adj_y, width, fontid, nullptr); // Update overlay properties over.SetImage(std::unique_ptr(image), adj_x - dummy_x, adj_y - dummy_y); @@ -237,7 +237,7 @@ ScreenOverlay *Overlay_CreateGraphicCore(bool room_layer, int x, int y, int slot } ScreenOverlay *Overlay_CreateTextCore(bool room_layer, int x, int y, int width, int font, int text_color, - const char *text, int over_type, DisplayTextStyle style, int allow_shrink, int speech_for_char) + const char *text, int over_type, DisplayTextStyle style, DisplayTextShrink allow_shrink, int speech_for_char) { if (width < 8) width = play.GetUIViewport().GetWidth() / 2; if (x < 0) x = play.GetUIViewport().GetWidth() / 2 - width / 2; @@ -245,7 +245,8 @@ ScreenOverlay *Overlay_CreateTextCore(bool room_layer, int x, int y, int width, // Skip a voice-over token, if present const char *draw_text = skip_voiceover_token(text); return display_main(x, y, width, draw_text, nullptr, kDisplayText_NormalOverlay, over_type, - style, font, text_color, 0 /* not thought */, allow_shrink, speech_for_char, room_layer); + DisplayTextLooks(style, false /* not thought */, allow_shrink), + font, text_color, speech_for_char, room_layer); } ScriptOverlay* Overlay_CreateGraphicalImpl(bool room_layer, int x, int y, int slot, bool clone) @@ -256,7 +257,7 @@ ScriptOverlay* Overlay_CreateGraphicalImpl(bool room_layer, int x, int y, int sl ScriptOverlay* Overlay_CreateTextualImpl(bool room_layer, int x, int y, int width, int font, int colour, const char* text) { - auto *over = Overlay_CreateTextCore(room_layer, x, y, width, font, colour, text, OVER_CUSTOM, kDisplayTextStyle_TextWindow, 0 /* not shrink */); + auto *over = Overlay_CreateTextCore(room_layer, x, y, width, font, colour, text, OVER_CUSTOM, kDisplayTextStyle_TextWindow, kDisplayTextShrink_None); return over ? create_scriptoverlay(*over) : nullptr; } diff --git a/Engine/ac/overlay.h b/Engine/ac/overlay.h index e28fa2aff5e..090f558eee5 100644 --- a/Engine/ac/overlay.h +++ b/Engine/ac/overlay.h @@ -38,7 +38,7 @@ ScriptOverlay* Overlay_CreateGraphical(int x, int y, int slot, bool clone); ScriptOverlay* Overlay_CreateTextual(int x, int y, int width, int font, int colour, const char* text); ScreenOverlay *Overlay_CreateGraphicCore(bool room_layer, int x, int y, int slot, bool clone = false); ScreenOverlay *Overlay_CreateTextCore(bool room_layer, int x, int y, int width, int font, int text_color, - const char *text, int over_type, DisplayTextStyle style, int allow_shrink, int speech_for_char = -1); + const char *text, int over_type, DisplayTextStyle style, DisplayTextShrink allow_shrink, int speech_for_char = -1); ScreenOverlay *get_overlay(int type); // Gets overlay position for drawing in its respective layer (screen or room)