CInfoBar.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  58. else
  59. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::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, boost::lexical_cast<std::string>(halls[i])));
  138. }
  139. }
  140. CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const Component & compToDisplay, std::string message)
  141. {
  142. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  143. background = std::make_shared<CPicture>("ADSTATOT", 1, 0);
  144. comp = std::make_shared<CComponent>(compToDisplay);
  145. comp->moveTo(Point(pos.x+47, pos.y+50));
  146. text = std::make_shared<CTextBox>(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  147. }
  148. void CInfoBar::playNewDaySound()
  149. {
  150. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  151. CCS->soundh->playSound(soundBase::newDay);
  152. else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  153. CCS->soundh->playSound(soundBase::newWeek);
  154. else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  155. CCS->soundh->playSound(soundBase::newMonth);
  156. else
  157. CCS->soundh->playSound(soundBase::newDay);
  158. }
  159. void CInfoBar::reset()
  160. {
  161. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  162. state = EMPTY;
  163. visibleInfo = std::make_shared<EmptyVisibleInfo>();
  164. }
  165. void CInfoBar::showSelection()
  166. {
  167. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  168. if(adventureInt->curHero())
  169. {
  170. showHeroSelection(adventureInt->curHero());
  171. return;
  172. }
  173. if(adventureInt->curTown())
  174. {
  175. showTownSelection(adventureInt->curTown());
  176. return;
  177. }
  178. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  179. }
  180. void CInfoBar::tick()
  181. {
  182. removeUsedEvents(TIME);
  183. if(GH.topInt() == adventureInt)
  184. showSelection();
  185. }
  186. void CInfoBar::clickLeft(tribool down, bool previousState)
  187. {
  188. if(down)
  189. {
  190. if(state == HERO || state == TOWN)
  191. showGameStatus();
  192. else if(state == GAME)
  193. showDate();
  194. else
  195. showSelection();
  196. }
  197. }
  198. void CInfoBar::clickRight(tribool down, bool previousState)
  199. {
  200. if (down)
  201. CRClickPopup::createAndPush(CGI->generaltexth->allTexts[109]);
  202. }
  203. void CInfoBar::hover(bool on)
  204. {
  205. if(on)
  206. GH.statusbar->write(CGI->generaltexth->zelp[292].first);
  207. else
  208. GH.statusbar->clear();
  209. }
  210. CInfoBar::CInfoBar(const Rect & position)
  211. : CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  212. state(EMPTY)
  213. {
  214. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  215. pos.w = position.w;
  216. pos.h = position.h;
  217. reset();
  218. }
  219. void CInfoBar::showDate()
  220. {
  221. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  222. playNewDaySound();
  223. state = DATE;
  224. visibleInfo = std::make_shared<VisibleDateInfo>();
  225. setTimer(3000); // confirmed to match H3
  226. redraw();
  227. }
  228. void CInfoBar::showComponent(const Component & comp, std::string message)
  229. {
  230. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  231. state = COMPONENT;
  232. visibleInfo = std::make_shared<VisibleComponentInfo>(comp, message);
  233. setTimer(3000);
  234. redraw();
  235. }
  236. void CInfoBar::startEnemyTurn(PlayerColor color)
  237. {
  238. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  239. state = AITURN;
  240. visibleInfo = std::make_shared<VisibleEnemyTurnInfo>(color);
  241. redraw();
  242. }
  243. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  244. {
  245. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  246. if(!hero)
  247. {
  248. reset();
  249. }
  250. else
  251. {
  252. state = HERO;
  253. visibleInfo = std::make_shared<VisibleHeroInfo>(hero);
  254. }
  255. redraw();
  256. }
  257. void CInfoBar::showTownSelection(const CGTownInstance * town)
  258. {
  259. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  260. if(!town)
  261. {
  262. reset();
  263. }
  264. else
  265. {
  266. state = TOWN;
  267. visibleInfo = std::make_shared<VisibleTownInfo>(town);
  268. }
  269. redraw();
  270. }
  271. void CInfoBar::showGameStatus()
  272. {
  273. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  274. state = GAME;
  275. visibleInfo = std::make_shared<VisibleGameStatusInfo>();
  276. setTimer(3000);
  277. redraw();
  278. }