CList.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "AdventureMapInterface.h"
  13. #include "../widgets/Images.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../widgets/ObjectLists.h"
  16. #include "../windows/InfoWindows.h"
  17. #include "../CGameInfo.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../PlayerLocalState.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../render/Canvas.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. #include "../../lib/CHeroHandler.h"
  24. #include "../../lib/CModHandler.h"
  25. #include "../../lib/GameSettings.h"
  26. #include "../../lib/mapObjects/CGHeroInstance.h"
  27. #include "../../lib/mapObjects/CGTownInstance.h"
  28. CList::CListItem::CListItem(CList * Parent)
  29. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  30. parent(Parent),
  31. selection()
  32. {
  33. defActions = 255-DISPOSE;
  34. }
  35. CList::CListItem::~CListItem() = default;
  36. void CList::CListItem::showPopupWindow()
  37. {
  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, Rect widgetDimensions)
  73. : Scrollable(0, widgetDimensions.topLeft(), Orientation::VERTICAL),
  74. size(Size),
  75. selected(nullptr)
  76. {
  77. pos.w = widgetDimensions.w;
  78. pos.h = widgetDimensions.h;
  79. }
  80. void CList::showAll(Canvas & to)
  81. {
  82. to.drawColor(pos, Colors::BLACK);
  83. CIntObject::showAll(to);
  84. }
  85. void CList::createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount)
  86. {
  87. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  88. listBox = std::make_shared<CListBox>(std::bind(&CList::createItem, this, _1), firstItemPosition, itemPositionDelta, size, listAmount);
  89. }
  90. void CList::setScrollUpButton(std::shared_ptr<CButton> button)
  91. {
  92. addChild(button.get());
  93. scrollUp = button;
  94. scrollUp->addCallback(std::bind(&CList::scrollPrev, this));
  95. update();
  96. }
  97. void CList::setScrollDownButton(std::shared_ptr<CButton> button)
  98. {
  99. addChild(button.get());
  100. scrollDown = button;
  101. scrollDown->addCallback(std::bind(&CList::scrollNext, this));
  102. update();
  103. }
  104. void CList::scrollBy(int distance)
  105. {
  106. if (distance < 0 && listBox->getPos() < -distance)
  107. listBox->moveToPos(0);
  108. else
  109. listBox->moveToPos(static_cast<int>(listBox->getPos()) + distance);
  110. update();
  111. }
  112. void CList::scrollPrev()
  113. {
  114. listBox->moveToPrev();
  115. update();
  116. }
  117. void CList::scrollNext()
  118. {
  119. listBox->moveToNext();
  120. update();
  121. }
  122. void CList::update()
  123. {
  124. bool onTop = listBox->getPos() == 0;
  125. bool onBottom = listBox->getPos() + size >= listBox->size();
  126. if (scrollUp)
  127. scrollUp->block(onTop);
  128. if (scrollDown)
  129. scrollDown->block(onBottom);
  130. }
  131. void CList::select(std::shared_ptr<CListItem> which)
  132. {
  133. if(selected == which)
  134. return;
  135. if(selected)
  136. selected->onSelect(false);
  137. selected = which;
  138. if(which)
  139. {
  140. which->onSelect(true);
  141. onSelect();
  142. }
  143. }
  144. int CList::getSelectedIndex()
  145. {
  146. return static_cast<int>(listBox->getIndexOf(selected));
  147. }
  148. void CList::selectIndex(int which)
  149. {
  150. if(which < 0)
  151. {
  152. if(selected)
  153. select(nullptr);
  154. }
  155. else
  156. {
  157. listBox->scrollTo(which);
  158. update();
  159. select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
  160. }
  161. }
  162. void CList::selectNext()
  163. {
  164. int index = getSelectedIndex() + 1;
  165. if(index >= listBox->size())
  166. index = 0;
  167. selectIndex(index);
  168. }
  169. void CList::selectPrev()
  170. {
  171. int index = getSelectedIndex();
  172. if(index <= 0)
  173. selectIndex(0);
  174. else
  175. selectIndex(index-1);
  176. }
  177. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  178. {
  179. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  180. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  181. portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1, 0);
  182. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  183. pos.w = mana->pos.w + mana->pos.x - pos.x;
  184. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  185. }
  186. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  187. : CListItem(parent),
  188. hero(Hero)
  189. {
  190. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  191. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  192. portrait = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  193. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
  194. pos.w = mana->pos.w + mana->pos.x - pos.x;
  195. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  196. update();
  197. }
  198. void CHeroList::CHeroItem::update()
  199. {
  200. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  201. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  202. redraw();
  203. }
  204. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  205. {
  206. return std::make_shared<CPicture>("HPSYYY", movement->pos.w + 1, 0);
  207. }
  208. void CHeroList::CHeroItem::select(bool on)
  209. {
  210. if(on)
  211. LOCPLINT->localState->setSelection(hero);
  212. }
  213. void CHeroList::CHeroItem::open()
  214. {
  215. LOCPLINT->openHeroWindow(hero);
  216. }
  217. void CHeroList::CHeroItem::showTooltip()
  218. {
  219. CRClickPopup::createAndPush(hero, GH.getCursorPosition());
  220. }
  221. std::string CHeroList::CHeroItem::getHoverText()
  222. {
  223. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
  224. }
  225. std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
  226. {
  227. if (LOCPLINT->localState->getWanderingHeroes().size() > index)
  228. return std::make_shared<CHeroItem>(this, LOCPLINT->localState->getWanderingHero(index));
  229. return std::make_shared<CEmptyHeroItem>();
  230. }
  231. CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  232. : CList(visibleItemsCount, widgetPosition)
  233. {
  234. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  235. }
  236. void CHeroList::select(const CGHeroInstance * hero)
  237. {
  238. selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
  239. }
  240. void CHeroList::update(const CGHeroInstance * hero)
  241. {
  242. //this hero is already present, update its status
  243. for(auto & elem : listBox->getItems())
  244. {
  245. auto item = std::dynamic_pointer_cast<CHeroItem>(elem);
  246. if(item && item->hero == hero && vstd::contains(LOCPLINT->localState->getWanderingHeroes(), hero))
  247. {
  248. item->update();
  249. return;
  250. }
  251. }
  252. //simplest solution for now: reset list and restore selection
  253. listBox->resize(LOCPLINT->localState->getWanderingHeroes().size());
  254. if (LOCPLINT->localState->getCurrentHero())
  255. select(LOCPLINT->localState->getCurrentHero());
  256. CList::update();
  257. }
  258. std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
  259. {
  260. if (LOCPLINT->localState->getOwnedTowns().size() > index)
  261. return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
  262. return std::make_shared<CAnimImage>("ITPA", 0);
  263. }
  264. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  265. CListItem(parent),
  266. town(Town)
  267. {
  268. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  269. picture = std::make_shared<CAnimImage>("ITPA", 0);
  270. pos = picture->pos;
  271. update();
  272. }
  273. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  274. {
  275. return std::make_shared<CAnimImage>("ITPA", 1);
  276. }
  277. void CTownList::CTownItem::update()
  278. {
  279. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  280. picture->setFrame(iconIndex + 2);
  281. redraw();
  282. }
  283. void CTownList::CTownItem::select(bool on)
  284. {
  285. if(on)
  286. LOCPLINT->localState->setSelection(town);
  287. }
  288. void CTownList::CTownItem::open()
  289. {
  290. LOCPLINT->openTownWindow(town);
  291. }
  292. void CTownList::CTownItem::showTooltip()
  293. {
  294. CRClickPopup::createAndPush(town, GH.getCursorPosition());
  295. }
  296. std::string CTownList::CTownItem::getHoverText()
  297. {
  298. return town->getObjectName();
  299. }
  300. CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  301. : CList(visibleItemsCount, widgetPosition)
  302. {
  303. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  304. }
  305. void CTownList::select(const CGTownInstance * town)
  306. {
  307. selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
  308. }
  309. void CTownList::update(const CGTownInstance *)
  310. {
  311. //simplest solution for now: reset list and restore selection
  312. listBox->resize(LOCPLINT->localState->getOwnedTowns().size());
  313. if (LOCPLINT->localState->getCurrentTown())
  314. select(LOCPLINT->localState->getCurrentTown());
  315. CList::update();
  316. }