CList.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "CList.h"
  12. #include "CAdvMapInt.h"
  13. #include "../widgets/Images.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../windows/InfoWindows.h"
  16. #include "../CGameInfo.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../../lib/CGeneralTextHandler.h"
  20. #include "../../lib/CHeroHandler.h"
  21. #include "../../lib/CModHandler.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/mapObjects/CGTownInstance.h"
  24. CList::CListItem::CListItem(CList * Parent)
  25. : CIntObject(LCLICK | RCLICK | HOVER),
  26. parent(Parent),
  27. selection()
  28. {
  29. defActions = 255-DISPOSE;
  30. }
  31. CList::CListItem::~CListItem()
  32. {
  33. }
  34. void CList::CListItem::clickRight(tribool down, bool previousState)
  35. {
  36. if (down == true)
  37. showTooltip();
  38. }
  39. void CList::CListItem::clickLeft(tribool down, bool previousState)
  40. {
  41. if(down == true)
  42. {
  43. //second click on already selected item
  44. if(parent->selected == this->shared_from_this())
  45. {
  46. open();
  47. }
  48. else
  49. {
  50. //first click - switch selection
  51. parent->select(this->shared_from_this());
  52. }
  53. }
  54. }
  55. void CList::CListItem::hover(bool on)
  56. {
  57. if (on)
  58. GH.statusbar->write(getHoverText());
  59. else
  60. GH.statusbar->clear();
  61. }
  62. void CList::CListItem::onSelect(bool on)
  63. {
  64. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  65. selection.reset();
  66. if(on)
  67. selection = genSelection();
  68. select(on);
  69. GH.totalRedraw();
  70. }
  71. CList::CList(int Size, Point position, std::string btnUp, std::string btnDown, size_t listAmount, int helpUp, int helpDown, CListBox::CreateFunc create)
  72. : CIntObject(0, position),
  73. size(Size),
  74. selected(nullptr)
  75. {
  76. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  77. scrollUp = std::make_shared<CButton>(Point(0, 0), btnUp, CGI->generaltexth->zelp[helpUp]);
  78. scrollDown = std::make_shared<CButton>(Point(0, scrollUp->pos.h + 32*(int)size), btnDown, CGI->generaltexth->zelp[helpDown]);
  79. listBox = std::make_shared<CListBox>(create, Point(1,scrollUp->pos.h), Point(0, 32), size, listAmount);
  80. //assign callback only after list was created
  81. scrollUp->addCallback(std::bind(&CListBox::moveToPrev, listBox));
  82. scrollDown->addCallback(std::bind(&CListBox::moveToNext, listBox));
  83. scrollUp->addCallback(std::bind(&CList::update, this));
  84. scrollDown->addCallback(std::bind(&CList::update, this));
  85. update();
  86. }
  87. void CList::update()
  88. {
  89. bool onTop = listBox->getPos() == 0;
  90. bool onBottom = listBox->getPos() + size >= listBox->size();
  91. scrollUp->block(onTop);
  92. scrollDown->block(onBottom);
  93. }
  94. void CList::select(std::shared_ptr<CListItem> which)
  95. {
  96. if(selected == which)
  97. return;
  98. if(selected)
  99. selected->onSelect(false);
  100. selected = which;
  101. if(which)
  102. {
  103. which->onSelect(true);
  104. onSelect();
  105. }
  106. }
  107. int CList::getSelectedIndex()
  108. {
  109. return static_cast<int>(listBox->getIndexOf(selected));
  110. }
  111. void CList::selectIndex(int which)
  112. {
  113. if(which < 0)
  114. {
  115. if(selected)
  116. select(nullptr);
  117. }
  118. else
  119. {
  120. listBox->scrollTo(which);
  121. update();
  122. select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
  123. }
  124. }
  125. void CList::selectNext()
  126. {
  127. int index = getSelectedIndex() + 1;
  128. if(index >= listBox->size())
  129. index = 0;
  130. selectIndex(index);
  131. }
  132. void CList::selectPrev()
  133. {
  134. int index = getSelectedIndex();
  135. if(index <= 0)
  136. selectIndex(0);
  137. else
  138. selectIndex(index-1);
  139. }
  140. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  141. {
  142. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  143. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  144. portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1, 0);
  145. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  146. pos.w = mana->pos.w + mana->pos.x - pos.x;
  147. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  148. }
  149. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  150. : CListItem(parent),
  151. hero(Hero)
  152. {
  153. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  154. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  155. portrait = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  156. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
  157. pos.w = mana->pos.w + mana->pos.x - pos.x;
  158. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  159. update();
  160. }
  161. void CHeroList::CHeroItem::update()
  162. {
  163. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  164. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  165. redraw();
  166. }
  167. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  168. {
  169. return std::make_shared<CPicture>("HPSYYY", movement->pos.w + 1, 0);
  170. }
  171. void CHeroList::CHeroItem::select(bool on)
  172. {
  173. if(on && adventureInt->selection != hero)
  174. adventureInt->select(hero);
  175. }
  176. void CHeroList::CHeroItem::open()
  177. {
  178. LOCPLINT->openHeroWindow(hero);
  179. }
  180. void CHeroList::CHeroItem::showTooltip()
  181. {
  182. CRClickPopup::createAndPush(hero, GH.getCursorPosition());
  183. }
  184. std::string CHeroList::CHeroItem::getHoverText()
  185. {
  186. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
  187. }
  188. std::shared_ptr<CIntObject> CHeroList::createHeroItem(size_t index)
  189. {
  190. if (LOCPLINT->wanderingHeroes.size() > index)
  191. return std::make_shared<CHeroItem>(this, LOCPLINT->wanderingHeroes[index]);
  192. return std::make_shared<CEmptyHeroItem>();
  193. }
  194. CHeroList::CHeroList(int size, Point position, std::string btnUp, std::string btnDown):
  195. CList(size, position, btnUp, btnDown, LOCPLINT->wanderingHeroes.size(), 303, 304, std::bind(&CHeroList::createHeroItem, this, _1))
  196. {
  197. }
  198. void CHeroList::select(const CGHeroInstance * hero)
  199. {
  200. selectIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  201. }
  202. void CHeroList::update(const CGHeroInstance * hero)
  203. {
  204. //this hero is already present, update its status
  205. for(auto & elem : listBox->getItems())
  206. {
  207. auto item = std::dynamic_pointer_cast<CHeroItem>(elem);
  208. if(item && item->hero == hero && vstd::contains(LOCPLINT->wanderingHeroes, hero))
  209. {
  210. item->update();
  211. return;
  212. }
  213. }
  214. //simplest solution for now: reset list and restore selection
  215. listBox->resize(LOCPLINT->wanderingHeroes.size());
  216. if (adventureInt->selection)
  217. {
  218. auto selectedHero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  219. if (selectedHero)
  220. select(selectedHero);
  221. }
  222. CList::update();
  223. }
  224. std::shared_ptr<CIntObject> CTownList::createTownItem(size_t index)
  225. {
  226. if (LOCPLINT->towns.size() > index)
  227. return std::make_shared<CTownItem>(this, LOCPLINT->towns[index]);
  228. return std::make_shared<CAnimImage>("ITPA", 0);
  229. }
  230. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  231. CListItem(parent),
  232. town(Town)
  233. {
  234. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  235. picture = std::make_shared<CAnimImage>("ITPA", 0);
  236. pos = picture->pos;
  237. update();
  238. }
  239. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  240. {
  241. return std::make_shared<CAnimImage>("ITPA", 1);
  242. }
  243. void CTownList::CTownItem::update()
  244. {
  245. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  246. picture->setFrame(iconIndex + 2);
  247. redraw();
  248. }
  249. void CTownList::CTownItem::select(bool on)
  250. {
  251. if (on && adventureInt->selection != town)
  252. adventureInt->select(town);
  253. }
  254. void CTownList::CTownItem::open()
  255. {
  256. LOCPLINT->openTownWindow(town);
  257. }
  258. void CTownList::CTownItem::showTooltip()
  259. {
  260. CRClickPopup::createAndPush(town, GH.getCursorPosition());
  261. }
  262. std::string CTownList::CTownItem::getHoverText()
  263. {
  264. return town->getObjectName();
  265. }
  266. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  267. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, std::bind(&CTownList::createTownItem, this, _1))
  268. {
  269. }
  270. void CTownList::select(const CGTownInstance * town)
  271. {
  272. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  273. }
  274. void CTownList::update(const CGTownInstance *)
  275. {
  276. //simplest solution for now: reset list and restore selection
  277. listBox->resize(LOCPLINT->towns.size());
  278. if (adventureInt->selection)
  279. {
  280. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  281. if (town)
  282. select(town);
  283. }
  284. CList::update();
  285. }