CList.cpp 15 KB

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