CInfoBar.cpp 16 KB

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