CList.cpp 8.8 KB

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