2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-02-01 16:42:03 +02:00
|
|
|
* CList.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CList.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-08 14:18:34 +02:00
|
|
|
#include "AdventureMapInterface.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../widgets/Images.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
2023-05-08 23:04:44 +02:00
|
|
|
#include "../widgets/ObjectLists.h"
|
2023-10-15 14:31:33 +02:00
|
|
|
#include "../widgets/RadialMenu.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../windows/InfoWindows.h"
|
2024-08-04 14:00:40 +02:00
|
|
|
#include "../windows/CCastleInterface.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
2023-04-17 01:02:31 +02:00
|
|
|
#include "../PlayerLocalState.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2024-08-04 14:00:40 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2023-10-15 14:31:33 +02:00
|
|
|
#include "../gui/WindowHandler.h"
|
2023-06-02 15:42:18 +02:00
|
|
|
#include "../render/Canvas.h"
|
2023-07-31 18:50:55 +02:00
|
|
|
#include "../render/Colors.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/CGeneralTextHandler.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../../lib/CHeroHandler.h"
|
2024-08-31 13:00:36 +02:00
|
|
|
#include "../../lib/IGameSettings.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
2023-01-30 19:55:32 +02:00
|
|
|
|
2024-08-04 14:00:40 +02:00
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CList::CListItem::CListItem(CList * Parent)
|
2023-06-13 18:33:35 +02:00
|
|
|
: CIntObject(LCLICK | SHOW_POPUP | HOVER),
|
2015-01-13 21:57:41 +02:00
|
|
|
parent(Parent),
|
2018-04-07 13:34:11 +02:00
|
|
|
selection()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-27 16:29:19 +02:00
|
|
|
CList::CListItem::~CListItem() = default;
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CList::CListItem::showPopupWindow(const Point & cursorPosition)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-06-11 17:20:10 +02:00
|
|
|
showTooltip();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CList::CListItem::clickPressed(const Point & cursorPosition)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
//second click on already selected item
|
|
|
|
if(parent->selected == this->shared_from_this())
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
open();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//first click - switch selection
|
|
|
|
parent->select(this->shared_from_this());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::CListItem::hover(bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
2023-05-16 17:34:23 +02:00
|
|
|
GH.statusbar()->write(getHoverText());
|
2012-06-13 16:04:06 +03:00
|
|
|
else
|
2023-05-16 17:34:23 +02:00
|
|
|
GH.statusbar()->clear();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CList::CListItem::onSelect(bool on)
|
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2018-04-07 13:34:11 +02:00
|
|
|
selection.reset();
|
|
|
|
if(on)
|
2012-06-13 16:04:06 +03:00
|
|
|
selection = genSelection();
|
|
|
|
select(on);
|
2023-03-22 00:57:08 +02:00
|
|
|
redraw();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 15:52:43 +02:00
|
|
|
CList::CList(int Size, Rect widgetDimensions)
|
2023-05-30 17:53:51 +02:00
|
|
|
: Scrollable(0, widgetDimensions.topLeft(), Orientation::VERTICAL),
|
2015-01-13 21:57:41 +02:00
|
|
|
size(Size),
|
|
|
|
selected(nullptr)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-05-06 15:52:43 +02:00
|
|
|
pos.w = widgetDimensions.w;
|
|
|
|
pos.h = widgetDimensions.h;
|
|
|
|
}
|
2018-04-07 13:34:11 +02:00
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CList::showAll(Canvas & to)
|
2023-05-06 15:52:43 +02:00
|
|
|
{
|
2023-06-02 15:42:18 +02:00
|
|
|
to.drawColor(pos, Colors::BLACK);
|
2023-05-06 15:52:43 +02:00
|
|
|
CIntObject::showAll(to);
|
2023-04-26 14:44:10 +02:00
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-05-06 15:52:43 +02:00
|
|
|
void CList::createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount)
|
2023-04-26 14:44:10 +02:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-05-06 15:52:43 +02:00
|
|
|
listBox = std::make_shared<CListBox>(std::bind(&CList::createItem, this, _1), firstItemPosition, itemPositionDelta, size, listAmount);
|
2023-04-26 14:44:10 +02:00
|
|
|
}
|
2014-08-09 15:14:31 +03:00
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
void CList::setScrollUpButton(std::shared_ptr<CButton> button)
|
|
|
|
{
|
2023-05-06 15:52:43 +02:00
|
|
|
addChild(button.get());
|
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
scrollUp = button;
|
2023-05-30 17:53:51 +02:00
|
|
|
scrollUp->addCallback(std::bind(&CList::scrollPrev, this));
|
2023-04-26 14:44:10 +02:00
|
|
|
update();
|
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
void CList::setScrollDownButton(std::shared_ptr<CButton> button)
|
|
|
|
{
|
2023-05-06 15:52:43 +02:00
|
|
|
addChild(button.get());
|
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
scrollDown = button;
|
2023-05-30 17:53:51 +02:00
|
|
|
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();
|
2012-06-13 16:04:06 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::update()
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
bool onTop = listBox->getPos() == 0;
|
|
|
|
bool onBottom = listBox->getPos() + size >= listBox->size();
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
if (scrollUp)
|
|
|
|
scrollUp->block(onTop);
|
|
|
|
|
|
|
|
if (scrollDown)
|
|
|
|
scrollDown->block(onBottom);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
void CList::select(std::shared_ptr<CListItem> which)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(selected == which)
|
2012-06-13 16:04:06 +03:00
|
|
|
return;
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
if(selected)
|
2012-06-13 16:04:06 +03:00
|
|
|
selected->onSelect(false);
|
|
|
|
|
|
|
|
selected = which;
|
2018-04-07 13:34:11 +02:00
|
|
|
if(which)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
which->onSelect(true);
|
|
|
|
onSelect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CList::getSelectedIndex()
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
return static_cast<int>(listBox->getIndexOf(selected));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CList::selectIndex(int which)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(which < 0)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(selected)
|
2012-06-13 16:04:06 +03:00
|
|
|
select(nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
listBox->scrollTo(which);
|
2012-06-13 16:04:06 +03:00
|
|
|
update();
|
2018-04-07 13:34:11 +02:00
|
|
|
select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::selectNext()
|
|
|
|
{
|
2017-05-13 16:57:15 +02:00
|
|
|
int index = getSelectedIndex() + 1;
|
2018-04-07 13:34:11 +02:00
|
|
|
if(index >= listBox->size())
|
2017-05-13 16:57:15 +02:00
|
|
|
index = 0;
|
|
|
|
selectIndex(index);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CList::selectPrev()
|
|
|
|
{
|
|
|
|
int index = getSelectedIndex();
|
2018-04-07 13:34:11 +02:00
|
|
|
if(index <= 0)
|
2012-06-13 16:04:06 +03:00
|
|
|
selectIndex(0);
|
|
|
|
else
|
|
|
|
selectIndex(index-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHeroList::CEmptyHeroItem::CEmptyHeroItem()
|
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-08-23 14:07:50 +02:00
|
|
|
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 );
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
pos.w = mana->pos.w + mana->pos.x - pos.x;
|
2023-01-30 19:55:32 +02:00
|
|
|
pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
|
|
|
|
: CListItem(parent),
|
2023-12-16 12:49:05 +02:00
|
|
|
hero(Hero)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-08-23 14:07:50 +02:00
|
|
|
movement = std::make_shared<CAnimImage>(AnimationPath::builtin("IMOBIL"), 0, 0, 0, 1);
|
2023-09-28 18:43:04 +02:00
|
|
|
portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), hero->getIconIndex(), 0, movement->pos.w + 1);
|
2023-08-23 14:07:50 +02:00
|
|
|
mana = std::make_shared<CAnimImage>(AnimationPath::builtin("IMANA"), 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
pos.w = mana->pos.w + mana->pos.x - pos.x;
|
2023-01-30 19:55:32 +02:00
|
|
|
pos.h = std::max(std::max<int>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
update();
|
2023-10-17 21:13:08 +02:00
|
|
|
|
2024-08-04 14:00:40 +02:00
|
|
|
addUsedEvents(GESTURE | KEYBOARD);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::CHeroItem::update()
|
|
|
|
{
|
2023-06-21 19:38:26 +02:00
|
|
|
movement->setFrame(std::min<size_t>(movement->size()-1, hero->movementPointsRemaining() / 100));
|
2012-06-17 01:40:28 +03:00
|
|
|
mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
|
2012-06-13 16:04:06 +03:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
return std::make_shared<CPicture>(ImagePath::builtin("HPSYYY"), movement->pos.w + 1, 0);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::CHeroItem::select(bool on)
|
|
|
|
{
|
2023-04-18 22:08:27 +02:00
|
|
|
if(on)
|
|
|
|
LOCPLINT->localState->setSelection(hero);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::CHeroItem::open()
|
|
|
|
{
|
|
|
|
LOCPLINT->openHeroWindow(hero);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::CHeroItem::showTooltip()
|
|
|
|
{
|
2023-01-27 00:27:06 +02:00
|
|
|
CRClickPopup::createAndPush(hero, GH.getCursorPosition());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CHeroList::CHeroItem::getHoverText()
|
|
|
|
{
|
2024-09-17 21:38:28 +02:00
|
|
|
return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->getClassNameTranslated()) + hero->getMovementPointsTextIfOwner(hero->getOwner());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 21:13:08 +02:00
|
|
|
void CHeroList::CHeroItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
|
|
|
|
{
|
|
|
|
if(!on)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!hero)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto & heroes = LOCPLINT->localState->getWanderingHeroes();
|
|
|
|
|
|
|
|
if(heroes.size() < 2)
|
|
|
|
return;
|
|
|
|
|
2023-12-24 00:16:29 +02:00
|
|
|
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);
|
2023-10-17 21:13:08 +02:00
|
|
|
|
|
|
|
std::vector<RadialMenuConfig> menuElements = {
|
2023-12-16 12:49:05 +02:00
|
|
|
{ RadialMenuConfig::ITEM_ALT_NN, heroUpper != nullptr, "altUpTop", "vcmi.radialWheel.moveTop", [heroPos]()
|
2023-10-17 21:13:08 +02:00
|
|
|
{
|
2023-12-24 00:16:29 +02:00
|
|
|
for (size_t i = heroPos; i > 0; i--)
|
2023-10-17 21:13:08 +02:00
|
|
|
LOCPLINT->localState->swapWanderingHero(i, i - 1);
|
|
|
|
} },
|
2023-12-16 12:49:05 +02:00
|
|
|
{ RadialMenuConfig::ITEM_ALT_NW, heroUpper != nullptr, "altUp", "vcmi.radialWheel.moveUp", [heroPos](){LOCPLINT->localState->swapWanderingHero(heroPos, heroPos - 1); } },
|
|
|
|
{ RadialMenuConfig::ITEM_ALT_SW, heroLower != nullptr, "altDown", "vcmi.radialWheel.moveDown", [heroPos](){ LOCPLINT->localState->swapWanderingHero(heroPos, heroPos + 1); } },
|
|
|
|
{ RadialMenuConfig::ITEM_ALT_SS, heroLower != nullptr, "altDownBottom", "vcmi.radialWheel.moveBottom", [heroPos, heroes]()
|
2023-10-17 21:13:08 +02:00
|
|
|
{
|
|
|
|
for (int i = heroPos; i < heroes.size() - 1; i++)
|
|
|
|
LOCPLINT->localState->swapWanderingHero(i, i + 1);
|
|
|
|
} },
|
|
|
|
};
|
|
|
|
|
|
|
|
GH.windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
|
|
|
|
}
|
|
|
|
|
2024-08-04 14:00:40 +02:00
|
|
|
void CHeroList::CHeroItem::keyPressed(EShortcut key)
|
|
|
|
{
|
|
|
|
if(!hero)
|
|
|
|
return;
|
|
|
|
|
2024-08-09 22:49:05 +02:00
|
|
|
if(parent->selected != this->shared_from_this())
|
2024-08-04 14:00:40 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto & heroes = LOCPLINT->localState->getWanderingHeroes();
|
|
|
|
|
2024-08-12 17:53:24 +02:00
|
|
|
if(key == EShortcut::LIST_HERO_DISMISS)
|
2024-08-04 14:00:40 +02:00
|
|
|
{
|
2024-08-06 19:34:50 +02:00
|
|
|
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[22], [=](){ LOCPLINT->cb->dismissHero(hero); }, nullptr);
|
2024-08-04 14:00:40 +02:00
|
|
|
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)
|
|
|
|
LOCPLINT->localState->swapWanderingHero(heroPos, heroPos - 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EShortcut::LIST_HERO_DOWN:
|
|
|
|
if(heroLower)
|
|
|
|
LOCPLINT->localState->swapWanderingHero(heroPos, heroPos + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EShortcut::LIST_HERO_TOP:
|
|
|
|
if(heroUpper)
|
|
|
|
for (size_t i = heroPos; i > 0; i--)
|
|
|
|
LOCPLINT->localState->swapWanderingHero(i, i - 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EShortcut::LIST_HERO_BOTTOM:
|
|
|
|
if(heroLower)
|
|
|
|
for (int i = heroPos; i < heroes.size() - 1; i++)
|
|
|
|
LOCPLINT->localState->swapWanderingHero(i, i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-04-17 14:17:15 +02:00
|
|
|
if (LOCPLINT->localState->getWanderingHeroes().size() > index)
|
|
|
|
return std::make_shared<CHeroItem>(this, LOCPLINT->localState->getWanderingHero(index));
|
2018-04-07 13:34:11 +02:00
|
|
|
return std::make_shared<CEmptyHeroItem>();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 15:52:43 +02:00
|
|
|
CHeroList::CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
|
|
|
|
: CList(visibleItemsCount, widgetPosition)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-05-06 15:52:43 +02:00
|
|
|
createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::select(const CGHeroInstance * hero)
|
|
|
|
{
|
2023-04-17 14:17:15 +02:00
|
|
|
selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-07-13 13:35:24 +02:00
|
|
|
void CHeroList::updateElement(const CGHeroInstance * hero)
|
|
|
|
{
|
|
|
|
updateWidget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::updateWidget()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-12-24 00:16:29 +02:00
|
|
|
const auto & heroes = LOCPLINT->localState->getWanderingHeroes();
|
2023-07-17 16:40:28 +02:00
|
|
|
|
|
|
|
listBox->resize(heroes.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < heroes.size(); ++i)
|
|
|
|
{
|
2023-10-15 14:31:33 +02:00
|
|
|
auto item = std::dynamic_pointer_cast<CHeroItem>(listBox->getItem(i));
|
2023-07-17 16:40:28 +02:00
|
|
|
|
|
|
|
if (!item)
|
|
|
|
continue;
|
|
|
|
|
2023-12-24 00:16:29 +02:00
|
|
|
if (item->hero == heroes.at(i))
|
2023-07-17 16:40:28 +02:00
|
|
|
{
|
|
|
|
item->update();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listBox->reset();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 12:06:58 +02:00
|
|
|
if (LOCPLINT->localState->getCurrentHero())
|
|
|
|
select(LOCPLINT->localState->getCurrentHero());
|
2023-02-10 23:29:13 +02:00
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
CList::update();
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-04-17 14:32:18 +02:00
|
|
|
if (LOCPLINT->localState->getOwnedTowns().size() > index)
|
|
|
|
return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
|
2023-08-23 14:07:50 +02:00
|
|
|
return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
|
2023-12-24 00:16:29 +02:00
|
|
|
CListItem(parent),
|
|
|
|
town(Town)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-08-23 14:07:50 +02:00
|
|
|
picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
|
2012-06-13 16:04:06 +03:00
|
|
|
pos = picture->pos;
|
|
|
|
update();
|
2023-10-15 14:31:33 +02:00
|
|
|
|
2024-08-04 14:00:40 +02:00
|
|
|
addUsedEvents(GESTURE | KEYBOARD);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
return std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 1);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::update()
|
|
|
|
{
|
2024-08-31 13:00:36 +02:00
|
|
|
size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->built >= LOCPLINT->cb->getSettings().getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
picture->setFrame(iconIndex + 2);
|
2012-06-13 16:04:06 +03:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::select(bool on)
|
|
|
|
{
|
2023-04-18 22:08:27 +02:00
|
|
|
if(on)
|
2023-12-24 00:16:29 +02:00
|
|
|
LOCPLINT->localState->setSelection(town);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::open()
|
|
|
|
{
|
2023-12-24 00:16:29 +02:00
|
|
|
LOCPLINT->openTownWindow(town);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::showTooltip()
|
|
|
|
{
|
2023-12-24 00:16:29 +02:00
|
|
|
CRClickPopup::createAndPush(town, GH.getCursorPosition());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-10-15 14:31:33 +02:00
|
|
|
void CTownList::CTownItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
|
|
|
|
{
|
|
|
|
if(!on)
|
|
|
|
return;
|
|
|
|
|
2023-10-17 21:17:57 +02:00
|
|
|
const std::vector<const CGTownInstance *> towns = LOCPLINT->localState->getOwnedTowns();
|
2023-12-24 00:16:29 +02:00
|
|
|
size_t townIndex = vstd::find_pos(towns, town);
|
2023-10-17 21:13:08 +02:00
|
|
|
|
2023-12-24 00:16:29 +02:00
|
|
|
if(townIndex + 1 > towns.size() || !towns.at(townIndex))
|
2023-10-16 01:42:01 +02:00
|
|
|
return;
|
2023-10-15 14:31:33 +02:00
|
|
|
|
|
|
|
if(towns.size() < 2)
|
|
|
|
return;
|
|
|
|
|
2023-10-16 01:43:50 +02:00
|
|
|
int townUpperPos = (townIndex < 1) ? -1 : townIndex - 1;
|
|
|
|
int townLowerPos = (townIndex > towns.size() - 2) ? -1 : townIndex + 1;
|
2023-10-15 14:31:33 +02:00
|
|
|
|
2024-08-04 14:00:40 +02:00
|
|
|
auto updateList = [](){
|
|
|
|
for (auto ki : GH.windows().findWindows<CCastleInterface>())
|
|
|
|
ki->townChange(); //update list
|
|
|
|
};
|
|
|
|
|
2023-10-15 14:31:33 +02:00
|
|
|
std::vector<RadialMenuConfig> menuElements = {
|
2024-08-04 14:00:40 +02:00
|
|
|
{ RadialMenuConfig::ITEM_ALT_NN, townUpperPos > -1, "altUpTop", "vcmi.radialWheel.moveTop", [updateList, townIndex]()
|
2023-10-17 21:13:08 +02:00
|
|
|
{
|
|
|
|
for (int i = townIndex; i > 0; i--)
|
|
|
|
LOCPLINT->localState->swapOwnedTowns(i, i - 1);
|
2024-08-04 14:00:40 +02:00
|
|
|
updateList();
|
2023-10-17 21:13:08 +02:00
|
|
|
} },
|
2024-08-04 14:00:40 +02:00
|
|
|
{ RadialMenuConfig::ITEM_ALT_NW, townUpperPos > -1, "altUp", "vcmi.radialWheel.moveUp", [updateList, townIndex, townUpperPos](){LOCPLINT->localState->swapOwnedTowns(townIndex, townUpperPos); updateList(); } },
|
|
|
|
{ RadialMenuConfig::ITEM_ALT_SW, townLowerPos > -1, "altDown", "vcmi.radialWheel.moveDown", [updateList, townIndex, townLowerPos](){ LOCPLINT->localState->swapOwnedTowns(townIndex, townLowerPos); updateList(); } },
|
|
|
|
{ RadialMenuConfig::ITEM_ALT_SS, townLowerPos > -1, "altDownBottom", "vcmi.radialWheel.moveBottom", [updateList, townIndex, towns]()
|
2023-10-17 21:13:08 +02:00
|
|
|
{
|
|
|
|
for (int i = townIndex; i < towns.size() - 1; i++)
|
|
|
|
LOCPLINT->localState->swapOwnedTowns(i, i + 1);
|
2024-08-04 14:00:40 +02:00
|
|
|
updateList();
|
2023-10-17 21:13:08 +02:00
|
|
|
} },
|
2023-10-15 14:31:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
GH.windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
|
|
|
|
}
|
|
|
|
|
2024-08-04 14:00:40 +02:00
|
|
|
void CTownList::CTownItem::keyPressed(EShortcut key)
|
|
|
|
{
|
2024-08-09 22:49:05 +02:00
|
|
|
if(parent->selected != this->shared_from_this())
|
2024-08-04 14:00:40 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
const std::vector<const CGTownInstance *> towns = LOCPLINT->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)
|
|
|
|
LOCPLINT->localState->swapOwnedTowns(townIndex, townUpperPos);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EShortcut::LIST_TOWN_DOWN:
|
|
|
|
if(townLowerPos > -1)
|
|
|
|
LOCPLINT->localState->swapOwnedTowns(townIndex, townLowerPos);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EShortcut::LIST_TOWN_TOP:
|
|
|
|
if(townUpperPos > -1)
|
|
|
|
for (int i = townIndex; i > 0; i--)
|
|
|
|
LOCPLINT->localState->swapOwnedTowns(i, i - 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EShortcut::LIST_TOWN_BOTTOM:
|
|
|
|
if(townLowerPos > -1)
|
|
|
|
for (int i = townIndex; i < towns.size() - 1; i++)
|
|
|
|
LOCPLINT->localState->swapOwnedTowns(i, i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto ki : GH.windows().findWindows<CCastleInterface>())
|
|
|
|
ki->townChange(); //update list
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
std::string CTownList::CTownItem::getHoverText()
|
|
|
|
{
|
2023-12-24 00:16:29 +02:00
|
|
|
return town->getObjectName();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 15:52:43 +02:00
|
|
|
CTownList::CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount)
|
|
|
|
: CList(visibleItemsCount, widgetPosition)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-05-06 15:52:43 +02:00
|
|
|
createList(firstItemOffset, itemOffsetDelta, initialItemsCount);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::select(const CGTownInstance * town)
|
|
|
|
{
|
2023-04-17 14:32:18 +02:00
|
|
|
selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-07-13 13:35:24 +02:00
|
|
|
void CTownList::updateElement(const CGTownInstance * town)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-07-13 13:35:24 +02:00
|
|
|
updateWidget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::updateWidget()
|
|
|
|
{
|
2023-07-17 16:40:28 +02:00
|
|
|
auto & towns = LOCPLINT->localState->getOwnedTowns();
|
|
|
|
|
|
|
|
listBox->resize(towns.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < towns.size(); ++i)
|
|
|
|
{
|
2023-10-15 15:53:19 +02:00
|
|
|
auto item = std::dynamic_pointer_cast<CTownItem>(listBox->getItem(i));
|
2023-07-17 16:40:28 +02:00
|
|
|
|
|
|
|
if (!item)
|
|
|
|
continue;
|
|
|
|
|
2023-12-24 00:16:29 +02:00
|
|
|
if (item->town == towns[i])
|
|
|
|
{
|
|
|
|
item->update();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listBox->reset();
|
|
|
|
break;
|
|
|
|
}
|
2023-07-17 16:40:28 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:06:58 +02:00
|
|
|
if (LOCPLINT->localState->getCurrentTown())
|
|
|
|
select(LOCPLINT->localState->getCurrentTown());
|
2023-02-10 23:29:13 +02:00
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
CList::update();
|
|
|
|
}
|