CList.cpp 8.8 KB

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