From c62640fa891d7a8717ef80704ffa665b99693405 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 13 Jul 2024 11:16:33 +0200 Subject: [PATCH] Allow to define stretch mode to title screen: ``` SetTitleScreens( {Image = "ui/title.png", StretchMode = "none"}, -- Keep original image size {Image = "ui/title.png", StretchMode = "keep-ratio"}, -- resize to touch border, and keep original size ratio {Image = "ui/title.png", StretchMode = "stretch"} -- (default) resize to the full windows ) ``` --- src/include/title.h | 13 +++++++++++-- src/stratagus/title.cpp | 36 ++++++++++++++++++++++++++++++++---- src/ui/script_ui.cpp | 7 +++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/include/title.h b/src/include/title.h index df75b01294..a0da9c9515 100644 --- a/src/include/title.h +++ b/src/include/title.h @@ -55,6 +55,15 @@ class TitleScreenLabel int Flags = 0; }; +enum class EStretchMode +{ + None, + KeepRatio, + Stretch +}; + +std::optional StretchModeFromString(std::string_view); + class TitleScreen { public: @@ -64,11 +73,11 @@ class TitleScreen void ShowTitleImage(); private: - void ShowLabels(); + void ShowLabels() const; public: fs::path File; std::string Music; - bool StretchImage = true; + EStretchMode StretchMode = EStretchMode::Stretch; int Timeout = 0; int Editor = 0; std::vector> Labels; diff --git a/src/stratagus/title.cpp b/src/stratagus/title.cpp index 27da84058d..19166c2ca6 100644 --- a/src/stratagus/title.cpp +++ b/src/stratagus/title.cpp @@ -42,7 +42,37 @@ std::vector> TitleScreens; /// Title screens to sho static bool WaitNoEvent; /// Flag got an event extern std::string CliMapName; -void TitleScreen::ShowLabels() +static void ApplyStretchMode(CGraphic &g, EStretchMode mode, int width, int height) +{ + switch (mode) { + case EStretchMode::None: break; + case EStretchMode::KeepRatio: + { + if (g.Width * height < width * g.Height) { + g.Resize(g.Width * height / g.Height, height); + } else { + g.Resize(width, g.Height * width / g.Width); + } + break; + } + case EStretchMode::Stretch: g.Resize(width, height); break; + } +} + +std::optional StretchModeFromString(std::string_view s) +{ + if (s == "none") { + return EStretchMode::None; + } else if (s == "keep-ratio") { + return EStretchMode::KeepRatio; + } else if (s == "stretch") { + return EStretchMode::Stretch; + } else { + return std::nullopt; + } +} + +void TitleScreen::ShowLabels() const { for (auto &titleScreenlabel : this->Labels) { if (!titleScreenlabel->Font) { @@ -85,9 +115,7 @@ void TitleScreen::ShowTitleImage() auto g = CGraphic::New(this->File.string()); g->Load(); - if (this->StretchImage) { - g->Resize(Video.Width, Video.Height); - } + ApplyStretchMode(*g, this->StretchMode, Video.Width, Video.Height); int timeout = this->Timeout ? this->Timeout * CYCLES_PER_SECOND : -1; diff --git a/src/ui/script_ui.cpp b/src/ui/script_ui.cpp index d26233dfb2..5400e3ccec 100644 --- a/src/ui/script_ui.cpp +++ b/src/ui/script_ui.cpp @@ -377,6 +377,13 @@ static int CclSetTitleScreens(lua_State *l) const std::string_view value = LuaToString(l, -2); if (value == "Image") { TitleScreens[j]->File = LuaToString(l, -1); + } else if (value == "StretchMode") { + const std::string_view value = LuaToString(l, -1); + if (auto stretchMode = StretchModeFromString(value)) { + TitleScreens[j]->StretchMode = *stretchMode; + } else { + LuaError(l, "Unknown StretchMode %s", value.data()); + } } else if (value == "Music") { TitleScreens[j]->Music = LuaToString(l, -1); } else if (value == "Timeout") {