1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Add improvements

This commit is contained in:
nordsoft 2022-12-12 11:38:27 +04:00
parent 8dbc5c1c1f
commit 5e3504f578
4 changed files with 25 additions and 26 deletions

View File

@ -42,14 +42,6 @@ void InterfaceBuilder::addCallback(const std::string & callbackName, std::functi
callbacks[callbackName] = callback; callbacks[callbackName] = callback;
} }
const std::shared_ptr<CIntObject> InterfaceBuilder::widget(const std::string & name) const
{
auto iter = widgets.find(name);
if(iter == widgets.end())
return nullptr;
return iter->second;
}
void InterfaceBuilder::init(const JsonNode &config) void InterfaceBuilder::init(const JsonNode &config)
{ {
OBJ_CONSTRUCTION; OBJ_CONSTRUCTION;

View File

@ -26,7 +26,14 @@ protected:
void addCallback(const std::string & callbackName, std::function<void(int)> callback); void addCallback(const std::string & callbackName, std::function<void(int)> callback);
const std::shared_ptr<CIntObject> widget(const std::string &) const; template<class T>
const std::shared_ptr<T> widget(const std::string & name) const
{
auto iter = widgets.find(name);
if(iter == widgets.end())
return nullptr;
return dynamic_pointer_cast<T>(iter->second);
}
private: private:

View File

@ -53,11 +53,11 @@ RandomMapTab::RandomMapTab():
{ {
mapGenOptions->setPlayerCount(btnId); mapGenOptions->setPlayerCount(btnId);
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams"))) if(auto w = widget<CToggleGroup>("groupMaxTeams"))
deactivateButtonsFrom(w.get(), btnId); deactivateButtonsFrom(w.get(), btnId);
// deactive some CompOnlyPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I // deactive some CompOnlyPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers"))) if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1); deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
validatePlayersCnt(btnId); validatePlayersCnt(btnId);
@ -75,10 +75,10 @@ RandomMapTab::RandomMapTab():
mapGenOptions->setCompOnlyPlayerCount(btnId); mapGenOptions->setCompOnlyPlayerCount(btnId);
// deactive some MaxPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I // deactive some MaxPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxPlayers"))) if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1); deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams"))) if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
deactivateButtonsFrom(w.get(), (btnId == 0 ? 1 : btnId)); deactivateButtonsFrom(w.get(), (btnId == 0 ? 1 : btnId));
validateCompOnlyPlayersCnt(btnId); validateCompOnlyPlayersCnt(btnId);
updateMapInfoByHost(); updateMapInfoByHost();
@ -96,7 +96,7 @@ RandomMapTab::RandomMapTab():
updateMapInfoByHost(); updateMapInfoByHost();
}); });
addCallback("setMonsterStrenght", [&](int btnId) addCallback("setMonsterStrength", [&](int btnId)
{ {
if(btnId < 0) if(btnId < 0)
mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM); mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
@ -165,21 +165,21 @@ void RandomMapTab::updateMapInfoByHost()
void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts) void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
{ {
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMapSize"))) if(auto w = widget<CToggleGroup>("groupMapSize"))
w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth())); w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
if(auto w = dynamic_pointer_cast<CToggleButton>(widget("buttonTwoLevels"))) if(auto w = widget<CToggleButton>("buttonTwoLevels"))
w->setSelected(opts->getHasTwoLevels()); w->setSelected(opts->getHasTwoLevels());
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxPlayers"))) if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
w->setSelected(opts->getPlayerCount()); w->setSelected(opts->getPlayerCount());
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams"))) if(auto w = widget<CToggleGroup>("groupMaxTeams"))
w->setSelected(opts->getTeamCount()); w->setSelected(opts->getTeamCount());
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers"))) if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
w->setSelected(opts->getCompOnlyPlayerCount()); w->setSelected(opts->getCompOnlyPlayerCount());
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams"))) if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
w->setSelected(opts->getCompOnlyTeamCount()); w->setSelected(opts->getCompOnlyTeamCount());
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupWaterContent"))) if(auto w = widget<CToggleGroup>("groupWaterContent"))
w->setSelected(opts->getWaterContent()); w->setSelected(opts->getWaterContent());
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupgroupMonsterStrengthMaxTeams"))) if(auto w = widget<CToggleGroup>("groupMonsterStrength"))
w->setSelected(opts->getMonsterStrength()); w->setSelected(opts->getMonsterStrength());
} }
@ -212,14 +212,14 @@ void RandomMapTab::validatePlayersCnt(int playersCnt)
if(mapGenOptions->getTeamCount() >= playersCnt) if(mapGenOptions->getTeamCount() >= playersCnt)
{ {
mapGenOptions->setTeamCount(playersCnt - 1); mapGenOptions->setTeamCount(playersCnt - 1);
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams"))) if(auto w = widget<CToggleGroup>("groupMaxTeams"))
w->setSelected(mapGenOptions->getTeamCount()); w->setSelected(mapGenOptions->getTeamCount());
} }
// total players should not exceed PlayerColor::PLAYER_LIMIT_I (8 in homm3) // total players should not exceed PlayerColor::PLAYER_LIMIT_I (8 in homm3)
if(mapGenOptions->getCompOnlyPlayerCount() + playersCnt > PlayerColor::PLAYER_LIMIT_I) if(mapGenOptions->getCompOnlyPlayerCount() + playersCnt > PlayerColor::PLAYER_LIMIT_I)
{ {
mapGenOptions->setCompOnlyPlayerCount(PlayerColor::PLAYER_LIMIT_I - playersCnt); mapGenOptions->setCompOnlyPlayerCount(PlayerColor::PLAYER_LIMIT_I - playersCnt);
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers"))) if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
w->setSelected(mapGenOptions->getCompOnlyPlayerCount()); w->setSelected(mapGenOptions->getCompOnlyPlayerCount());
} }
@ -238,7 +238,7 @@ void RandomMapTab::validateCompOnlyPlayersCnt(int compOnlyPlayersCnt)
int compOnlyTeamCount = compOnlyPlayersCnt == 0 ? 0 : compOnlyPlayersCnt - 1; int compOnlyTeamCount = compOnlyPlayersCnt == 0 ? 0 : compOnlyPlayersCnt - 1;
mapGenOptions->setCompOnlyTeamCount(compOnlyTeamCount); mapGenOptions->setCompOnlyTeamCount(compOnlyTeamCount);
updateMapInfoByHost(); updateMapInfoByHost();
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams"))) if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
w->setSelected(compOnlyTeamCount); w->setSelected(compOnlyTeamCount);
} }
} }

View File

@ -462,7 +462,7 @@
} }
], ],
"selected": 3, "selected": 3,
"callback": "setMonsterStrenght" "callback": "setMonsterStrength"
}, },
{ {