2
0

CList.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "CAdventureMapInterface.h"
  13. #include "../widgets/Images.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../windows/InfoWindows.h"
  16. #include "../CGameInfo.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../PlayerLocalState.h"
  19. #include "../gui/CGuiHandler.h"
  20. #include "../renderSDL/SDL_Extensions.h"
  21. #include "../../lib/CGeneralTextHandler.h"
  22. #include "../../lib/CHeroHandler.h"
  23. #include "../../lib/CModHandler.h"
  24. #include "../../lib/GameSettings.h"
  25. #include "../../lib/mapObjects/CGHeroInstance.h"
  26. #include "../../lib/mapObjects/CGTownInstance.h"
  27. CList::CListItem::CListItem(CList * Parent)
  28. : CIntObject(LCLICK | RCLICK | HOVER),
  29. parent(Parent),
  30. selection()
  31. {
  32. defActions = 255-DISPOSE;
  33. }
  34. CList::CListItem::~CListItem()
  35. {
  36. }
  37. void CList::CListItem::clickRight(tribool down, bool previousState)
  38. {
  39. if (down == true)
  40. showTooltip();
  41. }
  42. void CList::CListItem::clickLeft(tribool down, bool previousState)
  43. {
  44. if(down == true)
  45. {
  46. //second click on already selected item
  47. if(parent->selected == this->shared_from_this())
  48. {
  49. open();
  50. }
  51. else
  52. {
  53. //first click - switch selection
  54. parent->select(this->shared_from_this());
  55. }
  56. }
  57. }
  58. void CList::CListItem::hover(bool on)
  59. {
  60. if (on)
  61. GH.statusbar->write(getHoverText());
  62. else
  63. GH.statusbar->clear();
  64. }
  65. void CList::CListItem::onSelect(bool on)
  66. {
  67. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  68. selection.reset();
  69. if(on)
  70. selection = genSelection();
  71. select(on);
  72. redraw();
  73. }
  74. CList::CList(int Size, Rect widgetDimensions)
  75. : CIntObject(0, widgetDimensions.topLeft()),
  76. size(Size),
  77. selected(nullptr)
  78. {
  79. pos.w = widgetDimensions.w;
  80. pos.h = widgetDimensions.h;
  81. }
  82. void CList::showAll(SDL_Surface * to)
  83. {
  84. CSDL_Ext::fillRect(to, 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. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  164. {
  165. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  166. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  167. portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1, 0);
  168. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  169. pos.w = mana->pos.w + mana->pos.x - pos.x;
  170. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  171. }
  172. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  173. : CListItem(parent),
  174. hero(Hero)
  175. {
  176. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  177. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  178. portrait = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  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. update();
  183. }
  184. void CHeroList::CHeroItem::update()
  185. {
  186. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  187. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  188. redraw();
  189. }
  190. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  191. {
  192. return std::make_shared<CPicture>("HPSYYY", movement->pos.w + 1, 0);
  193. }
  194. void CHeroList::CHeroItem::select(bool on)
  195. {
  196. if(on)
  197. LOCPLINT->localState->setSelection(hero);
  198. }
  199. void CHeroList::CHeroItem::open()
  200. {
  201. LOCPLINT->openHeroWindow(hero);
  202. }
  203. void CHeroList::CHeroItem::showTooltip()
  204. {
  205. CRClickPopup::createAndPush(hero, GH.getCursorPosition());
  206. }
  207. std::string CHeroList::CHeroItem::getHoverText()
  208. {
  209. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
  210. }
  211. std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
  212. {
  213. if (LOCPLINT->localState->getWanderingHeroes().size() > index)
  214. return std::make_shared<CHeroItem>(this, LOCPLINT->localState->getWanderingHero(index));
  215. return std::make_shared<CEmptyHeroItem>();
  216. }
  217. CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  218. : CList(visibleItemsCount, widgetPosition)
  219. {
  220. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  221. }
  222. void CHeroList::select(const CGHeroInstance * hero)
  223. {
  224. selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
  225. }
  226. void CHeroList::update(const CGHeroInstance * hero)
  227. {
  228. //this hero is already present, update its status
  229. for(auto & elem : listBox->getItems())
  230. {
  231. auto item = std::dynamic_pointer_cast<CHeroItem>(elem);
  232. if(item && item->hero == hero && vstd::contains(LOCPLINT->localState->getWanderingHeroes(), hero))
  233. {
  234. item->update();
  235. return;
  236. }
  237. }
  238. //simplest solution for now: reset list and restore selection
  239. listBox->resize(LOCPLINT->localState->getWanderingHeroes().size());
  240. if (LOCPLINT->localState->getCurrentHero())
  241. select(LOCPLINT->localState->getCurrentHero());
  242. CList::update();
  243. }
  244. std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
  245. {
  246. if (LOCPLINT->localState->getOwnedTowns().size() > index)
  247. return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
  248. return std::make_shared<CAnimImage>("ITPA", 0);
  249. }
  250. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  251. CListItem(parent),
  252. town(Town)
  253. {
  254. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  255. picture = std::make_shared<CAnimImage>("ITPA", 0);
  256. pos = picture->pos;
  257. update();
  258. }
  259. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  260. {
  261. return std::make_shared<CAnimImage>("ITPA", 1);
  262. }
  263. void CTownList::CTownItem::update()
  264. {
  265. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  266. picture->setFrame(iconIndex + 2);
  267. redraw();
  268. }
  269. void CTownList::CTownItem::select(bool on)
  270. {
  271. if(on)
  272. LOCPLINT->localState->setSelection(town);
  273. }
  274. void CTownList::CTownItem::open()
  275. {
  276. LOCPLINT->openTownWindow(town);
  277. }
  278. void CTownList::CTownItem::showTooltip()
  279. {
  280. CRClickPopup::createAndPush(town, GH.getCursorPosition());
  281. }
  282. std::string CTownList::CTownItem::getHoverText()
  283. {
  284. return town->getObjectName();
  285. }
  286. CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  287. : CList(visibleItemsCount, widgetPosition)
  288. {
  289. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  290. }
  291. void CTownList::select(const CGTownInstance * town)
  292. {
  293. selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
  294. }
  295. void CTownList::update(const CGTownInstance *)
  296. {
  297. //simplest solution for now: reset list and restore selection
  298. listBox->resize(LOCPLINT->localState->getOwnedTowns().size());
  299. if (LOCPLINT->localState->getCurrentTown())
  300. select(LOCPLINT->localState->getCurrentTown());
  301. CList::update();
  302. }