CInfoBar.cpp 14 KB

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