CInfoBar.cpp 15 KB

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