AdventureMapClasses.cpp 31 KB

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