AdventureMapClasses.cpp 30 KB

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