CInfoBar.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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->selection)
  169. {
  170. if(auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection))
  171. {
  172. showHeroSelection(hero);
  173. return;
  174. }
  175. else if(auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection))
  176. {
  177. showTownSelection(town);
  178. return;
  179. }
  180. }
  181. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  182. }
  183. void CInfoBar::tick()
  184. {
  185. removeUsedEvents(TIME);
  186. if(GH.topInt() == adventureInt)
  187. showSelection();
  188. }
  189. void CInfoBar::clickLeft(tribool down, bool previousState)
  190. {
  191. if(down)
  192. {
  193. if(state == HERO || state == TOWN)
  194. showGameStatus();
  195. else if(state == GAME)
  196. showDate();
  197. else
  198. showSelection();
  199. }
  200. }
  201. void CInfoBar::clickRight(tribool down, bool previousState)
  202. {
  203. if (down)
  204. CRClickPopup::createAndPush(CGI->generaltexth->allTexts[109]);
  205. }
  206. void CInfoBar::hover(bool on)
  207. {
  208. if(on)
  209. GH.statusbar->write(CGI->generaltexth->zelp[292].first);
  210. else
  211. GH.statusbar->clear();
  212. }
  213. CInfoBar::CInfoBar(const Rect & position)
  214. : CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  215. state(EMPTY)
  216. {
  217. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  218. pos.w = position.w;
  219. pos.h = position.h;
  220. reset();
  221. }
  222. void CInfoBar::showDate()
  223. {
  224. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  225. playNewDaySound();
  226. state = DATE;
  227. visibleInfo = std::make_shared<VisibleDateInfo>();
  228. setTimer(3000); // confirmed to match H3
  229. redraw();
  230. }
  231. void CInfoBar::showComponent(const Component & comp, std::string message)
  232. {
  233. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  234. state = COMPONENT;
  235. visibleInfo = std::make_shared<VisibleComponentInfo>(comp, message);
  236. setTimer(3000);
  237. redraw();
  238. }
  239. void CInfoBar::startEnemyTurn(PlayerColor color)
  240. {
  241. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  242. state = AITURN;
  243. visibleInfo = std::make_shared<VisibleEnemyTurnInfo>(color);
  244. redraw();
  245. }
  246. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  247. {
  248. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  249. if(!hero)
  250. {
  251. reset();
  252. }
  253. else
  254. {
  255. state = HERO;
  256. visibleInfo = std::make_shared<VisibleHeroInfo>(hero);
  257. }
  258. redraw();
  259. }
  260. void CInfoBar::showTownSelection(const CGTownInstance * town)
  261. {
  262. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  263. if(!town)
  264. {
  265. reset();
  266. }
  267. else
  268. {
  269. state = TOWN;
  270. visibleInfo = std::make_shared<VisibleTownInfo>(town);
  271. }
  272. redraw();
  273. }
  274. void CInfoBar::showGameStatus()
  275. {
  276. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  277. state = GAME;
  278. visibleInfo = std::make_shared<VisibleGameStatusInfo>();
  279. setTimer(3000);
  280. redraw();
  281. }