CInfoBar.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * CInfoBar.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 "CInfoBar.h"
  12. #include "AdventureMapInterface.h"
  13. #include "../widgets/CComponent.h"
  14. #include "../widgets/Images.h"
  15. #include "../windows/CMessage.h"
  16. #include "../widgets/TextControls.h"
  17. #include "../widgets/MiscWidgets.h"
  18. #include "../windows/InfoWindows.h"
  19. #include "../CGameInfo.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../PlayerLocalState.h"
  22. #include "../gui/CGuiHandler.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../media/ISoundPlayer.h"
  25. #include "../render/IScreenHandler.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include "../../lib/mapObjects/CGHeroInstance.h"
  30. #include "../../lib/mapObjects/CGTownInstance.h"
  31. CInfoBar::CVisibleInfo::CVisibleInfo()
  32. : CIntObject(0, Point(offset_x, offset_y))
  33. {
  34. }
  35. void CInfoBar::CVisibleInfo::show(Canvas & to)
  36. {
  37. CIntObject::show(to);
  38. for(auto object : forceRefresh)
  39. object->showAll(to);
  40. }
  41. CInfoBar::EmptyVisibleInfo::EmptyVisibleInfo()
  42. {
  43. }
  44. CInfoBar::VisibleHeroInfo::VisibleHeroInfo(const CGHeroInstance * hero)
  45. {
  46. OBJECT_CONSTRUCTION;
  47. background = std::make_shared<CPicture>(ImagePath::builtin("ADSTATHR"));
  48. if(settings["gameTweaks"]["infoBarCreatureManagement"].Bool())
  49. heroTooltip = std::make_shared<CInteractableHeroTooltip>(Point(0,0), hero);
  50. else
  51. heroTooltip = std::make_shared<CHeroTooltip>(Point(0,0), hero);
  52. }
  53. CInfoBar::VisibleTownInfo::VisibleTownInfo(const CGTownInstance * town)
  54. {
  55. OBJECT_CONSTRUCTION;
  56. background = std::make_shared<CPicture>(ImagePath::builtin("ADSTATCS"));
  57. if(settings["gameTweaks"]["infoBarCreatureManagement"].Bool())
  58. townTooltip = std::make_shared<CInteractableTownTooltip>(Point(0,0), town);
  59. else
  60. townTooltip = std::make_shared<CTownTooltip>(Point(0,0), town);
  61. }
  62. CInfoBar::VisibleDateInfo::VisibleDateInfo()
  63. {
  64. OBJECT_CONSTRUCTION;
  65. animation = std::make_shared<CShowableAnim>(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE, 180);// H3 uses around 175-180 ms per frame
  66. animation->setDuration(1500);
  67. std::string labelText;
  68. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) == 1 && LOCPLINT->cb->getDate(Date::DAY) != 1) // monday of any week but first - show new week info
  69. labelText = CGI->generaltexth->allTexts[63] + " " + std::to_string(LOCPLINT->cb->getDate(Date::WEEK));
  70. else
  71. labelText = CGI->generaltexth->allTexts[64] + " " + std::to_string(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  72. label = std::make_shared<CLabel>(95, 31, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, labelText);
  73. forceRefresh.push_back(label);
  74. }
  75. AnimationPath CInfoBar::VisibleDateInfo::getNewDayName()
  76. {
  77. if(LOCPLINT->cb->getDate(Date::DAY) == 1)
  78. return AnimationPath::builtin("NEWDAY");
  79. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1)
  80. return AnimationPath::builtin("NEWDAY");
  81. switch(LOCPLINT->cb->getDate(Date::WEEK))
  82. {
  83. case 1:
  84. return AnimationPath::builtin("NEWWEEK1");
  85. case 2:
  86. return AnimationPath::builtin("NEWWEEK2");
  87. case 3:
  88. return AnimationPath::builtin("NEWWEEK3");
  89. case 4:
  90. return AnimationPath::builtin("NEWWEEK4");
  91. default:
  92. return AnimationPath();
  93. }
  94. }
  95. CInfoBar::VisibleEnemyTurnInfo::VisibleEnemyTurnInfo(PlayerColor player)
  96. {
  97. OBJECT_CONSTRUCTION;
  98. background = std::make_shared<CPicture>(ImagePath::builtin("ADSTATNX"));
  99. banner = std::make_shared<CAnimImage>(AnimationPath::builtin("CREST58"), player.getNum(), 0, 20, 51);
  100. sand = std::make_shared<CShowableAnim>(99, 51, AnimationPath::builtin("HOURSAND"), 0, 100); // H3 uses around 100 ms per frame
  101. glass = std::make_shared<CShowableAnim>(99, 51, AnimationPath::builtin("HOURGLAS"), CShowableAnim::PLAY_ONCE, 1000); // H3 scales this nicely for AI turn duration, don't have anything like that in vcmi
  102. }
  103. CInfoBar::VisibleGameStatusInfo::VisibleGameStatusInfo()
  104. {
  105. OBJECT_CONSTRUCTION;
  106. //get amount of halls of each level
  107. std::vector<int> halls(4, 0);
  108. for(auto town : LOCPLINT->localState->getOwnedTowns())
  109. {
  110. int hallLevel = town->hallLevel();
  111. //negative value means no village hall, unlikely but possible
  112. if(hallLevel >= 0)
  113. halls.at(hallLevel)++;
  114. }
  115. std::vector<PlayerColor> allies;
  116. std::vector<PlayerColor> enemies;
  117. //generate list of allies and enemies
  118. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  119. {
  120. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  121. {
  122. if(LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  123. allies.push_back(PlayerColor(i));
  124. else
  125. enemies.push_back(PlayerColor(i));
  126. }
  127. }
  128. //generate widgets
  129. background = std::make_shared<CPicture>(ImagePath::builtin("ADSTATIN"));
  130. allyLabel = std::make_shared<CLabel>(10, 106, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  131. enemyLabel = std::make_shared<CLabel>(10, 136, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  132. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  133. for(PlayerColor & player : allies)
  134. {
  135. auto image = std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), player.getNum(), 0, posx, 102);
  136. posx += image->pos.w;
  137. flags.push_back(image);
  138. }
  139. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  140. for(PlayerColor & player : enemies)
  141. {
  142. auto image = std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), player.getNum(), 0, posx, 132);
  143. posx += image->pos.w;
  144. flags.push_back(image);
  145. }
  146. for(size_t i=0; i<halls.size(); i++)
  147. {
  148. hallIcons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("itmtl"), i, 0, 6 + 42 * (int)i , 11));
  149. if(halls[i])
  150. hallLabels.push_back(std::make_shared<CLabel>( 26 + 42 * (int)i, 64, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(halls[i])));
  151. }
  152. }
  153. CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const std::vector<Component> & compsToDisplay, std::string message, int textH, bool tiny)
  154. {
  155. OBJECT_CONSTRUCTION;
  156. background = std::make_shared<CPicture>(ImagePath::builtin("ADSTATOT"), 1, 0);
  157. auto fullRect = Rect(CInfoBar::offset, CInfoBar::offset, data_width - 2 * CInfoBar::offset, data_height - 2 * CInfoBar::offset);
  158. auto textRect = fullRect;
  159. auto imageRect = fullRect;
  160. auto font = tiny ? FONT_TINY : FONT_SMALL;
  161. auto maxComponents = 2;
  162. if(!compsToDisplay.empty())
  163. {
  164. auto size = CComponent::large;
  165. if(compsToDisplay.size() > 2)
  166. {
  167. size = CComponent::medium;
  168. font = FONT_TINY;
  169. }
  170. if(!message.empty())
  171. {
  172. textRect = Rect(CInfoBar::offset,
  173. CInfoBar::offset,
  174. data_width - 2 * CInfoBar::offset,
  175. textH);
  176. imageRect = Rect(CInfoBar::offset,
  177. textH,
  178. data_width - 2 * CInfoBar::offset,
  179. CInfoBar::data_height - 2* CInfoBar::offset - textH);
  180. }
  181. if(compsToDisplay.size() > 4) {
  182. maxComponents = 3;
  183. size = CComponent::small;
  184. }
  185. if(compsToDisplay.size() > 6)
  186. maxComponents = 4;
  187. std::vector<std::shared_ptr<CComponent>> vect;
  188. for(const auto & c : compsToDisplay)
  189. vect.emplace_back(std::make_shared<CComponent>(c, size, font));
  190. comps = std::make_shared<CComponentBox>(vect, imageRect, 4, 4, 1, maxComponents);
  191. }
  192. if(!message.empty())
  193. text = std::make_shared<CMultiLineLabel>(textRect, font, ETextAlignment::CENTER, Colors::WHITE, message);
  194. }
  195. void CInfoBar::playNewDaySound()
  196. {
  197. int volume = CCS->soundh->getVolume();
  198. int handle = -1;
  199. if(volume == 0)
  200. CCS->soundh->setVolume(settings["general"]["sound"].Integer());
  201. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  202. handle = CCS->soundh->playSound(soundBase::newDay);
  203. else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  204. handle = CCS->soundh->playSound(soundBase::newWeek);
  205. else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  206. handle = CCS->soundh->playSound(soundBase::newMonth);
  207. else
  208. handle = CCS->soundh->playSound(soundBase::newDay);
  209. if(volume == 0)
  210. CCS->soundh->setCallback(handle, [&]() { if(!GH.screenHandler().hasFocus()) CCS->soundh->setVolume(0); });
  211. }
  212. void CInfoBar::reset()
  213. {
  214. OBJECT_CONSTRUCTION;
  215. state = EMPTY;
  216. visibleInfo = std::make_shared<EmptyVisibleInfo>();
  217. }
  218. void CInfoBar::showSelection()
  219. {
  220. OBJECT_CONSTRUCTION;
  221. if(LOCPLINT->localState->getCurrentHero())
  222. {
  223. showHeroSelection(LOCPLINT->localState->getCurrentHero());
  224. return;
  225. }
  226. if(LOCPLINT->localState->getCurrentTown())
  227. {
  228. showTownSelection(LOCPLINT->localState->getCurrentTown());
  229. return;
  230. }
  231. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  232. }
  233. void CInfoBar::tick(uint32_t msPassed)
  234. {
  235. assert(timerCounter > 0);
  236. if (msPassed >= timerCounter)
  237. {
  238. timerCounter = 0;
  239. removeUsedEvents(TIME);
  240. if(GH.windows().isTopWindow(adventureInt))
  241. popComponents(true);
  242. }
  243. else
  244. {
  245. timerCounter -= msPassed;
  246. }
  247. }
  248. void CInfoBar::clickReleased(const Point & cursorPosition, bool lastActivated)
  249. {
  250. timerCounter = 0;
  251. removeUsedEvents(TIME); //expiration trigger from just clicked element is not valid anymore
  252. if(state == HERO || state == TOWN)
  253. {
  254. if(lastActivated)
  255. showGameStatus();
  256. }
  257. else if(state == GAME)
  258. showDate();
  259. else
  260. popComponents(true);
  261. }
  262. void CInfoBar::showPopupWindow(const Point & cursorPosition)
  263. {
  264. CRClickPopup::createAndPush(CGI->generaltexth->allTexts[109]);
  265. }
  266. void CInfoBar::hover(bool on)
  267. {
  268. if(on)
  269. GH.statusbar()->write(CGI->generaltexth->zelp[292].first);
  270. else
  271. GH.statusbar()->clear();
  272. }
  273. CInfoBar::CInfoBar(const Rect & position)
  274. : CIntObject(LCLICK | SHOW_POPUP | HOVER, position.topLeft()),
  275. timerCounter(0),
  276. state(EMPTY),
  277. listener(settings.listen["gameTweaks"]["infoBarCreatureManagement"])
  278. {
  279. OBJECT_CONSTRUCTION;
  280. pos.w = position.w;
  281. pos.h = position.h;
  282. listener(std::bind(&CInfoBar::OnInfoBarCreatureManagementChanged, this));
  283. reset();
  284. }
  285. CInfoBar::CInfoBar(const Point & position): CInfoBar(Rect(position.x, position.y, width, height))
  286. {
  287. }
  288. void CInfoBar::OnInfoBarCreatureManagementChanged()
  289. {
  290. showSelection();
  291. }
  292. void CInfoBar::setTimer(uint32_t msToTrigger)
  293. {
  294. addUsedEvents(TIME);
  295. timerCounter = msToTrigger;
  296. }
  297. void CInfoBar::showDate()
  298. {
  299. OBJECT_CONSTRUCTION;
  300. playNewDaySound();
  301. state = DATE;
  302. visibleInfo = std::make_shared<VisibleDateInfo>();
  303. setTimer(3000); // confirmed to match H3
  304. redraw();
  305. }
  306. void CInfoBar::pushComponents(const std::vector<Component> & components, std::string message, int timer)
  307. {
  308. auto actualPush = [&](const std::vector<Component> & components, std::string message, int timer, size_t max){
  309. std::vector<Component> vect = components; //I do not know currently how to avoid copy here
  310. while(!vect.empty())
  311. {
  312. std::vector<Component> sender = {vect.begin(), vect.begin() + std::min(vect.size(), max)};
  313. prepareComponents(sender, message, timer);
  314. vect.erase(vect.begin(), vect.begin() + std::min(vect.size(), max));
  315. };
  316. };
  317. if(shouldPopAll)
  318. popAll();
  319. if(components.empty())
  320. prepareComponents(components, message, timer);
  321. else
  322. {
  323. std::array<std::pair<std::vector<Component>, int>, 10> reward_map;
  324. for(const auto & c : components)
  325. {
  326. switch(c.type)
  327. {
  328. case ComponentType::PRIM_SKILL:
  329. case ComponentType::EXPERIENCE:
  330. case ComponentType::LEVEL:
  331. case ComponentType::MANA:
  332. reward_map.at(0).first.push_back(c);
  333. reward_map.at(0).second = 8; //At most 8, cannot be more
  334. break;
  335. case ComponentType::NONE:
  336. case ComponentType::SEC_SKILL:
  337. reward_map.at(1).first.push_back(c);
  338. reward_map.at(1).second = 4; //At most 4
  339. break;
  340. case ComponentType::SPELL:
  341. reward_map.at(2).first.push_back(c);
  342. reward_map.at(2).second = 4; //At most 4
  343. break;
  344. case ComponentType::ARTIFACT:
  345. case ComponentType::SPELL_SCROLL:
  346. reward_map.at(3).first.push_back(c);
  347. reward_map.at(3).second = 4; //At most 4, too long names
  348. break;
  349. case ComponentType::CREATURE:
  350. reward_map.at(4).first.push_back(c);
  351. reward_map.at(4).second = 4; //At most 4, too long names
  352. break;
  353. case ComponentType::RESOURCE:
  354. case ComponentType::RESOURCE_PER_DAY:
  355. reward_map.at(5).first.push_back(c);
  356. reward_map.at(5).second = 7; //At most 7
  357. break;
  358. case ComponentType::MORALE:
  359. case ComponentType::LUCK:
  360. reward_map.at(6).first.push_back(c);
  361. reward_map.at(6).second = 2; //At most 2 - 1 for morale + 1 for luck
  362. break;
  363. case ComponentType::BUILDING:
  364. reward_map.at(7).first.push_back(c);
  365. reward_map.at(7).second = 1; //At most 1 - only large icons available AFAIK
  366. break;
  367. case ComponentType::HERO_PORTRAIT:
  368. reward_map.at(8).first.push_back(c);
  369. reward_map.at(8).second = 1; //I do not think than we even can get more than 1 hero
  370. break;
  371. case ComponentType::FLAG:
  372. reward_map.at(9).first.push_back(c);
  373. reward_map.at(9).second = 1; //I do not think than we even can get more than 1 player in notification
  374. break;
  375. default:
  376. logGlobal->warn("Invalid component received!");
  377. }
  378. }
  379. for(const auto & kv : reward_map)
  380. if(!kv.first.empty())
  381. actualPush(kv.first, message, timer, kv.second);
  382. }
  383. popComponents();
  384. }
  385. void CInfoBar::prepareComponents(const std::vector<Component> & components, std::string message, int timer)
  386. {
  387. auto imageH = CMessage::getEstimatedComponentHeight(components.size()) + (components.empty() ? 0 : 2 * CInfoBar::offset);
  388. auto textH = CMessage::guessHeight(message,CInfoBar::data_width - 2 * CInfoBar::offset, FONT_SMALL);
  389. auto tinyH = CMessage::guessHeight(message,CInfoBar::data_width - 2 * CInfoBar::offset, FONT_TINY);
  390. auto header = CMessage::guessHeader(message);
  391. auto headerH = CMessage::guessHeight(header, CInfoBar::data_width - 2 * CInfoBar::offset, FONT_SMALL);
  392. auto headerTinyH = CMessage::guessHeight(header, CInfoBar::data_width - 2 * CInfoBar::offset, FONT_TINY);
  393. // Order matters - priority form should be chosen first
  394. if(imageH + textH < CInfoBar::data_height)
  395. pushComponents(components, message, textH, false, timer);
  396. else if(imageH + tinyH < CInfoBar::data_height)
  397. pushComponents(components, message, tinyH, true, timer);
  398. else if(imageH + headerH < CInfoBar::data_height)
  399. pushComponents(components, header, headerH, false, timer);
  400. else if(imageH + headerTinyH < CInfoBar::data_height)
  401. pushComponents(components, header, headerTinyH, true, timer);
  402. else
  403. pushComponents(components, "", 0, false, timer);
  404. return;
  405. }
  406. void CInfoBar::requestPopAll()
  407. {
  408. shouldPopAll = true;
  409. }
  410. void CInfoBar::popAll()
  411. {
  412. componentsQueue = {};
  413. shouldPopAll = false;
  414. }
  415. void CInfoBar::popComponents(bool remove)
  416. {
  417. OBJECT_CONSTRUCTION;
  418. if(remove && !componentsQueue.empty())
  419. componentsQueue.pop();
  420. if(!componentsQueue.empty())
  421. {
  422. state = COMPONENT;
  423. const auto & extracted = componentsQueue.front();
  424. visibleInfo = std::make_shared<VisibleComponentInfo>(extracted.first);
  425. setTimer(extracted.second);
  426. redraw();
  427. return;
  428. }
  429. showSelection();
  430. }
  431. void CInfoBar::pushComponents(const std::vector<Component> & comps, std::string message, int textH, bool tiny, int timer)
  432. {
  433. OBJECT_CONSTRUCTION;
  434. componentsQueue.emplace(VisibleComponentInfo::Cache(comps, message, textH, tiny), timer);
  435. }
  436. bool CInfoBar::showingComponents()
  437. {
  438. return state == COMPONENT;
  439. }
  440. void CInfoBar::startEnemyTurn(PlayerColor color)
  441. {
  442. OBJECT_CONSTRUCTION;
  443. state = AITURN;
  444. visibleInfo = std::make_shared<VisibleEnemyTurnInfo>(color);
  445. redraw();
  446. }
  447. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  448. {
  449. OBJECT_CONSTRUCTION;
  450. if(!hero)
  451. {
  452. reset();
  453. }
  454. else
  455. {
  456. state = HERO;
  457. visibleInfo = std::make_shared<VisibleHeroInfo>(hero);
  458. }
  459. redraw();
  460. }
  461. void CInfoBar::showTownSelection(const CGTownInstance * town)
  462. {
  463. OBJECT_CONSTRUCTION;
  464. if(!town)
  465. {
  466. reset();
  467. }
  468. else
  469. {
  470. state = TOWN;
  471. visibleInfo = std::make_shared<VisibleTownInfo>(town);
  472. }
  473. redraw();
  474. }
  475. void CInfoBar::showGameStatus()
  476. {
  477. OBJECT_CONSTRUCTION;
  478. state = GAME;
  479. visibleInfo = std::make_shared<VisibleGameStatusInfo>();
  480. setTimer(3000);
  481. redraw();
  482. }