CList.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "../widgets/RadialMenu.h"
  17. #include "../windows/InfoWindows.h"
  18. #include "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../PlayerLocalState.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/WindowHandler.h"
  23. #include "../render/Canvas.h"
  24. #include "../render/Colors.h"
  25. #include "../../lib/CGeneralTextHandler.h"
  26. #include "../../lib/CHeroHandler.h"
  27. #include "../../lib/GameSettings.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapObjects/CGTownInstance.h"
  30. CList::CListItem::CListItem(CList * Parent)
  31. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  32. parent(Parent),
  33. selection()
  34. {
  35. defActions = 255-DISPOSE;
  36. }
  37. CList::CListItem::~CListItem() = default;
  38. void CList::CListItem::showPopupWindow(const Point & cursorPosition)
  39. {
  40. showTooltip();
  41. }
  42. void CList::CListItem::clickPressed(const Point & cursorPosition)
  43. {
  44. //second click on already selected item
  45. if(parent->selected == this->shared_from_this())
  46. {
  47. open();
  48. }
  49. else
  50. {
  51. //first click - switch selection
  52. parent->select(this->shared_from_this());
  53. }
  54. }
  55. void CList::CListItem::hover(bool on)
  56. {
  57. if (on)
  58. GH.statusbar()->write(getHoverText());
  59. else
  60. GH.statusbar()->clear();
  61. }
  62. void CList::CListItem::onSelect(bool on)
  63. {
  64. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  65. selection.reset();
  66. if(on)
  67. selection = genSelection();
  68. select(on);
  69. redraw();
  70. }
  71. CList::CList(int Size, Rect widgetDimensions)
  72. : Scrollable(0, widgetDimensions.topLeft(), Orientation::VERTICAL),
  73. size(Size),
  74. selected(nullptr)
  75. {
  76. pos.w = widgetDimensions.w;
  77. pos.h = widgetDimensions.h;
  78. }
  79. void CList::showAll(Canvas & to)
  80. {
  81. to.drawColor(pos, Colors::BLACK);
  82. CIntObject::showAll(to);
  83. }
  84. void CList::createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount)
  85. {
  86. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  87. listBox = std::make_shared<CListBox>(std::bind(&CList::createItem, this, _1), firstItemPosition, itemPositionDelta, size, listAmount);
  88. }
  89. void CList::setScrollUpButton(std::shared_ptr<CButton> button)
  90. {
  91. addChild(button.get());
  92. scrollUp = button;
  93. scrollUp->addCallback(std::bind(&CList::scrollPrev, this));
  94. update();
  95. }
  96. void CList::setScrollDownButton(std::shared_ptr<CButton> button)
  97. {
  98. addChild(button.get());
  99. scrollDown = button;
  100. scrollDown->addCallback(std::bind(&CList::scrollNext, this));
  101. update();
  102. }
  103. void CList::scrollBy(int distance)
  104. {
  105. if (distance < 0 && listBox->getPos() < -distance)
  106. listBox->moveToPos(0);
  107. else
  108. listBox->moveToPos(static_cast<int>(listBox->getPos()) + distance);
  109. update();
  110. }
  111. void CList::scrollPrev()
  112. {
  113. listBox->moveToPrev();
  114. update();
  115. }
  116. void CList::scrollNext()
  117. {
  118. listBox->moveToNext();
  119. update();
  120. }
  121. void CList::update()
  122. {
  123. bool onTop = listBox->getPos() == 0;
  124. bool onBottom = listBox->getPos() + size >= listBox->size();
  125. if (scrollUp)
  126. scrollUp->block(onTop);
  127. if (scrollDown)
  128. scrollDown->block(onBottom);
  129. }
  130. void CList::select(std::shared_ptr<CListItem> which)
  131. {
  132. if(selected == which)
  133. return;
  134. if(selected)
  135. selected->onSelect(false);
  136. selected = which;
  137. if(which)
  138. {
  139. which->onSelect(true);
  140. onSelect();
  141. }
  142. }
  143. int CList::getSelectedIndex()
  144. {
  145. return static_cast<int>(listBox->getIndexOf(selected));
  146. }
  147. void CList::selectIndex(int which)
  148. {
  149. if(which < 0)
  150. {
  151. if(selected)
  152. select(nullptr);
  153. }
  154. else
  155. {
  156. listBox->scrollTo(which);
  157. update();
  158. select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
  159. }
  160. }
  161. void CList::selectNext()
  162. {
  163. int index = getSelectedIndex() + 1;
  164. if(index >= listBox->size())
  165. index = 0;
  166. selectIndex(index);
  167. }
  168. void CList::selectPrev()
  169. {
  170. int index = getSelectedIndex();
  171. if(index <= 0)
  172. selectIndex(0);
  173. else
  174. selectIndex(index-1);
  175. }
  176. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  177. {
  178. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  179. movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
  180. portrait = std::make_shared<CPicture>(ImagePath::builtin("HPSXXX"), movement->pos.w + 1, 0);
  181. mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  182. pos.w = mana->pos.w + mana->pos.x - pos.x;
  183. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  184. }
  185. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  186. : CListItem(parent),
  187. hero(Hero),
  188. parentList(parent)
  189. {
  190. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  191. movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
  192. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), hero->getIconIndex(), 0, movement->pos.w + 1);
  193. mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
  194. pos.w = mana->pos.w + mana->pos.x - pos.x;
  195. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  196. update();
  197. addUsedEvents(GESTURE);
  198. }
  199. void CHeroList::CHeroItem::update()
  200. {
  201. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movementPointsRemaining() / 100));
  202. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  203. redraw();
  204. }
  205. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  206. {
  207. return std::make_shared<CPicture>(ImagePath::builtin("HPSYYY"), movement->pos.w + 1, 0);
  208. }
  209. void CHeroList::CHeroItem::select(bool on)
  210. {
  211. if(on)
  212. LOCPLINT->localState->setSelection(hero);
  213. }
  214. void CHeroList::CHeroItem::open()
  215. {
  216. LOCPLINT->openHeroWindow(hero);
  217. }
  218. void CHeroList::CHeroItem::showTooltip()
  219. {
  220. CRClickPopup::createAndPush(hero, GH.getCursorPosition());
  221. }
  222. std::string CHeroList::CHeroItem::getHoverText()
  223. {
  224. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
  225. }
  226. void CHeroList::CHeroItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  227. {
  228. if(!on)
  229. return;
  230. if(!hero)
  231. return;
  232. auto & heroes = LOCPLINT->localState->getWanderingHeroes();
  233. if(heroes.size() < 2)
  234. return;
  235. int heroPos = vstd::find_pos(heroes, hero);
  236. const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes[heroPos - 1];
  237. const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes[heroPos + 1];
  238. std::vector<RadialMenuConfig> menuElements = {
  239. { RadialMenuConfig::ITEM_ALT_NN, heroUpper != nullptr, "altUpTop", "vcmi.radialWheel.moveTop", [this, heroPos]()
  240. {
  241. for (int i = heroPos; i > 0; i--)
  242. LOCPLINT->localState->swapWanderingHero(i, i - 1);
  243. parentList->updateWidget();
  244. } },
  245. { RadialMenuConfig::ITEM_ALT_NW, heroUpper != nullptr, "altUp", "vcmi.radialWheel.moveUp", [this, heroPos](){LOCPLINT->localState->swapWanderingHero(heroPos, heroPos - 1); parentList->updateWidget(); } },
  246. { RadialMenuConfig::ITEM_ALT_SW, heroLower != nullptr, "altDown", "vcmi.radialWheel.moveDown", [this, heroPos](){ LOCPLINT->localState->swapWanderingHero(heroPos, heroPos + 1); parentList->updateWidget(); } },
  247. { RadialMenuConfig::ITEM_ALT_SS, heroLower != nullptr, "altDownBottom", "vcmi.radialWheel.moveBottom", [this, heroPos, heroes]()
  248. {
  249. for (int i = heroPos; i < heroes.size() - 1; i++)
  250. LOCPLINT->localState->swapWanderingHero(i, i + 1);
  251. parentList->updateWidget();
  252. } },
  253. };
  254. GH.windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
  255. }
  256. std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
  257. {
  258. if (LOCPLINT->localState->getWanderingHeroes().size() > index)
  259. return std::make_shared<CHeroItem>(this, LOCPLINT->localState->getWanderingHero(index));
  260. return std::make_shared<CEmptyHeroItem>();
  261. }
  262. CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  263. : CList(visibleItemsCount, widgetPosition)
  264. {
  265. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  266. }
  267. void CHeroList::select(const CGHeroInstance * hero)
  268. {
  269. selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
  270. }
  271. void CHeroList::updateElement(const CGHeroInstance * hero)
  272. {
  273. updateWidget();
  274. }
  275. void CHeroList::updateWidget()
  276. {
  277. auto & heroes = LOCPLINT->localState->getWanderingHeroes();
  278. listBox->resize(heroes.size());
  279. for (size_t i = 0; i < heroes.size(); ++i)
  280. {
  281. auto item = std::dynamic_pointer_cast<CHeroItem>(listBox->getItem(i));
  282. if (!item)
  283. continue;
  284. if (item->hero == heroes[i])
  285. {
  286. item->update();
  287. }
  288. else
  289. {
  290. listBox->reset();
  291. break;
  292. }
  293. }
  294. if (LOCPLINT->localState->getCurrentHero())
  295. select(LOCPLINT->localState->getCurrentHero());
  296. CList::update();
  297. }
  298. std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
  299. {
  300. if (LOCPLINT->localState->getOwnedTowns().size() > index)
  301. return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
  302. return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
  303. }
  304. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  305. CListItem(parent),
  306. parentList(parent)
  307. {
  308. const std::vector<const CGTownInstance *> towns = LOCPLINT->localState->getOwnedTowns();
  309. townIndex = std::distance(towns.begin(), std::find(towns.begin(), towns.end(), Town));
  310. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  311. picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
  312. pos = picture->pos;
  313. update();
  314. addUsedEvents(GESTURE);
  315. }
  316. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  317. {
  318. return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 1);
  319. }
  320. void CTownList::CTownItem::update()
  321. {
  322. const CGTownInstance * town = LOCPLINT->localState->getOwnedTowns()[townIndex];
  323. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  324. picture->setFrame(iconIndex + 2);
  325. redraw();
  326. }
  327. void CTownList::CTownItem::select(bool on)
  328. {
  329. if(on)
  330. LOCPLINT->localState->setSelection(LOCPLINT->localState->getOwnedTowns()[townIndex]);
  331. }
  332. void CTownList::CTownItem::open()
  333. {
  334. LOCPLINT->openTownWindow(LOCPLINT->localState->getOwnedTowns()[townIndex]);
  335. }
  336. void CTownList::CTownItem::showTooltip()
  337. {
  338. CRClickPopup::createAndPush(LOCPLINT->localState->getOwnedTowns()[townIndex], GH.getCursorPosition());
  339. }
  340. void CTownList::CTownItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  341. {
  342. if(!on)
  343. return;
  344. const std::vector<const CGTownInstance *> towns = LOCPLINT->localState->getOwnedTowns();
  345. if(townIndex < 0 || townIndex > towns.size() - 1 || !towns[townIndex])
  346. return;
  347. if(towns.size() < 2)
  348. return;
  349. int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
  350. int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
  351. std::vector<RadialMenuConfig> menuElements = {
  352. { RadialMenuConfig::ITEM_ALT_NN, townUpperPos > -1, "altUpTop", "vcmi.radialWheel.moveTop", [this]()
  353. {
  354. for (int i = townIndex; i > 0; i--)
  355. LOCPLINT->localState->swapOwnedTowns(i, i - 1);
  356. parentList->updateWidget();
  357. } },
  358. { RadialMenuConfig::ITEM_ALT_NW, townUpperPos > -1, "altUp", "vcmi.radialWheel.moveUp", [this, townUpperPos](){LOCPLINT->localState->swapOwnedTowns(townIndex, townUpperPos); parentList->updateWidget(); } },
  359. { RadialMenuConfig::ITEM_ALT_SW, townLowerPos > -1, "altDown", "vcmi.radialWheel.moveDown", [this, townLowerPos](){ LOCPLINT->localState->swapOwnedTowns(townIndex, townLowerPos); parentList->updateWidget(); } },
  360. { RadialMenuConfig::ITEM_ALT_SS, townLowerPos > -1, "altDownBottom", "vcmi.radialWheel.moveBottom", [this, towns]()
  361. {
  362. for (int i = townIndex; i < towns.size() - 1; i++)
  363. LOCPLINT->localState->swapOwnedTowns(i, i + 1);
  364. parentList->updateWidget();
  365. } },
  366. };
  367. GH.windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
  368. }
  369. std::string CTownList::CTownItem::getHoverText()
  370. {
  371. return LOCPLINT->localState->getOwnedTowns()[townIndex]->getObjectName();
  372. }
  373. CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  374. : CList(visibleItemsCount, widgetPosition)
  375. {
  376. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  377. }
  378. void CTownList::select(const CGTownInstance * town)
  379. {
  380. selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
  381. }
  382. void CTownList::updateElement(const CGTownInstance * town)
  383. {
  384. updateWidget();
  385. }
  386. void CTownList::updateWidget()
  387. {
  388. auto & towns = LOCPLINT->localState->getOwnedTowns();
  389. listBox->resize(towns.size());
  390. for (size_t i = 0; i < towns.size(); ++i)
  391. {
  392. auto item = std::dynamic_pointer_cast<CTownItem>(listBox->getItem(i));
  393. if (!item)
  394. continue;
  395. listBox->reset();
  396. }
  397. if (LOCPLINT->localState->getCurrentTown())
  398. select(LOCPLINT->localState->getCurrentTown());
  399. CList::update();
  400. }