CList.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 "../windows/CCastleInterface.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../PlayerLocalState.h"
  21. #include "../GameEngine.h"
  22. #include "../GameInstance.h"
  23. #include "../gui/Shortcut.h"
  24. #include "../gui/WindowHandler.h"
  25. #include "../render/Canvas.h"
  26. #include "../render/Colors.h"
  27. #include "../../lib/texts/CGeneralTextHandler.h"
  28. #include "../../lib/IGameSettings.h"
  29. #include "../../lib/mapObjects/CGHeroInstance.h"
  30. #include "../../lib/mapObjects/CGTownInstance.h"
  31. #include "../../CCallback.h"
  32. CList::CListItem::CListItem(CList * Parent)
  33. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  34. parent(Parent),
  35. selection()
  36. {
  37. }
  38. CList::CListItem::~CListItem() = default;
  39. void CList::CListItem::showPopupWindow(const Point & cursorPosition)
  40. {
  41. showTooltip();
  42. }
  43. void CList::CListItem::clickPressed(const Point & cursorPosition)
  44. {
  45. //second click on already selected item
  46. if(parent->selected == this->shared_from_this())
  47. {
  48. open();
  49. }
  50. else
  51. {
  52. //first click - switch selection
  53. parent->select(this->shared_from_this());
  54. }
  55. }
  56. void CList::CListItem::hover(bool on)
  57. {
  58. if (on)
  59. ENGINE->statusbar()->write(getHoverText());
  60. else
  61. ENGINE->statusbar()->clear();
  62. }
  63. void CList::CListItem::onSelect(bool on)
  64. {
  65. OBJECT_CONSTRUCTION;
  66. selection.reset();
  67. if(on)
  68. selection = genSelection();
  69. select(on);
  70. redraw();
  71. }
  72. CList::CList(int Size, Rect widgetDimensions)
  73. : Scrollable(0, widgetDimensions.topLeft(), Orientation::VERTICAL),
  74. size(Size),
  75. selected(nullptr)
  76. {
  77. pos.w = widgetDimensions.w;
  78. pos.h = widgetDimensions.h;
  79. }
  80. void CList::showAll(Canvas & to)
  81. {
  82. to.drawColor(pos, Colors::BLACK);
  83. CIntObject::showAll(to);
  84. }
  85. void CList::createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount)
  86. {
  87. OBJECT_CONSTRUCTION;
  88. listBox = std::make_shared<CListBox>(std::bind(&CList::createItem, this, _1), firstItemPosition, itemPositionDelta, size, listAmount);
  89. }
  90. void CList::setScrollUpButton(std::shared_ptr<CButton> button)
  91. {
  92. addChild(button.get());
  93. scrollUp = button;
  94. scrollUp->addCallback(std::bind(&CList::scrollPrev, this));
  95. update();
  96. }
  97. void CList::setScrollDownButton(std::shared_ptr<CButton> button)
  98. {
  99. addChild(button.get());
  100. scrollDown = button;
  101. scrollDown->addCallback(std::bind(&CList::scrollNext, this));
  102. update();
  103. }
  104. void CList::scrollBy(int distance)
  105. {
  106. if (distance < 0 && listBox->getPos() < -distance)
  107. listBox->moveToPos(0);
  108. else
  109. listBox->moveToPos(static_cast<int>(listBox->getPos()) + distance);
  110. update();
  111. }
  112. void CList::scrollPrev()
  113. {
  114. listBox->moveToPrev();
  115. update();
  116. }
  117. void CList::scrollNext()
  118. {
  119. listBox->moveToNext();
  120. update();
  121. }
  122. void CList::update()
  123. {
  124. bool onTop = listBox->getPos() == 0;
  125. bool onBottom = listBox->getPos() + size >= listBox->size();
  126. if (scrollUp)
  127. scrollUp->block(onTop);
  128. if (scrollDown)
  129. scrollDown->block(onBottom);
  130. }
  131. void CList::select(std::shared_ptr<CListItem> which)
  132. {
  133. if(selected == which)
  134. return;
  135. if(selected)
  136. selected->onSelect(false);
  137. selected = which;
  138. if(which)
  139. {
  140. which->onSelect(true);
  141. onSelect();
  142. }
  143. }
  144. int CList::getSelectedIndex()
  145. {
  146. return static_cast<int>(listBox->getIndexOf(selected));
  147. }
  148. void CList::selectIndex(int which)
  149. {
  150. if(which < 0)
  151. {
  152. if(selected)
  153. select(nullptr);
  154. }
  155. else
  156. {
  157. listBox->scrollTo(which);
  158. update();
  159. select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
  160. }
  161. }
  162. void CList::selectNext()
  163. {
  164. int index = getSelectedIndex() + 1;
  165. if(index >= listBox->size())
  166. index = 0;
  167. selectIndex(index);
  168. }
  169. void CList::selectPrev()
  170. {
  171. int index = getSelectedIndex();
  172. if(index <= 0)
  173. selectIndex(0);
  174. else
  175. selectIndex(index-1);
  176. }
  177. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  178. {
  179. OBJECT_CONSTRUCTION;
  180. movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
  181. portrait = std::make_shared<CPicture>(ImagePath::builtin("HPSXXX"), movement->pos.w + 1, 0);
  182. mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  183. pos.w = mana->pos.w + mana->pos.x - pos.x;
  184. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  185. }
  186. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  187. : CListItem(parent),
  188. hero(Hero)
  189. {
  190. OBJECT_CONSTRUCTION;
  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 | KEYBOARD);
  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. GAME->interface()->localState->setSelection(hero);
  213. }
  214. void CHeroList::CHeroItem::open()
  215. {
  216. GAME->interface()->openHeroWindow(hero);
  217. }
  218. void CHeroList::CHeroItem::showTooltip()
  219. {
  220. CRClickPopup::createAndPush(hero, ENGINE->getCursorPosition());
  221. }
  222. std::string CHeroList::CHeroItem::getHoverText()
  223. {
  224. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->getClassNameTranslated()) + hero->getMovementPointsTextIfOwner(hero->getOwner());
  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 = GAME->interface()->localState->getWanderingHeroes();
  233. if(heroes.size() < 2)
  234. return;
  235. size_t heroPos = vstd::find_pos(heroes, hero);
  236. const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes.at(heroPos - 1);
  237. const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes.at(heroPos + 1);
  238. std::vector<RadialMenuConfig> menuElements = {
  239. { RadialMenuConfig::ITEM_ALT_NN, heroUpper != nullptr, "altUpTop", "vcmi.radialWheel.moveTop", [heroPos]()
  240. {
  241. for (size_t i = heroPos; i > 0; i--)
  242. GAME->interface()->localState->swapWanderingHero(i, i - 1);
  243. } },
  244. { RadialMenuConfig::ITEM_ALT_NW, heroUpper != nullptr, "altUp", "vcmi.radialWheel.moveUp", [heroPos](){GAME->interface()->localState->swapWanderingHero(heroPos, heroPos - 1); } },
  245. { RadialMenuConfig::ITEM_ALT_SW, heroLower != nullptr, "altDown", "vcmi.radialWheel.moveDown", [heroPos](){ GAME->interface()->localState->swapWanderingHero(heroPos, heroPos + 1); } },
  246. { RadialMenuConfig::ITEM_ALT_SS, heroLower != nullptr, "altDownBottom", "vcmi.radialWheel.moveBottom", [heroPos, heroes]()
  247. {
  248. for (int i = heroPos; i < heroes.size() - 1; i++)
  249. GAME->interface()->localState->swapWanderingHero(i, i + 1);
  250. } },
  251. };
  252. ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
  253. }
  254. void CHeroList::CHeroItem::keyPressed(EShortcut key)
  255. {
  256. if(!hero)
  257. return;
  258. if(parent->selected != this->shared_from_this())
  259. return;
  260. auto & heroes = GAME->interface()->localState->getWanderingHeroes();
  261. if(key == EShortcut::LIST_HERO_DISMISS)
  262. {
  263. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[22], [this](){ GAME->interface()->cb->dismissHero(hero); }, nullptr);
  264. return;
  265. }
  266. if(heroes.size() < 2)
  267. return;
  268. size_t heroPos = vstd::find_pos(heroes, hero);
  269. const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes.at(heroPos - 1);
  270. const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes.at(heroPos + 1);
  271. switch(key)
  272. {
  273. case EShortcut::LIST_HERO_UP:
  274. if(heroUpper)
  275. GAME->interface()->localState->swapWanderingHero(heroPos, heroPos - 1);
  276. break;
  277. case EShortcut::LIST_HERO_DOWN:
  278. if(heroLower)
  279. GAME->interface()->localState->swapWanderingHero(heroPos, heroPos + 1);
  280. break;
  281. case EShortcut::LIST_HERO_TOP:
  282. if(heroUpper)
  283. for (size_t i = heroPos; i > 0; i--)
  284. GAME->interface()->localState->swapWanderingHero(i, i - 1);
  285. break;
  286. case EShortcut::LIST_HERO_BOTTOM:
  287. if(heroLower)
  288. for (int i = heroPos; i < heroes.size() - 1; i++)
  289. GAME->interface()->localState->swapWanderingHero(i, i + 1);
  290. break;
  291. }
  292. }
  293. std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
  294. {
  295. if (GAME->interface()->localState->getWanderingHeroes().size() > index)
  296. return std::make_shared<CHeroItem>(this, GAME->interface()->localState->getWanderingHero(index));
  297. return std::make_shared<CEmptyHeroItem>();
  298. }
  299. CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  300. : CList(visibleItemsCount, widgetPosition)
  301. {
  302. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  303. }
  304. void CHeroList::select(const CGHeroInstance * hero)
  305. {
  306. selectIndex(vstd::find_pos(GAME->interface()->localState->getWanderingHeroes(), hero));
  307. }
  308. void CHeroList::updateElement(const CGHeroInstance * hero)
  309. {
  310. updateWidget();
  311. }
  312. void CHeroList::updateWidget()
  313. {
  314. const auto & heroes = GAME->interface()->localState->getWanderingHeroes();
  315. listBox->resize(heroes.size());
  316. for (size_t i = 0; i < heroes.size(); ++i)
  317. {
  318. auto item = std::dynamic_pointer_cast<CHeroItem>(listBox->getItem(i));
  319. if (!item)
  320. continue;
  321. if (item->hero == heroes.at(i))
  322. {
  323. item->update();
  324. }
  325. else
  326. {
  327. listBox->reset();
  328. break;
  329. }
  330. }
  331. if (GAME->interface()->localState->getCurrentHero())
  332. select(GAME->interface()->localState->getCurrentHero());
  333. CList::update();
  334. }
  335. std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
  336. {
  337. if (GAME->interface()->localState->getOwnedTowns().size() > index)
  338. return std::make_shared<CTownItem>(this, GAME->interface()->localState->getOwnedTown(index));
  339. return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
  340. }
  341. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  342. CListItem(parent),
  343. town(Town)
  344. {
  345. OBJECT_CONSTRUCTION;
  346. picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
  347. pos = picture->pos;
  348. update();
  349. addUsedEvents(GESTURE | KEYBOARD);
  350. }
  351. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  352. {
  353. return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 1);
  354. }
  355. void CTownList::CTownItem::update()
  356. {
  357. size_t iconIndex = town->getTown()->clientInfo.icons[town->hasFort()][town->built >= GAME->interface()->cb->getSettings().getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  358. picture->setFrame(iconIndex + 2);
  359. redraw();
  360. }
  361. void CTownList::CTownItem::select(bool on)
  362. {
  363. if(on)
  364. GAME->interface()->localState->setSelection(town);
  365. }
  366. void CTownList::CTownItem::open()
  367. {
  368. GAME->interface()->openTownWindow(town);
  369. }
  370. void CTownList::CTownItem::showTooltip()
  371. {
  372. CRClickPopup::createAndPush(town, pos.center());
  373. }
  374. void CTownList::CTownItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  375. {
  376. if(!on)
  377. return;
  378. const std::vector<const CGTownInstance *> towns = GAME->interface()->localState->getOwnedTowns();
  379. size_t townIndex = vstd::find_pos(towns, town);
  380. if(townIndex + 1 > towns.size() || !towns.at(townIndex))
  381. return;
  382. if(towns.size() < 2)
  383. return;
  384. int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
  385. int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
  386. auto updateList = [](){
  387. for (auto ci : ENGINE->windows().findWindows<CCastleInterface>())
  388. {
  389. ci->townlist->updateWidget();
  390. ci->townlist->select(ci->town);
  391. }
  392. };
  393. std::vector<RadialMenuConfig> menuElements = {
  394. { RadialMenuConfig::ITEM_ALT_NN, townUpperPos > -1, "altUpTop", "vcmi.radialWheel.moveTop", [updateList, townIndex]()
  395. {
  396. for (int i = townIndex; i > 0; i--)
  397. GAME->interface()->localState->swapOwnedTowns(i, i - 1);
  398. updateList();
  399. } },
  400. { RadialMenuConfig::ITEM_ALT_NW, townUpperPos > -1, "altUp", "vcmi.radialWheel.moveUp", [updateList, townIndex, townUpperPos](){GAME->interface()->localState->swapOwnedTowns(townIndex, townUpperPos); updateList(); } },
  401. { RadialMenuConfig::ITEM_ALT_SW, townLowerPos > -1, "altDown", "vcmi.radialWheel.moveDown", [updateList, townIndex, townLowerPos](){ GAME->interface()->localState->swapOwnedTowns(townIndex, townLowerPos); updateList(); } },
  402. { RadialMenuConfig::ITEM_ALT_SS, townLowerPos > -1, "altDownBottom", "vcmi.radialWheel.moveBottom", [updateList, townIndex, towns]()
  403. {
  404. for (int i = townIndex; i < towns.size() - 1; i++)
  405. GAME->interface()->localState->swapOwnedTowns(i, i + 1);
  406. updateList();
  407. } },
  408. };
  409. ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
  410. }
  411. void CTownList::CTownItem::keyPressed(EShortcut key)
  412. {
  413. if(parent->selected != this->shared_from_this())
  414. return;
  415. const std::vector<const CGTownInstance *> towns = GAME->interface()->localState->getOwnedTowns();
  416. size_t townIndex = vstd::find_pos(towns, town);
  417. if(townIndex + 1 > towns.size() || !towns.at(townIndex))
  418. return;
  419. if(towns.size() < 2)
  420. return;
  421. int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
  422. int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
  423. switch(key)
  424. {
  425. case EShortcut::LIST_TOWN_UP:
  426. if(townUpperPos > -1)
  427. GAME->interface()->localState->swapOwnedTowns(townIndex, townUpperPos);
  428. break;
  429. case EShortcut::LIST_TOWN_DOWN:
  430. if(townLowerPos > -1)
  431. GAME->interface()->localState->swapOwnedTowns(townIndex, townLowerPos);
  432. break;
  433. case EShortcut::LIST_TOWN_TOP:
  434. if(townUpperPos > -1)
  435. for (int i = townIndex; i > 0; i--)
  436. GAME->interface()->localState->swapOwnedTowns(i, i - 1);
  437. break;
  438. case EShortcut::LIST_TOWN_BOTTOM:
  439. if(townLowerPos > -1)
  440. for (int i = townIndex; i < towns.size() - 1; i++)
  441. GAME->interface()->localState->swapOwnedTowns(i, i + 1);
  442. break;
  443. }
  444. for (auto ki : ENGINE->windows().findWindows<CCastleInterface>())
  445. ki->townChange(); //update list
  446. }
  447. std::string CTownList::CTownItem::getHoverText()
  448. {
  449. return town->getObjectName();
  450. }
  451. CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  452. : CList(visibleItemsCount, widgetPosition)
  453. {
  454. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  455. }
  456. void CTownList::select(const CGTownInstance * town)
  457. {
  458. selectIndex(vstd::find_pos(GAME->interface()->localState->getOwnedTowns(), town));
  459. }
  460. void CTownList::updateElement(const CGTownInstance * town)
  461. {
  462. updateWidget();
  463. }
  464. void CTownList::updateWidget()
  465. {
  466. auto & towns = GAME->interface()->localState->getOwnedTowns();
  467. listBox->resize(towns.size());
  468. for (size_t i = 0; i < towns.size(); ++i)
  469. {
  470. auto item = std::dynamic_pointer_cast<CTownItem>(listBox->getItem(i));
  471. if (!item)
  472. continue;
  473. if (item->town == towns[i])
  474. {
  475. item->update();
  476. }
  477. else
  478. {
  479. listBox->reset();
  480. break;
  481. }
  482. }
  483. if (GAME->interface()->localState->getCurrentTown())
  484. select(GAME->interface()->localState->getCurrentTown());
  485. CList::update();
  486. }