AdventureMapClasses.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. #include "StdInc.h"
  2. #include "AdventureMapClasses.h"
  3. #include "../CCallback.h"
  4. #include "../lib/JsonNode.h"
  5. #include "../lib/Filesystem/CResourceLoader.h"
  6. #include "../lib/Mapping/CMap.h"
  7. #include "../lib/CModHandler.h"
  8. #include "../lib/CObjectHandler.h"
  9. #include "../lib/CGameState.h"
  10. #include "../lib/CGeneralTextHandler.h"
  11. #include "../lib/CTownHandler.h"
  12. #include "../lib/NetPacks.h"
  13. #include "../lib/CHeroHandler.h"
  14. #include "../lib/StringConstants.h"
  15. #include "CAdvmapInterface.h"
  16. #include "CAnimation.h"
  17. #include "CGameInfo.h"
  18. #include "CPlayerInterface.h"
  19. #include "CMusicHandler.h"
  20. #include "Graphics.h"
  21. #include "GUIClasses.h"
  22. #include "UIFramework/CGuiHandler.h"
  23. #include "UIFramework/SDL_Pixels.h"
  24. /*
  25. * CAdventureMapClasses.h, part of VCMI engine
  26. *
  27. * Authors: listed in file AUTHORS in main folder
  28. *
  29. * License: GNU General Public License v2.0 or later
  30. * Full text of license available in license.txt file, in main folder
  31. *
  32. */
  33. CList::CListItem::CListItem(CList * Parent):
  34. CIntObject(LCLICK | RCLICK | HOVER),
  35. parent(Parent),
  36. selection(nullptr)
  37. {
  38. }
  39. CList::CListItem::~CListItem()
  40. {
  41. // select() method in this was already destroyed so we can't safely call method in parent
  42. if (parent->selected == this)
  43. parent->selected = nullptr;
  44. }
  45. void CList::CListItem::clickRight(tribool down, bool previousState)
  46. {
  47. if (down == true)
  48. showTooltip();
  49. }
  50. void CList::CListItem::clickLeft(tribool down, bool previousState)
  51. {
  52. if (down == true)
  53. {
  54. //second click on already selected item
  55. if (parent->selected == this)
  56. open();
  57. else
  58. {
  59. //first click - switch selection
  60. parent->select(this);
  61. }
  62. }
  63. }
  64. void CList::CListItem::hover(bool on)
  65. {
  66. if (on)
  67. GH.statusbar->print(getHoverText());
  68. else
  69. GH.statusbar->clear();
  70. }
  71. void CList::CListItem::onSelect(bool on)
  72. {
  73. OBJ_CONSTRUCTION_CAPTURING_ALL;
  74. vstd::clear_pointer(selection);
  75. if (on)
  76. selection = genSelection();
  77. select(on);
  78. GH.totalRedraw();
  79. }
  80. CList::CList(int Size, Point position, std::string btnUp, std::string btnDown, size_t listAmount,
  81. int helpUp, int helpDown, CListBox::CreateFunc create, CListBox::DestroyFunc destroy):
  82. CIntObject(0, position),
  83. size(Size),
  84. selected(nullptr)
  85. {
  86. OBJ_CONSTRUCTION_CAPTURING_ALL;
  87. scrollUp = new CAdventureMapButton(CGI->generaltexth->zelp[helpUp], 0, 0, 0, btnUp);
  88. list = new CListBox(create, destroy, Point(1,scrollUp->pos.h), Point(0, 32), size, listAmount);
  89. //assign callback only after list was created
  90. scrollUp->callback = boost::bind(&CListBox::moveToPrev, list);
  91. scrollDown = new CAdventureMapButton(CGI->generaltexth->zelp[helpDown], boost::bind(&CListBox::moveToNext, list), 0, scrollUp->pos.h + 32*size, btnDown);
  92. scrollDown->callback += boost::bind(&CList::update, this);
  93. scrollUp->callback += boost::bind(&CList::update, this);
  94. update();
  95. }
  96. void CList::update()
  97. {
  98. bool onTop = list->getPos() == 0;
  99. bool onBottom = list->getPos() + size >= list->size();
  100. scrollUp->block(onTop);
  101. scrollDown->block(onBottom);
  102. }
  103. void CList::select(CListItem *which)
  104. {
  105. if (selected == which)
  106. return;
  107. if (selected)
  108. selected->onSelect(false);
  109. selected = which;
  110. if (which)
  111. {
  112. which->onSelect(true);
  113. onSelect();
  114. }
  115. }
  116. int CList::getSelectedIndex()
  117. {
  118. return list->getIndexOf(selected);
  119. }
  120. void CList::selectIndex(int which)
  121. {
  122. if (which < 0)
  123. {
  124. if (selected)
  125. select(nullptr);
  126. }
  127. else
  128. {
  129. list->scrollTo(which);
  130. update();
  131. select(dynamic_cast<CListItem*>(list->getItem(which)));
  132. }
  133. }
  134. void CList::selectNext()
  135. {
  136. int index = getSelectedIndex();
  137. if (index < 0)
  138. selectIndex(0);
  139. else if ((size_t)++index < list->size())
  140. selectIndex(index);
  141. }
  142. void CList::selectPrev()
  143. {
  144. int index = getSelectedIndex();
  145. if (index <= 0)
  146. selectIndex(0);
  147. else
  148. selectIndex(index-1);
  149. }
  150. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  151. {
  152. OBJ_CONSTRUCTION_CAPTURING_ALL;
  153. auto move = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  154. auto img = new CPicture("HPSXXX", move->pos.w + 1);
  155. auto mana = new CAnimImage("IMANA", 0, 0, move->pos.w + img->pos.w + 2, 1 );
  156. pos.w = mana->pos.w + mana->pos.x - pos.x;
  157. pos.h = std::max(std::max<ui32>(move->pos.h + 1, mana->pos.h + 1), img->pos.h);
  158. }
  159. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero):
  160. CListItem(parent),
  161. hero(Hero)
  162. {
  163. OBJ_CONSTRUCTION_CAPTURING_ALL;
  164. movement = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  165. portrait = new CAnimImage("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  166. mana = new CAnimImage("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  167. pos.w = mana->pos.w + mana->pos.x - pos.x;
  168. pos.h = std::max(std::max<ui32>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  169. update();
  170. }
  171. void CHeroList::CHeroItem::update()
  172. {
  173. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  174. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  175. redraw();
  176. }
  177. CIntObject * CHeroList::CHeroItem::genSelection()
  178. {
  179. return new CPicture("HPSYYY", movement->pos.w + 1);
  180. }
  181. void CHeroList::CHeroItem::select(bool on)
  182. {
  183. if (on && adventureInt->selection != hero)
  184. adventureInt->select(hero);
  185. }
  186. void CHeroList::CHeroItem::open()
  187. {
  188. LOCPLINT->openHeroWindow(hero);
  189. }
  190. void CHeroList::CHeroItem::showTooltip()
  191. {
  192. auto &motion = GH.current->motion;
  193. CRClickPopup::createAndPush(hero, Point(motion.x, motion.y));
  194. }
  195. std::string CHeroList::CHeroItem::getHoverText()
  196. {
  197. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->name % hero->type->heroClass->name);
  198. }
  199. CIntObject * CHeroList::createHeroItem(size_t index)
  200. {
  201. if (LOCPLINT->wanderingHeroes.size() > index)
  202. return new CHeroItem(this, LOCPLINT->wanderingHeroes[index]);
  203. return new CEmptyHeroItem();
  204. }
  205. CHeroList::CHeroList(int size, Point position, std::string btnUp, std::string btnDown):
  206. CList(size, position, btnUp, btnDown, LOCPLINT->wanderingHeroes.size(), 303, 304, boost::bind(&CHeroList::createHeroItem, this, _1))
  207. {
  208. }
  209. void CHeroList::select(const CGHeroInstance * hero)
  210. {
  211. selectIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  212. }
  213. void CHeroList::update(const CGHeroInstance * hero)
  214. {
  215. //this hero is already present, update its status
  216. for (auto iter = list->getItems().begin(); iter != list->getItems().end(); iter++)
  217. {
  218. auto item = dynamic_cast<CHeroItem*>(*iter);
  219. if (item && item->hero == hero && vstd::contains(LOCPLINT->wanderingHeroes, hero))
  220. {
  221. item->update();
  222. return;
  223. }
  224. }
  225. //simplest solution for now: reset list and restore selection
  226. list->resize(LOCPLINT->wanderingHeroes.size());
  227. if (adventureInt->selection)
  228. {
  229. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  230. if (hero)
  231. select(hero);
  232. }
  233. CList::update();
  234. }
  235. CIntObject * CTownList::createTownItem(size_t index)
  236. {
  237. if (LOCPLINT->towns.size() > index)
  238. return new CTownItem(this, LOCPLINT->towns[index]);
  239. return new CAnimImage("ITPA", 0);
  240. }
  241. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  242. CListItem(parent),
  243. town(Town)
  244. {
  245. OBJ_CONSTRUCTION_CAPTURING_ALL;
  246. picture = new CAnimImage("ITPA", 0);
  247. pos = picture->pos;
  248. update();
  249. }
  250. CIntObject * CTownList::CTownItem::genSelection()
  251. {
  252. return new CAnimImage("ITPA", 1);
  253. }
  254. void CTownList::CTownItem::update()
  255. {
  256. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  257. picture->setFrame(iconIndex + 2);
  258. redraw();
  259. }
  260. void CTownList::CTownItem::select(bool on)
  261. {
  262. if (on && adventureInt->selection != town)
  263. adventureInt->select(town);
  264. }
  265. void CTownList::CTownItem::open()
  266. {
  267. LOCPLINT->openTownWindow(town);
  268. }
  269. void CTownList::CTownItem::showTooltip()
  270. {
  271. auto &motion = GH.current->motion;
  272. CRClickPopup::createAndPush(town, Point(motion.x, motion.y));
  273. }
  274. std::string CTownList::CTownItem::getHoverText()
  275. {
  276. return town->hoverName;
  277. }
  278. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  279. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, boost::bind(&CTownList::createTownItem, this, _1))
  280. {
  281. }
  282. void CTownList::select(const CGTownInstance * town)
  283. {
  284. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  285. }
  286. void CTownList::update(const CGTownInstance *)
  287. {
  288. //simplest solution for now: reset list and restore selection
  289. list->resize(LOCPLINT->towns.size());
  290. if (adventureInt->selection)
  291. {
  292. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  293. if (town)
  294. select(town);
  295. }
  296. CList::update();
  297. }
  298. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  299. {
  300. static const SDL_Color fogOfWar = {0, 0, 0, 255};
  301. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  302. // if tile is not visible it will be black on minimap
  303. if(!tile)
  304. return fogOfWar;
  305. // if object at tile is owned - it will be colored as its owner
  306. BOOST_FOREACH(const CGObjectInstance *obj, tile->blockingObjects)
  307. {
  308. //heroes will be blitted later
  309. if (obj->ID == Obj::HERO)
  310. continue;
  311. PlayerColor player = obj->getOwner();
  312. if(player == PlayerColor::NEUTRAL)
  313. return *graphics->neutralColor;
  314. else
  315. if (player < PlayerColor::PLAYER_LIMIT)
  316. return graphics->playerColors[player.getNum()];
  317. }
  318. // else - use terrain color (blocked version or normal)
  319. if (tile->blocked && (!tile->visitable))
  320. return parent->colors.find(tile->terType)->second.second;
  321. else
  322. return parent->colors.find(tile->terType)->second.first;
  323. }
  324. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  325. {
  326. int3 mapSizes = LOCPLINT->cb->getMapSize();
  327. double stepX = double(pos.w) / mapSizes.x;
  328. double stepY = double(pos.h) / mapSizes.y;
  329. x = toX + stepX * tile.x;
  330. y = toY + stepY * tile.y;
  331. }
  332. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  333. {
  334. //coordinates of rectangle on minimap representing this tile
  335. // begin - first to blit, end - first NOT to blit
  336. int xBegin, yBegin, xEnd, yEnd;
  337. tileToPixels (tile, xBegin, yBegin, toX, toY);
  338. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  339. for (int y=yBegin; y<yEnd; y++)
  340. {
  341. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  342. for (int x=xBegin; x<xEnd; x++)
  343. ColorPutter<4, 1>::PutColor(ptr, color);
  344. }
  345. }
  346. void CMinimapInstance::refreshTile(const int3 &tile)
  347. {
  348. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  349. }
  350. void CMinimapInstance::drawScaled(int level)
  351. {
  352. int3 mapSizes = LOCPLINT->cb->getMapSize();
  353. //size of one map tile on our minimap
  354. double stepX = double(pos.w) / mapSizes.x;
  355. double stepY = double(pos.h) / mapSizes.y;
  356. double currY = 0;
  357. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  358. {
  359. double currX = 0;
  360. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  361. {
  362. const SDL_Color &color = getTileColor(int3(x,y,level));
  363. //coordinates of rectangle on minimap representing this tile
  364. // begin - first to blit, end - first NOT to blit
  365. int xBegin = currX;
  366. int yBegin = currY;
  367. int xEnd = currX + stepX;
  368. int yEnd = currY + stepY;
  369. for (int y=yBegin; y<yEnd; y++)
  370. {
  371. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  372. for (int x=xBegin; x<xEnd; x++)
  373. ColorPutter<4, 1>::PutColor(ptr, color);
  374. }
  375. }
  376. }
  377. }
  378. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  379. parent(Parent),
  380. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  381. level(Level)
  382. {
  383. pos.w = parent->pos.w;
  384. pos.h = parent->pos.h;
  385. drawScaled(level);
  386. }
  387. CMinimapInstance::~CMinimapInstance()
  388. {
  389. SDL_FreeSurface(minimap);
  390. }
  391. void CMinimapInstance::showAll(SDL_Surface *to)
  392. {
  393. blitAtLoc(minimap, 0, 0, to);
  394. //draw heroes
  395. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false);
  396. BOOST_FOREACH(auto & hero, heroes)
  397. {
  398. int3 position = hero->getPosition(false);
  399. if (position.z == level)
  400. {
  401. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  402. blitTileWithColor(color, position, to, pos.x, pos.y);
  403. }
  404. }
  405. }
  406. std::map<int, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors(std::string from)
  407. {
  408. std::map<int, std::pair<SDL_Color, SDL_Color> > ret;
  409. const JsonNode config(ResourceID(from, EResType::TEXT));
  410. BOOST_FOREACH(auto &m, config.Struct())
  411. {
  412. auto index = boost::find(GameConstants::TERRAIN_NAMES, m.first);
  413. if (index == boost::end(GameConstants::TERRAIN_NAMES))
  414. {
  415. tlog1 << "Error: unknown terrain in terrains.json: " << m.first << "\n";
  416. continue;
  417. }
  418. int terrainID = index - boost::begin(GameConstants::TERRAIN_NAMES);
  419. const JsonVector &unblockedVec = m.second["minimapUnblocked"].Vector();
  420. SDL_Color normal =
  421. {
  422. ui8(unblockedVec[0].Float()),
  423. ui8(unblockedVec[1].Float()),
  424. ui8(unblockedVec[2].Float()),
  425. ui8(255)
  426. };
  427. const JsonVector &blockedVec = m.second["minimapBlocked"].Vector();
  428. SDL_Color blocked =
  429. {
  430. ui8(blockedVec[0].Float()),
  431. ui8(blockedVec[1].Float()),
  432. ui8(blockedVec[2].Float()),
  433. ui8(255)
  434. };
  435. ret.insert(std::make_pair(terrainID, std::make_pair(normal, blocked)));
  436. }
  437. return ret;
  438. }
  439. CMinimap::CMinimap(const Rect &position):
  440. CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  441. aiShield(nullptr),
  442. minimap(nullptr),
  443. level(0),
  444. colors(loadColors("config/terrains.json"))
  445. {
  446. pos.w = position.w;
  447. pos.h = position.h;
  448. }
  449. int3 CMinimap::translateMousePosition()
  450. {
  451. // 0 = top-left corner, 1 = bottom-right corner
  452. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  453. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  454. int3 mapSizes = LOCPLINT->cb->getMapSize();
  455. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  456. return tile;
  457. }
  458. void CMinimap::moveAdvMapSelection()
  459. {
  460. int3 newLocation = translateMousePosition();
  461. adventureInt->centerOn(newLocation);
  462. if (!(adventureInt->active & GENERAL))
  463. GH.totalRedraw(); //redraw this as well as inactive adventure map
  464. else
  465. redraw();//redraw only this
  466. }
  467. void CMinimap::clickLeft(tribool down, bool previousState)
  468. {
  469. if (down)
  470. moveAdvMapSelection();
  471. }
  472. void CMinimap::clickRight(tribool down, bool previousState)
  473. {
  474. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  475. }
  476. void CMinimap::hover(bool on)
  477. {
  478. if (on)
  479. GH.statusbar->print(CGI->generaltexth->zelp[291].first);
  480. else
  481. GH.statusbar->clear();
  482. }
  483. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  484. {
  485. if (pressedL)
  486. moveAdvMapSelection();
  487. }
  488. void CMinimap::showAll()
  489. {
  490. CIntObject::showAll();
  491. if (minimap)
  492. {
  493. int3 mapSizes = LOCPLINT->cb->getMapSize();
  494. //draw radar
  495. // SDL_Rect oldClip;
  496. /*SDL_Rect radar =
  497. {
  498. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  499. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  500. ui16(adventureInt->terrain.tilesw * pos.w / mapSizes.x),
  501. ui16(adventureInt->terrain.tilesh * pos.h / mapSizes.y)
  502. };*/
  503. //* SDL_GetClipRect(to, &oldClip);
  504. //* SDL_SetClipRect(to, &pos);
  505. //* CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  506. //* SDL_SetClipRect(to, &oldClip);
  507. }
  508. }
  509. void CMinimap::update()
  510. {
  511. if (aiShield) //AI turn is going on. There is no need to update minimap
  512. return;
  513. OBJ_CONSTRUCTION_CAPTURING_ALL;
  514. vstd::clear_pointer(minimap);
  515. minimap = new CMinimapInstance(this, level);
  516. redraw();
  517. }
  518. void CMinimap::setLevel(int newLevel)
  519. {
  520. level = newLevel;
  521. update();
  522. }
  523. void CMinimap::setAIRadar(bool on)
  524. {
  525. if (on)
  526. {
  527. OBJ_CONSTRUCTION_CAPTURING_ALL;
  528. vstd::clear_pointer(minimap);
  529. if (!aiShield)
  530. aiShield = new CPicture("AIShield");
  531. }
  532. else
  533. {
  534. vstd::clear_pointer(aiShield);
  535. update();
  536. }
  537. }
  538. void CMinimap::hideTile(const int3 &pos)
  539. {
  540. if (minimap)
  541. minimap->refreshTile(pos);
  542. }
  543. void CMinimap::showTile(const int3 &pos)
  544. {
  545. if (minimap)
  546. minimap->refreshTile(pos);
  547. }
  548. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  549. CIntObject(0, position),
  550. aiProgress(nullptr)
  551. {
  552. }
  553. void CInfoBar::CVisibleInfo::show()
  554. {
  555. CIntObject::show();
  556. BOOST_FOREACH(auto object, forceRefresh)
  557. object->showAll();
  558. }
  559. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  560. {
  561. assert(children.empty()); // visible info should be re-created to change type
  562. OBJ_CONSTRUCTION_CAPTURING_ALL;
  563. new CPicture("ADSTATHR");
  564. new CHeroTooltip(Point(0,0), hero);
  565. }
  566. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  567. {
  568. assert(children.empty()); // visible info should be re-created to change type
  569. OBJ_CONSTRUCTION_CAPTURING_ALL;
  570. new CPicture("ADSTATCS");
  571. new CTownTooltip(Point(0,0), town);
  572. }
  573. void CInfoBar::CVisibleInfo::playNewDaySound()
  574. {
  575. if (LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  576. CCS->soundh->playSound(soundBase::newDay);
  577. else
  578. if (LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  579. CCS->soundh->playSound(soundBase::newWeek);
  580. else
  581. if (LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  582. CCS->soundh->playSound(soundBase::newMonth);
  583. else
  584. CCS->soundh->playSound(soundBase::newDay);
  585. }
  586. std::string CInfoBar::CVisibleInfo::getNewDayName()
  587. {
  588. if (LOCPLINT->cb->getDate(Date::DAY) == 1)
  589. return "NEWDAY";
  590. if (LOCPLINT->cb->getDate(Date::DAY) != 1)
  591. return "NEWDAY";
  592. switch(LOCPLINT->cb->getDate(Date::WEEK))
  593. {
  594. case 1: return "NEWWEEK1";
  595. case 2: return "NEWWEEK2";
  596. case 3: return "NEWWEEK3";
  597. case 4: return "NEWWEEK4";
  598. default: assert(0); return "";
  599. }
  600. }
  601. void CInfoBar::CVisibleInfo::loadDay()
  602. {
  603. assert(children.empty()); // visible info should be re-created first to change type
  604. playNewDaySound();
  605. OBJ_CONSTRUCTION_CAPTURING_ALL;
  606. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  607. std::string labelText;
  608. 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
  609. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  610. else
  611. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  612. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText));
  613. }
  614. void CInfoBar::CVisibleInfo::loadEnemyTurn(PlayerColor player)
  615. {
  616. assert(children.empty()); // visible info should be re-created to change type
  617. OBJ_CONSTRUCTION_CAPTURING_ALL;
  618. new CPicture("ADSTATNX");
  619. new CAnimImage("CREST58", player.getNum(), 0, 20, 51);
  620. new CShowableAnim(99, 51, "HOURSAND");
  621. // FIXME: currently there is no way to get progress from VCAI
  622. // if this will change at some point switch this ifdef to enable correct code
  623. #if 0
  624. //prepare hourglass for updating AI turn
  625. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  626. forceRefresh.push_back(aiProgress);
  627. #else
  628. //create hourglass that will be always animated ignoring AI status
  629. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  630. #endif
  631. }
  632. void CInfoBar::CVisibleInfo::loadGameStatus()
  633. {
  634. assert(children.empty()); // visible info should be re-created to change type
  635. //get amount of halls of each level
  636. std::vector<int> halls(4, 0);
  637. BOOST_FOREACH(auto town, LOCPLINT->towns)
  638. halls[town->hallLevel()]++;
  639. std::vector<PlayerColor> allies, enemies;
  640. //generate list of allies and enemies
  641. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  642. {
  643. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  644. {
  645. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  646. allies.push_back(PlayerColor(i));
  647. else
  648. enemies.push_back(PlayerColor(i));
  649. }
  650. }
  651. //generate component
  652. OBJ_CONSTRUCTION_CAPTURING_ALL;
  653. new CPicture("ADSTATIN");
  654. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  655. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  656. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  657. BOOST_FOREACH(PlayerColor & player, allies)
  658. {
  659. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 102);
  660. posx += image->pos.w;
  661. }
  662. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  663. BOOST_FOREACH(PlayerColor & player, enemies)
  664. {
  665. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 132);
  666. posx += image->pos.w;
  667. }
  668. for (size_t i=0; i<halls.size(); i++)
  669. {
  670. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  671. if (halls[i])
  672. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i]));
  673. }
  674. }
  675. void CInfoBar::CVisibleInfo::loadComponent(const Component & compToDisplay, std::string message)
  676. {
  677. assert(children.empty()); // visible info should be re-created to change type
  678. OBJ_CONSTRUCTION_CAPTURING_ALL;
  679. new CPicture("ADSTATOT", 1);
  680. auto comp = new CComponent(compToDisplay);
  681. comp->moveTo(Point(pos.x+47, pos.y+50));
  682. new CTextBox(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  683. }
  684. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  685. {
  686. if (aiProgress)
  687. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  688. }
  689. void CInfoBar::reset(EState newState = EMPTY)
  690. {
  691. OBJ_CONSTRUCTION_CAPTURING_ALL;
  692. vstd::clear_pointer(visibleInfo);
  693. currentObject = nullptr;
  694. state = newState;
  695. visibleInfo = new CVisibleInfo(Point(8, 12));
  696. }
  697. void CInfoBar::showSelection()
  698. {
  699. if (adventureInt->selection)
  700. {
  701. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  702. if (hero)
  703. {
  704. showHeroSelection(hero);
  705. return;
  706. }
  707. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  708. if (town)
  709. {
  710. showTownSelection(town);
  711. return;
  712. }
  713. }
  714. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  715. }
  716. void CInfoBar::tick()
  717. {
  718. removeUsedEvents(TIME);
  719. showSelection();
  720. }
  721. void CInfoBar::clickLeft(tribool down, bool previousState)
  722. {
  723. if (down)
  724. {
  725. if (state == HERO || state == TOWN)
  726. showGameStatus();
  727. else if (state == GAME)
  728. showDate();
  729. else
  730. showSelection();
  731. }
  732. }
  733. void CInfoBar::clickRight(tribool down, bool previousState)
  734. {
  735. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  736. }
  737. void CInfoBar::hover(bool on)
  738. {
  739. if (on)
  740. GH.statusbar->print(CGI->generaltexth->zelp[292].first);
  741. else
  742. GH.statusbar->clear();
  743. }
  744. CInfoBar::CInfoBar(const Rect &position):
  745. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  746. visibleInfo(nullptr),
  747. state(EMPTY),
  748. currentObject(nullptr)
  749. {
  750. pos.w = position.w;
  751. pos.h = position.h;
  752. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  753. }
  754. void CInfoBar::showDate()
  755. {
  756. reset(DATE);
  757. visibleInfo->loadDay();
  758. setTimer(3000);
  759. redraw();
  760. }
  761. void CInfoBar::showComponent(const Component & comp, std::string message)
  762. {
  763. reset(COMPONENT);
  764. visibleInfo->loadComponent(comp, message);
  765. setTimer(3000);
  766. redraw();
  767. }
  768. void CInfoBar::startEnemyTurn(PlayerColor color)
  769. {
  770. reset(AITURN);
  771. visibleInfo->loadEnemyTurn(color);
  772. redraw();
  773. }
  774. void CInfoBar::updateEnemyTurn(double progress)
  775. {
  776. assert(state == AITURN);
  777. visibleInfo->updateEnemyTurn(progress);
  778. redraw();
  779. }
  780. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  781. {
  782. if (!hero)
  783. return;
  784. reset(HERO);
  785. currentObject = hero;
  786. visibleInfo->loadHero(hero);
  787. redraw();
  788. }
  789. void CInfoBar::showTownSelection(const CGTownInstance * town)
  790. {
  791. if (!town)
  792. return;
  793. reset(TOWN);
  794. currentObject = town;
  795. visibleInfo->loadTown(town);
  796. redraw();
  797. }
  798. void CInfoBar::showGameStatus()
  799. {
  800. reset(GAME);
  801. visibleInfo->loadGameStatus();
  802. setTimer(3000);
  803. redraw();
  804. }