CList.cpp 8.6 KB

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