2
0

CList.cpp 8.3 KB

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