CInfoBar.cpp 8.6 KB

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