| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 | 
							- /*
 
-  * 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 "../CMusicHandler.h"
 
- #include "../gui/CGuiHandler.h"
 
- #include "../gui/Shortcut.h"
 
- #include "../gui/WindowHandler.h"
 
- #include "../render/Graphics.h"
 
- #include "../render/IFont.h"
 
- #include "../widgets/CComponent.h"
 
- #include "../widgets/ComboBox.h"
 
- #include "../widgets/Buttons.h"
 
- #include "../widgets/Images.h"
 
- #include "../widgets/MiscWidgets.h"
 
- #include "../widgets/ObjectLists.h"
 
- #include "../widgets/Slider.h"
 
- #include "../widgets/TextControls.h"
 
- #include "../windows/GUIClasses.h"
 
- #include "../windows/InfoWindows.h"
 
- #include "../windows/CHeroOverview.h"
 
- #include "../eventsSDL/InputHandler.h"
 
- #include "../../lib/filesystem/Filesystem.h"
 
- #include "../../lib/networkPacks/PacksForLobby.h"
 
- #include "../../lib/CGeneralTextHandler.h"
 
- #include "../../lib/CArtHandler.h"
 
- #include "../../lib/CTownHandler.h"
 
- #include "../../lib/CHeroHandler.h"
 
- #include "../../lib/mapping/CMapInfo.h"
 
- #include "../../lib/mapping/CMapHeader.h"
 
- OptionsTab::OptionsTab() : humanPlayers(0)
 
- {
 
- 	recActions = 0;
 
- 		
 
- 	addCallback("setTimerPreset", [&](int index){
 
- 		if(!variables["timerPresets"].isNull())
 
- 		{
 
- 			auto tpreset = variables["timerPresets"].Vector().at(index).Vector();
 
- 			TurnTimerInfo tinfo;
 
- 			tinfo.baseTimer = tpreset.at(0).Integer() * 1000;
 
- 			tinfo.turnTimer = tpreset.at(1).Integer() * 1000;
 
- 			tinfo.battleTimer = tpreset.at(2).Integer() * 1000;
 
- 			tinfo.creatureTimer = tpreset.at(3).Integer() * 1000;
 
- 			CSH->setTurnTimerInfo(tinfo);
 
- 		}
 
- 	});
 
- 	addCallback("setSimturnDuration", [&](int index){
 
- 		SimturnsInfo info;
 
- 		info.optionalTurns = index;
 
- 		CSH->setSimturnsInfo(info);
 
- 	});
 
- 	
 
- 	//helper function to parse string containing time to integer reflecting time in seconds
 
- 	//assumed that input string can be modified by user, function shall support user's intention
 
- 	// normal: 2:00, 12:30
 
- 	// adding symbol: 2:005 -> 2:05, 2:305 -> 23:05,
 
- 	// adding symbol (>60 seconds): 12:095 -> 129:05
 
- 	// removing symbol: 129:0 -> 12:09, 2:0 -> 0:20, 0:2 -> 0:02
 
- 	auto parseTimerString = [](const std::string & str) -> int
 
- 	{
 
- 		auto sc = str.find(":");
 
- 		if(sc == std::string::npos)
 
- 			return str.empty() ? 0 : std::stoi(str);
 
- 		
 
- 		auto l = str.substr(0, sc);
 
- 		auto r = str.substr(sc + 1, std::string::npos);
 
- 		if(r.length() == 3) //symbol added
 
- 		{
 
- 			l.push_back(r.front());
 
- 			r.erase(r.begin());
 
- 		}
 
- 		else if(r.length() == 1) //symbol removed
 
- 		{
 
- 			r.insert(r.begin(), l.back());
 
- 			l.pop_back();
 
- 		}
 
- 		else if(r.empty())
 
- 			r = "0";
 
- 		
 
- 		int sec = std::stoi(r);
 
- 		if(sec >= 60)
 
- 		{
 
- 			if(l.empty()) //9:00 -> 0:09
 
- 				return sec / 10;
 
- 			
 
- 			l.push_back(r.front()); //0:090 -> 9:00
 
- 			r.erase(r.begin());
 
- 		}
 
- 		else if(l.empty())
 
- 			return sec;
 
- 		
 
- 		return std::stoi(l) * 60 + std::stoi(r);
 
- 	};
 
- 	
 
- 	addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
 
- 		int time = parseTimerString(str) * 1000;
 
- 		if(time >= 0)
 
- 		{
 
- 			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
 
- 			tinfo.baseTimer = time;
 
- 			CSH->setTurnTimerInfo(tinfo);
 
- 		}
 
- 	});
 
- 	addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){
 
- 		int time = parseTimerString(str) * 1000;
 
- 		if(time >= 0)
 
- 		{
 
- 			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
 
- 			tinfo.turnTimer = time;
 
- 			CSH->setTurnTimerInfo(tinfo);
 
- 		}
 
- 	});
 
- 	addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){
 
- 		int time = parseTimerString(str) * 1000;
 
- 		if(time >= 0)
 
- 		{
 
- 			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
 
- 			tinfo.battleTimer = time;
 
- 			CSH->setTurnTimerInfo(tinfo);
 
- 		}
 
- 	});
 
- 	addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){
 
- 		int time = parseTimerString(str) * 1000;
 
- 		if(time >= 0)
 
- 		{
 
- 			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
 
- 			tinfo.creatureTimer = time;
 
- 			CSH->setTurnTimerInfo(tinfo);
 
- 		}
 
- 	});
 
- 	
 
- 	const JsonNode config(JsonPath::builtin("config/widgets/optionsTab.json"));
 
- 	build(config);
 
- 	
 
- 	//set timers combo box callbacks
 
- 	if(auto w = widget<ComboBox>("timerModeSwitch"))
 
- 	{
 
- 		w->onConstructItems = [&](std::vector<const void *> & curItems){
 
- 			if(variables["timers"].isNull())
 
- 				return;
 
- 			
 
- 			for(auto & p : variables["timers"].Vector())
 
- 			{
 
- 				curItems.push_back(&p);
 
- 			}
 
- 		};
 
- 		
 
- 		w->onSetItem = [&](const void * item){
 
- 			if(item)
 
- 			{
 
- 				if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
 
- 				{
 
- 					for(auto wname : (*tObj)["hideWidgets"].Vector())
 
- 					{
 
- 						if(auto w = widget<CIntObject>(wname.String()))
 
- 							w->setEnabled(false);
 
- 					}
 
- 					for(auto wname : (*tObj)["showWidgets"].Vector())
 
- 					{
 
- 						if(auto w = widget<CIntObject>(wname.String()))
 
- 							w->setEnabled(true);
 
- 					}
 
- 					if((*tObj)["default"].isVector())
 
- 					{
 
- 						TurnTimerInfo tinfo;
 
- 						tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
 
- 						tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
 
- 						tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
 
- 						tinfo.creatureTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
 
- 						CSH->setTurnTimerInfo(tinfo);
 
- 					}
 
- 				}
 
- 				redraw();
 
- 			}
 
- 		};
 
- 		
 
- 		w->getItemText = [this](int idx, const void * item){
 
- 			if(item)
 
- 			{
 
- 				if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
 
- 					return readText((*tObj)["text"]);
 
- 			}
 
- 			return std::string("");
 
- 		};
 
- 		
 
- 		w->setItem(0);
 
- 	}
 
- }
 
- void OptionsTab::recreate()
 
- {
 
- 	entries.clear();
 
- 	humanPlayers = 0;
 
- 	for (auto selectionWindow : GH.windows().findWindows<SelectionWindow>())
 
- 	{
 
- 		selectionWindow->reopen();
 
- 	}
 
- 	OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
 
- 	for(auto & pInfo : SEL->getStartInfo()->playerInfos)
 
- 	{
 
- 		if(pInfo.second.isControlledByHuman())
 
- 			humanPlayers++;
 
- 		entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
 
- 	}
 
- 	//Simultaneous turns
 
- 	if(auto turnSlider = widget<CSlider>("labelSimturnsDurationValue"))
 
- 		turnSlider->scrollTo(SEL->getStartInfo()->simturnsInfo.optionalTurns);
 
- 	if(auto w = widget<CLabel>("labelSimturnsDurationValue"))
 
- 	{
 
- 		MetaString message;
 
- 		message.appendRawString("Simturns: up to %d days");
 
- 		message.replaceNumber(SEL->getStartInfo()->simturnsInfo.optionalTurns);
 
- 		w->setText(message.toString());
 
- 	}
 
- 	
 
- 	const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
 
- 	
 
- 	//classic timer
 
- 	if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
 
- 	{
 
- 		if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.creatureTimer && !turnTimerRemote.baseTimer)
 
- 		{
 
- 			for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
 
- 			{
 
- 				auto & tpreset = variables["timerPresets"].Vector()[idx];
 
- 				if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
 
- 				{
 
- 					turnSlider->scrollTo(idx);
 
- 					if(auto w = widget<CLabel>("labelTurnDurationValue"))
 
- 						w->setText(CGI->generaltexth->turnDurations[idx]);
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- 	
 
- 	//chess timer
 
- 	auto timeToString = [](int time) -> std::string
 
- 	{
 
- 		std::stringstream ss;
 
- 		ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
 
- 		return ss.str();
 
- 	};
 
- 	
 
- 	if(auto ww = widget<CTextInput>("chessFieldBase"))
 
- 		ww->setText(timeToString(turnTimerRemote.baseTimer), false);
 
- 	if(auto ww = widget<CTextInput>("chessFieldTurn"))
 
- 		ww->setText(timeToString(turnTimerRemote.turnTimer), false);
 
- 	if(auto ww = widget<CTextInput>("chessFieldBattle"))
 
- 		ww->setText(timeToString(turnTimerRemote.battleTimer), false);
 
- 	if(auto ww = widget<CTextInput>("chessFieldCreature"))
 
- 		ww->setText(timeToString(turnTimerRemote.creatureTimer), false);
 
- 	
 
- 	if(auto w = widget<ComboBox>("timerModeSwitch"))
 
- 	{
 
- 		if(turnTimerRemote.battleTimer || turnTimerRemote.creatureTimer || turnTimerRemote.baseTimer)
 
- 		{
 
- 			if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
 
- 				if(turnSlider->isActive())
 
- 					w->setItem(1);
 
- 		}
 
- 	}
 
- }
 
- 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:
 
- 	{
 
- 		switch(playerSettings.bonus)
 
- 		{
 
- 		case PlayerStartingBonus::RANDOM:
 
- 			return RANDOM;
 
- 		case PlayerStartingBonus::ARTIFACT:
 
- 			return ARTIFACT;
 
- 		case PlayerStartingBonus::GOLD:
 
- 			return GOLD;
 
- 		case PlayerStartingBonus::RESOURCE:
 
- 		{
 
- 			switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
 
- 			{
 
- 			case EGameResID::WOOD_AND_ORE:
 
- 				return WOOD_ORE;
 
- 			case EGameResID::WOOD:
 
- 				return WOOD;
 
- 			case EGameResID::MERCURY:
 
- 				return MERCURY;
 
- 			case EGameResID::ORE:
 
- 				return ORE;
 
- 			case EGameResID::SULFUR:
 
- 				return SULFUR;
 
- 			case EGameResID::CRYSTAL:
 
- 				return CRYSTAL;
 
- 			case EGameResID::GEMS:
 
- 				return GEM;
 
- 			case EGameResID::GOLD:
 
- 				return GOLD;
 
- 			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];
 
- 			if(!playerSettings.heroNameTextId.empty())
 
- 				return 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:
 
- 	{
 
- 		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:
 
- 	{
 
- 		switch(playerSettings.bonus)
 
- 		{
 
- 		case PlayerStartingBonus::GOLD:
 
- 			return CGI->generaltexth->allTexts[87]; //500-1000
 
- 		case PlayerStartingBonus::RESOURCE:
 
- 		{
 
- 			switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
 
- 			{
 
- 			case EGameResID::MERCURY:
 
- 				return CGI->generaltexth->allTexts[694];
 
- 			case EGameResID::SULFUR:
 
- 				return CGI->generaltexth->allTexts[695];
 
- 			case EGameResID::CRYSTAL:
 
- 				return CGI->generaltexth->allTexts[692];
 
- 			case EGameResID::GEMS:
 
- 				return CGI->generaltexth->allTexts[693];
 
- 			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:
 
- 	{
 
- 		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:
 
- 		{
 
- 			switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
 
- 			{
 
- 			case EGameResID::MERCURY:
 
- 				return CGI->generaltexth->allTexts[690];
 
- 			case EGameResID::SULFUR:
 
- 				return CGI->generaltexth->allTexts[691];
 
- 			case EGameResID::CRYSTAL:
 
- 				return CGI->generaltexth->allTexts[688];
 
- 			case EGameResID::GEMS:
 
- 				return CGI->generaltexth->allTexts[689];
 
- 			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));
 
- }
 
- 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);
 
- }
 
- OptionsTab::SelectionWindow::SelectionWindow(PlayerColor _color, SelType _type)
 
- 	: CWindowObject(BORDERED)
 
- {
 
- 	addUsedEvents(LCLICK | SHOW_POPUP);
 
- 	color = _color;
 
- 	type = _type;
 
- 	initialFaction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
 
- 	initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
 
- 	initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
 
- 	selectedFaction = initialFaction;
 
- 	selectedHero = initialHero;
 
- 	selectedBonus = initialBonus;
 
- 	allowedFactions = SEL->getPlayerInfo(color).allowedFactions;
 
- 	std::vector<bool> allowedHeroesFlag = SEL->getMapInfo()->mapHeader->allowedHeroes;
 
- 	for(int i = 0; i < allowedHeroesFlag.size(); i++)
 
- 		if(allowedHeroesFlag[i])
 
- 			allowedHeroes.insert(HeroTypeID(i));
 
- 	for(auto & player : SEL->getStartInfo()->playerInfos)
 
- 	{
 
- 		if(player.first != color && (int)player.second.hero > HeroTypeID::RANDOM)
 
- 			unusableHeroes.insert(player.second.hero);
 
- 	}
 
- 	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);
 
- 	recreate();
 
- }
 
- int OptionsTab::SelectionWindow::calcLines(FactionID faction)
 
- {
 
- 	double additionalItems = 1; // random
 
- 	if(!faction.isValid())
 
- 		return std::ceil(((double)allowedFactions.size() + additionalItems) / elementsPerLine);
 
- 	int count = 0;
 
- 	for(auto & elemh : allowedHeroes)
 
- 	{
 
- 		CHero * type = VLC->heroh->objects[elemh];
 
- 		if(type->heroClass->faction == faction)
 
- 			count++;
 
- 	}
 
- 	return std::ceil(std::max((double)count + additionalItems, (double)allowedFactions.size() + additionalItems) / (double)elementsPerLine);
 
- }
 
- void OptionsTab::SelectionWindow::apply()
 
- {
 
- 	if(GH.windows().isTopWindow(this))
 
- 	{
 
- 		GH.input().hapticFeedback();
 
- 		CCS->soundh->playSound(soundBase::button);
 
- 		close();
 
- 		setSelection();
 
- 	}
 
- }
 
- void OptionsTab::SelectionWindow::setSelection()
 
- {
 
- 	if(selectedFaction != initialFaction)
 
- 		CSH->setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
 
- 	if(selectedHero != initialHero)
 
- 		CSH->setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
 
- 	if(selectedBonus != initialBonus)
 
- 		CSH->setPlayerOption(LobbyChangePlayerOption::BONUS_ID, static_cast<int>(selectedBonus), color);
 
- }
 
- void OptionsTab::SelectionWindow::reopen()
 
- {
 
- 	std::shared_ptr<SelectionWindow> window = std::shared_ptr<SelectionWindow>(new SelectionWindow(color, type));
 
- 	close();
 
- 	GH.windows().pushWindow(window);
 
- }
 
- void OptionsTab::SelectionWindow::recreate()
 
- {
 
- 	OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
 
- 	int amountLines = 1;
 
- 	if(type == SelType::BONUS)
 
- 		elementsPerLine = allowedBonus.size();
 
- 	else
 
- 	{
 
- 		// try to make squarish
 
- 		if(type == SelType::TOWN)
 
- 			elementsPerLine = floor(sqrt(allowedFactions.size()));
 
- 		if(type == SelType::HERO)
 
- 		{
 
- 			int count = 0;
 
- 			for(auto & elem : allowedHeroes)
 
- 			{
 
- 				CHero * type = VLC->heroh->objects[elem];
 
- 				if(type->heroClass->faction == selectedFaction)
 
- 				{
 
- 					count++;
 
- 				}
 
- 			}
 
- 			elementsPerLine = floor(sqrt(count));
 
- 		}
 
- 		amountLines = calcLines((type > SelType::TOWN) ? selectedFaction : FactionID::RANDOM);
 
- 	}
 
- 	int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
 
- 	int y = (amountLines) * (ICON_BIG_HEIGHT-1);
 
- 	pos = Rect(0, 0, x, y);
 
- 	backgroundTexture = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), pos);
 
- 	backgroundTexture->playerColored(PlayerColor(1));
 
- 	updateShadow();
 
- 	if(type == SelType::TOWN)
 
- 		genContentFactions();
 
- 	if(type == SelType::HERO)
 
- 		genContentHeroes();
 
- 	if(type == SelType::BONUS)
 
- 		genContentBonus();
 
- 	genContentGrid(amountLines);
 
- 	center();
 
- }
 
- void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
 
- {
 
- 	components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
 
- 	components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
 
- 	components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
 
- 	components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
 
- 	components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text));
 
- }
 
- void OptionsTab::SelectionWindow::genContentGrid(int lines)
 
- {
 
- 	for(int y = 0; y < lines; y++)
 
- 	{
 
- 		for(int x = 0; x < elementsPerLine; x++)
 
- 		{
 
- 			components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
 
- 		}
 
- 	}
 
- }
 
- void OptionsTab::SelectionWindow::genContentFactions()
 
- {
 
- 	int i = 1;
 
- 	// random
 
- 	PlayerSettings set = PlayerSettings();
 
- 	set.castle = FactionID::RANDOM;
 
- 	CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
 
- 	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)));
 
- 	for(auto & elem : allowedFactions)
 
- 	{
 
- 		int x = i % elementsPerLine;
 
- 		int y = i / elementsPerLine;
 
- 		PlayerSettings set = PlayerSettings();
 
- 		set.castle = elem;
 
- 		CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
 
- 		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());
 
- 		factions.push_back(elem);
 
- 		i++;
 
- 	}
 
- }
 
- void OptionsTab::SelectionWindow::genContentHeroes()
 
- {
 
- 	int i = 1;
 
- 	// random
 
- 	PlayerSettings set = PlayerSettings();
 
- 	set.hero = HeroTypeID::RANDOM;
 
- 	CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
 
- 	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)));
 
- 	for(auto & elem : allowedHeroes)
 
- 	{
 
- 		CHero * type = VLC->heroh->objects[elem];
 
- 		if(type->heroClass->faction == selectedFaction)
 
- 		{
 
- 			int x = i % elementsPerLine;
 
- 			int y = i / elementsPerLine;
 
- 			PlayerSettings set = PlayerSettings();
 
- 			set.hero = elem;
 
- 			CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
 
- 			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)));
 
- 			heroes.push_back(elem);
 
- 			i++;
 
- 		}
 
- 	}
 
- }
 
- void OptionsTab::SelectionWindow::genContentBonus()
 
- {
 
- 	PlayerSettings set = PlayerSettings();
 
- 	int i = 0;
 
- 	for(auto elem : allowedBonus)
 
- 	{
 
- 		int x = i;
 
- 		int y = 0;
 
- 		set.bonus = elem;
 
- 		CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
 
- 		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());
 
- 		if(selectedBonus == elem)
 
- 		{
 
- 			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)));
 
- 			drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
 
- 		}
 
- 		i++;
 
- 	}
 
- }
 
- int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
 
- {
 
- 	int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
 
- 	int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1);
 
- 	return x + y * elementsPerLine;
 
- }
 
- void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
 
- {
 
- 	PlayerSettings set = PlayerSettings();
 
- 	if(type == SelType::TOWN)
 
- 	{
 
- 		if(elem > 0)
 
- 		{
 
- 			elem--;
 
- 			if(elem >= factions.size())
 
- 				return;
 
- 			set.castle = factions[elem];
 
- 		}
 
- 		else
 
- 		{
 
- 			set.castle = FactionID::RANDOM;
 
- 		}
 
- 		if(set.castle != FactionID::NONE)
 
- 		{
 
- 			if(!doApply)
 
- 			{
 
- 				CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
 
- 				GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
 
- 			}
 
- 			else
 
- 				selectedFaction = set.castle;
 
- 		}
 
- 	}
 
- 	if(type == SelType::HERO)
 
- 	{
 
- 		if(elem > 0)
 
- 		{
 
- 			elem--;
 
- 			if(elem >= heroes.size())
 
- 				return;
 
- 			set.hero = heroes[elem];
 
- 		}
 
- 		else
 
- 		{
 
- 			set.hero = HeroTypeID::RANDOM;
 
- 		}
 
- 		if(doApply && unusableHeroes.count(heroes[elem]))
 
- 			return;
 
- 		if(set.hero != HeroTypeID::NONE)
 
- 		{
 
- 			if(!doApply)
 
- 			{
 
- 				CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
 
- 				if(settings["general"]["enableUiEnhancements"].Bool() && helper.playerSettings.hero.isValid() && helper.playerSettings.heroNameTextId.empty())
 
- 					GH.windows().createAndPushWindow<CHeroOverview>(helper.playerSettings.hero);
 
- 				else
 
- 					GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
 
- 			}
 
- 			else
 
- 				selectedHero = set.hero;
 
- 		}
 
- 	}
 
- 	if(type == SelType::BONUS)
 
- 	{
 
- 		if(elem >= 4)
 
- 			return;
 
- 		set.bonus = static_cast<PlayerStartingBonus>(allowedBonus[elem]);
 
- 		if(!doApply)
 
- 		{
 
- 			CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
 
- 			GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
 
- 		}
 
- 		else
 
- 			selectedBonus = set.bonus;
 
- 	}
 
- 	if(doApply)
 
- 		apply();
 
- }
 
- bool OptionsTab::SelectionWindow::receiveEvent(const Point & position, int eventType) const
 
- {
 
- 	return true;  // capture click also outside of window
 
- }
 
- void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
 
- {
 
- 	if(!pos.isInside(cursorPosition))
 
- 	{
 
- 		close();
 
- 		return;
 
- 	}
 
- 	int elem = getElement(cursorPosition);
 
- 	setElement(elem, true);
 
- }
 
- void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
 
- {
 
- 	if(!pos.isInside(cursorPosition))
 
- 		return;
 
- 	int elem = getElement(cursorPosition);
 
- 	setElement(elem, false);
 
- }
 
- OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSettings, SelType type)
 
- 	: Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
 
- 	, CPlayerSettingsHelper(playerSettings, type)
 
- {
 
- 	OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
 
- 	image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
 
- 	subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName());
 
- 	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())
 
- 		GH.windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
 
- 	else
 
- 		GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
 
- }
 
- void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
 
- {
 
- 	if(SEL->screenType != ESelectionScreen::newGame)
 
- 		return;
 
- 	
 
- 	PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
 
- 	const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(playerSettings.color);
 
- 	if(selectionType == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
 
- 		return;
 
- 	if(selectionType == SelType::HERO && ((pi.defaultHero() == HeroTypeID::NONE || !playerSettings.castle.isValid() || foreignPlayer)))
 
- 		return;
 
- 	if(selectionType == SelType::BONUS && foreignPlayer)
 
- 		return;
 
- 	GH.input().hapticFeedback();
 
- 	GH.windows().createAndPushWindow<SelectionWindow>(playerSettings.color, selectionType);
 
- }
 
- 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:
 
- 			CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
 
- 			break;
 
- 		case HERO:
 
- 			CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
 
- 			break;
 
- 		case BONUS:
 
- 			CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, playerSettings.color);
 
- 			break;
 
- 	}
 
- 	setScrollingEnabled(false);
 
- }
 
- OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
 
- 	: CIntObject(LCLICK | KEYBOARD | TEXTINPUT)
 
- 	, pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
 
- 	, s(std::make_unique<PlayerSettings>(S))
 
- 	, parentTab(parent)
 
- 	, 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;
 
- 	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;
 
- 	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"
 
- 	}};
 
- 	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"
 
- 	}};
 
- 	background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
 
- 	if(s->isControlledByAI() || CSH->isGuest())
 
- 		labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name);
 
- 	else
 
- 	{
 
- 		labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, false);
 
- 		labelPlayerNameEdit->setText(name);
 
- 	}
 
- 	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]);
 
- 	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)
 
- 	{
 
- 		flag = std::make_shared<CButton>(
 
- 			Point(-43, 2),
 
- 			AnimationPath::builtin(flags[s->color.getNum()]),
 
- 			CGI->generaltexth->zelp[180],
 
- 			std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
 
- 		);
 
- 		flag->hoverable = 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);
 
- }
 
- bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
 
- {
 
- 	return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
 
- }
 
- void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
 
- {
 
- 	if(labelPlayerNameEdit && key == EShortcut::GLOBAL_ACCEPT)
 
- 		updateName();
 
- }
 
- 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)
 
- {
 
- 	if(labelPlayerNameEdit && !labelPlayerNameEdit->pos.isInside(cursorPosition))
 
- 		updateName();
 
- }
 
- void OptionsTab::PlayerOptionsEntry::updateName() {
 
- 	if(labelPlayerNameEdit->getText() != name)
 
- 	{
 
- 		CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
 
- 		if(CSH->isMyColor(s->color))
 
- 		{
 
- 			Settings set = settings.write["general"]["playerName"];
 
- 			set->String() = labelPlayerNameEdit->getText();
 
- 		}
 
- 	}
 
- 	labelPlayerNameEdit->removeFocus();
 
- 	name = labelPlayerNameEdit->getText();
 
- }
 
- void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
 
- {
 
- 	if(ps.isControlledByAI() || humanPlayers > 0)
 
- 		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();
 
- 	}
 
- }
 
 
  |