CList.cpp 12 KB

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