AdventureMapClasses.cpp 30 KB

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