CList.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * CList.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_PixelAccess.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. }