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