1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/client/lobby/OptionsTab.cpp

1070 lines
34 KiB
C++
Raw Normal View History

/*
* OptionsTab.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 "OptionsTab.h"
#include "CSelectionBase.h"
#include "../CGameInfo.h"
#include "../CServerHandler.h"
#include "../gui/CGuiHandler.h"
2023-08-12 02:41:48 +02:00
#include "../gui/Shortcut.h"
#include "../gui/WindowHandler.h"
#include "../render/Graphics.h"
#include "../render/IFont.h"
#include "../media/ISoundPlayer.h"
#include "../widgets/CComponent.h"
2023-08-25 18:20:26 +02:00
#include "../widgets/ComboBox.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Buttons.h"
2023-08-14 22:22:43 +02:00
#include "../widgets/Images.h"
#include "../widgets/MiscWidgets.h"
#include "../widgets/ObjectLists.h"
2023-05-30 16:31:45 +02:00
#include "../widgets/Slider.h"
#include "../widgets/TextControls.h"
#include "../windows/GUIClasses.h"
#include "../windows/InfoWindows.h"
2023-09-17 00:32:10 +02:00
#include "../windows/CHeroOverview.h"
2023-08-13 19:23:27 +02:00
#include "../eventsSDL/InputHandler.h"
2024-07-21 12:49:40 +02:00
#include "../../lib/entities/faction/CFaction.h"
#include "../../lib/entities/faction/CTown.h"
#include "../../lib/entities/faction/CTownHandler.h"
2023-08-23 17:29:50 +02:00
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/networkPacks/PacksForLobby.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/CArtHandler.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/mapping/CMapInfo.h"
2023-05-24 01:05:59 +02:00
#include "../../lib/mapping/CMapHeader.h"
static JsonPath optionsTabConfigLocation()
{
if(settings["general"]["enableUiEnhancements"].Bool())
return JsonPath::builtin("config/widgets/playerOptionsTab.json");
else
return JsonPath::builtin("config/widgets/advancedOptionsTab.json");
}
OptionsTab::OptionsTab()
: OptionsTabBase(optionsTabConfigLocation())
, humanPlayers(0)
{
}
void OptionsTab::recreate()
{
entries.clear();
2021-11-08 18:49:04 +02:00
humanPlayers = 0;
2023-08-29 02:04:32 +02:00
for (auto selectionWindow : GH.windows().findWindows<SelectionWindow>())
{
selectionWindow->reopen();
}
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
for(auto & pInfo : SEL->getStartInfo()->playerInfos)
{
2021-11-08 18:49:04 +02:00
if(pInfo.second.isControlledByHuman())
humanPlayers++;
entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
}
OptionsTabBase::recreate();
}
2023-08-12 13:19:58 +02:00
size_t OptionsTab::CPlayerSettingsHelper::getImageIndex(bool big)
{
enum EBonusSelection //frames of bonuses file
{
WOOD_ORE = 0, CRYSTAL = 1, GEM = 2,
MERCURY = 3, SULFUR = 5, GOLD = 8,
ARTIFACT = 9, RANDOM = 10,
WOOD = 0, ORE = 0, MITHRIL = 10, // resources unavailable in bonuses file
TOWN_RANDOM = 38, TOWN_NONE = 39, // Special frames in ITPA
HERO_RANDOM = 156, HERO_NONE = 157 // Special frames in PortraitsSmall
};
auto factionIndex = playerSettings.getCastleValidated();
switch(selectionType)
{
case TOWN:
{
if (playerSettings.castle == FactionID::NONE)
return TOWN_NONE;
if (playerSettings.castle == FactionID::RANDOM)
return TOWN_RANDOM;
return (*CGI->townh)[factionIndex]->town->clientInfo.icons[true][false] + (big ? 0 : 2);
}
case HERO:
{
if (playerSettings.hero == HeroTypeID::NONE)
return HERO_NONE;
if (playerSettings.hero == HeroTypeID::RANDOM)
return HERO_RANDOM;
if(playerSettings.heroPortrait != HeroTypeID::NONE)
return playerSettings.heroPortrait;
auto index = playerSettings.getHeroValidated();
return (*CGI->heroh)[index]->imageIndex;
}
case BONUS:
{
2023-09-17 00:32:10 +02:00
switch(playerSettings.bonus)
{
case PlayerStartingBonus::RANDOM:
return RANDOM;
case PlayerStartingBonus::ARTIFACT:
return ARTIFACT;
case PlayerStartingBonus::GOLD:
return GOLD;
case PlayerStartingBonus::RESOURCE:
{
2023-04-05 02:26:29 +02:00
switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
{
2023-04-05 02:26:29 +02:00
case EGameResID::WOOD_AND_ORE:
return WOOD_ORE;
2023-04-05 02:26:29 +02:00
case EGameResID::WOOD:
return WOOD;
2023-04-05 02:26:29 +02:00
case EGameResID::MERCURY:
return MERCURY;
2023-04-05 02:26:29 +02:00
case EGameResID::ORE:
return ORE;
2023-04-05 02:26:29 +02:00
case EGameResID::SULFUR:
return SULFUR;
2023-04-05 02:26:29 +02:00
case EGameResID::CRYSTAL:
return CRYSTAL;
2023-04-05 02:26:29 +02:00
case EGameResID::GEMS:
return GEM;
2023-04-05 02:26:29 +02:00
case EGameResID::GOLD:
return GOLD;
2023-04-05 02:26:29 +02:00
case EGameResID::MITHRIL:
return MITHRIL;
}
}
}
}
}
return 0;
}
AnimationPath OptionsTab::CPlayerSettingsHelper::getImageName(bool big)
{
switch(selectionType)
{
case OptionsTab::TOWN:
return AnimationPath::builtin(big ? "ITPt": "ITPA");
case OptionsTab::HERO:
return AnimationPath::builtin(big ? "PortraitsLarge": "PortraitsSmall");
case OptionsTab::BONUS:
return AnimationPath::builtin("SCNRSTAR");
}
return {};
}
std::string OptionsTab::CPlayerSettingsHelper::getName()
{
switch(selectionType)
{
case TOWN:
{
if (playerSettings.castle == FactionID::NONE)
return CGI->generaltexth->allTexts[523];
if (playerSettings.castle == FactionID::RANDOM)
return CGI->generaltexth->allTexts[522];
auto factionIndex = playerSettings.getCastleValidated();
return (*CGI->townh)[factionIndex]->getNameTranslated();
}
case HERO:
{
if (playerSettings.hero == HeroTypeID::NONE)
return CGI->generaltexth->allTexts[523];
if (playerSettings.hero == HeroTypeID::RANDOM)
return CGI->generaltexth->allTexts[522];
2023-09-28 00:04:05 +02:00
if(!playerSettings.heroNameTextId.empty())
return CGI->generaltexth->translate(playerSettings.heroNameTextId);
auto index = playerSettings.getHeroValidated();
return (*CGI->heroh)[index]->getNameTranslated();
}
case BONUS:
{
if (playerSettings.bonus == PlayerStartingBonus::RANDOM)
return CGI->generaltexth->allTexts[522];
return CGI->generaltexth->arraytxt[214 + static_cast<int>(playerSettings.bonus)];
}
}
return "";
}
std::string OptionsTab::CPlayerSettingsHelper::getTitle()
{
switch(selectionType)
{
case OptionsTab::TOWN:
return playerSettings.castle.isValid() ? CGI->generaltexth->allTexts[80] : CGI->generaltexth->allTexts[103];
case OptionsTab::HERO:
return playerSettings.hero.isValid() ? CGI->generaltexth->allTexts[77] : CGI->generaltexth->allTexts[101];
case OptionsTab::BONUS:
{
2023-09-17 00:32:10 +02:00
switch(playerSettings.bonus)
{
case PlayerStartingBonus::RANDOM:
return CGI->generaltexth->allTexts[86]; //{Random Bonus}
case PlayerStartingBonus::ARTIFACT:
return CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
case PlayerStartingBonus::GOLD:
return CGI->generaltexth->allTexts[84]; //{Gold Bonus}
case PlayerStartingBonus::RESOURCE:
return CGI->generaltexth->allTexts[85]; //{Resource Bonus}
}
}
}
return "";
}
std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
{
auto factionIndex = playerSettings.getCastleValidated();
auto heroIndex = playerSettings.getHeroValidated();
switch(selectionType)
{
case TOWN:
return getName();
case HERO:
{
if(playerSettings.hero.isValid())
return getName() + " - " + (*CGI->heroh)[heroIndex]->heroClass->getNameTranslated();
return getName();
}
case BONUS:
{
2023-09-17 00:32:10 +02:00
switch(playerSettings.bonus)
{
case PlayerStartingBonus::GOLD:
return CGI->generaltexth->allTexts[87]; //500-1000
case PlayerStartingBonus::RESOURCE:
{
2023-04-05 02:26:29 +02:00
switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
{
2023-04-05 02:26:29 +02:00
case EGameResID::MERCURY:
return CGI->generaltexth->allTexts[694];
2023-04-05 02:26:29 +02:00
case EGameResID::SULFUR:
return CGI->generaltexth->allTexts[695];
2023-04-05 02:26:29 +02:00
case EGameResID::CRYSTAL:
return CGI->generaltexth->allTexts[692];
2023-04-05 02:26:29 +02:00
case EGameResID::GEMS:
return CGI->generaltexth->allTexts[693];
2023-04-05 02:26:29 +02:00
case EGameResID::WOOD_AND_ORE:
return CGI->generaltexth->allTexts[89]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
}
}
}
}
}
return "";
}
std::string OptionsTab::CPlayerSettingsHelper::getDescription()
{
auto factionIndex = playerSettings.getCastleValidated();
switch(selectionType)
{
case TOWN:
return CGI->generaltexth->allTexts[104];
case HERO:
return CGI->generaltexth->allTexts[102];
case BONUS:
{
2023-09-17 00:32:10 +02:00
switch(playerSettings.bonus)
{
case PlayerStartingBonus::RANDOM:
return CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
case PlayerStartingBonus::ARTIFACT:
return CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
case PlayerStartingBonus::GOLD:
return CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
case PlayerStartingBonus::RESOURCE:
{
2023-04-05 02:26:29 +02:00
switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
{
2023-04-05 02:26:29 +02:00
case EGameResID::MERCURY:
return CGI->generaltexth->allTexts[690];
2023-04-05 02:26:29 +02:00
case EGameResID::SULFUR:
return CGI->generaltexth->allTexts[691];
2023-04-05 02:26:29 +02:00
case EGameResID::CRYSTAL:
return CGI->generaltexth->allTexts[688];
2023-04-05 02:26:29 +02:00
case EGameResID::GEMS:
return CGI->generaltexth->allTexts[689];
2023-04-05 02:26:29 +02:00
case EGameResID::WOOD_AND_ORE:
return CGI->generaltexth->allTexts[93]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
}
}
}
}
}
return "";
}
OptionsTab::CPlayerOptionTooltipBox::CPlayerOptionTooltipBox(CPlayerSettingsHelper & helper)
: CWindowObject(BORDERED | RCLICK_POPUP), CPlayerSettingsHelper(helper)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
switch(selectionType)
{
case TOWN:
genTownWindow();
break;
case HERO:
genHeroWindow();
break;
case BONUS:
genBonusWindow();
break;
}
center();
}
void OptionsTab::CPlayerOptionTooltipBox::genHeader()
{
backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
updateShadow();
labelTitle = std::make_shared<CLabel>(pos.w / 2 + 8, 21, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, getTitle());
labelSubTitle = std::make_shared<CLabel>(pos.w / 2, 88, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getSubtitle());
image = std::make_shared<CAnimImage>(getImageName(), getImageIndex(), 0, pos.w / 2 - 24, 45);
}
void OptionsTab::CPlayerOptionTooltipBox::genTownWindow()
{
auto factionIndex = playerSettings.getCastleValidated();
if (playerSettings.castle == FactionID::RANDOM)
return genBonusWindow();
pos = Rect(0, 0, 228, 290);
genHeader();
labelAssociatedCreatures = std::make_shared<CLabel>(pos.w / 2 + 8, 122, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[79]);
std::vector<std::shared_ptr<CComponent>> components;
const CTown * town = (*CGI->townh)[factionIndex]->town;
for(auto & elem : town->creatures)
{
if(!elem.empty())
components.push_back(std::make_shared<CComponent>(ComponentType::CREATURE, elem.front(), std::nullopt, CComponent::tiny));
}
boxAssociatedCreatures = std::make_shared<CComponentBox>(components, Rect(10, 140, pos.w - 20, 140), 20, 10, 22, 4);
}
void OptionsTab::CPlayerOptionTooltipBox::genHeroWindow()
{
auto heroIndex = playerSettings.getHeroValidated();
if (playerSettings.hero == HeroTypeID::RANDOM)
return genBonusWindow();
pos = Rect(0, 0, 292, 226);
genHeader();
labelHeroSpeciality = std::make_shared<CLabel>(pos.w / 2 + 4, 117, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[78]);
imageSpeciality = std::make_shared<CAnimImage>(AnimationPath::builtin("UN44"), (*CGI->heroh)[heroIndex]->imageIndex, 0, pos.w / 2 - 22, 134);
labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, (*CGI->heroh)[heroIndex]->getSpecialtyNameTranslated());
}
void OptionsTab::CPlayerOptionTooltipBox::genBonusWindow()
{
pos = Rect(0, 0, 228, 162);
genHeader();
textBonusDescription = std::make_shared<CTextBox>(getDescription(), Rect(10, 100, pos.w - 20, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
}
2024-07-06 22:55:56 +02:00
OptionsTab::SelectionWindow::SelectionWindow(const PlayerColor & color, SelType _type, int sliderPos)
: CWindowObject(BORDERED), color(color)
2023-08-12 02:41:48 +02:00
{
addUsedEvents(LCLICK | SHOW_POPUP);
2023-08-13 19:23:27 +02:00
type = _type;
2023-08-12 16:49:42 +02:00
2023-08-14 00:40:12 +02:00
initialFaction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
2023-08-12 17:46:35 +02:00
initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
2023-08-14 00:40:12 +02:00
selectedFaction = initialFaction;
2023-08-12 20:31:00 +02:00
selectedHero = initialHero;
selectedBonus = initialBonus;
allowedFactions = SEL->getPlayerInfo(color).allowedFactions;
allowedHeroes = SEL->getMapInfo()->mapHeader->allowedHeroes;
2023-08-12 21:50:40 +02:00
2023-08-29 00:14:49 +02:00
for(auto & player : SEL->getStartInfo()->playerInfos)
{
if(player.first != color && (int)player.second.hero > HeroTypeID::RANDOM)
2023-08-29 21:08:18 +02:00
unusableHeroes.insert(player.second.hero);
2023-08-29 00:14:49 +02:00
}
allowedBonus.push_back(PlayerStartingBonus::RANDOM);
if(initialHero != HeroTypeID::NONE|| SEL->getPlayerInfo(color).heroesNames.size() > 0)
allowedBonus.push_back(PlayerStartingBonus::ARTIFACT);
allowedBonus.push_back(PlayerStartingBonus::GOLD);
if(initialFaction.isValid())
allowedBonus.push_back(PlayerStartingBonus::RESOURCE);
2023-08-13 19:23:27 +02:00
2024-07-06 22:55:56 +02:00
recreate(sliderPos);
2023-08-13 15:09:48 +02:00
}
2023-08-12 21:50:40 +02:00
2024-07-06 23:22:40 +02:00
std::tuple<int, int> OptionsTab::SelectionWindow::calcLines(FactionID faction)
2023-08-13 15:09:48 +02:00
{
2024-07-06 23:22:40 +02:00
int additionalItems = 1; // random
2023-08-13 20:21:09 +02:00
if(!faction.isValid())
2024-07-06 23:22:40 +02:00
return std::make_tuple(
std::ceil(((double)allowedFactions.size() + additionalItems) / MAX_ELEM_PER_LINES),
(allowedFactions.size() + additionalItems) % MAX_ELEM_PER_LINES
);
2023-08-12 17:46:35 +02:00
2023-08-13 15:09:48 +02:00
int count = 0;
for(auto & elemh : allowedHeroes)
{
const CHero * type = elemh.toHeroType();
2023-08-13 15:09:48 +02:00
if(type->heroClass->faction == faction)
count++;
}
2023-08-12 02:41:48 +02:00
2024-07-06 23:22:40 +02:00
return std::make_tuple(
std::ceil(((double)count + additionalItems) / MAX_ELEM_PER_LINES),
(count + additionalItems) % MAX_ELEM_PER_LINES
);
2023-08-12 02:41:48 +02:00
}
2023-08-12 16:49:42 +02:00
void OptionsTab::SelectionWindow::apply()
{
if(GH.windows().isTopWindow(this))
{
2023-08-13 19:23:27 +02:00
GH.input().hapticFeedback();
CCS->soundh->playSound(soundBase::button);
2023-08-13 19:23:27 +02:00
2023-08-12 16:49:42 +02:00
close();
2023-08-12 17:46:35 +02:00
2023-08-12 18:20:44 +02:00
setSelection();
2023-08-12 16:49:42 +02:00
}
}
2023-08-12 18:20:44 +02:00
void OptionsTab::SelectionWindow::setSelection()
{
2023-08-14 00:40:12 +02:00
if(selectedFaction != initialFaction)
CSH->setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
2023-08-12 19:33:25 +02:00
2023-08-14 00:16:53 +02:00
if(selectedHero != initialHero)
CSH->setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
2023-08-12 19:33:25 +02:00
2023-08-14 00:16:53 +02:00
if(selectedBonus != initialBonus)
CSH->setPlayerOption(LobbyChangePlayerOption::BONUS_ID, static_cast<int>(selectedBonus), color);
2023-08-12 18:20:44 +02:00
}
2023-08-29 02:04:32 +02:00
void OptionsTab::SelectionWindow::reopen()
{
2024-06-29 18:58:48 +02:00
if(type == SelType::HERO && SEL->getStartInfo()->playerInfos.find(color)->second.castle == FactionID::RANDOM)
close();
else{
2024-07-06 22:55:56 +02:00
auto window = std::shared_ptr<SelectionWindow>(new SelectionWindow(color, type, slider ? slider->getValue() : 0));
2024-06-29 18:58:48 +02:00
close();
if(CSH->isMyColor(color) || CSH->isHost())
GH.windows().pushWindow(window);
}
2023-08-29 02:04:32 +02:00
}
2024-07-06 22:55:56 +02:00
void OptionsTab::SelectionWindow::recreate(int sliderPos)
2023-08-12 16:49:42 +02:00
{
2023-08-12 18:20:44 +02:00
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-08-13 22:58:49 +02:00
int amountLines = 1;
2023-08-13 19:23:27 +02:00
if(type == SelType::BONUS)
2023-08-13 22:58:49 +02:00
elementsPerLine = allowedBonus.size();
else
{
2024-07-06 23:22:40 +02:00
std::tie(amountLines, elementsPerLine) = calcLines((type > SelType::TOWN) ? selectedFaction : FactionID::RANDOM);
2024-07-06 23:57:08 +02:00
if(amountLines > 1 || elementsPerLine == 0)
2024-07-06 23:22:40 +02:00
elementsPerLine = MAX_ELEM_PER_LINES;
2023-08-13 22:58:49 +02:00
}
2023-08-17 17:54:53 +02:00
int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
2024-06-29 16:14:16 +02:00
int y = (std::min(amountLines, MAX_LINES)) * (ICON_BIG_HEIGHT-1);
2023-08-13 15:09:48 +02:00
2024-07-06 22:55:56 +02:00
int sliderWidth = ((amountLines > MAX_LINES) ? 16 : 0);
2023-08-13 15:09:48 +02:00
2024-07-06 22:55:56 +02:00
pos = Rect(pos.x, pos.y, x + sliderWidth, y);
backgroundTexture = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w - sliderWidth, pos.h));
backgroundTexture->setPlayerColor(PlayerColor(1));
2023-08-13 15:09:48 +02:00
updateShadow();
2023-08-13 19:23:27 +02:00
if(type == SelType::TOWN)
2023-08-14 00:40:12 +02:00
genContentFactions();
2023-08-13 19:23:27 +02:00
if(type == SelType::HERO)
2023-08-13 15:09:48 +02:00
genContentHeroes();
2023-08-13 19:23:27 +02:00
if(type == SelType::BONUS)
2023-08-13 15:09:48 +02:00
genContentBonus();
2024-06-29 16:14:16 +02:00
genContentGrid(std::min(amountLines, MAX_LINES));
2024-07-06 22:55:56 +02:00
if(!slider && amountLines > MAX_LINES)
2024-06-29 16:14:16 +02:00
{
slider = std::make_shared<CSlider>(Point(x, 0), y, std::bind(&OptionsTab::SelectionWindow::sliderMove, this, _1), MAX_LINES, amountLines, 0, Orientation::VERTICAL, CSlider::BLUE);
slider->setPanningStep(ICON_BIG_HEIGHT);
2024-06-29 16:53:28 +02:00
slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, x + slider->pos.w, y));
2024-07-06 22:55:56 +02:00
slider->scrollTo(sliderPos);
2024-06-29 16:14:16 +02:00
}
2023-08-12 16:49:42 +02:00
2023-08-13 19:23:27 +02:00
center();
2023-08-12 22:59:45 +02:00
}
2023-08-13 22:58:49 +02:00
void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
{
2024-01-08 00:42:48 +02:00
components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text, 56));
2023-08-13 22:58:49 +02:00
}
2023-08-13 19:23:27 +02:00
void OptionsTab::SelectionWindow::genContentGrid(int lines)
2023-08-12 22:59:45 +02:00
{
2023-08-17 17:29:31 +02:00
for(int y = 0; y < lines; y++)
2023-08-12 22:59:45 +02:00
{
2023-08-17 17:29:31 +02:00
for(int x = 0; x < elementsPerLine; x++)
2023-08-12 22:59:45 +02:00
{
components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
2023-08-12 22:59:45 +02:00
}
}
2023-08-12 15:19:40 +02:00
}
2023-08-14 00:40:12 +02:00
void OptionsTab::SelectionWindow::genContentFactions()
2023-08-12 02:41:48 +02:00
{
2023-08-13 20:21:09 +02:00
int i = 1;
// random
PlayerSettings set = PlayerSettings();
set.castle = FactionID::RANDOM;
2023-08-13 20:21:09 +02:00
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
2023-08-17 17:54:53 +02:00
components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedFaction == FactionID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
if(selectedFaction == FactionID::RANDOM)
components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
2023-08-13 19:23:27 +02:00
2024-06-29 16:53:28 +02:00
factions.clear();
2023-08-12 17:46:35 +02:00
for(auto & elem : allowedFactions)
2023-08-12 02:41:48 +02:00
{
2023-08-13 22:58:49 +02:00
int x = i % elementsPerLine;
2024-07-06 22:55:56 +02:00
int y = (i / elementsPerLine) - (slider ? slider->getValue() : 0);
2023-08-12 02:41:48 +02:00
PlayerSettings set = PlayerSettings();
set.castle = elem;
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
factions.push_back(elem);
i++;
2024-07-06 22:55:56 +02:00
if(y < 0 || y > MAX_LINES - 1)
continue;
components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
components.push_back(std::make_shared<CPicture>(ImagePath::builtin(selectedFaction == elem ? "lobby/townBorderBigActivated" : "lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, (selectedFaction == elem) ? Colors::YELLOW : Colors::WHITE, helper.getName());
2023-08-12 02:41:48 +02:00
}
}
2023-08-12 16:49:42 +02:00
void OptionsTab::SelectionWindow::genContentHeroes()
2023-08-12 02:41:48 +02:00
{
2023-08-13 20:21:09 +02:00
int i = 1;
// random
PlayerSettings set = PlayerSettings();
set.hero = HeroTypeID::RANDOM;
2023-08-13 20:21:09 +02:00
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
2023-08-17 17:54:53 +02:00
components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedHero == HeroTypeID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
if(selectedHero == HeroTypeID::RANDOM)
components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
2023-08-13 19:23:27 +02:00
2024-06-29 16:53:28 +02:00
heroes.clear();
2023-08-12 13:19:58 +02:00
for(auto & elem : allowedHeroes)
{
const CHero * type = elem.toHeroType();
2023-08-12 13:19:58 +02:00
2024-07-06 22:55:56 +02:00
if(type->heroClass->faction != selectedFaction)
continue;
2023-08-12 13:19:58 +02:00
2024-07-06 22:55:56 +02:00
int x = i % elementsPerLine;
int y = (i / elementsPerLine) - (slider ? slider->getValue() : 0);
2023-08-12 13:19:58 +02:00
2024-07-06 22:55:56 +02:00
PlayerSettings set = PlayerSettings();
set.hero = elem;
2023-08-12 15:05:26 +02:00
2024-07-06 22:55:56 +02:00
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
2023-08-12 15:05:26 +02:00
2024-07-06 22:55:56 +02:00
heroes.push_back(elem);
i++;
2023-08-12 15:05:26 +02:00
2024-07-06 22:55:56 +02:00
if(y < 0 || y > MAX_LINES - 1)
continue;
components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, (selectedHero == elem) ? Colors::YELLOW : Colors::WHITE, helper.getName());
ImagePath image = ImagePath::builtin("lobby/townBorderBig");
if(selectedHero == elem)
image = ImagePath::builtin("lobby/townBorderBigActivated");
if(unusableHeroes.count(elem))
image = ImagePath::builtin("lobby/townBorderBigGrayedOut");
components.push_back(std::make_shared<CPicture>(image, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
2023-08-12 15:05:26 +02:00
}
}
2023-08-12 16:49:42 +02:00
void OptionsTab::SelectionWindow::genContentBonus()
2023-08-12 15:05:26 +02:00
{
2024-01-13 22:51:56 +02:00
PlayerSettings set = SEL->getStartInfo()->playerInfos.find(color)->second;
2023-08-12 15:05:26 +02:00
int i = 0;
2023-08-13 19:23:27 +02:00
for(auto elem : allowedBonus)
2023-08-12 15:05:26 +02:00
{
2023-08-13 19:23:27 +02:00
int x = i;
int y = 0;
2023-08-12 15:05:26 +02:00
set.bonus = elem;
2023-08-12 15:05:26 +02:00
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
2023-08-17 17:54:53 +02:00
components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, x * (ICON_BIG_WIDTH-1) + 6, y * (ICON_BIG_HEIGHT-1) + (ICON_SMALL_HEIGHT/2)));
drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::WHITE , helper.getName());
2023-08-12 22:59:45 +02:00
if(selectedBonus == elem)
2023-08-14 00:40:12 +02:00
{
components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), x * (ICON_BIG_WIDTH-1) + 6, y * (ICON_BIG_HEIGHT-1) + (ICON_SMALL_HEIGHT/2)));
2023-08-17 17:54:53 +02:00
drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
2023-08-14 00:40:12 +02:00
}
2023-08-12 13:19:58 +02:00
i++;
}
2023-08-12 02:41:48 +02:00
}
2023-08-13 19:23:27 +02:00
int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
2023-08-12 02:41:48 +02:00
{
2023-08-17 17:54:53 +02:00
int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
2024-07-06 22:55:56 +02:00
int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1) + (slider ? slider->getValue() : 0);
2023-08-12 02:41:48 +02:00
2023-08-13 22:58:49 +02:00
return x + y * elementsPerLine;
2023-08-12 02:41:48 +02:00
}
2023-08-17 20:34:31 +02:00
void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
2023-08-12 02:41:48 +02:00
{
PlayerSettings set = PlayerSettings();
2023-08-13 19:23:27 +02:00
if(type == SelType::TOWN)
2023-08-12 15:05:26 +02:00
{
2023-08-14 00:16:53 +02:00
if(elem > 0)
{
elem--;
if(elem >= factions.size())
return;
set.castle = factions[elem];
}
else
{
set.castle = FactionID::RANDOM;
2023-08-14 00:16:53 +02:00
}
if(set.castle != FactionID::NONE)
2023-08-13 19:23:27 +02:00
{
2023-08-17 20:34:31 +02:00
if(!doApply)
2023-08-17 18:15:42 +02:00
{
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
}
2023-08-18 00:15:47 +02:00
else
selectedFaction = set.castle;
2023-08-13 19:23:27 +02:00
}
2023-08-12 15:05:26 +02:00
}
2023-08-13 19:23:27 +02:00
if(type == SelType::HERO)
2023-08-12 15:05:26 +02:00
{
2023-08-14 00:16:53 +02:00
if(elem > 0)
{
elem--;
if(elem >= heroes.size())
return;
set.hero = heroes[elem];
}
else
{
set.hero = HeroTypeID::RANDOM;
2023-08-14 00:16:53 +02:00
}
2023-08-29 00:14:49 +02:00
2023-08-29 21:08:18 +02:00
if(doApply && unusableHeroes.count(heroes[elem]))
2023-08-29 00:14:49 +02:00
return;
if(set.hero != HeroTypeID::NONE)
2023-08-13 19:23:27 +02:00
{
2023-08-17 20:34:31 +02:00
if(!doApply)
2023-08-17 18:15:42 +02:00
{
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
if(settings["general"]["enableUiEnhancements"].Bool() && helper.playerSettings.hero.isValid() && helper.playerSettings.heroNameTextId.empty())
2023-09-17 00:32:10 +02:00
GH.windows().createAndPushWindow<CHeroOverview>(helper.playerSettings.hero);
else
GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
2023-08-17 18:15:42 +02:00
}
2023-08-18 00:15:47 +02:00
else
selectedHero = set.hero;
2023-08-13 19:23:27 +02:00
}
2023-08-12 15:05:26 +02:00
}
2023-08-13 19:23:27 +02:00
if(type == SelType::BONUS)
2023-08-12 15:05:26 +02:00
{
2023-08-13 19:23:27 +02:00
if(elem >= 4)
return;
set.bonus = static_cast<PlayerStartingBonus>(allowedBonus[elem]);
if(!doApply)
2023-08-13 19:23:27 +02:00
{
CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
2023-08-13 19:23:27 +02:00
}
else
selectedBonus = set.bonus;
2023-08-12 15:05:26 +02:00
}
2023-08-17 18:15:42 +02:00
2023-08-17 20:34:31 +02:00
if(doApply)
2023-08-17 18:15:42 +02:00
apply();
}
2024-06-29 16:14:16 +02:00
void OptionsTab::SelectionWindow::sliderMove(int slidPos)
{
if(!slider)
return; // ignore spurious call when slider is being created
2024-07-06 22:55:56 +02:00
recreate();
2024-06-29 16:14:16 +02:00
redraw();
}
bool OptionsTab::SelectionWindow::receiveEvent(const Point & position, int eventType) const
{
return true; // capture click also outside of window
}
2023-08-17 18:15:42 +02:00
void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
{
2024-06-29 16:14:16 +02:00
if(slider && slider->pos.isInside(cursorPosition))
return;
if(!pos.isInside(cursorPosition))
{
close();
return;
}
2023-08-17 18:15:42 +02:00
int elem = getElement(cursorPosition);
setElement(elem, true);
}
void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
{
2024-06-29 16:14:16 +02:00
if(!pos.isInside(cursorPosition) || (slider && slider->pos.isInside(cursorPosition)))
return;
2023-08-17 18:15:42 +02:00
int elem = getElement(cursorPosition);
setElement(elem, false);
2023-08-12 02:41:48 +02:00
}
2023-09-17 00:32:10 +02:00
OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSettings, SelType type)
2023-08-12 02:41:48 +02:00
: Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
2023-09-17 00:32:10 +02:00
, CPlayerSettingsHelper(playerSettings, type)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
2024-01-08 00:42:48 +02:00
subtitle = std::make_shared<CLabel>(24, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName(), 71);
pos = image->pos;
setPanningStep(pos.w);
}
void OptionsTab::SelectedBox::update()
{
image->setFrame(getImageIndex());
subtitle->setText(getName());
}
void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
{
// cases when we do not need to display a message
if(playerSettings.castle == FactionID::NONE && CPlayerSettingsHelper::selectionType == TOWN)
return;
if(playerSettings.hero == HeroTypeID::NONE && !SEL->getPlayerInfo(playerSettings.color).hasCustomMainHero() && CPlayerSettingsHelper::selectionType == HERO)
return;
if(settings["general"]["enableUiEnhancements"].Bool() && CPlayerSettingsHelper::selectionType == HERO && playerSettings.hero.isValid() && playerSettings.heroNameTextId.empty())
2023-09-17 00:32:10 +02:00
GH.windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
else
GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
}
2023-08-12 02:41:48 +02:00
void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
{
2023-10-21 19:10:02 +02:00
if(SEL->screenType != ESelectionScreen::newGame)
return;
PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
2023-09-17 00:32:10 +02:00
const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(playerSettings.color);
2023-08-13 19:23:27 +02:00
if(selectionType == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
2023-08-13 19:23:27 +02:00
return;
if(selectionType == SelType::HERO && ((pi.defaultHero() == HeroTypeID::NONE || !playerSettings.castle.isValid() || foreignPlayer)))
2023-08-13 19:23:27 +02:00
return;
if(selectionType == SelType::BONUS && foreignPlayer)
2023-08-13 19:23:27 +02:00
return;
GH.input().hapticFeedback();
GH.windows().createAndPushWindow<SelectionWindow>(playerSettings.color, selectionType);
2023-08-12 02:41:48 +02:00
}
void OptionsTab::SelectedBox::scrollBy(int distance)
{
// FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
// because of that, panning event gets interrupted (due to destruction of element)
// so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
distance = std::clamp(distance, -1, 1);
switch(CPlayerSettingsHelper::selectionType)
{
case TOWN:
2023-09-17 00:32:10 +02:00
CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
break;
case HERO:
2023-09-17 00:32:10 +02:00
CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
break;
case BONUS:
2023-09-17 00:32:10 +02:00
CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, playerSettings.color);
break;
}
setScrollingEnabled(false);
}
2021-11-08 18:49:04 +02:00
OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
2023-10-14 12:08:38 +02:00
: CIntObject(LCLICK | KEYBOARD | TEXTINPUT)
2023-10-13 23:04:35 +02:00
, pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
, s(std::make_unique<PlayerSettings>(S))
, parentTab(parent)
2023-10-14 13:34:39 +02:00
, name(S.name)
{
OBJ_CONSTRUCTION;
defActions |= SHARE_POS;
int serial = 0;
for(PlayerColor g = PlayerColor(0); g < s->color; ++g)
{
auto itred = SEL->getPlayerInfo(g);
if(itred.canComputerPlay || itred.canHumanPlay)
serial++;
}
pos.x += 54;
2023-08-23 17:29:50 +02:00
pos.y += 128 + serial * 50;
assert(CSH->mi && CSH->mi->mapHeader);
const PlayerInfo & p = SEL->getPlayerInfo(s->color);
assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
if(p.canHumanPlay && p.canComputerPlay)
whoCanPlay = HUMAN_OR_CPU;
else if(p.canComputerPlay)
whoCanPlay = CPU;
else
whoCanPlay = HUMAN;
2022-11-28 21:20:46 +02:00
static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
{{
"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
"AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
2022-11-28 21:20:46 +02:00
}};
static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
{{
"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
"ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
2022-11-28 21:20:46 +02:00
}};
background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
2023-10-14 13:34:39 +02:00
if(s->isControlledByAI() || CSH->isGuest())
2024-01-08 00:42:48 +02:00
labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name, 95);
2023-10-13 23:04:35 +02:00
else
{
2024-05-12 16:52:20 +02:00
labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, ETextAlignment::CENTER, false);
2023-10-14 13:34:39 +02:00
labelPlayerNameEdit->setText(name);
2023-10-13 23:04:35 +02:00
}
labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 23, 45, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]);
2024-07-16 03:23:54 +02:00
labelHandicap = std::make_shared<CMultiLineLabel>(Rect(56, 24, 49, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, s->handicap.startBonus.empty() && s->handicap.percentIncome.empty() ? CGI->generaltexth->arraytxt[210] : MetaString::createFromTextID("vcmi.lobby.handicap").toString());
2024-07-16 02:01:55 +02:00
handicap = std::make_shared<LRClickableArea>(Rect(56, 24, 49, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), [this](){
2024-07-16 02:20:49 +02:00
if(!CSH->isHost())
return;
2024-07-16 03:23:54 +02:00
TResources resourcesStart = TResources();
resourcesStart[EGameResID::GOLD] = 50000;
TResources resourcesPercent = TResources(); //reset 100 % to 0!!!
CSH->setPlayerHandicap(s->color, PlayerSettings::Handicap{resourcesStart, resourcesPercent});
2024-07-16 02:01:55 +02:00
}, [this](){
2024-07-16 03:23:54 +02:00
if(s->handicap.startBonus.empty() && s->handicap.percentIncome.empty())
2024-07-16 02:01:55 +02:00
CRClickPopup::createAndPush(MetaString::createFromTextID("core.help.124.help").toString());
else
{
auto str = MetaString::createFromTextID("vcmi.lobby.handicap");
str.appendRawString(":\n");
for(auto & res : EGameResID::ALL_RESOURCES())
2024-07-16 03:23:54 +02:00
if(s->handicap.startBonus[res] != 0 || s->handicap.percentIncome[res] != 0)
2024-07-16 02:01:55 +02:00
{
str.appendRawString("\n");
str.appendName(res);
str.appendRawString(": ");
2024-07-16 03:23:54 +02:00
str.appendRawString(std::to_string(s->handicap.startBonus[res]) + "|" + std::to_string(s->handicap.percentIncome[res] == 0 ? 100 : s->handicap.percentIncome[res]) + "%");
2024-07-16 02:01:55 +02:00
}
CRClickPopup::createAndPush(str.toString());
}
});
if(SEL->screenType == ESelectionScreen::newGame)
{
buttonTownLeft = std::make_shared<CButton>(Point(107, 5), AnimationPath::builtin("ADOPLFA.DEF"), CGI->generaltexth->zelp[132], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, -1, s->color));
buttonTownRight = std::make_shared<CButton>(Point(168, 5), AnimationPath::builtin("ADOPRTA.DEF"), CGI->generaltexth->zelp[133], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, +1, s->color));
buttonHeroLeft = std::make_shared<CButton>(Point(183, 5), AnimationPath::builtin("ADOPLFA.DEF"), CGI->generaltexth->zelp[148], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, -1, s->color));
buttonHeroRight = std::make_shared<CButton>(Point(244, 5), AnimationPath::builtin("ADOPRTA.DEF"), CGI->generaltexth->zelp[149], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, +1, s->color));
buttonBonusLeft = std::make_shared<CButton>(Point(259, 5), AnimationPath::builtin("ADOPLFA.DEF"), CGI->generaltexth->zelp[164], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, -1, s->color));
buttonBonusRight = std::make_shared<CButton>(Point(320, 5), AnimationPath::builtin("ADOPRTA.DEF"), CGI->generaltexth->zelp[165], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, +1, s->color));
}
hideUnavailableButtons();
if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color).canHumanPlay)
{
2021-11-08 18:49:04 +02:00
flag = std::make_shared<CButton>(
Point(-43, 2),
AnimationPath::builtin(flags[s->color.getNum()]),
2021-11-08 18:49:04 +02:00
CGI->generaltexth->zelp[180],
std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
2021-11-08 18:49:04 +02:00
);
flag->setHoverable(true);
flag->block(CSH->isGuest());
}
else
flag = nullptr;
town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
}
2023-10-13 23:04:35 +02:00
bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
{
return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
}
void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
{
2023-10-14 13:34:39 +02:00
if(labelPlayerNameEdit && key == EShortcut::GLOBAL_ACCEPT)
2023-10-16 21:28:44 +02:00
updateName();
2023-10-13 23:04:35 +02:00
}
2023-10-14 12:08:38 +02:00
bool OptionsTab::PlayerOptionsEntry::receiveEvent(const Point & position, int eventType) const
{
return eventType == AEventsReceiver::LCLICK; // capture all left clicks (not only within control)
}
void OptionsTab::PlayerOptionsEntry::clickReleased(const Point & cursorPosition)
{
2023-10-14 13:34:39 +02:00
if(labelPlayerNameEdit && !labelPlayerNameEdit->pos.isInside(cursorPosition))
2023-10-16 21:28:44 +02:00
updateName();
2023-10-14 12:08:38 +02:00
}
void OptionsTab::PlayerOptionsEntry::updateName() {
2023-10-14 13:34:39 +02:00
if(labelPlayerNameEdit->getText() != name)
2023-10-14 12:08:38 +02:00
{
CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
2023-10-14 13:34:39 +02:00
if(CSH->isMyColor(s->color))
{
Settings set = settings.write["general"]["playerName"];
set->String() = labelPlayerNameEdit->getText();
}
2023-10-14 12:08:38 +02:00
}
labelPlayerNameEdit->removeFocus();
2023-10-14 13:34:39 +02:00
name = labelPlayerNameEdit->getText();
2023-10-14 12:08:38 +02:00
}
2021-11-08 18:49:04 +02:00
void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
{
if(ps.isControlledByAI() || humanPlayers > 1)
2021-11-08 18:49:04 +02:00
CSH->setPlayer(ps.color);
}
void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
{
if(!buttonTownLeft)
return;
const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
{
buttonTownLeft->disable();
buttonTownRight->disable();
}
else
{
buttonTownLeft->enable();
buttonTownRight->enable();
}
if((pi->defaultHero() != HeroTypeID::RANDOM || !s->castle.isValid()) //fixed hero
|| foreignPlayer) //or not our player
{
buttonHeroLeft->disable();
buttonHeroRight->disable();
}
else
{
buttonHeroLeft->enable();
buttonHeroRight->enable();
}
if(foreignPlayer)
{
buttonBonusLeft->disable();
buttonBonusRight->disable();
}
else
{
buttonBonusLeft->enable();
buttonBonusRight->enable();
}
}