CInfoBar.cpp 15 KB

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