CList.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../render/Canvas.h"
  25. #include "../render/Colors.h"
  26. #include "../../lib/texts/CGeneralTextHandler.h"
  27. #include "../../lib/IGameSettings.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapObjects/CGTownInstance.h"
  30. #include "../../CCallback.h"
  31. CList::CListItem::CListItem(CList * Parent)
  32. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  33. parent(Parent),
  34. selection()
  35. {
  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. ENGINE->statusbar()->write(getHoverText());
  59. else
  60. ENGINE->statusbar()->clear();
  61. }
  62. void CList::CListItem::onSelect(bool on)
  63. {
  64. OBJECT_CONSTRUCTION;
  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;
  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;
  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. {
  189. OBJECT_CONSTRUCTION;
  190. movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
  191. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), hero->getIconIndex(), 0, movement->pos.w + 1);
  192. mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
  193. pos.w = mana->pos.w + mana->pos.x - pos.x;
  194. pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  195. update();
  196. addUsedEvents(GESTURE | KEYBOARD);
  197. }
  198. void CHeroList::CHeroItem::update()
  199. {
  200. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movementPointsRemaining() / 100));
  201. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  202. redraw();
  203. }
  204. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  205. {
  206. return std::make_shared<CPicture>(ImagePath::builtin("HPSYYY"), movement->pos.w + 1, 0);
  207. }
  208. void CHeroList::CHeroItem::select(bool on)
  209. {
  210. if(on)
  211. LOCPLINT->localState->setSelection(hero);
  212. }
  213. void CHeroList::CHeroItem::open()
  214. {
  215. LOCPLINT->openHeroWindow(hero);
  216. }
  217. void CHeroList::CHeroItem::showTooltip()
  218. {
  219. CRClickPopup::createAndPush(hero, ENGINE->getCursorPosition());
  220. }
  221. std::string CHeroList::CHeroItem::getHoverText()
  222. {
  223. return boost::str(boost::format(VLC->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->getClassNameTranslated()) + hero->getMovementPointsTextIfOwner(hero->getOwner());
  224. }
  225. void CHeroList::CHeroItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  226. {
  227. if(!on)
  228. return;
  229. if(!hero)
  230. return;
  231. auto & heroes = LOCPLINT->localState->getWanderingHeroes();
  232. if(heroes.size() < 2)
  233. return;
  234. size_t heroPos = vstd::find_pos(heroes, hero);
  235. const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes.at(heroPos - 1);
  236. const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes.at(heroPos + 1);
  237. std::vector<RadialMenuConfig> menuElements = {
  238. { RadialMenuConfig::ITEM_ALT_NN, heroUpper != nullptr, "altUpTop", "vcmi.radialWheel.moveTop", [heroPos]()
  239. {
  240. for (size_t i = heroPos; i > 0; i--)
  241. LOCPLINT->localState->swapWanderingHero(i, i - 1);
  242. } },
  243. { RadialMenuConfig::ITEM_ALT_NW, heroUpper != nullptr, "altUp", "vcmi.radialWheel.moveUp", [heroPos](){LOCPLINT->localState->swapWanderingHero(heroPos, heroPos - 1); } },
  244. { RadialMenuConfig::ITEM_ALT_SW, heroLower != nullptr, "altDown", "vcmi.radialWheel.moveDown", [heroPos](){ LOCPLINT->localState->swapWanderingHero(heroPos, heroPos + 1); } },
  245. { RadialMenuConfig::ITEM_ALT_SS, heroLower != nullptr, "altDownBottom", "vcmi.radialWheel.moveBottom", [heroPos, heroes]()
  246. {
  247. for (int i = heroPos; i < heroes.size() - 1; i++)
  248. LOCPLINT->localState->swapWanderingHero(i, i + 1);
  249. } },
  250. };
  251. ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
  252. }
  253. void CHeroList::CHeroItem::keyPressed(EShortcut key)
  254. {
  255. if(!hero)
  256. return;
  257. if(parent->selected != this->shared_from_this())
  258. return;
  259. auto & heroes = LOCPLINT->localState->getWanderingHeroes();
  260. if(key == EShortcut::LIST_HERO_DISMISS)
  261. {
  262. LOCPLINT->showYesNoDialog(VLC->generaltexth->allTexts[22], [=](){ LOCPLINT->cb->dismissHero(hero); }, nullptr);
  263. return;
  264. }
  265. if(heroes.size() < 2)
  266. return;
  267. size_t heroPos = vstd::find_pos(heroes, hero);
  268. const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes.at(heroPos - 1);
  269. const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes.at(heroPos + 1);
  270. switch(key)
  271. {
  272. case EShortcut::LIST_HERO_UP:
  273. if(heroUpper)
  274. LOCPLINT->localState->swapWanderingHero(heroPos, heroPos - 1);
  275. break;
  276. case EShortcut::LIST_HERO_DOWN:
  277. if(heroLower)
  278. LOCPLINT->localState->swapWanderingHero(heroPos, heroPos + 1);
  279. break;
  280. case EShortcut::LIST_HERO_TOP:
  281. if(heroUpper)
  282. for (size_t i = heroPos; i > 0; i--)
  283. LOCPLINT->localState->swapWanderingHero(i, i - 1);
  284. break;
  285. case EShortcut::LIST_HERO_BOTTOM:
  286. if(heroLower)
  287. for (int i = heroPos; i < heroes.size() - 1; i++)
  288. LOCPLINT->localState->swapWanderingHero(i, i + 1);
  289. break;
  290. }
  291. }
  292. std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
  293. {
  294. if (LOCPLINT->localState->getWanderingHeroes().size() > index)
  295. return std::make_shared<CHeroItem>(this, LOCPLINT->localState->getWanderingHero(index));
  296. return std::make_shared<CEmptyHeroItem>();
  297. }
  298. CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  299. : CList(visibleItemsCount, widgetPosition)
  300. {
  301. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  302. }
  303. void CHeroList::select(const CGHeroInstance * hero)
  304. {
  305. selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
  306. }
  307. void CHeroList::updateElement(const CGHeroInstance * hero)
  308. {
  309. updateWidget();
  310. }
  311. void CHeroList::updateWidget()
  312. {
  313. const auto & heroes = LOCPLINT->localState->getWanderingHeroes();
  314. listBox->resize(heroes.size());
  315. for (size_t i = 0; i < heroes.size(); ++i)
  316. {
  317. auto item = std::dynamic_pointer_cast<CHeroItem>(listBox->getItem(i));
  318. if (!item)
  319. continue;
  320. if (item->hero == heroes.at(i))
  321. {
  322. item->update();
  323. }
  324. else
  325. {
  326. listBox->reset();
  327. break;
  328. }
  329. }
  330. if (LOCPLINT->localState->getCurrentHero())
  331. select(LOCPLINT->localState->getCurrentHero());
  332. CList::update();
  333. }
  334. std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
  335. {
  336. if (LOCPLINT->localState->getOwnedTowns().size() > index)
  337. return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
  338. return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
  339. }
  340. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  341. CListItem(parent),
  342. town(Town)
  343. {
  344. OBJECT_CONSTRUCTION;
  345. picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
  346. pos = picture->pos;
  347. update();
  348. addUsedEvents(GESTURE | KEYBOARD);
  349. }
  350. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  351. {
  352. return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 1);
  353. }
  354. void CTownList::CTownItem::update()
  355. {
  356. size_t iconIndex = town->getTown()->clientInfo.icons[town->hasFort()][town->built >= LOCPLINT->cb->getSettings().getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  357. picture->setFrame(iconIndex + 2);
  358. redraw();
  359. }
  360. void CTownList::CTownItem::select(bool on)
  361. {
  362. if(on)
  363. LOCPLINT->localState->setSelection(town);
  364. }
  365. void CTownList::CTownItem::open()
  366. {
  367. LOCPLINT->openTownWindow(town);
  368. }
  369. void CTownList::CTownItem::showTooltip()
  370. {
  371. CRClickPopup::createAndPush(town, pos.center());
  372. }
  373. void CTownList::CTownItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  374. {
  375. if(!on)
  376. return;
  377. const std::vector<const CGTownInstance *> towns = LOCPLINT->localState->getOwnedTowns();
  378. size_t townIndex = vstd::find_pos(towns, town);
  379. if(townIndex + 1 > towns.size() || !towns.at(townIndex))
  380. return;
  381. if(towns.size() < 2)
  382. return;
  383. int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
  384. int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
  385. auto updateList = [](){
  386. for (auto ci : ENGINE->windows().findWindows<CCastleInterface>())
  387. {
  388. ci->townlist->updateWidget();
  389. ci->townlist->select(ci->town);
  390. }
  391. };
  392. std::vector<RadialMenuConfig> menuElements = {
  393. { RadialMenuConfig::ITEM_ALT_NN, townUpperPos > -1, "altUpTop", "vcmi.radialWheel.moveTop", [updateList, townIndex]()
  394. {
  395. for (int i = townIndex; i > 0; i--)
  396. LOCPLINT->localState->swapOwnedTowns(i, i - 1);
  397. updateList();
  398. } },
  399. { RadialMenuConfig::ITEM_ALT_NW, townUpperPos > -1, "altUp", "vcmi.radialWheel.moveUp", [updateList, townIndex, townUpperPos](){LOCPLINT->localState->swapOwnedTowns(townIndex, townUpperPos); updateList(); } },
  400. { RadialMenuConfig::ITEM_ALT_SW, townLowerPos > -1, "altDown", "vcmi.radialWheel.moveDown", [updateList, townIndex, townLowerPos](){ LOCPLINT->localState->swapOwnedTowns(townIndex, townLowerPos); updateList(); } },
  401. { RadialMenuConfig::ITEM_ALT_SS, townLowerPos > -1, "altDownBottom", "vcmi.radialWheel.moveBottom", [updateList, townIndex, towns]()
  402. {
  403. for (int i = townIndex; i < towns.size() - 1; i++)
  404. LOCPLINT->localState->swapOwnedTowns(i, i + 1);
  405. updateList();
  406. } },
  407. };
  408. ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
  409. }
  410. void CTownList::CTownItem::keyPressed(EShortcut key)
  411. {
  412. if(parent->selected != this->shared_from_this())
  413. return;
  414. const std::vector<const CGTownInstance *> towns = LOCPLINT->localState->getOwnedTowns();
  415. size_t townIndex = vstd::find_pos(towns, town);
  416. if(townIndex + 1 > towns.size() || !towns.at(townIndex))
  417. return;
  418. if(towns.size() < 2)
  419. return;
  420. int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
  421. int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
  422. switch(key)
  423. {
  424. case EShortcut::LIST_TOWN_UP:
  425. if(townUpperPos > -1)
  426. LOCPLINT->localState->swapOwnedTowns(townIndex, townUpperPos);
  427. break;
  428. case EShortcut::LIST_TOWN_DOWN:
  429. if(townLowerPos > -1)
  430. LOCPLINT->localState->swapOwnedTowns(townIndex, townLowerPos);
  431. break;
  432. case EShortcut::LIST_TOWN_TOP:
  433. if(townUpperPos > -1)
  434. for (int i = townIndex; i > 0; i--)
  435. LOCPLINT->localState->swapOwnedTowns(i, i - 1);
  436. break;
  437. case EShortcut::LIST_TOWN_BOTTOM:
  438. if(townLowerPos > -1)
  439. for (int i = townIndex; i < towns.size() - 1; i++)
  440. LOCPLINT->localState->swapOwnedTowns(i, i + 1);
  441. break;
  442. }
  443. for (auto ki : ENGINE->windows().findWindows<CCastleInterface>())
  444. ki->townChange(); //update list
  445. }
  446. std::string CTownList::CTownItem::getHoverText()
  447. {
  448. return town->getObjectName();
  449. }
  450. CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
  451. : CList(visibleItemsCount, widgetPosition)
  452. {
  453. createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
  454. }
  455. void CTownList::select(const CGTownInstance * town)
  456. {
  457. selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
  458. }
  459. void CTownList::updateElement(const CGTownInstance * town)
  460. {
  461. updateWidget();
  462. }
  463. void CTownList::updateWidget()
  464. {
  465. auto & towns = LOCPLINT->localState->getOwnedTowns();
  466. listBox->resize(towns.size());
  467. for (size_t i = 0; i < towns.size(); ++i)
  468. {
  469. auto item = std::dynamic_pointer_cast<CTownItem>(listBox->getItem(i));
  470. if (!item)
  471. continue;
  472. if (item->town == towns[i])
  473. {
  474. item->update();
  475. }
  476. else
  477. {
  478. listBox->reset();
  479. break;
  480. }
  481. }
  482. if (LOCPLINT->localState->getCurrentTown())
  483. select(LOCPLINT->localState->getCurrentTown());
  484. CList::update();
  485. }