1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/client/adventureMap/CList.cpp

363 lines
8.6 KiB
C++
Raw Normal View History

/*
2023-02-01 16:42:03 +02:00
* 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"
2023-05-08 23:04:44 +02:00
#include "../widgets/ObjectLists.h"
#include "../windows/InfoWindows.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../PlayerLocalState.h"
#include "../gui/CGuiHandler.h"
#include "../renderSDL/SDL_Extensions.h"
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/CModHandler.h"
2023-03-15 21:34:29 +02:00
#include "../../lib/GameSettings.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapObjects/CGTownInstance.h"
CList::CListItem::CListItem(CList * Parent)
: CIntObject(LCLICK | RCLICK | HOVER),
2015-01-13 21:57:41 +02:00
parent(Parent),
selection()
{
defActions = 255-DISPOSE;
}
CList::CListItem::~CListItem()
{
}
void CList::CListItem::clickRight(tribool down, bool previousState)
{
if (down == true)
showTooltip();
}
void CList::CListItem::clickLeft(tribool down, bool previousState)
{
if(down == true)
{
//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)
2023-05-16 17:34:23 +02:00
GH.statusbar()->write(getHoverText());
else
2023-05-16 17:34:23 +02:00
GH.statusbar()->clear();
}
void CList::CListItem::onSelect(bool on)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
selection.reset();
if(on)
selection = genSelection();
select(on);
2023-03-22 00:57:08 +02:00
redraw();
}
CList::CList(int Size, Rect widgetDimensions)
: CIntObject(0, widgetDimensions.topLeft()),
2015-01-13 21:57:41 +02:00
size(Size),
selected(nullptr)
{
pos.w = widgetDimensions.w;
pos.h = widgetDimensions.h;
}
void CList::showAll(SDL_Surface * to)
{
CSDL_Ext::fillRect(to, pos, Colors::BLACK);
CIntObject::showAll(to);
}
void CList::createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
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(&CListBox::moveToPrev, listBox));
scrollUp->addCallback(std::bind(&CList::update, this));
update();
}
void CList::setScrollDownButton(std::shared_ptr<CButton> button)
{
addChild(button.get());
scrollDown = button;
scrollDown->addCallback(std::bind(&CList::update, this));
scrollDown->addCallback(std::bind(&CListBox::moveToNext, listBox));
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()
{
2017-05-13 16:57:15 +02:00
int index = getSelectedIndex() + 1;
if(index >= listBox->size())
2017-05-13 16:57:15 +02:00
index = 0;
selectIndex(index);
}
void CList::selectPrev()
{
int index = getSelectedIndex();
if(index <= 0)
selectIndex(0);
else
selectIndex(index-1);
}
CHeroList::CEmptyHeroItem::CEmptyHeroItem()
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1, 0);
mana = std::make_shared<CAnimImage>("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),
2015-01-13 21:57:41 +02:00
hero(Hero)
{
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);
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();
}
void CHeroList::CHeroItem::update()
{
movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 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>("HPSYYY", movement->pos.w + 1, 0);
}
void CHeroList::CHeroItem::select(bool on)
{
2023-04-18 22:08:27 +02:00
if(on)
LOCPLINT->localState->setSelection(hero);
}
void CHeroList::CHeroItem::open()
{
LOCPLINT->openHeroWindow(hero);
}
void CHeroList::CHeroItem::showTooltip()
{
CRClickPopup::createAndPush(hero, GH.getCursorPosition());
}
std::string CHeroList::CHeroItem::getHoverText()
{
return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->getNameTranslated() % hero->type->heroClass->getNameTranslated());
}
std::shared_ptr<CIntObject> CHeroList::createItem(size_t index)
{
2023-04-17 14:17:15 +02:00
if (LOCPLINT->localState->getWanderingHeroes().size() > index)
return std::make_shared<CHeroItem>(this, LOCPLINT->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)
{
2023-04-17 14:17:15 +02:00
selectIndex(vstd::find_pos(LOCPLINT->localState->getWanderingHeroes(), hero));
}
void CHeroList::update(const CGHeroInstance * hero)
{
//this hero is already present, update its status
for(auto & elem : listBox->getItems())
{
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))
{
item->update();
return;
}
}
//simplest solution for now: reset list and restore selection
2023-04-17 14:17:15 +02:00
listBox->resize(LOCPLINT->localState->getWanderingHeroes().size());
if (LOCPLINT->localState->getCurrentHero())
select(LOCPLINT->localState->getCurrentHero());
2023-02-10 23:29:13 +02:00
CList::update();
}
std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
{
2023-04-17 14:32:18 +02:00
if (LOCPLINT->localState->getOwnedTowns().size() > index)
return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
return std::make_shared<CAnimImage>("ITPA", 0);
}
CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
2015-01-13 21:57:41 +02:00
CListItem(parent),
town(Town)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
picture = std::make_shared<CAnimImage>("ITPA", 0);
pos = picture->pos;
update();
}
std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
{
return std::make_shared<CAnimImage>("ITPA", 1);
}
void CTownList::CTownItem::update()
{
size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
picture->setFrame(iconIndex + 2);
redraw();
}
void CTownList::CTownItem::select(bool on)
{
2023-04-18 22:08:27 +02:00
if(on)
LOCPLINT->localState->setSelection(town);
}
void CTownList::CTownItem::open()
{
LOCPLINT->openTownWindow(town);
}
void CTownList::CTownItem::showTooltip()
{
CRClickPopup::createAndPush(town, GH.getCursorPosition());
}
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)
{
2023-04-17 14:32:18 +02:00
selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
}
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());
if (LOCPLINT->localState->getCurrentTown())
select(LOCPLINT->localState->getCurrentTown());
2023-02-10 23:29:13 +02:00
CList::update();
}