AdventureMapClasses.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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, GH.getCursorPosition());
  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, GH.getCursorPosition());
  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. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  302. // if tile is not visible it will be black on minimap
  303. if(!tile)
  304. return Colors::BLACK;
  305. // if object at tile is owned - it will be colored as its owner
  306. for (const CGObjectInstance *obj : tile->blockingObjects)
  307. {
  308. //heroes will be blitted later
  309. switch (obj->ID)
  310. {
  311. case Obj::HERO:
  312. case Obj::PRISON:
  313. continue;
  314. }
  315. PlayerColor player = obj->getOwner();
  316. if(player == PlayerColor::NEUTRAL)
  317. return *graphics->neutralColor;
  318. else
  319. if (player < PlayerColor::PLAYER_LIMIT)
  320. return graphics->playerColors[player.getNum()];
  321. }
  322. // else - use terrain color (blocked version or normal)
  323. const auto & colorPair = parent->colors.find(tile->terType->getId())->second;
  324. if (tile->blocked && (!tile->visitable))
  325. return colorPair.second;
  326. else
  327. return colorPair.first;
  328. }
  329. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  330. {
  331. int3 mapSizes = LOCPLINT->cb->getMapSize();
  332. double stepX = double(pos.w) / mapSizes.x;
  333. double stepY = double(pos.h) / mapSizes.y;
  334. x = static_cast<int>(toX + stepX * tile.x);
  335. y = static_cast<int>(toY + stepY * tile.y);
  336. }
  337. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  338. {
  339. //coordinates of rectangle on minimap representing this tile
  340. // begin - first to blit, end - first NOT to blit
  341. int xBegin, yBegin, xEnd, yEnd;
  342. tileToPixels (tile, xBegin, yBegin, toX, toY);
  343. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  344. for (int y=yBegin; y<yEnd; y++)
  345. {
  346. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  347. for (int x=xBegin; x<xEnd; x++)
  348. ColorPutter<4, 1>::PutColor(ptr, color);
  349. }
  350. }
  351. void CMinimapInstance::refreshTile(const int3 &tile)
  352. {
  353. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  354. }
  355. void CMinimapInstance::drawScaled(int level)
  356. {
  357. int3 mapSizes = LOCPLINT->cb->getMapSize();
  358. //size of one map tile on our minimap
  359. double stepX = double(pos.w) / mapSizes.x;
  360. double stepY = double(pos.h) / mapSizes.y;
  361. double currY = 0;
  362. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  363. {
  364. double currX = 0;
  365. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  366. {
  367. const SDL_Color &color = getTileColor(int3(x,y,level));
  368. //coordinates of rectangle on minimap representing this tile
  369. // begin - first to blit, end - first NOT to blit
  370. int xBegin = static_cast<int>(currX);
  371. int yBegin = static_cast<int>(currY);
  372. int xEnd = static_cast<int>(currX + stepX);
  373. int yEnd = static_cast<int>(currY + stepY);
  374. for (int y=yBegin; y<yEnd; y++)
  375. {
  376. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  377. for (int x=xBegin; x<xEnd; x++)
  378. ColorPutter<4, 1>::PutColor(ptr, color);
  379. }
  380. }
  381. }
  382. }
  383. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  384. parent(Parent),
  385. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  386. level(Level)
  387. {
  388. pos.w = parent->pos.w;
  389. pos.h = parent->pos.h;
  390. drawScaled(level);
  391. }
  392. CMinimapInstance::~CMinimapInstance()
  393. {
  394. SDL_FreeSurface(minimap);
  395. }
  396. void CMinimapInstance::showAll(SDL_Surface * to)
  397. {
  398. blitAtLoc(minimap, 0, 0, to);
  399. //draw heroes
  400. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  401. for(auto & hero : heroes)
  402. {
  403. int3 position = hero->visitablePos();
  404. if(position.z == level)
  405. {
  406. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  407. blitTileWithColor(color, position, to, pos.x, pos.y);
  408. }
  409. }
  410. }
  411. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
  412. {
  413. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > ret;
  414. for(const auto & terrain : CGI->terrainTypeHandler->objects)
  415. {
  416. SDL_Color normal = CSDL_Ext::toSDL(terrain->minimapUnblocked);
  417. SDL_Color blocked = CSDL_Ext::toSDL(terrain->minimapBlocked);
  418. ret[terrain->getId()] = std::make_pair(normal, blocked);
  419. }
  420. return ret;
  421. }
  422. CMinimap::CMinimap(const Rect & position)
  423. : CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  424. level(0),
  425. colors(loadColors())
  426. {
  427. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  428. pos.w = position.w;
  429. pos.h = position.h;
  430. aiShield = std::make_shared<CPicture>("AIShield");
  431. aiShield->disable();
  432. }
  433. int3 CMinimap::translateMousePosition()
  434. {
  435. // 0 = top-left corner, 1 = bottom-right corner
  436. double dx = double(GH.getCursorPosition().x - pos.x) / pos.w;
  437. double dy = double(GH.getCursorPosition().y - pos.y) / pos.h;
  438. int3 mapSizes = LOCPLINT->cb->getMapSize();
  439. int3 tile ((si32)(mapSizes.x * dx), (si32)(mapSizes.y * dy), level);
  440. return tile;
  441. }
  442. void CMinimap::moveAdvMapSelection()
  443. {
  444. int3 newLocation = translateMousePosition();
  445. adventureInt->centerOn(newLocation);
  446. if (!(adventureInt->active & GENERAL))
  447. GH.totalRedraw(); //redraw this as well as inactive adventure map
  448. else
  449. redraw();//redraw only this
  450. }
  451. void CMinimap::clickLeft(tribool down, bool previousState)
  452. {
  453. if(down)
  454. moveAdvMapSelection();
  455. }
  456. void CMinimap::clickRight(tribool down, bool previousState)
  457. {
  458. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  459. }
  460. void CMinimap::hover(bool on)
  461. {
  462. if(on)
  463. GH.statusbar->write(CGI->generaltexth->zelp[291].first);
  464. else
  465. GH.statusbar->clear();
  466. }
  467. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  468. {
  469. if(mouseState(EIntObjMouseBtnType::LEFT))
  470. moveAdvMapSelection();
  471. }
  472. void CMinimap::showAll(SDL_Surface * to)
  473. {
  474. CIntObject::showAll(to);
  475. if(minimap)
  476. {
  477. int3 mapSizes = LOCPLINT->cb->getMapSize();
  478. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  479. //draw radar
  480. Rect oldClip;
  481. Rect radar =
  482. {
  483. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  484. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  485. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  486. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  487. };
  488. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  489. {
  490. // adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
  491. radar.x = std::min<int>(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
  492. radar.y = std::min<int>(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
  493. if(radar.x < pos.x && radar.y < pos.y)
  494. return; // whole map is visible at once, no point in redrawing border
  495. }
  496. CSDL_Ext::getClipRect(to, oldClip);
  497. CSDL_Ext::setClipRect(to, pos);
  498. CSDL_Ext::drawDashedBorder(to, radar, Colors::PURPLE);
  499. CSDL_Ext::setClipRect(to, oldClip);
  500. }
  501. }
  502. void CMinimap::update()
  503. {
  504. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  505. return;
  506. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  507. minimap = std::make_shared<CMinimapInstance>(this, level);
  508. redraw();
  509. }
  510. void CMinimap::setLevel(int newLevel)
  511. {
  512. level = newLevel;
  513. update();
  514. }
  515. void CMinimap::setAIRadar(bool on)
  516. {
  517. if(on)
  518. {
  519. aiShield->enable();
  520. minimap.reset();
  521. }
  522. else
  523. {
  524. aiShield->disable();
  525. update();
  526. }
  527. // this my happen during AI turn when this interface is inactive
  528. // force redraw in order to properly update interface
  529. GH.totalRedraw();
  530. }
  531. void CMinimap::hideTile(const int3 &pos)
  532. {
  533. if(minimap)
  534. minimap->refreshTile(pos);
  535. }
  536. void CMinimap::showTile(const int3 &pos)
  537. {
  538. if(minimap)
  539. minimap->refreshTile(pos);
  540. }
  541. CInfoBar::CVisibleInfo::CVisibleInfo()
  542. : CIntObject(0, Point(8, 12))
  543. {
  544. }
  545. void CInfoBar::CVisibleInfo::show(SDL_Surface * to)
  546. {
  547. CIntObject::show(to);
  548. for(auto object : forceRefresh)
  549. object->showAll(to);
  550. }
  551. CInfoBar::EmptyVisibleInfo::EmptyVisibleInfo()
  552. {
  553. }
  554. CInfoBar::VisibleHeroInfo::VisibleHeroInfo(const CGHeroInstance * hero)
  555. {
  556. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  557. background = std::make_shared<CPicture>("ADSTATHR");
  558. heroTooltip = std::make_shared<CHeroTooltip>(Point(0,0), hero);
  559. }
  560. CInfoBar::VisibleTownInfo::VisibleTownInfo(const CGTownInstance * town)
  561. {
  562. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  563. background = std::make_shared<CPicture>("ADSTATCS");
  564. townTooltip = std::make_shared<CTownTooltip>(Point(0,0), town);
  565. }
  566. CInfoBar::VisibleDateInfo::VisibleDateInfo()
  567. {
  568. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  569. animation = std::make_shared<CShowableAnim>(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  570. std::string labelText;
  571. 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
  572. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  573. else
  574. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  575. label = std::make_shared<CLabel>(95, 31, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, labelText);
  576. forceRefresh.push_back(label);
  577. }
  578. std::string CInfoBar::VisibleDateInfo::getNewDayName()
  579. {
  580. if(LOCPLINT->cb->getDate(Date::DAY) == 1)
  581. return "NEWDAY";
  582. if(LOCPLINT->cb->getDate(Date::DAY) != 1)
  583. return "NEWDAY";
  584. switch(LOCPLINT->cb->getDate(Date::WEEK))
  585. {
  586. case 1:
  587. return "NEWWEEK1";
  588. case 2:
  589. return "NEWWEEK2";
  590. case 3:
  591. return "NEWWEEK3";
  592. case 4:
  593. return "NEWWEEK4";
  594. default:
  595. return "";
  596. }
  597. }
  598. CInfoBar::VisibleEnemyTurnInfo::VisibleEnemyTurnInfo(PlayerColor player)
  599. {
  600. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  601. background = std::make_shared<CPicture>("ADSTATNX");
  602. banner = std::make_shared<CAnimImage>("CREST58", player.getNum(), 0, 20, 51);
  603. sand = std::make_shared<CShowableAnim>(99, 51, "HOURSAND");
  604. glass = std::make_shared<CShowableAnim>(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  605. }
  606. CInfoBar::VisibleGameStatusInfo::VisibleGameStatusInfo()
  607. {
  608. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  609. //get amount of halls of each level
  610. std::vector<int> halls(4, 0);
  611. for(auto town : LOCPLINT->towns)
  612. {
  613. int hallLevel = town->hallLevel();
  614. //negative value means no village hall, unlikely but possible
  615. if(hallLevel >= 0)
  616. halls.at(hallLevel)++;
  617. }
  618. std::vector<PlayerColor> allies, enemies;
  619. //generate list of allies and enemies
  620. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  621. {
  622. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  623. {
  624. if(LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  625. allies.push_back(PlayerColor(i));
  626. else
  627. enemies.push_back(PlayerColor(i));
  628. }
  629. }
  630. //generate widgets
  631. background = std::make_shared<CPicture>("ADSTATIN");
  632. allyLabel = std::make_shared<CLabel>(10, 106, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  633. enemyLabel = std::make_shared<CLabel>(10, 136, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  634. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  635. for(PlayerColor & player : allies)
  636. {
  637. auto image = std::make_shared<CAnimImage>("ITGFLAGS", player.getNum(), 0, posx, 102);
  638. posx += image->pos.w;
  639. flags.push_back(image);
  640. }
  641. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  642. for(PlayerColor & player : enemies)
  643. {
  644. auto image = std::make_shared<CAnimImage>("ITGFLAGS", player.getNum(), 0, posx, 132);
  645. posx += image->pos.w;
  646. flags.push_back(image);
  647. }
  648. for(size_t i=0; i<halls.size(); i++)
  649. {
  650. hallIcons.push_back(std::make_shared<CAnimImage>("itmtl", i, 0, 6 + 42 * (int)i , 11));
  651. if(halls[i])
  652. 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])));
  653. }
  654. }
  655. CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const Component & compToDisplay, std::string message)
  656. {
  657. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  658. background = std::make_shared<CPicture>("ADSTATOT", 1);
  659. comp = std::make_shared<CComponent>(compToDisplay);
  660. comp->moveTo(Point(pos.x+47, pos.y+50));
  661. text = std::make_shared<CTextBox>(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  662. }
  663. void CInfoBar::playNewDaySound()
  664. {
  665. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  666. CCS->soundh->playSound(soundBase::newDay);
  667. else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  668. CCS->soundh->playSound(soundBase::newWeek);
  669. else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  670. CCS->soundh->playSound(soundBase::newMonth);
  671. else
  672. CCS->soundh->playSound(soundBase::newDay);
  673. }
  674. void CInfoBar::reset()
  675. {
  676. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  677. state = EMPTY;
  678. visibleInfo = std::make_shared<EmptyVisibleInfo>();
  679. }
  680. void CInfoBar::showSelection()
  681. {
  682. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  683. if(adventureInt->selection)
  684. {
  685. if(auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection))
  686. {
  687. showHeroSelection(hero);
  688. return;
  689. }
  690. else if(auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection))
  691. {
  692. showTownSelection(town);
  693. return;
  694. }
  695. }
  696. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  697. }
  698. void CInfoBar::tick()
  699. {
  700. removeUsedEvents(TIME);
  701. if(GH.topInt() == adventureInt)
  702. showSelection();
  703. }
  704. void CInfoBar::clickLeft(tribool down, bool previousState)
  705. {
  706. if(down)
  707. {
  708. if(state == HERO || state == TOWN)
  709. showGameStatus();
  710. else if(state == GAME)
  711. showDate();
  712. else
  713. showSelection();
  714. }
  715. }
  716. void CInfoBar::clickRight(tribool down, bool previousState)
  717. {
  718. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  719. }
  720. void CInfoBar::hover(bool on)
  721. {
  722. if(on)
  723. GH.statusbar->write(CGI->generaltexth->zelp[292].first);
  724. else
  725. GH.statusbar->clear();
  726. }
  727. CInfoBar::CInfoBar(const Rect & position)
  728. : CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  729. state(EMPTY)
  730. {
  731. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  732. pos.w = position.w;
  733. pos.h = position.h;
  734. reset();
  735. }
  736. void CInfoBar::showDate()
  737. {
  738. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  739. playNewDaySound();
  740. state = DATE;
  741. visibleInfo = std::make_shared<VisibleDateInfo>();
  742. setTimer(3000);
  743. redraw();
  744. }
  745. void CInfoBar::showComponent(const Component & comp, std::string message)
  746. {
  747. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  748. state = COMPONENT;
  749. visibleInfo = std::make_shared<VisibleComponentInfo>(comp, message);
  750. setTimer(3000);
  751. redraw();
  752. }
  753. void CInfoBar::startEnemyTurn(PlayerColor color)
  754. {
  755. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  756. state = AITURN;
  757. visibleInfo = std::make_shared<VisibleEnemyTurnInfo>(color);
  758. redraw();
  759. }
  760. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  761. {
  762. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  763. if(!hero)
  764. {
  765. reset();
  766. }
  767. else
  768. {
  769. state = HERO;
  770. visibleInfo = std::make_shared<VisibleHeroInfo>(hero);
  771. }
  772. redraw();
  773. }
  774. void CInfoBar::showTownSelection(const CGTownInstance * town)
  775. {
  776. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  777. if(!town)
  778. {
  779. reset();
  780. }
  781. else
  782. {
  783. state = TOWN;
  784. visibleInfo = std::make_shared<VisibleTownInfo>(town);
  785. }
  786. redraw();
  787. }
  788. void CInfoBar::showGameStatus()
  789. {
  790. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  791. state = GAME;
  792. visibleInfo = std::make_shared<VisibleGameStatusInfo>();
  793. setTimer(3000);
  794. redraw();
  795. }
  796. CInGameConsole::CInGameConsole()
  797. : CIntObject(KEYBOARD | TEXTINPUT),
  798. prevEntDisp(-1),
  799. defaultTimeout(10000),
  800. maxDisplayedTexts(10)
  801. {
  802. }
  803. void CInGameConsole::show(SDL_Surface * to)
  804. {
  805. int number = 0;
  806. std::vector<std::list< std::pair< std::string, Uint32 > >::iterator> toDel;
  807. boost::unique_lock<boost::mutex> lock(texts_mx);
  808. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  809. {
  810. Point leftBottomCorner(0, pos.h);
  811. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  812. Point(leftBottomCorner.x + 50, leftBottomCorner.y - (int)texts.size() * 20 - 80 + number*20));
  813. if((int)(SDL_GetTicks() - it->second) > defaultTimeout)
  814. {
  815. toDel.push_back(it);
  816. }
  817. }
  818. for(auto & elem : toDel)
  819. {
  820. texts.erase(elem);
  821. }
  822. }
  823. void CInGameConsole::print(const std::string &txt)
  824. {
  825. boost::unique_lock<boost::mutex> lock(texts_mx);
  826. int lineLen = conf.go()->ac.outputLineLength;
  827. if(txt.size() < lineLen)
  828. {
  829. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  830. if(texts.size() > maxDisplayedTexts)
  831. {
  832. texts.pop_front();
  833. }
  834. }
  835. else
  836. {
  837. assert(lineLen);
  838. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  839. {
  840. std::string part = txt.substr(g * lineLen, lineLen);
  841. if(part.size() == 0)
  842. break;
  843. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  844. if(texts.size() > maxDisplayedTexts)
  845. {
  846. texts.pop_front();
  847. }
  848. }
  849. }
  850. }
  851. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  852. {
  853. if(key.type != SDL_KEYDOWN) return;
  854. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  855. switch(key.keysym.sym)
  856. {
  857. case SDLK_TAB:
  858. case SDLK_ESCAPE:
  859. {
  860. if(captureAllKeys)
  861. {
  862. endEnteringText(false);
  863. }
  864. else if(SDLK_TAB == key.keysym.sym)
  865. {
  866. startEnteringText();
  867. }
  868. break;
  869. }
  870. case SDLK_RETURN: //enter key
  871. {
  872. if(!enteredText.empty() && captureAllKeys)
  873. {
  874. bool anyTextExceptCaret = enteredText.size() > 1;
  875. endEnteringText(anyTextExceptCaret);
  876. if(anyTextExceptCaret)
  877. {
  878. CCS->soundh->playSound("CHAT");
  879. }
  880. }
  881. break;
  882. }
  883. case SDLK_BACKSPACE:
  884. {
  885. if(enteredText.size() > 1)
  886. {
  887. Unicode::trimRight(enteredText,2);
  888. enteredText += '_';
  889. refreshEnteredText();
  890. }
  891. break;
  892. }
  893. case SDLK_UP: //up arrow
  894. {
  895. if(previouslyEntered.size() == 0)
  896. break;
  897. if(prevEntDisp == -1)
  898. {
  899. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  900. enteredText = previouslyEntered[prevEntDisp] + "_";
  901. refreshEnteredText();
  902. }
  903. else if( prevEntDisp > 0)
  904. {
  905. --prevEntDisp;
  906. enteredText = previouslyEntered[prevEntDisp] + "_";
  907. refreshEnteredText();
  908. }
  909. break;
  910. }
  911. case SDLK_DOWN: //down arrow
  912. {
  913. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  914. {
  915. ++prevEntDisp;
  916. enteredText = previouslyEntered[prevEntDisp] + "_";
  917. refreshEnteredText();
  918. }
  919. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  920. {
  921. prevEntDisp = -1;
  922. enteredText = "_";
  923. refreshEnteredText();
  924. }
  925. break;
  926. }
  927. default:
  928. {
  929. break;
  930. }
  931. }
  932. }
  933. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  934. {
  935. if(!captureAllKeys || enteredText.size() == 0)
  936. return;
  937. enteredText.resize(enteredText.size()-1);
  938. enteredText += event.text;
  939. enteredText += "_";
  940. refreshEnteredText();
  941. }
  942. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  943. {
  944. //do nothing here
  945. }
  946. void CInGameConsole::startEnteringText()
  947. {
  948. if (!active)
  949. return;
  950. if (captureAllKeys)
  951. return;
  952. assert(GH.statusbar);
  953. assert(currentStatusBar.expired());//effectively, nullptr check
  954. currentStatusBar = GH.statusbar;
  955. captureAllKeys = true;
  956. enteredText = "_";
  957. GH.statusbar->setEnteringMode(true);
  958. GH.statusbar->setEnteredText(enteredText);
  959. }
  960. void CInGameConsole::endEnteringText(bool processEnteredText)
  961. {
  962. captureAllKeys = false;
  963. prevEntDisp = -1;
  964. if(processEnteredText)
  965. {
  966. std::string txt = enteredText.substr(0, enteredText.size()-1);
  967. previouslyEntered.push_back(txt);
  968. if(txt.at(0) == '/')
  969. {
  970. //some commands like gosolo don't work when executed from GUI thread
  971. auto threadFunction = [=]()
  972. {
  973. ClientCommandManager commandController;
  974. commandController.processCommand(txt.substr(1), true);
  975. };
  976. boost::thread clientCommandThread(threadFunction);
  977. clientCommandThread.detach();
  978. }
  979. else
  980. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  981. }
  982. enteredText.clear();
  983. auto statusbar = currentStatusBar.lock();
  984. assert(statusbar);
  985. if (statusbar)
  986. statusbar->setEnteringMode(false);
  987. currentStatusBar.reset();
  988. }
  989. void CInGameConsole::refreshEnteredText()
  990. {
  991. auto statusbar = currentStatusBar.lock();
  992. assert(statusbar);
  993. if (statusbar)
  994. statusbar->setEnteredText(enteredText);
  995. }
  996. CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position)
  997. : CIntObject(),
  998. background(bg)
  999. {
  1000. defActions = 255;
  1001. recActions = 255;
  1002. pos.x += position.x;
  1003. pos.y += position.y;
  1004. if (bg)
  1005. {
  1006. pos.w = bg->w;
  1007. pos.h = bg->h;
  1008. }
  1009. }
  1010. CAdvMapPanel::~CAdvMapPanel()
  1011. {
  1012. if (background)
  1013. SDL_FreeSurface(background);
  1014. }
  1015. void CAdvMapPanel::addChildColorableButton(std::shared_ptr<CButton> button)
  1016. {
  1017. colorableButtons.push_back(button);
  1018. addChildToPanel(button, ACTIVATE | DEACTIVATE);
  1019. }
  1020. void CAdvMapPanel::setPlayerColor(const PlayerColor & clr)
  1021. {
  1022. for(auto & button : colorableButtons)
  1023. {
  1024. button->setPlayerColor(clr);
  1025. }
  1026. }
  1027. void CAdvMapPanel::showAll(SDL_Surface * to)
  1028. {
  1029. if(background)
  1030. CSDL_Ext::blitAt(background, pos.x, pos.y, to);
  1031. CIntObject::showAll(to);
  1032. }
  1033. void CAdvMapPanel::addChildToPanel(std::shared_ptr<CIntObject> obj, ui8 actions)
  1034. {
  1035. otherObjects.push_back(obj);
  1036. obj->recActions |= actions | SHOWALL;
  1037. obj->recActions &= ~DISPOSE;
  1038. addChild(obj.get(), false);
  1039. }
  1040. CAdvMapWorldViewPanel::CAdvMapWorldViewPanel(std::shared_ptr<CAnimation> _icons, SDL_Surface * bg, Point position, int spaceBottom, const PlayerColor &color)
  1041. : CAdvMapPanel(bg, position), icons(_icons)
  1042. {
  1043. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1044. int fillerHeight = bg ? spaceBottom - pos.y - pos.h : 0;
  1045. if(fillerHeight > 0)
  1046. {
  1047. backgroundFiller = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, pos.h, pos.w, fillerHeight));
  1048. }
  1049. }
  1050. CAdvMapWorldViewPanel::~CAdvMapWorldViewPanel() = default;
  1051. void CAdvMapWorldViewPanel::recolorIcons(const PlayerColor & color, int indexOffset)
  1052. {
  1053. assert(iconsData.size() == currentIcons.size());
  1054. for(size_t idx = 0; idx < iconsData.size(); idx++)
  1055. {
  1056. const auto & data = iconsData.at(idx);
  1057. currentIcons[idx]->setFrame(data.first + indexOffset);
  1058. }
  1059. }
  1060. void CAdvMapWorldViewPanel::addChildIcon(std::pair<int, Point> data, int indexOffset)
  1061. {
  1062. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1063. iconsData.push_back(data);
  1064. currentIcons.push_back(std::make_shared<CAnimImage>(icons, data.first + indexOffset, 0, data.second.x, data.second.y));
  1065. }