CInfoBar.cpp 16 KB

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