CInfoBar.cpp 15 KB

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