CList.cpp 8.6 KB

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