CBonusSelection.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * CBonusSelection.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CBonusSelection.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include <vcmi/spells/Service.h>
  14. #include "CSelectionBase.h"
  15. #include "ExtraOptionsTab.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../CServerHandler.h"
  18. #include "../mainmenu/CMainMenu.h"
  19. #include "../mainmenu/CPrologEpilogVideo.h"
  20. #include "../media/IMusicPlayer.h"
  21. #include "../widgets/CComponent.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/MiscWidgets.h"
  24. #include "../widgets/ObjectLists.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../widgets/VideoWidget.h"
  27. #include "../windows/GUIClasses.h"
  28. #include "../windows/InfoWindows.h"
  29. #include "../render/IImage.h"
  30. #include "../render/IRenderHandler.h"
  31. #include "../render/CAnimation.h"
  32. #include "../render/Graphics.h"
  33. #include "../GameEngine.h"
  34. #include "../gui/Shortcut.h"
  35. #include "../gui/WindowHandler.h"
  36. #include "../adventureMap/AdventureMapInterface.h"
  37. #include "../../lib/CConfigHandler.h"
  38. #include "../../lib/CCreatureHandler.h"
  39. #include "../../lib/CSkillHandler.h"
  40. #include "../../lib/StartInfo.h"
  41. #include "../../lib/entities/building/CBuilding.h"
  42. #include "../../lib/entities/building/CBuildingHandler.h"
  43. #include "../../lib/entities/faction/CFaction.h"
  44. #include "../../lib/entities/faction/CTown.h"
  45. #include "../../lib/entities/faction/CTownHandler.h"
  46. #include "../../lib/entities/hero/CHeroHandler.h"
  47. #include "../../lib/filesystem/Filesystem.h"
  48. #include "../../lib/texts/CGeneralTextHandler.h"
  49. #include "../../lib/campaign/CampaignState.h"
  50. #include "../../lib/mapping/CMapService.h"
  51. #include "../../lib/mapping/CMapInfo.h"
  52. #include "../../lib/mapping/CMapHeader.h"
  53. #include "../../lib/mapObjects/CGHeroInstance.h"
  54. std::shared_ptr<CampaignState> CBonusSelection::getCampaign()
  55. {
  56. return CSH->si->campState;
  57. }
  58. CBonusSelection::CBonusSelection()
  59. : CWindowObject(BORDERED)
  60. {
  61. OBJECT_CONSTRUCTION;
  62. setBackground(getCampaign()->getRegions().getBackgroundName());
  63. panelBackground = std::make_shared<CPicture>(ImagePath::builtin("CAMPBRF.BMP"), 456, 6);
  64. const auto & playVideo = [this]()
  65. {
  66. ENGINE->windows().createAndPushWindow<CPrologEpilogVideo>(
  67. getCampaign()->scenario(CSH->campaignMap).prolog,
  68. [this]() { redraw(); } );
  69. };
  70. buttonStart = std::make_shared<CButton>(
  71. Point(475, 536), AnimationPath::builtin("CBBEGIB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::startMap, this), EShortcut::GLOBAL_ACCEPT
  72. );
  73. buttonRestart = std::make_shared<CButton>(Point(475, 536), AnimationPath::builtin("CBRESTB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::restartMap, this), EShortcut::GLOBAL_ACCEPT);
  74. buttonVideo = std::make_shared<CButton>(Point(705, 214), AnimationPath::builtin("CBVIDEB.DEF"), CButton::tooltip(), playVideo, EShortcut::LOBBY_REPLAY_VIDEO);
  75. buttonBack = std::make_shared<CButton>(Point(624, 536), AnimationPath::builtin("CBCANCB.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::goBack, this), EShortcut::GLOBAL_CANCEL);
  76. campaignName = std::make_shared<CLabel>(481, 28, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->si->getCampaignName(), 250);
  77. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 735, 26);
  78. labelCampaignDescription = std::make_shared<CLabel>(481, 63, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, VLC->generaltexth->allTexts[38]);
  79. campaignDescription = std::make_shared<CTextBox>(getCampaign()->getDescriptionTranslated(), Rect(480, 86, 286, 117), 1);
  80. bool videoButtonActive = CSH->getState() == EClientState::GAMEPLAY;
  81. int availableSpace = videoButtonActive ? 225 : 285;
  82. mapName = std::make_shared<CLabel>(481, 219, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->mi->getNameTranslated(), availableSpace );
  83. labelMapDescription = std::make_shared<CLabel>(481, 253, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, VLC->generaltexth->allTexts[496]);
  84. mapDescription = std::make_shared<CTextBox>("", Rect(480, 278, 286, 108), 1);
  85. labelChooseBonus = std::make_shared<CLabel>(475, 432, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, VLC->generaltexth->allTexts[71]);
  86. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
  87. flagbox = std::make_shared<CFlagBox>(Rect(486, 407, 335, 23));
  88. std::vector<std::string> difficulty;
  89. std::string difficultyString = VLC->generaltexth->allTexts[492];
  90. boost::split(difficulty, difficultyString, boost::is_any_of(" "));
  91. labelDifficulty = std::make_shared<CLabel>(724, settings["general"]["enableUiEnhancements"].Bool() ? 457 : 432, FONT_MEDIUM, ETextAlignment::TOPCENTER, Colors::WHITE, difficulty.back());
  92. for(size_t b = 0; b < difficultyIcons.size(); ++b)
  93. {
  94. difficultyIcons[b] = std::make_shared<CAnimImage>(AnimationPath::builtinTODO("GSPBUT" + std::to_string(b + 3) + ".DEF"), 0, 0, 709, settings["general"]["enableUiEnhancements"].Bool() ? 480 : 455);
  95. difficultyIconAreas[b] = std::make_shared<LRClickableArea>(difficultyIcons[b]->pos - pos.topLeft(), nullptr, [b]() { CRClickPopup::createAndPush(VLC->generaltexth->zelp[24 + b].second); });
  96. }
  97. if(getCampaign()->playerSelectedDifficulty())
  98. {
  99. Point posLeft = settings["general"]["enableUiEnhancements"].Bool() ? Point(693, 495) : Point(694, 508);
  100. Point posRight = settings["general"]["enableUiEnhancements"].Bool() ? Point(739, 495) : Point(738, 508);
  101. buttonDifficultyLeft = std::make_shared<CButton>(posLeft, AnimationPath::builtin("SCNRBLF.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::decreaseDifficulty, this), EShortcut::MOVE_LEFT);
  102. buttonDifficultyRight = std::make_shared<CButton>(posRight, AnimationPath::builtin("SCNRBRT.DEF"), CButton::tooltip(), std::bind(&CBonusSelection::increaseDifficulty, this), EShortcut::MOVE_RIGHT);
  103. }
  104. for(auto scenarioID : getCampaign()->allScenarios())
  105. {
  106. if(getCampaign()->isAvailable(scenarioID))
  107. regions.push_back(std::make_shared<CRegion>(scenarioID, true, true, false, getCampaign()->getRegions()));
  108. else if(getCampaign()->isConquered(scenarioID)) //display as striped
  109. regions.push_back(std::make_shared<CRegion>(scenarioID, false, false, false, getCampaign()->getRegions()));
  110. else
  111. regions.push_back(std::make_shared<CRegion>(scenarioID, false, false, true, getCampaign()->getRegions()));
  112. }
  113. if (!getCampaign()->getMusic().empty())
  114. ENGINE->music().playMusic( getCampaign()->getMusic(), true, false);
  115. if(CSH->getState() != EClientState::GAMEPLAY && settings["general"]["enableUiEnhancements"].Bool())
  116. {
  117. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  118. tabExtraOptions->recActions = UPDATE | SHOWALL | LCLICK | RCLICK_POPUP;
  119. tabExtraOptions->recreate(true);
  120. tabExtraOptions->setEnabled(false);
  121. buttonExtraOptions = std::make_shared<CButton>(Point(643, 431), AnimationPath::builtin("GSPBUT2.DEF"), VLC->generaltexth->zelp[46], [this]{ tabExtraOptions->setEnabled(!tabExtraOptions->isActive()); ENGINE->windows().totalRedraw(); }, EShortcut::LOBBY_EXTRA_OPTIONS);
  122. buttonExtraOptions->setTextOverlay(VLC->generaltexth->translate("vcmi.optionsTab.extraOptions.hover"), FONT_SMALL, Colors::WHITE);
  123. }
  124. }
  125. void CBonusSelection::createBonusesIcons()
  126. {
  127. OBJECT_CONSTRUCTION;
  128. const CampaignScenario & scenario = getCampaign()->scenario(CSH->campaignMap);
  129. const std::vector<CampaignBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  130. groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
  131. constexpr std::array bonusPics =
  132. {
  133. "SPELLBON.DEF", // Spell
  134. "TWCRPORT.DEF", // Monster
  135. "", // Building - BO*.BMP
  136. "ARTIFBON.DEF", // Artifact
  137. "SPELLBON.DEF", // Spell scroll
  138. "PSKILBON.DEF", // Primary skill
  139. "SSKILBON.DEF", // Secondary skill
  140. "BORES.DEF", // Resource
  141. "PORTRAITSLARGE", // Hero HPL*.BMP
  142. "PORTRAITSLARGE"
  143. // Player - CREST58.DEF
  144. };
  145. for(int i = 0; i < bonDescs.size(); i++)
  146. {
  147. int bonusType = static_cast<size_t>(bonDescs[i].type);
  148. std::string picName = bonusPics[bonusType];
  149. size_t picNumber = bonDescs[i].info2;
  150. MetaString desc;
  151. switch(bonDescs[i].type)
  152. {
  153. case CampaignBonusType::SPELL:
  154. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  155. desc.replaceName(SpellID(bonDescs[i].info2));
  156. break;
  157. case CampaignBonusType::MONSTER:
  158. picNumber = bonDescs[i].info2 + 2;
  159. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  160. desc.replaceNumber(bonDescs[i].info3);
  161. desc.replaceNamePlural(bonDescs[i].info2);
  162. break;
  163. case CampaignBonusType::BUILDING:
  164. {
  165. FactionID faction;
  166. for(auto & elem : CSH->si->playerInfos)
  167. {
  168. if(elem.second.isControlledByHuman())
  169. {
  170. faction = elem.second.castle;
  171. break;
  172. }
  173. }
  174. assert(faction != -1);
  175. BuildingID buildID;
  176. if(getCampaign()->formatVCMI())
  177. buildID = BuildingID(bonDescs[i].info1);
  178. else
  179. buildID = CBuildingHandler::campToERMU(bonDescs[i].info1, faction, std::set<BuildingID>());
  180. picName = graphics->ERMUtoPicture[faction][buildID];
  181. picNumber = -1;
  182. if(vstd::contains((*VLC->townh)[faction]->town->buildings, buildID))
  183. desc.appendTextID((*VLC->townh)[faction]->town->buildings.find(buildID)->second->getNameTextID());
  184. break;
  185. }
  186. case CampaignBonusType::ARTIFACT:
  187. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  188. desc.replaceName(ArtifactID(bonDescs[i].info2));
  189. break;
  190. case CampaignBonusType::SPELL_SCROLL:
  191. desc.appendLocalString(EMetaText::GENERAL_TXT, 716);
  192. desc.replaceName(SpellID(bonDescs[i].info2));
  193. break;
  194. case CampaignBonusType::PRIMARY_SKILL:
  195. {
  196. int leadingSkill = -1;
  197. std::vector<std::pair<int, int>> toPrint; //primary skills to be listed <num, val>
  198. const ui8 * ptr = reinterpret_cast<const ui8 *>(&bonDescs[i].info2);
  199. for(int g = 0; g < GameConstants::PRIMARY_SKILLS; ++g)
  200. {
  201. if(leadingSkill == -1 || ptr[g] > ptr[leadingSkill])
  202. {
  203. leadingSkill = g;
  204. }
  205. if(ptr[g] != 0)
  206. {
  207. toPrint.push_back(std::make_pair(g, ptr[g]));
  208. }
  209. }
  210. picNumber = leadingSkill;
  211. desc.appendLocalString(EMetaText::GENERAL_TXT, 715);
  212. std::string substitute; //text to be printed instead of %s
  213. for(int v = 0; v < toPrint.size(); ++v)
  214. {
  215. substitute += std::to_string(toPrint[v].second);
  216. substitute += " " + VLC->generaltexth->primarySkillNames[toPrint[v].first];
  217. if(v != toPrint.size() - 1)
  218. {
  219. substitute += ", ";
  220. }
  221. }
  222. desc.replaceRawString(substitute);
  223. break;
  224. }
  225. case CampaignBonusType::SECONDARY_SKILL:
  226. desc.appendLocalString(EMetaText::GENERAL_TXT, 718);
  227. desc.replaceTextID(TextIdentifier("core", "skilllev", bonDescs[i].info3 - 1).get());
  228. desc.replaceName(SecondarySkill(bonDescs[i].info2));
  229. picNumber = bonDescs[i].info2 * 3 + bonDescs[i].info3 - 1;
  230. break;
  231. case CampaignBonusType::RESOURCE:
  232. {
  233. desc.appendLocalString(EMetaText::GENERAL_TXT, 717);
  234. switch(bonDescs[i].info1)
  235. {
  236. case 0xFD: //wood + ore
  237. {
  238. desc.replaceLocalString(EMetaText::GENERAL_TXT, 721);
  239. picNumber = 7;
  240. break;
  241. }
  242. case 0xFE: //wood + ore
  243. {
  244. desc.replaceLocalString(EMetaText::GENERAL_TXT, 722);
  245. picNumber = 8;
  246. break;
  247. }
  248. default:
  249. {
  250. desc.replaceName(GameResID(bonDescs[i].info1));
  251. picNumber = bonDescs[i].info1;
  252. }
  253. }
  254. desc.replaceNumber(bonDescs[i].info2);
  255. break;
  256. }
  257. case CampaignBonusType::HEROES_FROM_PREVIOUS_SCENARIO:
  258. {
  259. auto superhero = getCampaign()->strongestHero(static_cast<CampaignScenarioID>(bonDescs[i].info2), PlayerColor(bonDescs[i].info1));
  260. if(!superhero)
  261. logGlobal->warn("No superhero! How could it be transferred?");
  262. picNumber = superhero ? superhero->getIconIndex() : 0;
  263. desc.appendLocalString(EMetaText::GENERAL_TXT, 719);
  264. desc.replaceRawString(getCampaign()->scenario(static_cast<CampaignScenarioID>(bonDescs[i].info2)).scenarioName.toString());
  265. break;
  266. }
  267. case CampaignBonusType::HERO:
  268. if(bonDescs[i].info2 == 0xFFFF)
  269. {
  270. desc.appendLocalString(EMetaText::GENERAL_TXT, 720); // Start with random hero
  271. picNumber = -1;
  272. picName = "CBONN1A3.BMP";
  273. }
  274. else
  275. {
  276. desc.appendLocalString(EMetaText::GENERAL_TXT, 715); // Start with %s
  277. desc.replaceTextID(VLC->heroh->objects[bonDescs[i].info2]->getNameTextID());
  278. }
  279. break;
  280. }
  281. std::shared_ptr<CToggleButton> bonusButton = std::make_shared<CToggleButton>(Point(475 + i * 68, 455), AnimationPath::builtin("campaignBonusSelection"), CButton::tooltip(desc.toString(), desc.toString()), nullptr, EShortcut::NONE, false, [this](){
  282. if(buttonStart->isActive() && !buttonStart->isBlocked())
  283. CBonusSelection::startMap();
  284. });
  285. if(picNumber != -1)
  286. bonusButton->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin(picName), picNumber));
  287. else
  288. bonusButton->setOverlay(std::make_shared<CPicture>(ImagePath::builtin(picName)));
  289. if(CSH->campaignBonus == i)
  290. bonusButton->setBorderColor(Colors::BRIGHT_YELLOW);
  291. groupBonuses->addToggle(i, bonusButton);
  292. }
  293. if(getCampaign()->getBonusID(CSH->campaignMap))
  294. groupBonuses->setSelected(*getCampaign()->getBonusID(CSH->campaignMap));
  295. }
  296. void CBonusSelection::updateAfterStateChange()
  297. {
  298. if(CSH->getState() != EClientState::GAMEPLAY)
  299. {
  300. buttonRestart->disable();
  301. buttonVideo->disable();
  302. buttonStart->enable();
  303. buttonBack->block(!getCampaign()->conqueredScenarios().empty());
  304. }
  305. else
  306. {
  307. buttonStart->disable();
  308. buttonRestart->enable();
  309. buttonVideo->enable();
  310. buttonBack->block(false);
  311. if(buttonDifficultyLeft)
  312. buttonDifficultyLeft->disable();
  313. if(buttonDifficultyRight)
  314. buttonDifficultyRight->disable();
  315. }
  316. if(CSH->campaignBonus == -1)
  317. {
  318. buttonStart->block(getCampaign()->scenario(CSH->campaignMap).travelOptions.bonusesToChoose.size());
  319. }
  320. else
  321. {
  322. buttonStart->block(false);
  323. }
  324. for(auto region : regions)
  325. region->updateState();
  326. if(!CSH->mi)
  327. return;
  328. iconsMapSizes->setFrame(CSH->mi->getMapSizeIconId());
  329. mapName->setText(CSH->mi->getNameTranslated());
  330. mapDescription->setText(CSH->mi->getDescriptionTranslated());
  331. for(size_t i = 0; i < difficultyIcons.size(); i++)
  332. {
  333. if(i == CSH->si->difficulty)
  334. {
  335. difficultyIcons[i]->enable();
  336. difficultyIconAreas[i]->enable();
  337. }
  338. else
  339. {
  340. difficultyIcons[i]->disable();
  341. difficultyIconAreas[i]->disable();
  342. }
  343. }
  344. flagbox->recreate();
  345. createBonusesIcons();
  346. }
  347. void CBonusSelection::goBack()
  348. {
  349. if(CSH->getState() != EClientState::GAMEPLAY)
  350. {
  351. ENGINE->windows().popWindows(2);
  352. CMM->playMusic();
  353. }
  354. else
  355. {
  356. close();
  357. if(adventureInt)
  358. adventureInt->onAudioResumed();
  359. }
  360. // TODO: we can actually only pop bonus selection interface for custom campaigns
  361. // Though this would require clearing CLobbyScreen::bonusSel pointer when poping this interface
  362. /*
  363. else
  364. {
  365. close();
  366. CSH->state = EClientState::LOBBY;
  367. }
  368. */
  369. }
  370. void CBonusSelection::startMap()
  371. {
  372. if (!CSH->validateGameStart())
  373. return;
  374. auto showPrologVideo = [=]()
  375. {
  376. auto exitCb = [=]()
  377. {
  378. logGlobal->info("Starting scenario %d", static_cast<int>(CSH->campaignMap));
  379. CSH->sendStartGame();
  380. };
  381. const CampaignScenario & scenario = getCampaign()->scenario(CSH->campaignMap);
  382. if(scenario.prolog.hasPrologEpilog)
  383. {
  384. ENGINE->windows().createAndPushWindow<CPrologEpilogVideo>(scenario.prolog, exitCb);
  385. }
  386. else
  387. {
  388. exitCb();
  389. }
  390. };
  391. //block buttons immediately
  392. buttonStart->block(true);
  393. buttonRestart->block(true);
  394. buttonVideo->block(true);
  395. buttonBack->block(true);
  396. if(LOCPLINT) // we're currently ingame, so ask for starting new map and end game
  397. {
  398. close();
  399. LOCPLINT->showYesNoDialog(VLC->generaltexth->allTexts[67], [=]()
  400. {
  401. showPrologVideo();
  402. }, 0);
  403. }
  404. else
  405. {
  406. showPrologVideo();
  407. }
  408. }
  409. void CBonusSelection::restartMap()
  410. {
  411. close();
  412. LOCPLINT->showYesNoDialog(
  413. VLC->generaltexth->allTexts[67],
  414. [=]()
  415. {
  416. ENGINE->dispatchMainThread(
  417. []()
  418. {
  419. CSH->sendRestartGame();
  420. }
  421. );
  422. },
  423. 0
  424. );
  425. }
  426. void CBonusSelection::increaseDifficulty()
  427. {
  428. CSH->setDifficulty(CSH->si->difficulty + 1);
  429. }
  430. void CBonusSelection::decreaseDifficulty()
  431. {
  432. // avoid negative overflow (0 - 1 = 255)
  433. if (CSH->si->difficulty > 0)
  434. CSH->setDifficulty(CSH->si->difficulty - 1);
  435. }
  436. CBonusSelection::CRegion::CRegion(CampaignScenarioID id, bool accessible, bool selectable, bool labelOnly, const CampaignRegions & campDsc)
  437. : CIntObject(LCLICK | SHOW_POPUP | TIME), idOfMapAndRegion(id), accessible(accessible), selectable(selectable), labelOnly(labelOnly), blinkAnim({})
  438. {
  439. OBJECT_CONSTRUCTION;
  440. pos += campDsc.getPosition(id);
  441. auto color = CSH->si->campState->scenario(idOfMapAndRegion).regionColor;
  442. graphicsNotSelected = std::make_shared<CPicture>(campDsc.getAvailableName(id, color));
  443. graphicsNotSelected->disable();
  444. graphicsSelected = std::make_shared<CPicture>(campDsc.getSelectedName(id, color));
  445. graphicsSelected->disable();
  446. graphicsStriped = std::make_shared<CPicture>(campDsc.getConqueredName(id, color));
  447. graphicsStriped->disable();
  448. pos.w = graphicsNotSelected->pos.w;
  449. pos.h = graphicsNotSelected->pos.h;
  450. auto labelPos = campDsc.getLabelPosition(id);
  451. if(labelPos)
  452. {
  453. auto mapHeader = CSH->si->campState->getMapHeader(idOfMapAndRegion);
  454. label = std::make_shared<CLabel>((*labelPos).x, (*labelPos).y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, mapHeader->name.toString());
  455. }
  456. }
  457. void CBonusSelection::CRegion::updateState(bool disableAll)
  458. {
  459. if(labelOnly)
  460. return;
  461. if(disableAll)
  462. {
  463. graphicsNotSelected->disable();
  464. graphicsSelected->disable();
  465. graphicsStriped->disable();
  466. }
  467. else if(!accessible)
  468. {
  469. graphicsNotSelected->disable();
  470. graphicsSelected->disable();
  471. graphicsStriped->enable();
  472. }
  473. else if(CSH->campaignMap == idOfMapAndRegion)
  474. {
  475. graphicsNotSelected->disable();
  476. graphicsSelected->enable();
  477. graphicsStriped->disable();
  478. }
  479. else
  480. {
  481. graphicsNotSelected->enable();
  482. graphicsSelected->disable();
  483. graphicsStriped->disable();
  484. }
  485. }
  486. void CBonusSelection::CRegion::tick(uint32_t msPassed)
  487. {
  488. if(!accessible)
  489. {
  490. removeUsedEvents(TIME);
  491. return;
  492. }
  493. blinkAnim.msPassed += msPassed;
  494. if(blinkAnim.msPassed >= 150)
  495. {
  496. blinkAnim.state = !blinkAnim.state;
  497. blinkAnim.msPassed -= 150;
  498. if(blinkAnim.state)
  499. blinkAnim.count++;
  500. else if(blinkAnim.count >= 3)
  501. removeUsedEvents(TIME);
  502. }
  503. updateState(blinkAnim.state);
  504. setRedrawParent(true);
  505. redraw();
  506. }
  507. void CBonusSelection::CRegion::clickReleased(const Point & cursorPosition)
  508. {
  509. if(!labelOnly && selectable && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()))
  510. {
  511. CSH->setCampaignMap(idOfMapAndRegion);
  512. }
  513. }
  514. void CBonusSelection::CRegion::showPopupWindow(const Point & cursorPosition)
  515. {
  516. // FIXME: For some reason "down" is only ever contain indeterminate_value
  517. auto & text = CSH->si->campState->scenario(idOfMapAndRegion).regionText;
  518. if(!labelOnly && !graphicsNotSelected->getSurface()->isTransparent(cursorPosition - pos.topLeft()) && !text.empty())
  519. {
  520. CRClickPopup::createAndPush(text.toString());
  521. }
  522. }