Save special text box state using functions

This adds a way to save the text box state of the crew remaining, ACTION
prompt, etc. text boxes by just letting there be a function that is
called to retranslate the text box when needed.

It also adds a way to ignore translating a text box and to leave it
alone, in case there's actually no text in the text box, which is the
case with Level Complete and Game Complete.

Both ways are now in an enum, TextboxTranslate. The former is
TEXTTRANSLATE_FUNCTION and the latter is TEXTTRANSLATE_NONE. The
existing way of translating text boxes became TEXTTRANSLATE_CUTSCENE,
since it's only used for cutscene scripts.

Here's a quick guide to the three ways of creating a text box now.

- TEXTTRANSLATE_NONE: You must call
  graphics.textboxoriginalcontextauto() to save the existing text to the
  original context of the text box, as that will be copied back to the
  text box after the text of the text box is updated due to not having a
  translation.
- TEXTTRANSLATE_CUTSCENE: Translates the text from cutscenes.xml, and
  overrides the spacing (padding and text centering). Shouldn't need to
  be used outside of scriptclass.
- TEXTTRANSLATE_FUNCTION: You must pass in a function that takes in a
  single parameter, a pointer to the textboxclass object to be modified.
  General advice when retranslating text is to clear the `lines` vector
  and then push_back the retranslated text. The function is also solely
  responsible for spacing.

In most cases, you will also need to call
graphics.textboxapplyposition() or graphics.textboxadjust() afterwards.
(Some text boxes shouldn't use graphics.textboxadjust() as they are
within the 10-pixel inner border around the screen that
textboxclass::adjust tries to push the text box out of.)

This commit doesn't fix every text box just yet, though. But it fixes
the Level Complete, Game Complete, crew remaining, and ACTION prompt
text boxes, for a start.
This commit is contained in:
Misa
2024-01-20 20:27:31 -08:00
committed by Misa Elizabeth Kai
parent 0ea0b8e00b
commit de00dd4031
7 changed files with 204 additions and 37 deletions

View File

@@ -1495,6 +1495,17 @@ void Graphics::setlarge(bool large)
textboxes[m].large = large;
}
void Graphics::textboxapplyposition(void)
{
if (!INBOUNDS_VEC(m, textboxes))
{
vlog_error("textboxapplyposition() out-of-bounds!");
return;
}
textboxes[m].applyposition();
}
void Graphics::textboxadjust(void)
{
if (!INBOUNDS_VEC(m, textboxes))
@@ -3272,7 +3283,8 @@ void Graphics::textboxpad(size_t left_pad, size_t right_pad)
return;
}
textboxes[m].pad(left_pad, right_pad);
textboxes[m].spacing.pad_left = left_pad;
textboxes[m].spacing.pad_right = right_pad;
}
void Graphics::textboxpadtowidth(size_t new_w)
@@ -3283,7 +3295,7 @@ void Graphics::textboxpadtowidth(size_t new_w)
return;
}
textboxes[m].padtowidth(new_w);
textboxes[m].spacing.padtowidth = new_w;
}
void Graphics::textboxcentertext(void)
@@ -3294,7 +3306,7 @@ void Graphics::textboxcentertext(void)
return;
}
textboxes[m].centertext();
textboxes[m].spacing.centertext = true;
}
void Graphics::textboxprintflags(const uint32_t flags)
@@ -3342,6 +3354,25 @@ void Graphics::textboxoriginalcontext(const TextboxOriginalContext* original_con
textboxes[m].original = *original_context;
}
void Graphics::textboxoriginalcontextauto(void)
{
if (!INBOUNDS_VEC(m, textboxes))
{
vlog_error("textboxoriginalcontextauto() out-of-bounds!");
return;
}
TextboxOriginalContext context = TextboxOriginalContext();
context.text_case = 1;
context.lines = textboxes[m].lines;
if (script.running)
{
context.script_name = script.scriptname;
}
textboxes[m].original = context;
}
void Graphics::textboxcase(char text_case)
{
if (!INBOUNDS_VEC(m, textboxes))
@@ -3353,7 +3384,7 @@ void Graphics::textboxcase(char text_case)
textboxes[m].original.text_case = text_case;
}
void Graphics::textboxtranslate(void)
void Graphics::textboxtranslate(const TextboxTranslate translate, const TextboxFunction function)
{
if (!INBOUNDS_VEC(m, textboxes))
{
@@ -3361,7 +3392,15 @@ void Graphics::textboxtranslate(void)
return;
}
textboxes[m].translate();
if (translate == TEXTTRANSLATE_FUNCTION && function == NULL)
{
SDL_assert(0 && "function is NULL!");
return;
}
textboxes[m].translate = translate;
textboxes[m].function = function;
textboxes[m].updatetext();
}
void Graphics::textboxcommsrelay(void)