From 45e85205a02aec0f9ab13a24950ad55b70aafa4f Mon Sep 17 00:00:00 2001 From: Sunrise Date: Sun, 23 Jul 2023 16:42:24 +0200 Subject: [PATCH 1/3] Add new function to enable/disable preview of map --- doc/lua/events.md | 3 +++ libs/s25main/desktops/dskGameLobby.cpp | 3 ++- libs/s25main/lua/LuaInterfaceSettings.cpp | 10 ++++++++++ libs/s25main/lua/LuaInterfaceSettings.h | 1 + tests/s25Main/lua/testLuaSettings.cpp | 7 +++++++ tests/testData/maps/LuaFunctions.lua | 4 ++++ 6 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/lua/events.md b/doc/lua/events.md index e5630720d..61f842fa1 100644 --- a/doc/lua/events.md +++ b/doc/lua/events.md @@ -56,6 +56,9 @@ end Only meaningfull if `addonsAll=false` and `addonsSome=true`: Return a list with addonIds that the host can change. +**isMapPreviewEnabled()** +Is map preview enabled. Default is true + ## Game **onStart(isFirstStart)** diff --git a/libs/s25main/desktops/dskGameLobby.cpp b/libs/s25main/desktops/dskGameLobby.cpp index 5af5be005..e4658db27 100644 --- a/libs/s25main/desktops/dskGameLobby.cpp +++ b/libs/s25main/desktops/dskGameLobby.cpp @@ -231,7 +231,8 @@ dskGameLobby::dskGameLobby(ServerType serverType, std::shared_ptr gam combo->AddString(_("Very fast")); // Sehr Schnell // Karte laden, um Kartenvorschau anzuzeigen - if(!gameLobby_->isSavegame()) + bool isMapPreviewEnabled = !lua || lua->IsMapPreviewEnabled(); + if(!gameLobby_->isSavegame() && isMapPreviewEnabled) { // Map laden libsiedler2::Archiv mapArchiv; diff --git a/libs/s25main/lua/LuaInterfaceSettings.cpp b/libs/s25main/lua/LuaInterfaceSettings.cpp index 78422907f..8f29d5658 100644 --- a/libs/s25main/lua/LuaInterfaceSettings.cpp +++ b/libs/s25main/lua/LuaInterfaceSettings.cpp @@ -237,3 +237,13 @@ std::vector LuaInterfaceSettings::GetAllowedAddons() } return std::vector(); } + +bool LuaInterfaceSettings::IsMapPreviewEnabled() +{ + kaguya::LuaRef func = lua["isMapPreviewEnabled"]; + if(func.type() == LUA_TFUNCTION) + { + return func.call(); + } + return true; +} diff --git a/libs/s25main/lua/LuaInterfaceSettings.h b/libs/s25main/lua/LuaInterfaceSettings.h index ccab4bdf4..a577a3e1e 100644 --- a/libs/s25main/lua/LuaInterfaceSettings.h +++ b/libs/s25main/lua/LuaInterfaceSettings.h @@ -35,6 +35,7 @@ class LuaInterfaceSettings : public LuaInterfaceGameBase bool IsChangeAllowed(const std::string& name, bool defaultVal = false); /// Get addons that are allowed to be changed std::vector GetAllowedAddons(); + bool IsMapPreviewEnabled(); private: IGameLobbyController& lobbyServerController_; diff --git a/tests/s25Main/lua/testLuaSettings.cpp b/tests/s25Main/lua/testLuaSettings.cpp index fc043217c..59ae6e3d5 100644 --- a/tests/s25Main/lua/testLuaSettings.cpp +++ b/tests/s25Main/lua/testLuaSettings.cpp @@ -149,6 +149,13 @@ BOOST_AUTO_TEST_CASE(Events) BOOST_TEST_REQUIRE(allowedAddons[0] == AddonId::LIMIT_CATAPULTS); BOOST_TEST_REQUIRE(allowedAddons[1] == AddonId::CHARBURNER); BOOST_TEST_REQUIRE(allowedAddons[2] == AddonId::TRADE); + + BOOST_TEST(lua.IsMapPreviewEnabled()); // Not defined in lua -> Default to enabled + executeLua("function isMapPreviewEnabled()\n return false\nend"); + BOOST_TEST(!lua.IsMapPreviewEnabled()); + executeLua("function isMapPreviewEnabled()\n return true\nend"); + BOOST_TEST(lua.IsMapPreviewEnabled()); + // Return invalid type -> ignored, but log output clearLog(); executeLua("function getAllowedAddons()\n return {'ADDON_FAIL_ME'}\nend"); diff --git a/tests/testData/maps/LuaFunctions.lua b/tests/testData/maps/LuaFunctions.lua index eaba5a2e8..d5609cd47 100644 --- a/tests/testData/maps/LuaFunctions.lua +++ b/tests/testData/maps/LuaFunctions.lua @@ -20,6 +20,10 @@ function getAllowedChanges() return {general=true, addonsAll=false, addonsSome=true, swapping=false, playerState = not isSinglePlayer, ownNation = true, ownColor=true, ownTeam=false, aiNation = false, aiColor=false, aiTeam=false} end +function isMapPreviewEnabled() + return false +end + function onSettingsInit(isSinglePlayerArg, isSavegameArg) isSinglePlayer = isSinglePlayerArg isSavegame = isSavegameArg From 3775018df4a8f5f6e1388e4b52eb66b0856ed1cc Mon Sep 17 00:00:00 2001 From: Sunrise Date: Wed, 16 Aug 2023 19:00:04 +0200 Subject: [PATCH 2/3] Increase feature level --- libs/s25main/lua/LuaInterfaceGameBase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/s25main/lua/LuaInterfaceGameBase.cpp b/libs/s25main/lua/LuaInterfaceGameBase.cpp index ab916aa6e..7861178cc 100644 --- a/libs/s25main/lua/LuaInterfaceGameBase.cpp +++ b/libs/s25main/lua/LuaInterfaceGameBase.cpp @@ -16,7 +16,7 @@ unsigned LuaInterfaceGameBase::GetVersion() unsigned LuaInterfaceGameBase::GetFeatureLevel() { - return 3; + return 4; } LuaInterfaceGameBase::LuaInterfaceGameBase(const ILocalGameState& localGameState) : localGameState(localGameState) From 3147469979e4f574cdb360f348f5cdb0a0e0dd3b Mon Sep 17 00:00:00 2001 From: Sunrise Date: Thu, 31 Aug 2023 20:49:29 +0200 Subject: [PATCH 3/3] Add no preview area with text if preview disabled --- libs/s25main/desktops/dskGameLobby.cpp | 43 ++++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/libs/s25main/desktops/dskGameLobby.cpp b/libs/s25main/desktops/dskGameLobby.cpp index e4658db27..9ab2d702f 100644 --- a/libs/s25main/desktops/dskGameLobby.cpp +++ b/libs/s25main/desktops/dskGameLobby.cpp @@ -231,27 +231,36 @@ dskGameLobby::dskGameLobby(ServerType serverType, std::shared_ptr gam combo->AddString(_("Very fast")); // Sehr Schnell // Karte laden, um Kartenvorschau anzuzeigen - bool isMapPreviewEnabled = !lua || lua->IsMapPreviewEnabled(); - if(!gameLobby_->isSavegame() && isMapPreviewEnabled) + if(!gameLobby_->isSavegame()) { - // Map laden - libsiedler2::Archiv mapArchiv; - // Karteninformationen laden - if(int ec = libsiedler2::loader::LoadMAP(GAMECLIENT.GetMapPath(), mapArchiv)) + bool isMapPreviewEnabled = !lua || lua->IsMapPreviewEnabled(); + if(!isMapPreviewEnabled) { - WINDOWMANAGER.ShowAfterSwitch( - std::make_unique(_("Error"), _("Could not load map:\n") + libsiedler2::getErrorString(ec), this, - MsgboxButton::Ok, MsgboxIcon::ExclamationRed, 0)); + AddTextDeepening(70, DrawPoint(560, 40), Extent(220, 220), TextureColor::Grey, _("No preview"), LargeFont, + COLOR_YELLOW); + AddText(71, DrawPoint(670, 40 + 220 + 10), _("Map: ") + GAMECLIENT.GetMapTitle(), COLOR_YELLOW, + FontStyle::CENTER, NormalFont); } else { - auto* map = static_cast(mapArchiv.get(0)); - ctrlPreviewMinimap* preview = AddPreviewMinimap(70, DrawPoint(560, 40), Extent(220, 220), map); - - // Titel der Karte, Y-Position relativ je nach Höhe der Minimap festlegen, daher nochmals danach - // verschieben, da diese Position sonst skaliert wird! - ctrlText* text = AddText(71, DrawPoint(670, 0), _("Map: ") + GAMECLIENT.GetMapTitle(), COLOR_YELLOW, - FontStyle::CENTER, NormalFont); - text->SetPos(DrawPoint(text->GetPos().x, preview->GetPos().y + preview->GetMapArea().bottom + 10)); + // Map laden + libsiedler2::Archiv mapArchiv; + // Karteninformationen laden + if(int ec = libsiedler2::loader::LoadMAP(GAMECLIENT.GetMapPath(), mapArchiv)) + { + WINDOWMANAGER.ShowAfterSwitch( + std::make_unique(_("Error"), _("Could not load map:\n") + libsiedler2::getErrorString(ec), + this, MsgboxButton::Ok, MsgboxIcon::ExclamationRed, 0)); + } else + { + auto* map = static_cast(mapArchiv.get(0)); + ctrlPreviewMinimap* preview = AddPreviewMinimap(70, DrawPoint(560, 40), Extent(220, 220), map); + + // Titel der Karte, Y-Position relativ je nach Höhe der Minimap festlegen, daher nochmals danach + // verschieben, da diese Position sonst skaliert wird! + ctrlText* text = AddText(71, DrawPoint(670, 0), _("Map: ") + GAMECLIENT.GetMapTitle(), COLOR_YELLOW, + FontStyle::CENTER, NormalFont); + text->SetPos(DrawPoint(text->GetPos().x, preview->GetPos().y + preview->GetMapArea().bottom + 10)); + } } }