CList.cpp 9.3 KB

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