CList.cpp 8.1 KB

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