CInfoBar.cpp 15 KB

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