2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* RandomMapTab.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "RandomMapTab.h"
|
|
|
|
#include "CSelectionBase.h"
|
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CServerHandler.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "../gui/WindowHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/mapping/CMapInfo.h"
|
2023-02-24 22:38:12 +02:00
|
|
|
#include "../../lib/mapping/CMap.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../../lib/rmg/CMapGenOptions.h"
|
2022-12-12 01:27:59 +02:00
|
|
|
#include "../../lib/CModHandler.h"
|
2022-12-13 02:38:18 +02:00
|
|
|
#include "../../lib/rmg/CRmgTemplateStorage.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2023-01-11 15:17:24 +02:00
|
|
|
#include "../../lib/RoadHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-12-12 01:27:59 +02:00
|
|
|
RandomMapTab::RandomMapTab():
|
2022-12-12 09:48:39 +02:00
|
|
|
InterfaceObjectConfigurable()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
recActions = 0;
|
|
|
|
mapGenOptions = std::make_shared<CMapGenOptions>();
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2022-12-16 00:19:23 +02:00
|
|
|
const JsonNode config(ResourceID("config/widgets/randomMapTab.json"));
|
2022-12-12 01:27:59 +02:00
|
|
|
addCallback("toggleMapSize", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
auto mapSizeVal = getPossibleMapSizes();
|
|
|
|
mapGenOptions->setWidth(mapSizeVal[btnId]);
|
|
|
|
mapGenOptions->setHeight(mapSizeVal[btnId]);
|
2022-12-14 03:23:21 +02:00
|
|
|
if(mapGenOptions->getMapTemplate())
|
|
|
|
if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
|
|
|
|
setTemplate(nullptr);
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
addCallback("toggleTwoLevels", [&](bool on)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setHasTwoLevels(on);
|
2022-12-14 03:23:21 +02:00
|
|
|
if(mapGenOptions->getMapTemplate())
|
|
|
|
if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
|
|
|
|
setTemplate(nullptr);
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setPlayersCount", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setPlayerCount(btnId);
|
2022-12-14 02:37:11 +02:00
|
|
|
setMapGenOptions(mapGenOptions);
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setTeamsCount", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setTeamCount(btnId);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setCompOnlyPlayers", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setCompOnlyPlayerCount(btnId);
|
2022-12-14 02:37:11 +02:00
|
|
|
setMapGenOptions(mapGenOptions);
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setCompOnlyTeams", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setCompOnlyTeamCount(btnId);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setWaterContent", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setWaterContent(static_cast<EWaterContent::EWaterContent>(btnId));
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2022-12-12 09:38:27 +02:00
|
|
|
addCallback("setMonsterStrength", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(btnId < 0)
|
|
|
|
mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
|
|
|
|
else
|
2018-08-24 13:37:05 +02:00
|
|
|
mapGenOptions->setMonsterStrength(static_cast<EMonsterStrength::EMonsterStrength>(btnId)); //value 2 to 4
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2022-12-13 01:47:29 +02:00
|
|
|
//new callbacks available only from mod
|
|
|
|
addCallback("templateSelection", [&](int)
|
|
|
|
{
|
2023-05-16 15:20:35 +02:00
|
|
|
GH.windows().createAndPushWindow<TemplatesDropBox>(*this, int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()});
|
2022-12-13 01:47:29 +02:00
|
|
|
});
|
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
addCallback("teamAlignments", [&](int)
|
|
|
|
{
|
2023-05-16 15:20:35 +02:00
|
|
|
GH.windows().createAndPushWindow<TeamAlignmentsWidget>(*this);
|
2022-12-15 22:46:36 +02:00
|
|
|
});
|
|
|
|
|
2022-12-20 18:35:40 +02:00
|
|
|
for(auto road : VLC->roadTypeHandler->objects)
|
2022-12-15 22:46:36 +02:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
std::string cbRoadType = "selectRoad_" + road->getJsonKey();
|
2022-12-17 02:54:37 +02:00
|
|
|
addCallback(cbRoadType, [&, road](bool on)
|
2022-12-15 22:46:36 +02:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
mapGenOptions->setRoadEnabled(road->getJsonKey(), on);
|
2022-12-17 02:54:37 +02:00
|
|
|
updateMapInfoByHost();
|
2022-12-15 22:46:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::updateMapInfoByHost()
|
|
|
|
{
|
|
|
|
if(CSH->isGuest())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate header info
|
|
|
|
mapInfo = std::make_shared<CMapInfo>();
|
|
|
|
mapInfo->isRandomMap = true;
|
2022-12-07 23:36:20 +02:00
|
|
|
mapInfo->mapHeader = std::make_unique<CMapHeader>();
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfo->mapHeader->version = EMapFormat::SOD;
|
|
|
|
mapInfo->mapHeader->name = CGI->generaltexth->allTexts[740];
|
|
|
|
mapInfo->mapHeader->description = CGI->generaltexth->allTexts[741];
|
|
|
|
mapInfo->mapHeader->difficulty = 1; // Normal
|
|
|
|
mapInfo->mapHeader->height = mapGenOptions->getHeight();
|
|
|
|
mapInfo->mapHeader->width = mapGenOptions->getWidth();
|
|
|
|
mapInfo->mapHeader->twoLevel = mapGenOptions->getHasTwoLevels();
|
|
|
|
|
|
|
|
// Generate player information
|
|
|
|
mapInfo->mapHeader->players.clear();
|
|
|
|
int playersToGen = PlayerColor::PLAYER_LIMIT_I;
|
|
|
|
if(mapGenOptions->getPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
2020-07-07 13:45:53 +02:00
|
|
|
{
|
|
|
|
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
|
|
|
playersToGen = mapGenOptions->getPlayerCount() + mapGenOptions->getCompOnlyPlayerCount();
|
|
|
|
else
|
|
|
|
playersToGen = mapGenOptions->getPlayerCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfo->mapHeader->howManyTeams = playersToGen;
|
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
std::set<TeamID> occupiedTeams;
|
2018-01-05 19:21:07 +02:00
|
|
|
for(int i = 0; i < playersToGen; ++i)
|
|
|
|
{
|
|
|
|
PlayerInfo player;
|
|
|
|
player.isFactionRandom = true;
|
|
|
|
player.canComputerPlay = true;
|
2020-07-07 13:45:53 +02:00
|
|
|
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE && i >= mapGenOptions->getPlayerCount())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
player.canHumanPlay = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player.canHumanPlay = true;
|
|
|
|
}
|
2022-12-17 06:19:16 +02:00
|
|
|
auto team = mapGenOptions->getPlayersSettings().at(PlayerColor(i)).getTeam();
|
|
|
|
player.team = team;
|
|
|
|
occupiedTeams.insert(team);
|
2018-01-05 19:21:07 +02:00
|
|
|
player.hasMainTown = true;
|
|
|
|
player.generateHeroAtMainTown = true;
|
|
|
|
mapInfo->mapHeader->players.push_back(player);
|
|
|
|
}
|
2022-12-17 06:19:16 +02:00
|
|
|
for(auto & player : mapInfo->mapHeader->players)
|
|
|
|
{
|
|
|
|
for(int i = 0; player.team == TeamID::NO_TEAM; ++i)
|
|
|
|
{
|
|
|
|
TeamID team(i);
|
|
|
|
if(!occupiedTeams.count(team))
|
|
|
|
{
|
|
|
|
player.team = team;
|
|
|
|
occupiedTeams.insert(team);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
mapInfoChanged(mapInfo, mapGenOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
|
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
mapGenOptions = opts;
|
|
|
|
|
|
|
|
//prepare allowed options
|
|
|
|
for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
|
|
|
|
{
|
|
|
|
playerCountAllowed.insert(i);
|
|
|
|
compCountAllowed.insert(i);
|
|
|
|
playerTeamsAllowed.insert(i);
|
|
|
|
compTeamsAllowed.insert(i);
|
|
|
|
}
|
|
|
|
auto * tmpl = mapGenOptions->getMapTemplate();
|
|
|
|
if(tmpl)
|
|
|
|
{
|
|
|
|
playerCountAllowed = tmpl->getPlayers().getNumbers();
|
|
|
|
compCountAllowed = tmpl->getCpuPlayers().getNumbers();
|
|
|
|
}
|
|
|
|
if(mapGenOptions->getPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
vstd::erase_if(compCountAllowed,
|
|
|
|
[opts](int el){
|
|
|
|
return PlayerColor::PLAYER_LIMIT_I - opts->getPlayerCount() < el;
|
|
|
|
});
|
|
|
|
vstd::erase_if(playerTeamsAllowed,
|
|
|
|
[opts](int el){
|
2022-12-14 03:45:50 +02:00
|
|
|
return opts->getPlayerCount() <= el;
|
2022-12-14 02:37:11 +02:00
|
|
|
});
|
2022-12-14 03:45:50 +02:00
|
|
|
|
|
|
|
if(!playerTeamsAllowed.count(opts->getTeamCount()))
|
|
|
|
opts->setTeamCount(CMapGenOptions::RANDOM_SIZE);
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
|
|
|
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
vstd::erase_if(playerCountAllowed,
|
|
|
|
[opts](int el){
|
|
|
|
return PlayerColor::PLAYER_LIMIT_I - opts->getCompOnlyPlayerCount() < el;
|
|
|
|
});
|
|
|
|
vstd::erase_if(compTeamsAllowed,
|
|
|
|
[opts](int el){
|
2022-12-14 03:45:50 +02:00
|
|
|
return opts->getCompOnlyPlayerCount() <= el;
|
2022-12-14 02:37:11 +02:00
|
|
|
});
|
2022-12-14 03:45:50 +02:00
|
|
|
|
|
|
|
if(!compTeamsAllowed.count(opts->getCompOnlyTeamCount()))
|
|
|
|
opts->setCompOnlyTeamCount(CMapGenOptions::RANDOM_SIZE);
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
|
|
|
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMapSize"))
|
2023-04-19 22:14:23 +02:00
|
|
|
{
|
|
|
|
for(auto toggle : w->buttons)
|
|
|
|
{
|
|
|
|
if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
|
|
|
|
{
|
|
|
|
const auto & mapSizes = getPossibleMapSizes();
|
|
|
|
int3 size( mapSizes[toggle.first], mapSizes[toggle.first], 1 + mapGenOptions->getHasTwoLevels());
|
|
|
|
|
|
|
|
bool sizeAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
|
|
|
|
button->block(!sizeAllowed);
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
|
2023-04-19 22:14:23 +02:00
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleButton>("buttonTwoLevels"))
|
2023-04-19 22:14:23 +02:00
|
|
|
{
|
|
|
|
int3 size( opts->getWidth(), opts->getWidth(), 2);
|
|
|
|
|
|
|
|
bool undergoundAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
|
|
|
|
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getHasTwoLevels());
|
2023-04-19 22:14:23 +02:00
|
|
|
w->block(!undergoundAllowed);
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getPlayerCount());
|
2022-12-14 02:37:11 +02:00
|
|
|
deactivateButtonsFrom(*w, playerCountAllowed);
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMaxTeams"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getTeamCount());
|
2022-12-14 03:23:21 +02:00
|
|
|
deactivateButtonsFrom(*w, playerTeamsAllowed);
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getCompOnlyPlayerCount());
|
2022-12-14 03:23:21 +02:00
|
|
|
deactivateButtonsFrom(*w, compCountAllowed);
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getCompOnlyTeamCount());
|
2022-12-14 02:37:11 +02:00
|
|
|
deactivateButtonsFrom(*w, compTeamsAllowed);
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupWaterContent"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getWaterContent());
|
2022-12-14 02:37:11 +02:00
|
|
|
if(opts->getMapTemplate())
|
|
|
|
{
|
|
|
|
std::set<int> allowedWater(opts->getMapTemplate()->getWaterContentAllowed().begin(), opts->getMapTemplate()->getWaterContentAllowed().end());
|
|
|
|
deactivateButtonsFrom(*w, allowedWater);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
deactivateButtonsFrom(*w, {-1});
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMonsterStrength"))
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getMonsterStrength());
|
2022-12-17 06:38:33 +02:00
|
|
|
if(auto w = widget<CButton>("templateButton"))
|
|
|
|
{
|
|
|
|
if(tmpl)
|
|
|
|
w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL);
|
|
|
|
else
|
2022-12-21 23:37:33 +02:00
|
|
|
w->addTextOverlay(readText(variables["defaultTemplate"]), EFonts::FONT_SMALL);
|
2022-12-17 06:38:33 +02:00
|
|
|
}
|
2022-12-20 18:35:40 +02:00
|
|
|
for(auto r : VLC->roadTypeHandler->objects)
|
2022-12-17 06:38:33 +02:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
if(auto w = widget<CToggleButton>(r->getJsonKey()))
|
2022-12-17 06:38:33 +02:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
w->setSelected(opts->isRoadEnabled(r->getJsonKey()));
|
2022-12-17 06:38:33 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
void RandomMapTab::setTemplate(const CRmgTemplate * tmpl)
|
|
|
|
{
|
|
|
|
mapGenOptions->setMapTemplate(tmpl);
|
2022-12-14 02:37:11 +02:00
|
|
|
setMapGenOptions(mapGenOptions);
|
2022-12-13 02:38:18 +02:00
|
|
|
if(auto w = widget<CButton>("templateButton"))
|
|
|
|
{
|
|
|
|
if(tmpl)
|
|
|
|
w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL);
|
|
|
|
else
|
2022-12-21 23:37:33 +02:00
|
|
|
w->addTextOverlay(readText(variables["defaultTemplate"]), EFonts::FONT_SMALL);
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
2022-12-14 02:37:11 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::deactivateButtonsFrom(CToggleGroup & group, const std::set<int> & allowed)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
logGlobal->debug("Blocking buttons");
|
|
|
|
for(auto toggle : group.buttons)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
if(allowed.count(CMapGenOptions::RANDOM_SIZE)
|
|
|
|
|| allowed.count(toggle.first)
|
|
|
|
|| toggle.first == CMapGenOptions::RANDOM_SIZE)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
button->block(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
button->block(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> RandomMapTab::getPossibleMapSizes()
|
|
|
|
{
|
2022-12-12 02:46:42 +02:00
|
|
|
return {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_GIANT};
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
TemplatesDropBox::ListItem::ListItem(const JsonNode & config, TemplatesDropBox & _dropBox, Point position)
|
|
|
|
: InterfaceObjectConfigurable(LCLICK | HOVER, position),
|
2022-12-13 02:38:18 +02:00
|
|
|
dropBox(_dropBox)
|
2022-12-13 01:47:29 +02:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION;
|
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
if(auto w = widget<CPicture>("hoverImage"))
|
|
|
|
{
|
|
|
|
pos.w = w->pos.w;
|
|
|
|
pos.h = w->pos.h;
|
|
|
|
}
|
2022-12-13 01:47:29 +02:00
|
|
|
type |= REDRAW_PARENT;
|
|
|
|
}
|
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
void TemplatesDropBox::ListItem::updateItem(int idx, const CRmgTemplate * _item)
|
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
if(auto w = widget<CLabel>("labelName"))
|
2022-12-13 02:38:18 +02:00
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
item = _item;
|
|
|
|
if(item)
|
|
|
|
{
|
|
|
|
w->setText(item->getName());
|
|
|
|
}
|
2022-12-13 02:38:18 +02:00
|
|
|
else
|
2022-12-16 00:15:53 +02:00
|
|
|
{
|
|
|
|
if(idx)
|
|
|
|
w->setText("");
|
|
|
|
else
|
2022-12-21 23:37:33 +02:00
|
|
|
w->setText(readText(dropBox.variables["defaultTemplate"]));
|
2022-12-16 00:15:53 +02:00
|
|
|
}
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:47:29 +02:00
|
|
|
void TemplatesDropBox::ListItem::hover(bool on)
|
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
auto h = widget<CPicture>("hoverImage");
|
|
|
|
auto w = widget<CLabel>("labelName");
|
|
|
|
if(h && w)
|
2022-12-13 02:38:18 +02:00
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
if(w->getText().empty())
|
|
|
|
{
|
|
|
|
hovered = false;
|
|
|
|
h->visible = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h->visible = on;
|
|
|
|
}
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
2022-12-13 01:47:29 +02:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
void TemplatesDropBox::ListItem::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if(down && hovered)
|
|
|
|
{
|
2022-12-15 22:57:10 +02:00
|
|
|
dropBox.setTemplate(item);
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
2023-04-06 18:32:21 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dropBox.clickLeft(true, true);
|
|
|
|
}
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-15 22:57:10 +02:00
|
|
|
TemplatesDropBox::TemplatesDropBox(RandomMapTab & randomMapTab, int3 size):
|
2022-12-16 00:15:53 +02:00
|
|
|
InterfaceObjectConfigurable(LCLICK | HOVER),
|
2022-12-13 02:38:18 +02:00
|
|
|
randomMapTab(randomMapTab)
|
2022-12-13 01:47:29 +02:00
|
|
|
{
|
2022-12-22 00:05:29 +02:00
|
|
|
REGISTER_BUILDER("templateListItem", &TemplatesDropBox::buildListItem);
|
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
curItems = VLC->tplh->getTemplates();
|
2023-04-19 22:14:23 +02:00
|
|
|
|
|
|
|
boost::range::sort(curItems, [](const CRmgTemplate * a, const CRmgTemplate * b){
|
|
|
|
return a->getName() < b->getName();
|
|
|
|
});
|
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
curItems.insert(curItems.begin(), nullptr); //default template
|
|
|
|
|
2022-12-16 00:19:23 +02:00
|
|
|
const JsonNode config(ResourceID("config/widgets/randomMapTemplateWidget.json"));
|
2022-12-16 00:15:53 +02:00
|
|
|
|
|
|
|
addCallback("sliderMove", std::bind(&TemplatesDropBox::sliderMove, this, std::placeholders::_1));
|
|
|
|
|
2022-12-13 01:47:29 +02:00
|
|
|
OBJ_CONSTRUCTION;
|
2022-12-26 01:38:25 +02:00
|
|
|
pos = randomMapTab.pos;
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
if(auto w = widget<CSlider>("slider"))
|
|
|
|
{
|
|
|
|
w->setAmount(curItems.size());
|
|
|
|
}
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
updateListItems();
|
2022-12-13 01:47:29 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 00:05:29 +02:00
|
|
|
std::shared_ptr<CIntObject> TemplatesDropBox::buildListItem(const JsonNode & config)
|
2022-12-16 00:15:53 +02:00
|
|
|
{
|
2022-12-22 00:05:29 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
listItems.push_back(std::make_shared<ListItem>(config, *this, position));
|
|
|
|
return listItems.back();
|
2022-12-16 00:15:53 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 01:47:29 +02:00
|
|
|
void TemplatesDropBox::sliderMove(int slidPos)
|
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
auto w = widget<CSlider>("slider");
|
|
|
|
if(!w)
|
2022-12-13 01:47:29 +02:00
|
|
|
return; // ignore spurious call when slider is being created
|
2022-12-13 02:38:18 +02:00
|
|
|
updateListItems();
|
2022-12-13 01:47:29 +02:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplatesDropBox::hover(bool on)
|
|
|
|
{
|
|
|
|
hovered = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplatesDropBox::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
2022-12-13 02:38:18 +02:00
|
|
|
if(down && !hovered)
|
2022-12-13 01:47:29 +02:00
|
|
|
{
|
2023-04-06 18:32:21 +02:00
|
|
|
auto w = widget<CSlider>("slider");
|
|
|
|
|
|
|
|
// pop the interface only if the mouse is not clicking on the slider
|
|
|
|
if (!w || !w->mouseState(MouseButton::LEFT))
|
|
|
|
{
|
2023-05-16 15:20:35 +02:00
|
|
|
assert(GH.windows().topWindow().get() == this);
|
|
|
|
GH.windows().popWindow(GH.windows().topWindow());
|
2023-04-06 18:32:21 +02:00
|
|
|
}
|
2022-12-13 01:47:29 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 02:38:18 +02:00
|
|
|
|
|
|
|
void TemplatesDropBox::updateListItems()
|
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
if(auto w = widget<CSlider>("slider"))
|
2022-12-13 02:38:18 +02:00
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
int elemIdx = w->getValue();
|
|
|
|
for(auto item : listItems)
|
2022-12-13 02:38:18 +02:00
|
|
|
{
|
2022-12-16 00:15:53 +02:00
|
|
|
if(elemIdx < curItems.size())
|
|
|
|
{
|
|
|
|
item->updateItem(elemIdx, curItems[elemIdx]);
|
|
|
|
elemIdx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->updateItem(elemIdx);
|
|
|
|
}
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplatesDropBox::setTemplate(const CRmgTemplate * tmpl)
|
|
|
|
{
|
2022-12-15 22:57:10 +02:00
|
|
|
randomMapTab.setTemplate(tmpl);
|
2023-05-16 15:20:35 +02:00
|
|
|
assert(GH.windows().topWindow().get() == this);
|
|
|
|
GH.windows().popWindow(GH.windows().topWindow());
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
2022-12-17 01:53:26 +02:00
|
|
|
|
|
|
|
TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
|
2022-12-30 01:35:16 +02:00
|
|
|
InterfaceObjectConfigurable()
|
2022-12-17 01:53:26 +02:00
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
const JsonNode config(ResourceID("config/widgets/randomMapTeamsWidget.json"));
|
|
|
|
variables = config["variables"];
|
2022-12-17 01:53:26 +02:00
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
int humanPlayers = randomMapTab.obtainMapGenOptions().getPlayerCount();
|
|
|
|
int cpuPlayers = randomMapTab.obtainMapGenOptions().getCompOnlyPlayerCount();
|
|
|
|
int totalPlayers = humanPlayers == CMapGenOptions::RANDOM_SIZE || cpuPlayers == CMapGenOptions::RANDOM_SIZE
|
|
|
|
? PlayerColor::PLAYER_LIMIT_I : humanPlayers + cpuPlayers;
|
|
|
|
assert(totalPlayers <= PlayerColor::PLAYER_LIMIT_I);
|
|
|
|
auto settings = randomMapTab.obtainMapGenOptions().getPlayersSettings();
|
2022-12-17 18:38:16 +02:00
|
|
|
variables["totalPlayers"].Integer() = totalPlayers;
|
|
|
|
|
|
|
|
pos.w = variables["windowSize"]["x"].Integer() + totalPlayers * variables["cellMargin"]["x"].Integer();
|
|
|
|
pos.h = variables["windowSize"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
|
|
|
|
variables["backgroundRect"]["x"].Integer() = pos.x;
|
|
|
|
variables["backgroundRect"]["y"].Integer() = pos.y;
|
|
|
|
variables["backgroundRect"]["w"].Integer() = pos.w;
|
|
|
|
variables["backgroundRect"]["h"].Integer() = pos.h;
|
|
|
|
variables["okButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["ok"]["x"].Integer();
|
|
|
|
variables["okButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["ok"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
|
|
|
|
variables["cancelButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["cancel"]["x"].Integer();
|
|
|
|
variables["cancelButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["cancel"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
|
|
|
|
|
|
|
|
addCallback("ok", [&](int)
|
|
|
|
{
|
|
|
|
for(int plId = 0; plId < players.size(); ++plId)
|
|
|
|
{
|
|
|
|
randomMapTab.obtainMapGenOptions().setPlayerTeam(PlayerColor(plId), TeamID(players[plId]->getSelected()));
|
|
|
|
}
|
|
|
|
randomMapTab.updateMapInfoByHost();
|
2023-05-16 15:20:35 +02:00
|
|
|
assert(GH.windows().topWindow().get() == this);
|
|
|
|
GH.windows().popWindow(GH.windows().topWindow());
|
2022-12-17 18:38:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
addCallback("cancel", [&](int)
|
|
|
|
{
|
2023-05-16 15:20:35 +02:00
|
|
|
assert(GH.windows().topWindow().get() == this);
|
|
|
|
GH.windows().popWindow(GH.windows().topWindow());
|
2022-12-17 18:38:16 +02:00
|
|
|
});
|
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-17 06:19:16 +02:00
|
|
|
|
2022-12-17 01:53:26 +02:00
|
|
|
center(pos);
|
|
|
|
|
2022-12-17 18:38:16 +02:00
|
|
|
OBJ_CONSTRUCTION;
|
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
for(int plId = 0; plId < totalPlayers; ++plId)
|
|
|
|
{
|
|
|
|
players.push_back(std::make_shared<CToggleGroup>([&, totalPlayers, plId](int sel)
|
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
variables["player_id"].Integer() = plId;
|
2022-12-21 22:54:06 +02:00
|
|
|
OBJ_CONSTRUCTION_TARGETED(players[plId].get());
|
2022-12-17 06:19:16 +02:00
|
|
|
for(int teamId = 0; teamId < totalPlayers; ++teamId)
|
|
|
|
{
|
|
|
|
auto button = std::dynamic_pointer_cast<CToggleButton>(players[plId]->buttons[teamId]);
|
|
|
|
assert(button);
|
|
|
|
if(sel == teamId)
|
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
button->addOverlay(buildWidget(variables["flagsAnimation"]));
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-17 18:50:33 +02:00
|
|
|
button->addOverlay(nullptr);
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-12-21 22:54:06 +02:00
|
|
|
OBJ_CONSTRUCTION_TARGETED(players.back().get());
|
2022-12-17 06:19:16 +02:00
|
|
|
for(int teamId = 0; teamId < totalPlayers; ++teamId)
|
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
variables["point"]["x"].Integer() = variables["cellOffset"]["x"].Integer() + plId * variables["cellMargin"]["x"].Integer();
|
|
|
|
variables["point"]["y"].Integer() = variables["cellOffset"]["y"].Integer() + teamId * variables["cellMargin"]["y"].Integer();
|
|
|
|
auto button = buildWidget(variables["button"]);
|
|
|
|
players.back()->addToggle(teamId, std::dynamic_pointer_cast<CToggleBase>(button));
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto team = settings.at(PlayerColor(plId)).getTeam();
|
|
|
|
if(team == TeamID::NO_TEAM)
|
|
|
|
players.back()->setSelected(plId);
|
|
|
|
else
|
|
|
|
players.back()->setSelected(team.getNum());
|
|
|
|
}
|
2022-12-17 01:53:26 +02:00
|
|
|
}
|