CInfoBar.cpp 9.1 KB

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