123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- /*
- * CList.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "CList.h"
- #include "AdventureMapInterface.h"
- #include "../widgets/Images.h"
- #include "../widgets/Buttons.h"
- #include "../widgets/ObjectLists.h"
- #include "../widgets/RadialMenu.h"
- #include "../windows/InfoWindows.h"
- #include "../windows/CCastleInterface.h"
- #include "../CPlayerInterface.h"
- #include "../PlayerLocalState.h"
- #include "../GameEngine.h"
- #include "../GameInstance.h"
- #include "../gui/Shortcut.h"
- #include "../gui/WindowHandler.h"
- #include "../render/Canvas.h"
- #include "../render/Colors.h"
- #include "../../lib/texts/CGeneralTextHandler.h"
- #include "../../lib/IGameSettings.h"
- #include "../../lib/mapObjects/CGHeroInstance.h"
- #include "../../lib/mapObjects/CGTownInstance.h"
- #include "../../CCallback.h"
- CList::CListItem::CListItem(CList * Parent)
- : CIntObject(LCLICK | SHOW_POPUP | HOVER),
- parent(Parent),
- selection()
- {
- }
- CList::CListItem::~CListItem() = default;
- void CList::CListItem::showPopupWindow(const Point & cursorPosition)
- {
- showTooltip();
- }
- void CList::CListItem::clickPressed(const Point & cursorPosition)
- {
- //second click on already selected item
- if(parent->selected == this->shared_from_this())
- {
- open();
- }
- else
- {
- //first click - switch selection
- parent->select(this->shared_from_this());
- }
- }
- void CList::CListItem::hover(bool on)
- {
- if (on)
- ENGINE->statusbar()->write(getHoverText());
- else
- ENGINE->statusbar()->clear();
- }
- void CList::CListItem::onSelect(bool on)
- {
- OBJECT_CONSTRUCTION;
- selection.reset();
- if(on)
- selection = genSelection();
- select(on);
- redraw();
- }
- CList::CList(int Size, Rect widgetDimensions)
- : Scrollable(0, widgetDimensions.topLeft(), Orientation::VERTICAL),
- size(Size),
- selected(nullptr)
- {
- pos.w = widgetDimensions.w;
- pos.h = widgetDimensions.h;
- }
- void CList::showAll(Canvas & to)
- {
- to.drawColor(pos, Colors::BLACK);
- CIntObject::showAll(to);
- }
- void CList::createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount)
- {
- OBJECT_CONSTRUCTION;
- listBox = std::make_shared<CListBox>(std::bind(&CList::createItem, this, _1), firstItemPosition, itemPositionDelta, size, listAmount);
- }
- void CList::setScrollUpButton(std::shared_ptr<CButton> button)
- {
- addChild(button.get());
- scrollUp = button;
- scrollUp->addCallback(std::bind(&CList::scrollPrev, this));
- update();
- }
- void CList::setScrollDownButton(std::shared_ptr<CButton> button)
- {
- addChild(button.get());
- scrollDown = button;
- scrollDown->addCallback(std::bind(&CList::scrollNext, this));
- update();
- }
- void CList::scrollBy(int distance)
- {
- if (distance < 0 && listBox->getPos() < -distance)
- listBox->moveToPos(0);
- else
- listBox->moveToPos(static_cast<int>(listBox->getPos()) + distance);
- update();
- }
- void CList::scrollPrev()
- {
- listBox->moveToPrev();
- update();
- }
- void CList::scrollNext()
- {
- listBox->moveToNext();
- update();
- }
- void CList::update()
- {
- bool onTop = listBox->getPos() == 0;
- bool onBottom = listBox->getPos() + size >= listBox->size();
- if (scrollUp)
- scrollUp->block(onTop);
- if (scrollDown)
- scrollDown->block(onBottom);
- }
- void CList::select(std::shared_ptr<CListItem> which)
- {
- if(selected == which)
- return;
- if(selected)
- selected->onSelect(false);
- selected = which;
- if(which)
- {
- which->onSelect(true);
- onSelect();
- }
- }
- int CList::getSelectedIndex()
- {
- return static_cast<int>(listBox->getIndexOf(selected));
- }
- void CList::selectIndex(int which)
- {
- if(which < 0)
- {
- if(selected)
- select(nullptr);
- }
- else
- {
- listBox->scrollTo(which);
- update();
- select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
- }
- }
- void CList::selectNext()
- {
- int index = getSelectedIndex() + 1;
- if(index >= listBox->size())
- index = 0;
- selectIndex(index);
- }
- void CList::selectPrev()
- {
- int index = getSelectedIndex();
- if(index <= 0)
- selectIndex(0);
- else
- selectIndex(index-1);
- }
- CHeroList::CEmptyHeroItem::CEmptyHeroItem()
- {
- OBJECT_CONSTRUCTION;
- movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
- portrait = std::make_shared<CPicture>(ImagePath::builtin("HPSXXX"), movement->pos.w + 1, 0);
- mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
- pos.w = mana->pos.w + mana->pos.x - pos.x;
- pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
- }
- CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
- : CListItem(parent),
- hero(Hero)
- {
- OBJECT_CONSTRUCTION;
- movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
- portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), hero->getIconIndex(), 0, movement->pos.w + 1);
- mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
- pos.w = mana->pos.w + mana->pos.x - pos.x;
- pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
- update();
- addUsedEvents(GESTURE | KEYBOARD);
- }
- void CHeroList::CHeroItem::update()
- {
- movement->setFrame(std::min<size_t>(movement->size()-1, hero->movementPointsRemaining() / 100));
- mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
- redraw();
- }
- std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
- {
- return std::make_shared<CPicture>(ImagePath::builtin("HPSYYY"), movement->pos.w + 1, 0);
- }
- void CHeroList::CHeroItem::select(bool on)
- {
- if(on)
- GAME->interface()->localState->setSelection(hero);
- }
- void CHeroList::CHeroItem::open()
- {
- GAME->interface()->openHeroWindow(hero);
- }
- void CHeroList::CHeroItem::showTooltip()
- {
- CRClickPopup::createAndPush(hero, ENGINE->getCursorPosition());
- }
- std::string CHeroList::CHeroItem::getHoverText()
- {
- return boost::str(boost::format(LIBRARY->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->getClassNameTranslated()) + hero->getMovementPointsTextIfOwner(hero->getOwner());
- }
- void CHeroList::CHeroItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
- {
- if(!on)
- return;
- if(!hero)
- return;
- auto & heroes = GAME->interface()->localState->getWanderingHeroes();
- if(heroes.size() < 2)
- return;
- size_t heroPos = vstd::find_pos(heroes, hero);
- const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes.at(heroPos - 1);
- const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes.at(heroPos + 1);
- std::vector<RadialMenuConfig> menuElements = {
- { RadialMenuConfig::ITEM_ALT_NN, heroUpper != nullptr, "altUpTop", "vcmi.radialWheel.moveTop", [heroPos]()
- {
- for (size_t i = heroPos; i > 0; i--)
- GAME->interface()->localState->swapWanderingHero(i, i - 1);
- } },
- { RadialMenuConfig::ITEM_ALT_NW, heroUpper != nullptr, "altUp", "vcmi.radialWheel.moveUp", [heroPos](){GAME->interface()->localState->swapWanderingHero(heroPos, heroPos - 1); } },
- { RadialMenuConfig::ITEM_ALT_SW, heroLower != nullptr, "altDown", "vcmi.radialWheel.moveDown", [heroPos](){ GAME->interface()->localState->swapWanderingHero(heroPos, heroPos + 1); } },
- { RadialMenuConfig::ITEM_ALT_SS, heroLower != nullptr, "altDownBottom", "vcmi.radialWheel.moveBottom", [heroPos, heroes]()
- {
- for (int i = heroPos; i < heroes.size() - 1; i++)
- GAME->interface()->localState->swapWanderingHero(i, i + 1);
- } },
- };
- ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
- }
- void CHeroList::CHeroItem::keyPressed(EShortcut key)
- {
- if(!hero)
- return;
- if(parent->selected != this->shared_from_this())
- return;
- auto & heroes = GAME->interface()->localState->getWanderingHeroes();
- if(key == EShortcut::LIST_HERO_DISMISS)
- {
- GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[22], [=](){ GAME->interface()->cb->dismissHero(hero); }, nullptr);
- return;
- }
- if(heroes.size() < 2)
- return;
- size_t heroPos = vstd::find_pos(heroes, hero);
- const CGHeroInstance * heroUpper = (heroPos < 1) ? nullptr : heroes.at(heroPos - 1);
- const CGHeroInstance * heroLower = (heroPos > heroes.size() - 2) ? nullptr : heroes.at(heroPos + 1);
- switch(key)
- {
- case EShortcut::LIST_HERO_UP:
- if(heroUpper)
- GAME->interface()->localState->swapWanderingHero(heroPos, heroPos - 1);
- break;
- case EShortcut::LIST_HERO_DOWN:
- if(heroLower)
- GAME->interface()->localState->swapWanderingHero(heroPos, heroPos + 1);
- break;
- case EShortcut::LIST_HERO_TOP:
- if(heroUpper)
- for (size_t i = heroPos; i > 0; i--)
- GAME->interface()->localState->swapWanderingHero(i, i - 1);
- break;
- case EShortcut::LIST_HERO_BOTTOM:
- if(heroLower)
- for (int i = heroPos; i < heroes.size() - 1; i++)
- GAME->interface()->localState->swapWanderingHero(i, i + 1);
- break;
- }
- }
- std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
- {
- if (GAME->interface()->localState->getWanderingHeroes().size() > index)
- return std::make_shared<CHeroItem>(this, GAME->interface()->localState->getWanderingHero(index));
- return std::make_shared<CEmptyHeroItem>();
- }
- CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
- : CList(visibleItemsCount, widgetPosition)
- {
- createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
- }
- void CHeroList::select(const CGHeroInstance * hero)
- {
- selectIndex(vstd::find_pos(GAME->interface()->localState->getWanderingHeroes(), hero));
- }
- void CHeroList::updateElement(const CGHeroInstance * hero)
- {
- updateWidget();
- }
- void CHeroList::updateWidget()
- {
- const auto & heroes = GAME->interface()->localState->getWanderingHeroes();
- listBox->resize(heroes.size());
- for (size_t i = 0; i < heroes.size(); ++i)
- {
- auto item = std::dynamic_pointer_cast<CHeroItem>(listBox->getItem(i));
- if (!item)
- continue;
- if (item->hero == heroes.at(i))
- {
- item->update();
- }
- else
- {
- listBox->reset();
- break;
- }
- }
- if (GAME->interface()->localState->getCurrentHero())
- select(GAME->interface()->localState->getCurrentHero());
- CList::update();
- }
- std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
- {
- if (GAME->interface()->localState->getOwnedTowns().size() > index)
- return std::make_shared<CTownItem>(this, GAME->interface()->localState->getOwnedTown(index));
- return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
- }
- CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
- CListItem(parent),
- town(Town)
- {
- OBJECT_CONSTRUCTION;
- picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
- pos = picture->pos;
- update();
- addUsedEvents(GESTURE | KEYBOARD);
- }
- std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
- {
- return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 1);
- }
- void CTownList::CTownItem::update()
- {
- size_t iconIndex = town->getTown()->clientInfo.icons[town->hasFort()][town->built >= GAME->interface()->cb->getSettings().getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
- picture->setFrame(iconIndex + 2);
- redraw();
- }
- void CTownList::CTownItem::select(bool on)
- {
- if(on)
- GAME->interface()->localState->setSelection(town);
- }
- void CTownList::CTownItem::open()
- {
- GAME->interface()->openTownWindow(town);
- }
- void CTownList::CTownItem::showTooltip()
- {
- CRClickPopup::createAndPush(town, pos.center());
- }
- void CTownList::CTownItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
- {
- if(!on)
- return;
- const std::vector<const CGTownInstance *> towns = GAME->interface()->localState->getOwnedTowns();
- size_t townIndex = vstd::find_pos(towns, town);
- if(townIndex + 1 > towns.size() || !towns.at(townIndex))
- return;
- if(towns.size() < 2)
- return;
- int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
- int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
- auto updateList = [](){
- for (auto ci : ENGINE->windows().findWindows<CCastleInterface>())
- {
- ci->townlist->updateWidget();
- ci->townlist->select(ci->town);
- }
- };
- std::vector<RadialMenuConfig> menuElements = {
- { RadialMenuConfig::ITEM_ALT_NN, townUpperPos > -1, "altUpTop", "vcmi.radialWheel.moveTop", [updateList, townIndex]()
- {
- for (int i = townIndex; i > 0; i--)
- GAME->interface()->localState->swapOwnedTowns(i, i - 1);
- updateList();
- } },
- { RadialMenuConfig::ITEM_ALT_NW, townUpperPos > -1, "altUp", "vcmi.radialWheel.moveUp", [updateList, townIndex, townUpperPos](){GAME->interface()->localState->swapOwnedTowns(townIndex, townUpperPos); updateList(); } },
- { RadialMenuConfig::ITEM_ALT_SW, townLowerPos > -1, "altDown", "vcmi.radialWheel.moveDown", [updateList, townIndex, townLowerPos](){ GAME->interface()->localState->swapOwnedTowns(townIndex, townLowerPos); updateList(); } },
- { RadialMenuConfig::ITEM_ALT_SS, townLowerPos > -1, "altDownBottom", "vcmi.radialWheel.moveBottom", [updateList, townIndex, towns]()
- {
- for (int i = townIndex; i < towns.size() - 1; i++)
- GAME->interface()->localState->swapOwnedTowns(i, i + 1);
- updateList();
- } },
- };
- ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
- }
- void CTownList::CTownItem::keyPressed(EShortcut key)
- {
- if(parent->selected != this->shared_from_this())
- return;
- const std::vector<const CGTownInstance *> towns = GAME->interface()->localState->getOwnedTowns();
- size_t townIndex = vstd::find_pos(towns, town);
- if(townIndex + 1 > towns.size() || !towns.at(townIndex))
- return;
- if(towns.size() < 2)
- return;
- int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
- int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
- switch(key)
- {
- case EShortcut::LIST_TOWN_UP:
- if(townUpperPos > -1)
- GAME->interface()->localState->swapOwnedTowns(townIndex, townUpperPos);
- break;
- case EShortcut::LIST_TOWN_DOWN:
- if(townLowerPos > -1)
- GAME->interface()->localState->swapOwnedTowns(townIndex, townLowerPos);
- break;
- case EShortcut::LIST_TOWN_TOP:
- if(townUpperPos > -1)
- for (int i = townIndex; i > 0; i--)
- GAME->interface()->localState->swapOwnedTowns(i, i - 1);
- break;
- case EShortcut::LIST_TOWN_BOTTOM:
- if(townLowerPos > -1)
- for (int i = townIndex; i < towns.size() - 1; i++)
- GAME->interface()->localState->swapOwnedTowns(i, i + 1);
- break;
- }
- for (auto ki : ENGINE->windows().findWindows<CCastleInterface>())
- ki->townChange(); //update list
- }
- std::string CTownList::CTownItem::getHoverText()
- {
- return town->getObjectName();
- }
- CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
- : CList(visibleItemsCount, widgetPosition)
- {
- createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
- }
- void CTownList::select(const CGTownInstance * town)
- {
- selectIndex(vstd::find_pos(GAME->interface()->localState->getOwnedTowns(), town));
- }
- void CTownList::updateElement(const CGTownInstance * town)
- {
- updateWidget();
- }
- void CTownList::updateWidget()
- {
- auto & towns = GAME->interface()->localState->getOwnedTowns();
- listBox->resize(towns.size());
- for (size_t i = 0; i < towns.size(); ++i)
- {
- auto item = std::dynamic_pointer_cast<CTownItem>(listBox->getItem(i));
- if (!item)
- continue;
- if (item->town == towns[i])
- {
- item->update();
- }
- else
- {
- listBox->reset();
- break;
- }
- }
- if (GAME->interface()->localState->getCurrentTown())
- select(GAME->interface()->localState->getCurrentTown());
- CList::update();
- }
|