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-04-12 15:04:38 +02:00
|
|
|
#include "CAdventureMapInterface.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"
|
|
|
|
#include "../windows/InfoWindows.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"
|
2023-05-06 15:52:43 +02:00
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/CHeroHandler.h"
|
|
|
|
#include "../../lib/CModHandler.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../../lib/GameSettings.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
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CList::CListItem::CListItem(CList * Parent)
|
|
|
|
: CIntObject(LCLICK | RCLICK | 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
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
defActions = 255-DISPOSE;
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CList::CListItem::~CListItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::CListItem::clickRight(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if (down == true)
|
|
|
|
showTooltip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::CListItem::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(down == true)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
//second click on already selected item
|
2018-04-07 13:34:11 +02:00
|
|
|
if(parent->selected == this->shared_from_this())
|
|
|
|
{
|
2012-06-13 16:04:06 +03:00
|
|
|
open();
|
2018-04-07 13:34:11 +02:00
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//first click - switch selection
|
2018-04-07 13:34:11 +02:00
|
|
|
parent->select(this->shared_from_this());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::CListItem::hover(bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
2022-11-18 17:54:10 +02:00
|
|
|
GH.statusbar->write(getHoverText());
|
2012-06-13 16:04:06 +03:00
|
|
|
else
|
|
|
|
GH.statusbar->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CList::CListItem::onSelect(bool on)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
|
|
|
|
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)
|
|
|
|
: CIntObject(0, widgetDimensions.topLeft()),
|
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-05-06 15:52:43 +02:00
|
|
|
void CList::showAll(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
CSDL_Ext::fillRect(to, pos, Colors::BLACK);
|
|
|
|
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
|
|
|
{
|
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
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;
|
|
|
|
scrollUp->addCallback(std::bind(&CListBox::moveToPrev, listBox));
|
2014-08-09 15:14:31 +03:00
|
|
|
scrollUp->addCallback(std::bind(&CList::update, 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;
|
|
|
|
scrollDown->addCallback(std::bind(&CList::update, this));
|
|
|
|
scrollDown->addCallback(std::bind(&CListBox::moveToNext, listBox));
|
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()
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
|
|
|
movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
|
2023-01-30 13:58:13 +02:00
|
|
|
portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1, 0);
|
2018-04-07 13:34:11 +02:00
|
|
|
mana = std::make_shared<CAnimImage>("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),
|
2015-01-13 21:57:41 +02:00
|
|
|
hero(Hero)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
|
|
|
movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
|
|
|
|
portrait = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
|
|
|
|
mana = std::make_shared<CAnimImage>("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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::CHeroItem::update()
|
|
|
|
{
|
2012-06-17 01:40:28 +03:00
|
|
|
movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
|
|
|
|
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-01-30 13:58:13 +02:00
|
|
|
return std::make_shared<CPicture>("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()
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroList::update(const CGHeroInstance * hero)
|
|
|
|
{
|
2012-06-17 01:40:28 +03:00
|
|
|
//this hero is already present, update its status
|
2018-04-07 13:34:11 +02:00
|
|
|
for(auto & elem : listBox->getItems())
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
auto item = std::dynamic_pointer_cast<CHeroItem>(elem);
|
2023-04-17 14:17:15 +02:00
|
|
|
if(item && item->hero == hero && vstd::contains(LOCPLINT->localState->getWanderingHeroes(), hero))
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2012-06-17 01:40:28 +03:00
|
|
|
item->update();
|
|
|
|
return;
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//simplest solution for now: reset list and restore selection
|
|
|
|
|
2023-04-17 14:17:15 +02:00
|
|
|
listBox->resize(LOCPLINT->localState->getWanderingHeroes().size());
|
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));
|
2018-04-07 13:34:11 +02:00
|
|
|
return std::make_shared<CAnimImage>("ITPA", 0);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
|
2015-01-13 21:57:41 +02:00
|
|
|
CListItem(parent),
|
|
|
|
town(Town)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
|
|
|
picture = std::make_shared<CAnimImage>("ITPA", 0);
|
2012-06-13 16:04:06 +03:00
|
|
|
pos = picture->pos;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
return std::make_shared<CAnimImage>("ITPA", 1);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::update()
|
|
|
|
{
|
2023-03-15 23:47:26 +02:00
|
|
|
size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->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)
|
|
|
|
LOCPLINT->localState->setSelection(town);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::open()
|
|
|
|
{
|
|
|
|
LOCPLINT->openTownWindow(town);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::CTownItem::showTooltip()
|
|
|
|
{
|
2023-01-27 00:27:06 +02:00
|
|
|
CRClickPopup::createAndPush(town, GH.getCursorPosition());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CTownList::CTownItem::getHoverText()
|
|
|
|
{
|
2014-06-24 20:39:36 +03: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
|
|
|
}
|
|
|
|
|
|
|
|
void CTownList::update(const CGTownInstance *)
|
|
|
|
{
|
|
|
|
//simplest solution for now: reset list and restore selection
|
|
|
|
|
2023-04-17 14:32:18 +02:00
|
|
|
listBox->resize(LOCPLINT->localState->getOwnedTowns().size());
|
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();
|
|
|
|
}
|
|
|
|
|