2
0

AdventureMapClasses.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. #include "StdInc.h"
  2. #include "AdventureMapClasses.h"
  3. #include <SDL.h>
  4. #include "MiscWidgets.h"
  5. #include "CComponent.h"
  6. #include "../CGameInfo.h"
  7. #include "../CMusicHandler.h"
  8. #include "../CDefHandler.h"
  9. #include "../CPlayerInterface.h"
  10. #include "../CPreGame.h"
  11. #include "../Graphics.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/SDL_Pixels.h"
  14. #include "../windows/InfoWindows.h"
  15. #include "../windows/CAdvmapInterface.h"
  16. #include "../windows/GUIClasses.h"
  17. #include "../battle/CBattleInterfaceClasses.h"
  18. #include "../battle/CBattleInterface.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/StartInfo.h"
  21. #include "../../lib/CGameState.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. #include "../../lib/CHeroHandler.h"
  24. #include "../../lib/CModHandler.h"
  25. #include "../../lib/CTownHandler.h"
  26. #include "../../lib/filesystem/Filesystem.h"
  27. #include "../../lib/JsonNode.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapping/CMap.h"
  30. #include "../../lib/NetPacksBase.h"
  31. #include "../../lib/StringConstants.h"
  32. /*
  33. * CAdventureMapClasses.cpp, part of VCMI engine
  34. *
  35. * Authors: listed in file AUTHORS in main folder
  36. *
  37. * License: GNU General Public License v2.0 or later
  38. * Full text of license available in license.txt file, in main folder
  39. *
  40. */
  41. CList::CListItem::CListItem(CList * Parent):
  42. CIntObject(LCLICK | RCLICK | HOVER),
  43. parent(Parent),
  44. selection(nullptr)
  45. {
  46. }
  47. CList::CListItem::~CListItem()
  48. {
  49. // select() method in this was already destroyed so we can't safely call method in parent
  50. if (parent->selected == this)
  51. parent->selected = nullptr;
  52. }
  53. void CList::CListItem::clickRight(tribool down, bool previousState)
  54. {
  55. if (down == true)
  56. showTooltip();
  57. }
  58. void CList::CListItem::clickLeft(tribool down, bool previousState)
  59. {
  60. if (down == true)
  61. {
  62. //second click on already selected item
  63. if (parent->selected == this)
  64. open();
  65. else
  66. {
  67. //first click - switch selection
  68. parent->select(this);
  69. }
  70. }
  71. }
  72. void CList::CListItem::hover(bool on)
  73. {
  74. if (on)
  75. GH.statusbar->setText(getHoverText());
  76. else
  77. GH.statusbar->clear();
  78. }
  79. void CList::CListItem::onSelect(bool on)
  80. {
  81. OBJ_CONSTRUCTION_CAPTURING_ALL;
  82. vstd::clear_pointer(selection);
  83. if (on)
  84. selection = genSelection();
  85. select(on);
  86. GH.totalRedraw();
  87. }
  88. CList::CList(int Size, Point position, std::string btnUp, std::string btnDown, size_t listAmount,
  89. int helpUp, int helpDown, CListBox::CreateFunc create, CListBox::DestroyFunc destroy):
  90. CIntObject(0, position),
  91. size(Size),
  92. selected(nullptr)
  93. {
  94. OBJ_CONSTRUCTION_CAPTURING_ALL;
  95. scrollUp = new CButton(Point(0, 0), btnUp, CGI->generaltexth->zelp[helpUp]);
  96. list = new CListBox(create, destroy, Point(1,scrollUp->pos.h), Point(0, 32), size, listAmount);
  97. //assign callback only after list was created
  98. scrollUp->addCallback(std::bind(&CListBox::moveToPrev, list));
  99. scrollDown = new CButton(Point(0, scrollUp->pos.h + 32*size), btnDown, CGI->generaltexth->zelp[helpDown], std::bind(&CListBox::moveToNext, list));
  100. scrollDown->addCallback(std::bind(&CList::update, this));
  101. scrollUp->addCallback(std::bind(&CList::update, this));
  102. update();
  103. }
  104. void CList::update()
  105. {
  106. bool onTop = list->getPos() == 0;
  107. bool onBottom = list->getPos() + size >= list->size();
  108. scrollUp->block(onTop);
  109. scrollDown->block(onBottom);
  110. }
  111. void CList::select(CListItem *which)
  112. {
  113. if (selected == which)
  114. return;
  115. if (selected)
  116. selected->onSelect(false);
  117. selected = which;
  118. if (which)
  119. {
  120. which->onSelect(true);
  121. onSelect();
  122. }
  123. }
  124. int CList::getSelectedIndex()
  125. {
  126. return list->getIndexOf(selected);
  127. }
  128. void CList::selectIndex(int which)
  129. {
  130. if (which < 0)
  131. {
  132. if (selected)
  133. select(nullptr);
  134. }
  135. else
  136. {
  137. list->scrollTo(which);
  138. update();
  139. select(dynamic_cast<CListItem*>(list->getItem(which)));
  140. }
  141. }
  142. void CList::selectNext()
  143. {
  144. int index = getSelectedIndex();
  145. if (index < 0)
  146. selectIndex(0);
  147. else if (index + 1 < list->size())
  148. selectIndex(index+1);
  149. }
  150. void CList::selectPrev()
  151. {
  152. int index = getSelectedIndex();
  153. if (index <= 0)
  154. selectIndex(0);
  155. else
  156. selectIndex(index-1);
  157. }
  158. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  159. {
  160. OBJ_CONSTRUCTION_CAPTURING_ALL;
  161. auto move = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  162. auto img = new CPicture("HPSXXX", move->pos.w + 1);
  163. auto mana = new CAnimImage("IMANA", 0, 0, move->pos.w + img->pos.w + 2, 1 );
  164. pos.w = mana->pos.w + mana->pos.x - pos.x;
  165. pos.h = std::max(std::max<SDLX_Size>(move->pos.h + 1, mana->pos.h + 1), img->pos.h);
  166. }
  167. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero):
  168. CListItem(parent),
  169. hero(Hero)
  170. {
  171. OBJ_CONSTRUCTION_CAPTURING_ALL;
  172. movement = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  173. portrait = new CAnimImage("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  174. mana = new CAnimImage("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  175. pos.w = mana->pos.w + mana->pos.x - pos.x;
  176. pos.h = std::max(std::max<SDLX_Size>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  177. update();
  178. }
  179. void CHeroList::CHeroItem::update()
  180. {
  181. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  182. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  183. redraw();
  184. }
  185. CIntObject * CHeroList::CHeroItem::genSelection()
  186. {
  187. return new CPicture("HPSYYY", movement->pos.w + 1);
  188. }
  189. void CHeroList::CHeroItem::select(bool on)
  190. {
  191. if (on && adventureInt->selection != hero)
  192. adventureInt->select(hero);
  193. }
  194. void CHeroList::CHeroItem::open()
  195. {
  196. LOCPLINT->openHeroWindow(hero);
  197. }
  198. void CHeroList::CHeroItem::showTooltip()
  199. {
  200. CRClickPopup::createAndPush(hero, GH.current->motion);
  201. }
  202. std::string CHeroList::CHeroItem::getHoverText()
  203. {
  204. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->name % hero->type->heroClass->name);
  205. }
  206. CIntObject * CHeroList::createHeroItem(size_t index)
  207. {
  208. if (LOCPLINT->wanderingHeroes.size() > index)
  209. return new CHeroItem(this, LOCPLINT->wanderingHeroes[index]);
  210. return new CEmptyHeroItem();
  211. }
  212. CHeroList::CHeroList(int size, Point position, std::string btnUp, std::string btnDown):
  213. CList(size, position, btnUp, btnDown, LOCPLINT->wanderingHeroes.size(), 303, 304, std::bind(&CHeroList::createHeroItem, this, _1))
  214. {
  215. }
  216. void CHeroList::select(const CGHeroInstance * hero)
  217. {
  218. selectIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  219. }
  220. void CHeroList::update(const CGHeroInstance * hero)
  221. {
  222. //this hero is already present, update its status
  223. for (auto & elem : list->getItems())
  224. {
  225. auto item = dynamic_cast<CHeroItem*>(elem);
  226. if (item && item->hero == hero && vstd::contains(LOCPLINT->wanderingHeroes, hero))
  227. {
  228. item->update();
  229. return;
  230. }
  231. }
  232. //simplest solution for now: reset list and restore selection
  233. list->resize(LOCPLINT->wanderingHeroes.size());
  234. if (adventureInt->selection)
  235. {
  236. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  237. if (hero)
  238. select(hero);
  239. }
  240. CList::update();
  241. }
  242. CIntObject * CTownList::createTownItem(size_t index)
  243. {
  244. if (LOCPLINT->towns.size() > index)
  245. return new CTownItem(this, LOCPLINT->towns[index]);
  246. return new CAnimImage("ITPA", 0);
  247. }
  248. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  249. CListItem(parent),
  250. town(Town)
  251. {
  252. OBJ_CONSTRUCTION_CAPTURING_ALL;
  253. picture = new CAnimImage("ITPA", 0);
  254. pos = picture->pos;
  255. update();
  256. }
  257. CIntObject * CTownList::CTownItem::genSelection()
  258. {
  259. return new CAnimImage("ITPA", 1);
  260. }
  261. void CTownList::CTownItem::update()
  262. {
  263. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  264. picture->setFrame(iconIndex + 2);
  265. redraw();
  266. }
  267. void CTownList::CTownItem::select(bool on)
  268. {
  269. if (on && adventureInt->selection != town)
  270. adventureInt->select(town);
  271. }
  272. void CTownList::CTownItem::open()
  273. {
  274. LOCPLINT->openTownWindow(town);
  275. }
  276. void CTownList::CTownItem::showTooltip()
  277. {
  278. CRClickPopup::createAndPush(town, GH.current->motion);
  279. }
  280. std::string CTownList::CTownItem::getHoverText()
  281. {
  282. return town->getObjectName();
  283. }
  284. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  285. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, std::bind(&CTownList::createTownItem, this, _1))
  286. {
  287. }
  288. void CTownList::select(const CGTownInstance * town)
  289. {
  290. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  291. }
  292. void CTownList::update(const CGTownInstance *)
  293. {
  294. //simplest solution for now: reset list and restore selection
  295. list->resize(LOCPLINT->towns.size());
  296. if (adventureInt->selection)
  297. {
  298. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  299. if (town)
  300. select(town);
  301. }
  302. CList::update();
  303. }
  304. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  305. {
  306. static const SDL_Color fogOfWar = {0, 0, 0, 255};
  307. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  308. // if tile is not visible it will be black on minimap
  309. if(!tile)
  310. return fogOfWar;
  311. // if object at tile is owned - it will be colored as its owner
  312. for (const CGObjectInstance *obj : tile->blockingObjects)
  313. {
  314. //heroes will be blitted later
  315. switch (obj->ID)
  316. {
  317. case Obj::HERO:
  318. case Obj::PRISON:
  319. continue;
  320. }
  321. PlayerColor player = obj->getOwner();
  322. if(player == PlayerColor::NEUTRAL)
  323. return *graphics->neutralColor;
  324. else
  325. if (player < PlayerColor::PLAYER_LIMIT)
  326. return graphics->playerColors[player.getNum()];
  327. }
  328. // else - use terrain color (blocked version or normal)
  329. if (tile->blocked && (!tile->visitable))
  330. return parent->colors.find(tile->terType)->second.second;
  331. else
  332. return parent->colors.find(tile->terType)->second.first;
  333. }
  334. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  335. {
  336. int3 mapSizes = LOCPLINT->cb->getMapSize();
  337. double stepX = double(pos.w) / mapSizes.x;
  338. double stepY = double(pos.h) / mapSizes.y;
  339. x = toX + stepX * tile.x;
  340. y = toY + stepY * tile.y;
  341. }
  342. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  343. {
  344. //coordinates of rectangle on minimap representing this tile
  345. // begin - first to blit, end - first NOT to blit
  346. int xBegin, yBegin, xEnd, yEnd;
  347. tileToPixels (tile, xBegin, yBegin, toX, toY);
  348. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  349. for (int y=yBegin; y<yEnd; y++)
  350. {
  351. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  352. for (int x=xBegin; x<xEnd; x++)
  353. ColorPutter<4, 1>::PutColor(ptr, color);
  354. }
  355. }
  356. void CMinimapInstance::refreshTile(const int3 &tile)
  357. {
  358. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  359. }
  360. void CMinimapInstance::drawScaled(int level)
  361. {
  362. int3 mapSizes = LOCPLINT->cb->getMapSize();
  363. //size of one map tile on our minimap
  364. double stepX = double(pos.w) / mapSizes.x;
  365. double stepY = double(pos.h) / mapSizes.y;
  366. double currY = 0;
  367. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  368. {
  369. double currX = 0;
  370. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  371. {
  372. const SDL_Color &color = getTileColor(int3(x,y,level));
  373. //coordinates of rectangle on minimap representing this tile
  374. // begin - first to blit, end - first NOT to blit
  375. int xBegin = currX;
  376. int yBegin = currY;
  377. int xEnd = currX + stepX;
  378. int yEnd = currY + stepY;
  379. for (int y=yBegin; y<yEnd; y++)
  380. {
  381. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  382. for (int x=xBegin; x<xEnd; x++)
  383. ColorPutter<4, 1>::PutColor(ptr, color);
  384. }
  385. }
  386. }
  387. }
  388. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  389. parent(Parent),
  390. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  391. level(Level)
  392. {
  393. pos.w = parent->pos.w;
  394. pos.h = parent->pos.h;
  395. drawScaled(level);
  396. }
  397. CMinimapInstance::~CMinimapInstance()
  398. {
  399. SDL_FreeSurface(minimap);
  400. }
  401. void CMinimapInstance::showAll(SDL_Surface *to)
  402. {
  403. blitAtLoc(minimap, 0, 0, to);
  404. //draw heroes
  405. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  406. for(auto & hero : heroes)
  407. {
  408. int3 position = hero->getPosition(false);
  409. if (position.z == level)
  410. {
  411. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  412. blitTileWithColor(color, position, to, pos.x, pos.y);
  413. }
  414. }
  415. }
  416. std::map<int, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors(std::string from)
  417. {
  418. std::map<int, std::pair<SDL_Color, SDL_Color> > ret;
  419. const JsonNode config(ResourceID(from, EResType::TEXT));
  420. for(auto &m : config.Struct())
  421. {
  422. auto index = boost::find(GameConstants::TERRAIN_NAMES, m.first);
  423. if (index == std::end(GameConstants::TERRAIN_NAMES))
  424. {
  425. logGlobal->errorStream() << "Error: unknown terrain in terrains.json: " << m.first;
  426. continue;
  427. }
  428. int terrainID = index - std::begin(GameConstants::TERRAIN_NAMES);
  429. const JsonVector &unblockedVec = m.second["minimapUnblocked"].Vector();
  430. SDL_Color normal =
  431. {
  432. ui8(unblockedVec[0].Float()),
  433. ui8(unblockedVec[1].Float()),
  434. ui8(unblockedVec[2].Float()),
  435. ui8(255)
  436. };
  437. const JsonVector &blockedVec = m.second["minimapBlocked"].Vector();
  438. SDL_Color blocked =
  439. {
  440. ui8(blockedVec[0].Float()),
  441. ui8(blockedVec[1].Float()),
  442. ui8(blockedVec[2].Float()),
  443. ui8(255)
  444. };
  445. ret.insert(std::make_pair(terrainID, std::make_pair(normal, blocked)));
  446. }
  447. return ret;
  448. }
  449. CMinimap::CMinimap(const Rect &position):
  450. CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  451. aiShield(nullptr),
  452. minimap(nullptr),
  453. level(0),
  454. colors(loadColors("config/terrains.json"))
  455. {
  456. pos.w = position.w;
  457. pos.h = position.h;
  458. }
  459. int3 CMinimap::translateMousePosition()
  460. {
  461. // 0 = top-left corner, 1 = bottom-right corner
  462. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  463. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  464. int3 mapSizes = LOCPLINT->cb->getMapSize();
  465. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  466. return tile;
  467. }
  468. void CMinimap::moveAdvMapSelection()
  469. {
  470. int3 newLocation = translateMousePosition();
  471. adventureInt->centerOn(newLocation);
  472. if (!(adventureInt->active & GENERAL))
  473. GH.totalRedraw(); //redraw this as well as inactive adventure map
  474. else
  475. redraw();//redraw only this
  476. }
  477. void CMinimap::clickLeft(tribool down, bool previousState)
  478. {
  479. if (down)
  480. moveAdvMapSelection();
  481. }
  482. void CMinimap::clickRight(tribool down, bool previousState)
  483. {
  484. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  485. }
  486. void CMinimap::hover(bool on)
  487. {
  488. if (on)
  489. GH.statusbar->setText(CGI->generaltexth->zelp[291].first);
  490. else
  491. GH.statusbar->clear();
  492. }
  493. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  494. {
  495. if (pressedL)
  496. moveAdvMapSelection();
  497. }
  498. void CMinimap::showAll(SDL_Surface * to)
  499. {
  500. CIntObject::showAll(to);
  501. if (minimap)
  502. {
  503. int3 mapSizes = LOCPLINT->cb->getMapSize();
  504. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  505. //draw radar
  506. SDL_Rect oldClip;
  507. SDL_Rect radar =
  508. {
  509. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  510. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  511. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  512. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  513. };
  514. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  515. {
  516. // adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
  517. radar.x = std::min(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
  518. radar.y = std::min(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
  519. if (radar.x < pos.x && radar.y < pos.y)
  520. return; // whole map is visible at once, no point in redrawing border
  521. }
  522. SDL_GetClipRect(to, &oldClip);
  523. SDL_SetClipRect(to, &pos);
  524. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  525. SDL_SetClipRect(to, &oldClip);
  526. }
  527. }
  528. void CMinimap::update()
  529. {
  530. if (aiShield) //AI turn is going on. There is no need to update minimap
  531. return;
  532. OBJ_CONSTRUCTION_CAPTURING_ALL;
  533. vstd::clear_pointer(minimap);
  534. minimap = new CMinimapInstance(this, level);
  535. redraw();
  536. }
  537. void CMinimap::setLevel(int newLevel)
  538. {
  539. level = newLevel;
  540. update();
  541. }
  542. void CMinimap::setAIRadar(bool on)
  543. {
  544. if (on)
  545. {
  546. OBJ_CONSTRUCTION_CAPTURING_ALL;
  547. vstd::clear_pointer(minimap);
  548. if (!aiShield)
  549. aiShield = new CPicture("AIShield");
  550. }
  551. else
  552. {
  553. vstd::clear_pointer(aiShield);
  554. update();
  555. }
  556. // this my happen during AI turn when this interface is inactive
  557. // force redraw in order to properly update interface
  558. GH.totalRedraw();
  559. }
  560. void CMinimap::hideTile(const int3 &pos)
  561. {
  562. if (minimap)
  563. minimap->refreshTile(pos);
  564. }
  565. void CMinimap::showTile(const int3 &pos)
  566. {
  567. if (minimap)
  568. minimap->refreshTile(pos);
  569. }
  570. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  571. CIntObject(0, position),
  572. aiProgress(nullptr)
  573. {
  574. }
  575. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  576. {
  577. CIntObject::show(to);
  578. for(auto object : forceRefresh)
  579. object->showAll(to);
  580. }
  581. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  582. {
  583. assert(children.empty()); // visible info should be re-created to change type
  584. OBJ_CONSTRUCTION_CAPTURING_ALL;
  585. new CPicture("ADSTATHR");
  586. new CHeroTooltip(Point(0,0), hero);
  587. }
  588. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  589. {
  590. assert(children.empty()); // visible info should be re-created to change type
  591. OBJ_CONSTRUCTION_CAPTURING_ALL;
  592. new CPicture("ADSTATCS");
  593. new CTownTooltip(Point(0,0), town);
  594. }
  595. void CInfoBar::CVisibleInfo::playNewDaySound()
  596. {
  597. if (LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  598. CCS->soundh->playSound(soundBase::newDay);
  599. else
  600. if (LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  601. CCS->soundh->playSound(soundBase::newWeek);
  602. else
  603. if (LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  604. CCS->soundh->playSound(soundBase::newMonth);
  605. else
  606. CCS->soundh->playSound(soundBase::newDay);
  607. }
  608. std::string CInfoBar::CVisibleInfo::getNewDayName()
  609. {
  610. if (LOCPLINT->cb->getDate(Date::DAY) == 1)
  611. return "NEWDAY";
  612. if (LOCPLINT->cb->getDate(Date::DAY) != 1)
  613. return "NEWDAY";
  614. switch(LOCPLINT->cb->getDate(Date::WEEK))
  615. {
  616. case 1: return "NEWWEEK1";
  617. case 2: return "NEWWEEK2";
  618. case 3: return "NEWWEEK3";
  619. case 4: return "NEWWEEK4";
  620. default: assert(0); return "";
  621. }
  622. }
  623. void CInfoBar::CVisibleInfo::loadDay()
  624. {
  625. assert(children.empty()); // visible info should be re-created first to change type
  626. playNewDaySound();
  627. OBJ_CONSTRUCTION_CAPTURING_ALL;
  628. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  629. std::string labelText;
  630. 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
  631. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  632. else
  633. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  634. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText));
  635. }
  636. void CInfoBar::CVisibleInfo::loadEnemyTurn(PlayerColor player)
  637. {
  638. assert(children.empty()); // visible info should be re-created to change type
  639. OBJ_CONSTRUCTION_CAPTURING_ALL;
  640. new CPicture("ADSTATNX");
  641. new CAnimImage("CREST58", player.getNum(), 0, 20, 51);
  642. new CShowableAnim(99, 51, "HOURSAND");
  643. // FIXME: currently there is no way to get progress from VCAI
  644. // if this will change at some point switch this ifdef to enable correct code
  645. #if 0
  646. //prepare hourglass for updating AI turn
  647. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  648. forceRefresh.push_back(aiProgress);
  649. #else
  650. //create hourglass that will be always animated ignoring AI status
  651. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  652. #endif
  653. }
  654. void CInfoBar::CVisibleInfo::loadGameStatus()
  655. {
  656. assert(children.empty()); // visible info should be re-created to change type
  657. //get amount of halls of each level
  658. std::vector<int> halls(4, 0);
  659. for(auto town : LOCPLINT->towns)
  660. halls[town->hallLevel()]++;
  661. std::vector<PlayerColor> allies, enemies;
  662. //generate list of allies and enemies
  663. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  664. {
  665. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  666. {
  667. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  668. allies.push_back(PlayerColor(i));
  669. else
  670. enemies.push_back(PlayerColor(i));
  671. }
  672. }
  673. //generate component
  674. OBJ_CONSTRUCTION_CAPTURING_ALL;
  675. new CPicture("ADSTATIN");
  676. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  677. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  678. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  679. for(PlayerColor & player : allies)
  680. {
  681. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 102);
  682. posx += image->pos.w;
  683. }
  684. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  685. for(PlayerColor & player : enemies)
  686. {
  687. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 132);
  688. posx += image->pos.w;
  689. }
  690. for (size_t i=0; i<halls.size(); i++)
  691. {
  692. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  693. if (halls[i])
  694. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i]));
  695. }
  696. }
  697. void CInfoBar::CVisibleInfo::loadComponent(const Component & compToDisplay, std::string message)
  698. {
  699. assert(children.empty()); // visible info should be re-created to change type
  700. OBJ_CONSTRUCTION_CAPTURING_ALL;
  701. new CPicture("ADSTATOT", 1);
  702. auto comp = new CComponent(compToDisplay);
  703. comp->moveTo(Point(pos.x+47, pos.y+50));
  704. new CTextBox(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  705. }
  706. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  707. {
  708. if (aiProgress)
  709. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  710. }
  711. void CInfoBar::reset(EState newState = EMPTY)
  712. {
  713. OBJ_CONSTRUCTION_CAPTURING_ALL;
  714. vstd::clear_pointer(visibleInfo);
  715. currentObject = nullptr;
  716. state = newState;
  717. visibleInfo = new CVisibleInfo(Point(8, 12));
  718. }
  719. void CInfoBar::showSelection()
  720. {
  721. if (adventureInt->selection)
  722. {
  723. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  724. if (hero)
  725. {
  726. showHeroSelection(hero);
  727. return;
  728. }
  729. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  730. if (town)
  731. {
  732. showTownSelection(town);
  733. return;
  734. }
  735. }
  736. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  737. }
  738. void CInfoBar::tick()
  739. {
  740. removeUsedEvents(TIME);
  741. showSelection();
  742. }
  743. void CInfoBar::clickLeft(tribool down, bool previousState)
  744. {
  745. if (down)
  746. {
  747. if (state == HERO || state == TOWN)
  748. showGameStatus();
  749. else if (state == GAME)
  750. showDate();
  751. else
  752. showSelection();
  753. }
  754. }
  755. void CInfoBar::clickRight(tribool down, bool previousState)
  756. {
  757. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  758. }
  759. void CInfoBar::hover(bool on)
  760. {
  761. if (on)
  762. GH.statusbar->setText(CGI->generaltexth->zelp[292].first);
  763. else
  764. GH.statusbar->clear();
  765. }
  766. CInfoBar::CInfoBar(const Rect &position):
  767. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  768. visibleInfo(nullptr),
  769. state(EMPTY),
  770. currentObject(nullptr)
  771. {
  772. pos.w = position.w;
  773. pos.h = position.h;
  774. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  775. }
  776. void CInfoBar::showDate()
  777. {
  778. reset(DATE);
  779. visibleInfo->loadDay();
  780. setTimer(3000);
  781. redraw();
  782. }
  783. void CInfoBar::showComponent(const Component & comp, std::string message)
  784. {
  785. reset(COMPONENT);
  786. visibleInfo->loadComponent(comp, message);
  787. setTimer(3000);
  788. redraw();
  789. }
  790. void CInfoBar::startEnemyTurn(PlayerColor color)
  791. {
  792. reset(AITURN);
  793. visibleInfo->loadEnemyTurn(color);
  794. redraw();
  795. }
  796. void CInfoBar::updateEnemyTurn(double progress)
  797. {
  798. assert(state == AITURN);
  799. visibleInfo->updateEnemyTurn(progress);
  800. redraw();
  801. }
  802. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  803. {
  804. if (!hero)
  805. return;
  806. reset(HERO);
  807. currentObject = hero;
  808. visibleInfo->loadHero(hero);
  809. redraw();
  810. }
  811. void CInfoBar::showTownSelection(const CGTownInstance * town)
  812. {
  813. if (!town)
  814. return;
  815. reset(TOWN);
  816. currentObject = town;
  817. visibleInfo->loadTown(town);
  818. redraw();
  819. }
  820. void CInfoBar::showGameStatus()
  821. {
  822. reset(GAME);
  823. visibleInfo->loadGameStatus();
  824. setTimer(3000);
  825. redraw();
  826. }
  827. void CInGameConsole::show(SDL_Surface * to)
  828. {
  829. int number = 0;
  830. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  831. boost::unique_lock<boost::mutex> lock(texts_mx);
  832. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  833. {
  834. Point leftBottomCorner(0, screen->h);
  835. if(LOCPLINT->battleInt)
  836. {
  837. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  838. }
  839. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  840. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  841. if(SDL_GetTicks() - it->second > defaultTimeout)
  842. {
  843. toDel.push_back(it);
  844. }
  845. }
  846. for(auto & elem : toDel)
  847. {
  848. texts.erase(elem);
  849. }
  850. }
  851. void CInGameConsole::print(const std::string &txt)
  852. {
  853. boost::unique_lock<boost::mutex> lock(texts_mx);
  854. int lineLen = conf.go()->ac.outputLineLength;
  855. if(txt.size() < lineLen)
  856. {
  857. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  858. if(texts.size() > maxDisplayedTexts)
  859. {
  860. texts.pop_front();
  861. }
  862. }
  863. else
  864. {
  865. assert(lineLen);
  866. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  867. {
  868. std::string part = txt.substr(g * lineLen, lineLen);
  869. if(part.size() == 0)
  870. break;
  871. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  872. if(texts.size() > maxDisplayedTexts)
  873. {
  874. texts.pop_front();
  875. }
  876. }
  877. }
  878. }
  879. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  880. {
  881. if(key.type != SDL_KEYDOWN) return;
  882. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  883. switch(key.keysym.sym)
  884. {
  885. case SDLK_TAB:
  886. case SDLK_ESCAPE:
  887. {
  888. if(captureAllKeys)
  889. {
  890. captureAllKeys = false;
  891. endEnteringText(false);
  892. }
  893. else if(SDLK_TAB)
  894. {
  895. captureAllKeys = true;
  896. startEnteringText();
  897. }
  898. break;
  899. }
  900. case SDLK_RETURN: //enter key
  901. {
  902. if(enteredText.size() > 0 && captureAllKeys)
  903. {
  904. captureAllKeys = false;
  905. endEnteringText(true);
  906. CCS->soundh->playSound("CHAT");
  907. }
  908. break;
  909. }
  910. case SDLK_BACKSPACE:
  911. {
  912. if(enteredText.size() > 1)
  913. {
  914. Unicode::trimRight(enteredText,2);
  915. enteredText += '_';
  916. refreshEnteredText();
  917. }
  918. break;
  919. }
  920. case SDLK_UP: //up arrow
  921. {
  922. if(previouslyEntered.size() == 0)
  923. break;
  924. if(prevEntDisp == -1)
  925. {
  926. prevEntDisp = previouslyEntered.size() - 1;
  927. enteredText = previouslyEntered[prevEntDisp] + "_";
  928. refreshEnteredText();
  929. }
  930. else if( prevEntDisp > 0)
  931. {
  932. --prevEntDisp;
  933. enteredText = previouslyEntered[prevEntDisp] + "_";
  934. refreshEnteredText();
  935. }
  936. break;
  937. }
  938. case SDLK_DOWN: //down arrow
  939. {
  940. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  941. {
  942. ++prevEntDisp;
  943. enteredText = previouslyEntered[prevEntDisp] + "_";
  944. refreshEnteredText();
  945. }
  946. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  947. {
  948. prevEntDisp = -1;
  949. enteredText = "_";
  950. refreshEnteredText();
  951. }
  952. break;
  953. }
  954. default:
  955. {
  956. #ifdef VCMI_SDL1
  957. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  958. {
  959. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  960. {
  961. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  962. enteredText += "_";
  963. refreshEnteredText();
  964. }
  965. }
  966. #endif // VCMI_SDL1
  967. break;
  968. }
  969. }
  970. }
  971. #ifndef VCMI_SDL1
  972. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  973. {
  974. if(!captureAllKeys || enteredText.size() == 0)
  975. return;
  976. enteredText.resize(enteredText.size()-1);
  977. enteredText += event.text;
  978. enteredText += "_";
  979. refreshEnteredText();
  980. }
  981. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  982. {
  983. //do nothing here
  984. }
  985. #endif // VCMI_SDL1
  986. void CInGameConsole::startEnteringText()
  987. {
  988. CSDL_Ext::startTextInput(&pos);
  989. enteredText = "_";
  990. if(GH.topInt() == adventureInt)
  991. {
  992. GH.statusbar->alignment = TOPLEFT;
  993. GH.statusbar->setText(enteredText);
  994. //Prevent changes to the text from mouse interaction with the adventure map
  995. GH.statusbar->lock(true);
  996. }
  997. else if(LOCPLINT->battleInt)
  998. {
  999. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1000. }
  1001. }
  1002. void CInGameConsole::endEnteringText(bool printEnteredText)
  1003. {
  1004. CSDL_Ext::stopTextInput();
  1005. prevEntDisp = -1;
  1006. if(printEnteredText)
  1007. {
  1008. std::string txt = enteredText.substr(0, enteredText.size()-1);
  1009. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  1010. previouslyEntered.push_back(txt);
  1011. //print(txt);
  1012. }
  1013. enteredText = "";
  1014. if(GH.topInt() == adventureInt)
  1015. {
  1016. GH.statusbar->alignment = CENTER;
  1017. GH.statusbar->lock(false);
  1018. GH.statusbar->clear();
  1019. }
  1020. else if(LOCPLINT->battleInt)
  1021. {
  1022. LOCPLINT->battleInt->console->ingcAlter = "";
  1023. }
  1024. }
  1025. void CInGameConsole::refreshEnteredText()
  1026. {
  1027. if(GH.topInt() == adventureInt)
  1028. {
  1029. GH.statusbar->lock(false);
  1030. GH.statusbar->clear();
  1031. GH.statusbar->setText(enteredText);
  1032. GH.statusbar->lock(true);
  1033. }
  1034. else if(LOCPLINT->battleInt)
  1035. {
  1036. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1037. }
  1038. }
  1039. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  1040. {
  1041. #ifdef VCMI_SDL1
  1042. addUsedEvents(KEYBOARD);
  1043. #else
  1044. addUsedEvents(KEYBOARD | TEXTINPUT);
  1045. #endif
  1046. }
  1047. CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position)
  1048. : CIntObject(),
  1049. background(bg)
  1050. {
  1051. defActions = 255;
  1052. recActions = 255;
  1053. pos.x += position.x;
  1054. pos.y += position.y;
  1055. }
  1056. CAdvMapPanel::~CAdvMapPanel()
  1057. {
  1058. if (background)
  1059. SDL_FreeSurface(background);
  1060. }
  1061. void CAdvMapPanel::addChildColorableButton(CButton * btn)
  1062. {
  1063. buttons.push_back(btn);
  1064. addChildToPanel(btn, ACTIVATE | DEACTIVATE);
  1065. }
  1066. void CAdvMapPanel::setPlayerColor(const PlayerColor & clr)
  1067. {
  1068. for (auto &btn : buttons)
  1069. {
  1070. btn->setPlayerColor(clr);
  1071. }
  1072. }
  1073. void CAdvMapPanel::showAll(SDL_Surface * to)
  1074. {
  1075. if (background)
  1076. blitAt(background, pos.x, pos.y, to);
  1077. CIntObject::showAll(to);
  1078. }
  1079. void CAdvMapPanel::addChildToPanel(CIntObject * obj, ui8 actions /* = 0 */)
  1080. {
  1081. obj->recActions |= actions | SHOWALL;
  1082. addChild(obj, false);
  1083. }
  1084. CAdvMapWorldViewPanel::CAdvMapWorldViewPanel(SDL_Surface * bg, Point position)
  1085. : CAdvMapPanel(bg, position)
  1086. {
  1087. }
  1088. void CAdvMapWorldViewPanel::recolorIcons(const CDefHandler *def, int indexOffset)
  1089. {
  1090. for (auto &pic : currentIcons)
  1091. {
  1092. removeChild(pic);
  1093. delete pic;
  1094. }
  1095. currentIcons.clear();
  1096. for (auto &data : iconsData)
  1097. {
  1098. auto pic = new CPicture(def->ourImages[data.first + indexOffset].bitmap, data.second.x, data.second.y, false);
  1099. pic->recActions |= SHOWALL;
  1100. currentIcons.push_back(pic);
  1101. addChildToPanel(pic);
  1102. }
  1103. }
  1104. void CAdvMapWorldViewPanel::addChildIcon(std::pair<int, Point> data, const CDefHandler *def, int indexOffset)
  1105. {
  1106. iconsData.push_back(data);
  1107. auto pic = new CPicture(def->ourImages[data.first + indexOffset].bitmap, data.second.x, data.second.y, false);
  1108. currentIcons.push_back(pic);
  1109. addChildToPanel(pic);
  1110. }