CList.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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)
  74. : CIntObject(0, position),
  75. size(Size),
  76. selected(nullptr)
  77. {
  78. }
  79. void CList::createList(Point itemOffset, size_t listAmount)
  80. {
  81. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  82. listBox = std::make_shared<CListBox>(std::bind(&CList::createItem, this, _1), Point(0, 0), itemOffset, size, listAmount);
  83. }
  84. void CList::setScrollUpButton(std::shared_ptr<CButton> button)
  85. {
  86. scrollUp = button;
  87. scrollUp->addCallback(std::bind(&CListBox::moveToPrev, listBox));
  88. scrollUp->addCallback(std::bind(&CList::update, this));
  89. update();
  90. }
  91. void CList::setScrollDownButton(std::shared_ptr<CButton> button)
  92. {
  93. scrollDown = button;
  94. scrollDown->addCallback(std::bind(&CList::update, this));
  95. scrollDown->addCallback(std::bind(&CListBox::moveToNext, listBox));
  96. update();
  97. }
  98. void CList::update()
  99. {
  100. bool onTop = listBox->getPos() == 0;
  101. bool onBottom = listBox->getPos() + size >= listBox->size();
  102. if (scrollUp)
  103. scrollUp->block(onTop);
  104. if (scrollDown)
  105. scrollDown->block(onBottom);
  106. }
  107. void CList::select(std::shared_ptr<CListItem> which)
  108. {
  109. if(selected == which)
  110. return;
  111. if(selected)
  112. selected->onSelect(false);
  113. selected = which;
  114. if(which)
  115. {
  116. which->onSelect(true);
  117. onSelect();
  118. }
  119. }
  120. int CList::getSelectedIndex()
  121. {
  122. return static_cast<int>(listBox->getIndexOf(selected));
  123. }
  124. void CList::selectIndex(int which)
  125. {
  126. if(which < 0)
  127. {
  128. if(selected)
  129. select(nullptr);
  130. }
  131. else
  132. {
  133. listBox->scrollTo(which);
  134. update();
  135. select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
  136. }
  137. }
  138. void CList::selectNext()
  139. {
  140. int index = getSelectedIndex() + 1;
  141. if(index >= listBox->size())
  142. index = 0;
  143. selectIndex(index);
  144. }
  145. void CList::selectPrev()
  146. {
  147. int index = getSelectedIndex();
  148. if(index <= 0)
  149. selectIndex(0);
  150. else
  151. selectIndex(index-1);
  152. }
  153. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  154. {
  155. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  156. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  157. portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1, 0);
  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. }
  162. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  163. : CListItem(parent),
  164. hero(Hero)
  165. {
  166. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  167. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  168. portrait = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  169. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
  170. pos.w = mana->pos.w + mana->pos.x - pos.x;
  171. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  172. update();
  173. }
  174. void CHeroList::CHeroItem::update()
  175. {
  176. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  177. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  178. redraw();
  179. }
  180. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  181. {
  182. return std::make_shared<CPicture>("HPSYYY", movement->pos.w + 1, 0);
  183. }
  184. void CHeroList::CHeroItem::select(bool on)
  185. {
  186. if(on)
  187. LOCPLINT->localState->setSelection(hero);
  188. }
  189. void CHeroList::CHeroItem::open()
  190. {
  191. LOCPLINT->openHeroWindow(hero);
  192. }
  193. void CHeroList::CHeroItem::showTooltip()
  194. {
  195. CRClickPopup::createAndPush(hero, GH.getCursorPosition());
  196. }
  197. std::string CHeroList::CHeroItem::getHoverText()
  198. {
  199. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
  200. }
  201. std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
  202. {
  203. if (LOCPLINT->localState->getWanderingHeroes().size() > index)
  204. return std::make_shared<CHeroItem>(this, LOCPLINT->localState->getWanderingHero(index));
  205. return std::make_shared<CEmptyHeroItem>();
  206. }
  207. CHeroList::CHeroList(int size, Point position, Point itemOffset, size_t listAmount)
  208. : CList(size, position)
  209. {
  210. createList(itemOffset, listAmount);
  211. }
  212. void CHeroList::select(const CGHeroInstance * hero)
  213. {
  214. selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
  215. }
  216. void CHeroList::update(const CGHeroInstance * hero)
  217. {
  218. //this hero is already present, update its status
  219. for(auto & elem : listBox->getItems())
  220. {
  221. auto item = std::dynamic_pointer_cast<CHeroItem>(elem);
  222. if(item && item->hero == hero && vstd::contains(LOCPLINT->localState->getWanderingHeroes(), hero))
  223. {
  224. item->update();
  225. return;
  226. }
  227. }
  228. //simplest solution for now: reset list and restore selection
  229. listBox->resize(LOCPLINT->localState->getWanderingHeroes().size());
  230. if (LOCPLINT->localState->getCurrentHero())
  231. select(LOCPLINT->localState->getCurrentHero());
  232. CList::update();
  233. }
  234. std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
  235. {
  236. if (LOCPLINT->localState->getOwnedTowns().size() > index)
  237. return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
  238. return std::make_shared<CAnimImage>("ITPA", 0);
  239. }
  240. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  241. CListItem(parent),
  242. town(Town)
  243. {
  244. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  245. picture = std::make_shared<CAnimImage>("ITPA", 0);
  246. pos = picture->pos;
  247. update();
  248. }
  249. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  250. {
  251. return std::make_shared<CAnimImage>("ITPA", 1);
  252. }
  253. void CTownList::CTownItem::update()
  254. {
  255. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  256. picture->setFrame(iconIndex + 2);
  257. redraw();
  258. }
  259. void CTownList::CTownItem::select(bool on)
  260. {
  261. if(on)
  262. LOCPLINT->localState->setSelection(town);
  263. }
  264. void CTownList::CTownItem::open()
  265. {
  266. LOCPLINT->openTownWindow(town);
  267. }
  268. void CTownList::CTownItem::showTooltip()
  269. {
  270. CRClickPopup::createAndPush(town, GH.getCursorPosition());
  271. }
  272. std::string CTownList::CTownItem::getHoverText()
  273. {
  274. return town->getObjectName();
  275. }
  276. CTownList::CTownList(int size, Point position, Point itemOffset, size_t listAmount)
  277. : CList(size, position)
  278. {
  279. createList(itemOffset, listAmount);
  280. }
  281. void CTownList::select(const CGTownInstance * town)
  282. {
  283. selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
  284. }
  285. void CTownList::update(const CGTownInstance *)
  286. {
  287. //simplest solution for now: reset list and restore selection
  288. listBox->resize(LOCPLINT->localState->getOwnedTowns().size());
  289. if (LOCPLINT->localState->getCurrentTown())
  290. select(LOCPLINT->localState->getCurrentTown());
  291. CList::update();
  292. }