CInfoBar.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "../widgets/TextControls.h"
  16. #include "../widgets/MiscWidgets.h"
  17. #include "../windows/InfoWindows.h"
  18. #include "../CGameInfo.h"
  19. #include "../CMusicHandler.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/mapObjects/CGHeroInstance.h"
  25. #include "../../lib/mapObjects/CGTownInstance.h"
  26. CInfoBar::CVisibleInfo::CVisibleInfo()
  27. : CIntObject(0, Point(8, 12))
  28. {
  29. }
  30. void CInfoBar::CVisibleInfo::show(SDL_Surface * to)
  31. {
  32. CIntObject::show(to);
  33. for(auto object : forceRefresh)
  34. object->showAll(to);
  35. }
  36. CInfoBar::EmptyVisibleInfo::EmptyVisibleInfo()
  37. {
  38. }
  39. CInfoBar::VisibleHeroInfo::VisibleHeroInfo(const CGHeroInstance * hero)
  40. {
  41. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  42. background = std::make_shared<CPicture>("ADSTATHR");
  43. heroTooltip = std::make_shared<CHeroTooltip>(Point(0,0), hero);
  44. }
  45. CInfoBar::VisibleTownInfo::VisibleTownInfo(const CGTownInstance * town)
  46. {
  47. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  48. background = std::make_shared<CPicture>("ADSTATCS");
  49. townTooltip = std::make_shared<CTownTooltip>(Point(0,0), town);
  50. }
  51. CInfoBar::VisibleDateInfo::VisibleDateInfo()
  52. {
  53. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  54. animation = std::make_shared<CShowableAnim>(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE, 180);// H3 uses around 175-180 ms per frame
  55. std::string labelText;
  56. 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
  57. labelText = CGI->generaltexth->allTexts[63] + " " + std::to_string(LOCPLINT->cb->getDate(Date::WEEK));
  58. else
  59. labelText = CGI->generaltexth->allTexts[64] + " " + std::to_string(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  60. label = std::make_shared<CLabel>(95, 31, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, labelText);
  61. forceRefresh.push_back(label);
  62. }
  63. std::string CInfoBar::VisibleDateInfo::getNewDayName()
  64. {
  65. if(LOCPLINT->cb->getDate(Date::DAY) == 1)
  66. return "NEWDAY";
  67. if(LOCPLINT->cb->getDate(Date::DAY) != 1)
  68. return "NEWDAY";
  69. switch(LOCPLINT->cb->getDate(Date::WEEK))
  70. {
  71. case 1:
  72. return "NEWWEEK1";
  73. case 2:
  74. return "NEWWEEK2";
  75. case 3:
  76. return "NEWWEEK3";
  77. case 4:
  78. return "NEWWEEK4";
  79. default:
  80. return "";
  81. }
  82. }
  83. CInfoBar::VisibleEnemyTurnInfo::VisibleEnemyTurnInfo(PlayerColor player)
  84. {
  85. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  86. background = std::make_shared<CPicture>("ADSTATNX");
  87. banner = std::make_shared<CAnimImage>("CREST58", player.getNum(), 0, 20, 51);
  88. sand = std::make_shared<CShowableAnim>(99, 51, "HOURSAND", 0, 100); // H3 uses around 100 ms per frame
  89. 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
  90. }
  91. CInfoBar::VisibleGameStatusInfo::VisibleGameStatusInfo()
  92. {
  93. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  94. //get amount of halls of each level
  95. std::vector<int> halls(4, 0);
  96. for(auto town : LOCPLINT->towns)
  97. {
  98. int hallLevel = town->hallLevel();
  99. //negative value means no village hall, unlikely but possible
  100. if(hallLevel >= 0)
  101. halls.at(hallLevel)++;
  102. }
  103. std::vector<PlayerColor> allies, enemies;
  104. //generate list of allies and enemies
  105. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  106. {
  107. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  108. {
  109. if(LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  110. allies.push_back(PlayerColor(i));
  111. else
  112. enemies.push_back(PlayerColor(i));
  113. }
  114. }
  115. //generate widgets
  116. background = std::make_shared<CPicture>("ADSTATIN");
  117. allyLabel = std::make_shared<CLabel>(10, 106, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  118. enemyLabel = std::make_shared<CLabel>(10, 136, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  119. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  120. for(PlayerColor & player : allies)
  121. {
  122. auto image = std::make_shared<CAnimImage>("ITGFLAGS", player.getNum(), 0, posx, 102);
  123. posx += image->pos.w;
  124. flags.push_back(image);
  125. }
  126. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  127. for(PlayerColor & player : enemies)
  128. {
  129. auto image = std::make_shared<CAnimImage>("ITGFLAGS", player.getNum(), 0, posx, 132);
  130. posx += image->pos.w;
  131. flags.push_back(image);
  132. }
  133. for(size_t i=0; i<halls.size(); i++)
  134. {
  135. hallIcons.push_back(std::make_shared<CAnimImage>("itmtl", i, 0, 6 + 42 * (int)i , 11));
  136. if(halls[i])
  137. hallLabels.push_back(std::make_shared<CLabel>( 26 + 42 * (int)i, 64, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(halls[i])));
  138. }
  139. }
  140. CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const std::vector<Component> & compsToDisplay, std::string message)
  141. {
  142. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  143. background = std::make_shared<CPicture>("ADSTATOT", 1, 0);
  144. auto fullRect = Rect(4, 4, 171, 171);
  145. auto textRect = fullRect;
  146. auto imageRect = fullRect;
  147. if(!compsToDisplay.empty())
  148. {
  149. auto size = CComponent::large;
  150. if(compsToDisplay.size() > 2)
  151. size = CComponent::small;
  152. if(message != "")
  153. {
  154. textRect = Rect(4, 4, 171, 42);
  155. imageRect = Rect(4, 42, 171, 121);
  156. if(compsToDisplay.size() > 4)
  157. size = CComponent::tiny;
  158. }
  159. else if(compsToDisplay.size() > 4)
  160. size = CComponent::small;
  161. std::vector<std::shared_ptr<CComponent>> vect;
  162. for(const auto & c : compsToDisplay)
  163. vect.emplace_back(std::make_shared<CComponent>(c, size));
  164. comps = std::make_shared<CComponentBox>(vect, imageRect, 4, 4, 1);
  165. }
  166. text = std::make_shared<CTextBox>(message, textRect, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  167. }
  168. void CInfoBar::playNewDaySound()
  169. {
  170. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  171. CCS->soundh->playSound(soundBase::newDay);
  172. else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  173. CCS->soundh->playSound(soundBase::newWeek);
  174. else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  175. CCS->soundh->playSound(soundBase::newMonth);
  176. else
  177. CCS->soundh->playSound(soundBase::newDay);
  178. }
  179. void CInfoBar::reset()
  180. {
  181. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  182. state = EMPTY;
  183. visibleInfo = std::make_shared<EmptyVisibleInfo>();
  184. }
  185. void CInfoBar::showSelection()
  186. {
  187. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  188. if(adventureInt->curHero())
  189. {
  190. showHeroSelection(adventureInt->curHero());
  191. return;
  192. }
  193. if(adventureInt->curTown())
  194. {
  195. showTownSelection(adventureInt->curTown());
  196. return;
  197. }
  198. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  199. }
  200. void CInfoBar::tick()
  201. {
  202. removeUsedEvents(TIME);
  203. if(GH.topInt() == adventureInt)
  204. showSelection();
  205. }
  206. void CInfoBar::clickLeft(tribool down, bool previousState)
  207. {
  208. if(down)
  209. {
  210. if(state == HERO || state == TOWN)
  211. showGameStatus();
  212. else if(state == GAME)
  213. showDate();
  214. else
  215. showSelection();
  216. }
  217. }
  218. void CInfoBar::clickRight(tribool down, bool previousState)
  219. {
  220. if (down)
  221. CRClickPopup::createAndPush(CGI->generaltexth->allTexts[109]);
  222. }
  223. void CInfoBar::hover(bool on)
  224. {
  225. if(on)
  226. GH.statusbar->write(CGI->generaltexth->zelp[292].first);
  227. else
  228. GH.statusbar->clear();
  229. }
  230. CInfoBar::CInfoBar(const Rect & position)
  231. : CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  232. state(EMPTY)
  233. {
  234. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  235. pos.w = position.w;
  236. pos.h = position.h;
  237. reset();
  238. }
  239. void CInfoBar::showDate()
  240. {
  241. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  242. playNewDaySound();
  243. state = DATE;
  244. visibleInfo = std::make_shared<VisibleDateInfo>();
  245. setTimer(3000); // confirmed to match H3
  246. redraw();
  247. }
  248. void CInfoBar::showComponents(const std::vector<Component> & comps, std::string message)
  249. {
  250. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  251. state = COMPONENT;
  252. visibleInfo = std::make_shared<VisibleComponentInfo>(comps, message);
  253. setTimer(3000);
  254. redraw();
  255. }
  256. bool CInfoBar::showingComponents()
  257. {
  258. return state == COMPONENT;
  259. }
  260. void CInfoBar::startEnemyTurn(PlayerColor color)
  261. {
  262. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  263. state = AITURN;
  264. visibleInfo = std::make_shared<VisibleEnemyTurnInfo>(color);
  265. redraw();
  266. }
  267. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  268. {
  269. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  270. if(!hero)
  271. {
  272. reset();
  273. }
  274. else
  275. {
  276. state = HERO;
  277. visibleInfo = std::make_shared<VisibleHeroInfo>(hero);
  278. }
  279. redraw();
  280. }
  281. void CInfoBar::showTownSelection(const CGTownInstance * town)
  282. {
  283. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  284. if(!town)
  285. {
  286. reset();
  287. }
  288. else
  289. {
  290. state = TOWN;
  291. visibleInfo = std::make_shared<VisibleTownInfo>(town);
  292. }
  293. redraw();
  294. }
  295. void CInfoBar::showGameStatus()
  296. {
  297. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  298. state = GAME;
  299. visibleInfo = std::make_shared<VisibleGameStatusInfo>();
  300. setTimer(3000);
  301. redraw();
  302. }