From b8ab64f448eb435af901ffc653c376666e5806d7 Mon Sep 17 00:00:00 2001 From: nordsoft Date: Wed, 23 Aug 2023 19:29:50 +0400 Subject: [PATCH] Make options tab configurable --- client/gui/InterfaceObjectConfigurable.cpp | 22 +++- client/gui/InterfaceObjectConfigurable.h | 2 + client/lobby/OptionsTab.cpp | 37 ++++--- client/lobby/OptionsTab.h | 36 +++---- client/lobby/RandomMapTab.cpp | 2 +- config/widgets/optionsTab.json | 112 +++++++++++++++++++++ 6 files changed, 168 insertions(+), 43 deletions(-) create mode 100644 config/widgets/optionsTab.json diff --git a/client/gui/InterfaceObjectConfigurable.cpp b/client/gui/InterfaceObjectConfigurable.cpp index b24729528..37cb233f3 100644 --- a/client/gui/InterfaceObjectConfigurable.cpp +++ b/client/gui/InterfaceObjectConfigurable.cpp @@ -17,6 +17,8 @@ #include "../gui/CGuiHandler.h" #include "../gui/ShortcutHandler.h" #include "../gui/Shortcut.h" +#include "../render/Graphics.h" +#include "../render/IFont.h" #include "../widgets/CComponent.h" #include "../widgets/Buttons.h" #include "../widgets/MiscWidgets.h" @@ -43,6 +45,7 @@ InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset) REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture); REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation); REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel); + REGISTER_BUILDER("multiLineLabel", &InterfaceObjectConfigurable::buildMultiLineLabel); REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup); REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton); REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton); @@ -301,6 +304,20 @@ std::shared_ptr InterfaceObjectConfigurable::buildLabel(const JsonNode & return std::make_shared(position.x, position.y, font, alignment, color, text); } +std::shared_ptr InterfaceObjectConfigurable::buildMultiLineLabel(const JsonNode & config) const +{ + logGlobal->debug("Building widget CMultiLineLabel"); + auto font = readFont(config["font"]); + auto alignment = readTextAlignment(config["alignment"]); + auto color = readColor(config["color"]); + auto text = readText(config["text"]); + Rect rect = readRect(config["rect"]); + if(!config["adoptHeight"].isNull() && config["adoptHeight"].Bool()) + rect.h = graphics->fonts[font]->getLineHeight() * 2; + return std::make_shared(rect, font, alignment, color, text); +} + + std::shared_ptr InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const { logGlobal->debug("Building widget CToggleGroup"); @@ -466,11 +483,14 @@ std::shared_ptr InterfaceObjectConfigurable::buildSlider(const JsonNode const auto & result = std::make_shared(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style); - if (!config["scrollBounds"].isNull()) + if(!config["scrollBounds"].isNull()) { Rect bounds = readRect(config["scrollBounds"]); result->setScrollBounds(bounds); } + + if(!config["panningStep"].isNull()) + result->setPanningStep(config["panningStep"].Integer()); return result; } diff --git a/client/gui/InterfaceObjectConfigurable.h b/client/gui/InterfaceObjectConfigurable.h index 07d0a57c1..86a3381c0 100644 --- a/client/gui/InterfaceObjectConfigurable.h +++ b/client/gui/InterfaceObjectConfigurable.h @@ -18,6 +18,7 @@ class CPicture; class CLabel; +class CMultiLineLabel; class CToggleGroup; class CToggleButton; class CButton; @@ -88,6 +89,7 @@ protected: //basic widgets std::shared_ptr buildPicture(const JsonNode &) const; std::shared_ptr buildLabel(const JsonNode &) const; + std::shared_ptr buildMultiLineLabel(const JsonNode &) const; std::shared_ptr buildToggleGroup(const JsonNode &) const; std::shared_ptr buildToggleButton(const JsonNode &) const; std::shared_ptr buildButton(const JsonNode &) const; diff --git a/client/lobby/OptionsTab.cpp b/client/lobby/OptionsTab.cpp index f76e1c12e..87968c2dd 100644 --- a/client/lobby/OptionsTab.cpp +++ b/client/lobby/OptionsTab.cpp @@ -31,6 +31,7 @@ #include "../windows/InfoWindows.h" #include "../eventsSDL/InputHandler.h" +#include "../../lib/filesystem/Filesystem.h" #include "../../lib/NetPacksLobby.h" #include "../../lib/CGeneralTextHandler.h" #include "../../lib/CArtHandler.h" @@ -42,23 +43,20 @@ OptionsTab::OptionsTab() : humanPlayers(0) { recActions = 0; - OBJ_CONSTRUCTION; - background = std::make_shared("ADVOPTBK", 0, 6); - pos = background->pos; - labelTitle = std::make_shared(222, 30, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[515]); - labelSubTitle = std::make_shared(Rect(60, 44, 320, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[516]); - - labelPlayerNameAndHandicap = std::make_shared(Rect(58, 86, 100, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[517]); - labelStartingTown = std::make_shared(Rect(163, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[518]); - labelStartingHero = std::make_shared(Rect(239, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[519]); - labelStartingBonus = std::make_shared(Rect(315, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[520]); + + addCallback("setTurnLength", std::bind(&IServerAPI::setTurnLength, CSH, _1)); + + const JsonNode config(ResourceID("config/widgets/optionsTab.json")); + build(config); + if(SEL->screenType == ESelectionScreen::newGame || SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::scenarioInfo) { - sliderTurnDuration = std::make_shared(Point(55, 551), 194, std::bind(&IServerAPI::setTurnLength, CSH, _1), 1, (int)GameConstants::POSSIBLE_TURNTIME.size(), (int)GameConstants::POSSIBLE_TURNTIME.size(), Orientation::HORIZONTAL, CSlider::BLUE); - sliderTurnDuration->setScrollBounds(Rect(-3, -25, 337, 43)); - sliderTurnDuration->setPanningStep(20); - labelPlayerTurnDuration = std::make_shared(222, 538, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[521]); - labelTurnDurationValue = std::make_shared(319, 559, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE); + if(auto w = widget("sliderTurnDuration")) + w->deactivate(); + if(auto w = widget("labelPlayerTurnDuration")) + w->deactivate(); + if(auto w = widget("labelTurnDurationValue")) + w->deactivate(); } } @@ -76,10 +74,11 @@ void OptionsTab::recreate() entries.insert(std::make_pair(pInfo.first, std::make_shared(pInfo.second, * this))); } - if(sliderTurnDuration) + if(auto turnSlider = widget("sliderTurnDuration")) { - sliderTurnDuration->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTimerInfo.turnTimer / (60 * 1000))); - labelTurnDurationValue->setText(CGI->generaltexth->turnDurations[sliderTurnDuration->getValue()]); + turnSlider->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTimerInfo.turnTimer / (60 * 1000))); + if(auto w = widget("labelTurnDurationValue")) + w->setText(CGI->generaltexth->turnDurations[turnSlider->getValue()]); } } @@ -851,7 +850,7 @@ OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, con } pos.x += 54; - pos.y += 122 + serial * 50; + pos.y += 128 + serial * 50; assert(CSH->mi && CSH->mi->mapHeader); const PlayerInfo & p = SEL->getPlayerInfo(s->color.getNum()); diff --git a/client/lobby/OptionsTab.h b/client/lobby/OptionsTab.h index 9af09bb6c..f9a5a6778 100644 --- a/client/lobby/OptionsTab.h +++ b/client/lobby/OptionsTab.h @@ -10,15 +10,14 @@ #pragma once #include "../windows/CWindowObject.h" +#include "../widgets/Scrollable.h" +#include "../gui/InterfaceObjectConfigurable.h" VCMI_LIB_NAMESPACE_BEGIN struct PlayerSettings; struct PlayerInfo; VCMI_LIB_NAMESPACE_END -#include "../widgets/Scrollable.h" - -class CSlider; class CLabel; class CMultiLineLabel; class CFilledTexture; @@ -30,21 +29,19 @@ class CButton; class FilledTexturePlayerColored; /// The options tab which is shown at the map selection phase. -class OptionsTab : public CIntObject +class OptionsTab : public InterfaceObjectConfigurable { - std::shared_ptr background; - std::shared_ptr labelTitle; - std::shared_ptr labelSubTitle; - std::shared_ptr labelPlayerNameAndHandicap; - std::shared_ptr labelStartingTown; - std::shared_ptr labelStartingHero; - std::shared_ptr labelStartingBonus; - - std::shared_ptr labelPlayerTurnDuration; - std::shared_ptr labelTurnDurationValue; + struct PlayerOptionsEntry; + ui8 humanPlayers; - + std::map> entries; + public: + + OptionsTab(); + void recreate(); + void onSetPlayerClicked(const PlayerSettings & ps) const; + enum SelType { TOWN, @@ -52,6 +49,8 @@ public: BONUS }; +private: + struct CPlayerSettingsHelper { const PlayerSettings & settings; @@ -187,11 +186,4 @@ public: private: const OptionsTab & parentTab; }; - - std::shared_ptr sliderTurnDuration; - std::map> entries; - - OptionsTab(); - void recreate(); - void onSetPlayerClicked(const PlayerSettings & ps) const; }; diff --git a/client/lobby/RandomMapTab.cpp b/client/lobby/RandomMapTab.cpp index 588dafef4..88f015e9b 100644 --- a/client/lobby/RandomMapTab.cpp +++ b/client/lobby/RandomMapTab.cpp @@ -41,7 +41,6 @@ RandomMapTab::RandomMapTab(): recActions = 0; mapGenOptions = std::make_shared(); - const JsonNode config(ResourceID("config/widgets/randomMapTab.json")); addCallback("toggleMapSize", [&](int btnId) { auto mapSizeVal = getPossibleMapSizes(); @@ -123,6 +122,7 @@ RandomMapTab::RandomMapTab(): }); } + const JsonNode config(ResourceID("config/widgets/randomMapTab.json")); build(config); updateMapInfoByHost(); diff --git a/config/widgets/optionsTab.json b/config/widgets/optionsTab.json new file mode 100644 index 000000000..8e9072d78 --- /dev/null +++ b/config/widgets/optionsTab.json @@ -0,0 +1,112 @@ +{ + "items": + [ + { + "name": "background", + "type": "picture", + "image": "ADVOPTBK", + "position": {"x": 0, "y": 6} + }, + + { + "name": "labelTitle", + "type": "label", + "font": "big", + "alignment": "center", + "color": "yellow", + "text": "core.genrltxt.515", + "position": {"x": 222, "y": 36} + }, + + { + "name": "labelSubTitle", + "type": "multiLineLabel", + "font": "small", + "alignment": "center", + "color": "white", + "text": "core.genrltxt.516", + "rect": {"x": 60, "y": 50, "w": 320, "h": 0}, + "adoptHeight": true + }, + + { + "name": "labelPlayerNameAndHandicap", + "type": "multiLineLabel", + "font": "small", + "alignment": "center", + "color": "yellow", + "text": "core.genrltxt.517", + "rect": {"x": 58, "y": 92, "w": 100, "h": 0}, + "adoptHeight": true + }, + + { + "name": "labelStartingTown", + "type": "multiLineLabel", + "font": "small", + "alignment": "center", + "color": "yellow", + "text": "core.genrltxt.518", + "rect": {"x": 163, "y": 92, "w": 70, "h": 0}, + "adoptHeight": true + }, + + { + "name": "labelStartingHero", + "type": "multiLineLabel", + "font": "small", + "alignment": "center", + "color": "yellow", + "text": "core.genrltxt.519", + "rect": {"x": 239, "y": 92, "w": 70, "h": 0}, + "adoptHeight": true + }, + + { + "name": "labelStartingBonus", + "type": "multiLineLabel", + "font": "small", + "alignment": "center", + "color": "yellow", + "text": "core.genrltxt.520", + "rect": {"x": 315, "y": 92, "w": 70, "h": 0}, + "adoptHeight": true + }, + + // timer + { + "name": "labelPlayerTurnDuration", + "type": "label", + "font": "small", + "alignment": "center", + "color": "yellow", + "text": "core.genrltxt.521", + "position": {"x": 222, "y": 544} + }, + + { + "name": "labelTurnDurationValue", + "type": "label", + "font": "small", + "alignment": "center", + "color": "white", + "text": "", + "position": {"x": 319, "y": 565} + }, + + { + "name": "sliderTurnDuration", + "type": "slider", + "orientation": "horizontal", + "position": {"x": 55, "y": 557}, + "size": 194, + "callback": "setTurnLength", + "itemsVisible": 1, + "itemsTotal": 11, + "selected": 11, + "style": "blue", + "scrollBounds": {"x": -3, "y": -25, "w": 337, "h": 43}, + "panningStep": 20 + }, + ] +}