1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Interface builder for random map tab

This commit is contained in:
nordsoft
2022-12-12 03:27:59 +04:00
parent ed70c69a2b
commit 0104c77d61
7 changed files with 804 additions and 140 deletions

View File

@@ -27,127 +27,72 @@
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/mapping/CMapInfo.h"
#include "../../lib/rmg/CMapGenOptions.h"
#include "../../lib/CModHandler.h"
RandomMapTab::RandomMapTab()
RandomMapTab::RandomMapTab():
InterfaceBuilder()
{
recActions = 0;
mapGenOptions = std::make_shared<CMapGenOptions>();
OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>("RANMAPBK", 0, 6);
labelHeadlineBig = std::make_shared<CLabel>(222, 36, FONT_BIG, EAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[738]);
labelHeadlineSmall = std::make_shared<CLabel>(222, 56, FONT_SMALL, EAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[739]);
labelMapSize = std::make_shared<CLabel>(104, 97, FONT_SMALL, EAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[752]);
groupMapSize = std::make_shared<CToggleGroup>(0);
groupMapSize->pos.y += 81;
groupMapSize->pos.x += 158;
const std::vector<std::string> mapSizeBtns = {"RANSIZS", "RANSIZM", "RANSIZL", "RANSIZX"};
addButtonsToGroup(groupMapSize.get(), mapSizeBtns, 0, 3, 47, 198);
groupMapSize->setSelected(1);
groupMapSize->addCallback([&](int btnId)
const JsonNode config(ResourceID("config/windows.json"));
addCallback("toggleMapSize", [&](int btnId)
{
auto mapSizeVal = getPossibleMapSizes();
mapGenOptions->setWidth(mapSizeVal[btnId]);
mapGenOptions->setHeight(mapSizeVal[btnId]);
updateMapInfoByHost();
});
buttonTwoLevels = std::make_shared<CToggleButton>(Point(346, 81), "RANUNDR", CGI->generaltexth->zelp[202]);
buttonTwoLevels->setSelected(true);
buttonTwoLevels->addCallback([&](bool on)
addCallback("toggleTwoLevels", [&](bool on)
{
mapGenOptions->setHasTwoLevels(on);
updateMapInfoByHost();
});
labelGroupForOptions = std::make_shared<CLabelGroup>(FONT_SMALL, EAlignment::TOPLEFT, Colors::WHITE);
// Create number defs list
std::vector<std::string> numberDefs;
for(int i = 0; i <= 8; ++i)
{
numberDefs.push_back("RANNUM" + boost::lexical_cast<std::string>(i));
}
const int NUMBERS_WIDTH = 32;
const int BTNS_GROUP_LEFT_MARGIN = 67;
labelGroupForOptions->add(68, 133, CGI->generaltexth->allTexts[753]);
groupMaxPlayers = std::make_shared<CToggleGroup>(0);
groupMaxPlayers->pos.y += 153;
groupMaxPlayers->pos.x += BTNS_GROUP_LEFT_MARGIN;
addButtonsWithRandToGroup(groupMaxPlayers.get(), numberDefs, 1, 8, NUMBERS_WIDTH, 204, 212);
groupMaxPlayers->addCallback([&](int btnId)
addCallback("setPlayersCount", [&](int btnId)
{
mapGenOptions->setPlayerCount(btnId);
deactivateButtonsFrom(groupMaxTeams.get(), btnId);
deactivateButtonsFrom(dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams")).get(), btnId);
// deactive some CompOnlyPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
deactivateButtonsFrom(groupCompOnlyPlayers.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
deactivateButtonsFrom(dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers")).get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
validatePlayersCnt(btnId);
updateMapInfoByHost();
});
labelGroupForOptions->add(68, 199, CGI->generaltexth->allTexts[754]);
groupMaxTeams = std::make_shared<CToggleGroup>(0);
groupMaxTeams->pos.y += 219;
groupMaxTeams->pos.x += BTNS_GROUP_LEFT_MARGIN;
addButtonsWithRandToGroup(groupMaxTeams.get(), numberDefs, 0, 7, NUMBERS_WIDTH, 214, 222);
groupMaxTeams->addCallback([&](int btnId)
addCallback("setTeamsCount", [&](int btnId)
{
mapGenOptions->setTeamCount(btnId);
updateMapInfoByHost();
});
labelGroupForOptions->add(68, 265, CGI->generaltexth->allTexts[755]);
groupCompOnlyPlayers = std::make_shared<CToggleGroup>(0);
groupCompOnlyPlayers->pos.y += 285;
groupCompOnlyPlayers->pos.x += BTNS_GROUP_LEFT_MARGIN;
addButtonsWithRandToGroup(groupCompOnlyPlayers.get(), numberDefs, 0, 7, NUMBERS_WIDTH, 224, 232);
groupCompOnlyPlayers->addCallback([&](int btnId)
addCallback("setCompOnlyPlayers", [&](int btnId)
{
mapGenOptions->setCompOnlyPlayerCount(btnId);
// deactive some MaxPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
deactivateButtonsFrom(groupMaxPlayers.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
deactivateButtonsFrom(dynamic_pointer_cast<CToggleGroup>(widget("groupMaxPlayers")).get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
deactivateButtonsFrom(groupCompOnlyTeams.get(), (btnId == 0 ? 1 : btnId));
deactivateButtonsFrom(dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams")).get(), (btnId == 0 ? 1 : btnId));
validateCompOnlyPlayersCnt(btnId);
updateMapInfoByHost();
});
labelGroupForOptions->add(68, 331, CGI->generaltexth->allTexts[756]);
groupCompOnlyTeams = std::make_shared<CToggleGroup>(0);
groupCompOnlyTeams->pos.y += 351;
groupCompOnlyTeams->pos.x += BTNS_GROUP_LEFT_MARGIN;
addButtonsWithRandToGroup(groupCompOnlyTeams.get(), numberDefs, 0, 6, NUMBERS_WIDTH, 234, 241);
deactivateButtonsFrom(groupCompOnlyTeams.get(), 1);
groupCompOnlyTeams->addCallback([&](int btnId)
addCallback("setCompOnlyTeams", [&](int btnId)
{
mapGenOptions->setCompOnlyTeamCount(btnId);
updateMapInfoByHost();
});
labelGroupForOptions->add(68, 398, CGI->generaltexth->allTexts[757]);
const int WIDE_BTN_WIDTH = 85;
groupWaterContent = std::make_shared<CToggleGroup>(0);
groupWaterContent->pos.y += 419;
groupWaterContent->pos.x += BTNS_GROUP_LEFT_MARGIN;
const std::vector<std::string> waterContentBtns = {"RANNONE", "RANNORM", "RANISLD"};
addButtonsWithRandToGroup(groupWaterContent.get(), waterContentBtns, 0, 2, WIDE_BTN_WIDTH, 243, 246);
groupWaterContent->addCallback([&](int btnId)
addCallback("setWaterContent", [&](int btnId)
{
mapGenOptions->setWaterContent(static_cast<EWaterContent::EWaterContent>(btnId));
updateMapInfoByHost();
});
labelGroupForOptions->add(68, 465, CGI->generaltexth->allTexts[758]);
groupMonsterStrength = std::make_shared<CToggleGroup>(0);
groupMonsterStrength->pos.y += 485;
groupMonsterStrength->pos.x += BTNS_GROUP_LEFT_MARGIN;
const std::vector<std::string> monsterStrengthBtns = {"RANWEAK", "RANNORM", "RANSTRG"};
addButtonsWithRandToGroup(groupMonsterStrength.get(), monsterStrengthBtns, 2, 4, WIDE_BTN_WIDTH, 248, 251, EMonsterStrength::RANDOM, false);
groupMonsterStrength->addCallback([&](int btnId)
addCallback("setMonsterStrenght", [&](int btnId)
{
if(btnId < 0)
mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
@@ -155,9 +100,9 @@ RandomMapTab::RandomMapTab()
mapGenOptions->setMonsterStrength(static_cast<EMonsterStrength::EMonsterStrength>(btnId)); //value 2 to 4
updateMapInfoByHost();
});
buttonShowRandomMaps = std::make_shared<CButton>(Point(54, 535), "RANSHOW", CGI->generaltexth->zelp[252]);
init(config["randomMapTab"]);
updateMapInfoByHost();
}
@@ -216,39 +161,14 @@ void RandomMapTab::updateMapInfoByHost()
void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
{
groupMapSize->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
buttonTwoLevels->setSelected(opts->getHasTwoLevels());
groupMaxPlayers->setSelected(opts->getPlayerCount());
groupMaxTeams->setSelected(opts->getTeamCount());
groupCompOnlyPlayers->setSelected(opts->getCompOnlyPlayerCount());
groupCompOnlyTeams->setSelected(opts->getCompOnlyTeamCount());
groupWaterContent->setSelected(opts->getWaterContent());
groupMonsterStrength->setSelected(opts->getMonsterStrength());
}
void RandomMapTab::addButtonsWithRandToGroup(CToggleGroup * group, const std::vector<std::string> & defs, int nStart, int nEnd, int btnWidth, int helpStartIndex, int helpRandIndex, int randIndex, bool animIdfromBtnId) const
{
addButtonsToGroup(group, defs, nStart, nEnd, btnWidth, helpStartIndex, animIdfromBtnId);
// Buttons are relative to button group, TODO better solution?
SObjectConstruction obj__i(group);
const std::string RANDOM_DEF = "RANRAND";
group->addToggle(randIndex, std::make_shared<CToggleButton>(Point(256, 0), RANDOM_DEF, CGI->generaltexth->zelp[helpRandIndex]));
group->setSelected(randIndex);
}
void RandomMapTab::addButtonsToGroup(CToggleGroup * group, const std::vector<std::string> & defs, int nStart, int nEnd, int btnWidth, int helpStartIndex, bool animIdfromBtnId) const
{
// Buttons are relative to button group, TODO better solution?
SObjectConstruction obj__i(group);
int cnt = nEnd - nStart + 1;
for(int i = 0; i < cnt; ++i)
{
auto button = std::make_shared<CToggleButton>(Point(i * btnWidth, 0), animIdfromBtnId ? defs[i + nStart] : defs[i], CGI->generaltexth->zelp[helpStartIndex + i]);
// For blocked state we should use pressed image actually
button->setImageOrder(0, 1, 1, 3);
group->addToggle(i + nStart, button);
}
dynamic_pointer_cast<CToggleGroup>(widget("groupMapSize"))->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
dynamic_pointer_cast<CToggleButton>(widget("buttonTwoLevels"))->setSelected(opts->getHasTwoLevels());
dynamic_pointer_cast<CToggleGroup>(widget("groupMaxPlayers"))->setSelected(opts->getPlayerCount());
dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams"))->setSelected(opts->getTeamCount());
dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers"))->setSelected(opts->getCompOnlyPlayerCount());
dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams"))->setSelected(opts->getCompOnlyTeamCount());
dynamic_pointer_cast<CToggleGroup>(widget("groupWaterContent"))->setSelected(opts->getWaterContent());
dynamic_pointer_cast<CToggleGroup>(widget("groupMonsterStrength"))->setSelected(opts->getMonsterStrength());
}
void RandomMapTab::deactivateButtonsFrom(CToggleGroup * group, int startId)
@@ -280,13 +200,13 @@ void RandomMapTab::validatePlayersCnt(int playersCnt)
if(mapGenOptions->getTeamCount() >= playersCnt)
{
mapGenOptions->setTeamCount(playersCnt - 1);
groupMaxTeams->setSelected(mapGenOptions->getTeamCount());
dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams"))->setSelected(mapGenOptions->getTeamCount());
}
// total players should not exceed PlayerColor::PLAYER_LIMIT_I (8 in homm3)
if(mapGenOptions->getCompOnlyPlayerCount() + playersCnt > PlayerColor::PLAYER_LIMIT_I)
{
mapGenOptions->setCompOnlyPlayerCount(PlayerColor::PLAYER_LIMIT_I - playersCnt);
groupCompOnlyPlayers->setSelected(mapGenOptions->getCompOnlyPlayerCount());
dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers"))->setSelected(mapGenOptions->getCompOnlyPlayerCount());
}
validateCompOnlyPlayersCnt(mapGenOptions->getCompOnlyPlayerCount());
@@ -304,7 +224,7 @@ void RandomMapTab::validateCompOnlyPlayersCnt(int compOnlyPlayersCnt)
int compOnlyTeamCount = compOnlyPlayersCnt == 0 ? 0 : compOnlyPlayersCnt - 1;
mapGenOptions->setCompOnlyTeamCount(compOnlyTeamCount);
updateMapInfoByHost();
groupCompOnlyTeams->setSelected(compOnlyTeamCount);
dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams"))->setSelected(compOnlyTeamCount);
}
}