CList.cpp 9.1 KB

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