CInfoBar.cpp 15 KB

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