1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/widgets/CComponentHolder.cpp

267 lines
6.9 KiB
C++
Raw Normal View History

/*
2024-10-20 14:27:21 +02:00
* CComponentHolder.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"
2024-10-20 14:27:21 +02:00
#include "CComponentHolder.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "CComponent.h"
2024-10-20 14:27:21 +02:00
#include "Images.h"
#include "../render/Canvas.h"
#include "../render/Colors.h"
2023-09-25 22:58:59 +02:00
#include "../render/IRenderHandler.h"
#include "../CPlayerInterface.h"
#include "../CGameInfo.h"
#include "../../CCallback.h"
#include "../../lib/texts/CGeneralTextHandler.h"
2023-05-17 15:52:16 +02:00
#include "../../lib/ArtifactUtils.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/networkPacks/ArtifactLocation.h"
2023-09-25 22:58:59 +02:00
#include "../../lib/CConfigHandler.h"
2024-10-20 14:27:21 +02:00
#include "../../lib/CSkillHandler.h"
CComponentHolder::CComponentHolder(const Rect & area, const Point & selectionOversize)
: SelectableSlot(area, selectionOversize)
{
}
void CComponentHolder::setClickPressedCallback(const ClickFunctor & callback)
{
clickPressedCallback = callback;
}
void CComponentHolder::setShowPopupCallback(const ClickFunctor & callback)
{
showPopupCallback = callback;
}
void CComponentHolder::setGestureCallback(const ClickFunctor & callback)
{
gestureCallback = callback;
}
void CComponentHolder::clickPressed(const Point & cursorPosition)
{
if(clickPressedCallback)
clickPressedCallback(*this, cursorPosition);
}
void CComponentHolder::showPopupWindow(const Point & cursorPosition)
{
if(showPopupCallback)
showPopupCallback(*this, cursorPosition);
}
void CComponentHolder::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
{
if(!on)
return;
if(gestureCallback)
gestureCallback(*this, initialPosition);
}
2024-10-19 15:25:26 +02:00
CArtPlace::CArtPlace(Point position, const ArtifactID & artId, const SpellID & spellId)
2024-10-20 14:27:21 +02:00
: CComponentHolder(Rect(position, Point(44, 44)), Point(1, 1))
2024-10-19 15:25:26 +02:00
, locked(false)
, imageIndex(0)
{
OBJECT_CONSTRUCTION;
image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), 0);
setArtifact(artId, spellId);
moveSelectionForeground();
}
void CArtPlace::setArtifact(const SpellID & spellId)
{
setArtifact(ArtifactID::SPELL_SCROLL, spellId);
}
void CArtPlace::setArtifact(const ArtifactID & artId, const SpellID & spellId)
{
2024-10-19 15:25:26 +02:00
this->artId = artId;
if(artId == ArtifactID::NONE)
2023-04-23 13:09:49 +02:00
{
image->disable();
text.clear();
2024-10-19 15:25:26 +02:00
lockSlot(false);
2023-04-23 13:09:49 +02:00
return;
}
2023-09-25 22:58:59 +02:00
2024-10-19 15:25:26 +02:00
const auto artType = artId.toArtifact();
imageIndex = artType->getIconIndex();
if(artId == ArtifactID::SPELL_SCROLL)
2023-04-23 13:09:49 +02:00
{
2024-10-19 15:25:26 +02:00
this->spellId = spellId;
assert(spellId.num > 0);
2023-09-25 22:58:59 +02:00
2023-10-19 20:22:26 +02:00
if(settings["general"]["enableUiEnhancements"].Bool())
{
2024-10-19 15:25:26 +02:00
imageIndex = spellId.num;
if(component.type != ComponentType::SPELL_SCROLL)
2023-09-25 22:58:59 +02:00
{
2023-10-19 20:22:26 +02:00
image->setScale(Point(pos.w, 34));
image->setAnimationPath(AnimationPath::builtin("spellscr"), imageIndex);
image->moveTo(Point(pos.x, pos.y + 4));
2023-09-25 22:58:59 +02:00
}
2023-04-23 13:09:49 +02:00
}
2023-10-19 20:22:26 +02:00
// Add spell component info (used to provide a pic in r-click popup)
component.type = ComponentType::SPELL_SCROLL;
2024-10-19 15:25:26 +02:00
component.subType = spellId;
2023-04-23 13:09:49 +02:00
}
else
{
if(settings["general"]["enableUiEnhancements"].Bool() && component.type != ComponentType::ARTIFACT)
2023-10-19 20:22:26 +02:00
{
image->setScale(Point());
image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
image->moveTo(Point(pos.x, pos.y));
}
component.type = ComponentType::ARTIFACT;
2024-10-19 15:25:26 +02:00
component.subType = artId;
2023-04-23 13:09:49 +02:00
}
2023-10-19 20:22:26 +02:00
image->enable();
2024-10-19 15:25:26 +02:00
lockSlot(locked);
2023-11-04 22:15:03 +02:00
2024-10-19 15:25:26 +02:00
text = artType->getDescriptionTranslated();
if(artType->isScroll())
ArtifactUtils::insertScrrollSpellName(text, spellId);
}
2024-10-19 15:25:26 +02:00
ArtifactID CArtPlace::getArtifactId() const
{
2024-10-19 15:25:26 +02:00
return artId;
2023-04-23 13:09:49 +02:00
}
2024-10-19 15:25:26 +02:00
CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot,
const ArtifactID & artId, const SpellID & spellId)
: CArtPlace(position, artId, spellId),
2023-04-23 13:09:49 +02:00
commanderOwner(commanderOwner),
commanderSlotID(artSlot.num)
{
}
void CCommanderArtPlace::returnArtToHeroCallback()
{
ArtifactPosition artifactPos = commanderSlotID;
2024-10-19 15:25:26 +02:00
ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArtifactId());
2023-04-23 13:09:49 +02:00
if(freeSlot == ArtifactPosition::PRE_FIRST)
{
2023-04-23 13:09:49 +02:00
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
}
2023-04-23 13:09:49 +02:00
else
{
ArtifactLocation src(commanderOwner->id, artifactPos);
src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
ArtifactLocation dst(commanderOwner->id, freeSlot);
2024-10-19 15:25:26 +02:00
if(getArtifactId().toArtifact()->canBePutAt(commanderOwner, freeSlot, true))
{
2023-04-23 13:09:49 +02:00
LOCPLINT->cb->swapArtifacts(src, dst);
2024-10-19 15:25:26 +02:00
setArtifact(ArtifactID(ArtifactID::NONE));
2023-04-23 13:09:49 +02:00
parent->redraw();
}
}
}
void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
2016-01-23 14:20:51 +02:00
{
2024-10-19 15:25:26 +02:00
if(getArtifactId() != ArtifactID::NONE && text.size())
2023-04-23 13:09:49 +02:00
LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
}
2016-01-23 14:20:51 +02:00
2024-04-23 19:26:21 +02:00
void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
2023-04-23 13:09:49 +02:00
{
2024-10-19 15:25:26 +02:00
if(getArtifactId() != ArtifactID::NONE && text.size())
CArtPlace::showPopupWindow(cursorPosition);
2016-01-23 14:20:51 +02:00
}
2023-11-04 22:15:03 +02:00
void CArtPlace::lockSlot(bool on)
{
2023-04-23 13:09:49 +02:00
locked = on;
if(on)
2024-10-19 15:25:26 +02:00
{
2023-04-23 13:09:49 +02:00
image->setFrame(ArtifactID::ART_LOCK);
2024-10-19 15:25:26 +02:00
hoverText = CGI->generaltexth->allTexts[507];
}
else if(artId != ArtifactID::NONE)
{
2023-10-19 20:22:26 +02:00
image->setFrame(imageIndex);
2024-10-19 15:25:26 +02:00
auto hoverText = MetaString::createFromRawString(CGI->generaltexth->heroscrn[1]);
hoverText.replaceName(artId);
this->hoverText = hoverText.toString();
}
2023-04-23 13:09:49 +02:00
else
2024-10-19 15:25:26 +02:00
{
hoverText = CGI->generaltexth->allTexts[507];
}
}
2023-11-04 22:15:03 +02:00
bool CArtPlace::isLocked() const
{
2023-04-23 13:09:49 +02:00
return locked;
}
2024-05-21 19:00:13 +02:00
void CArtPlace::addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts)
2023-04-23 13:09:49 +02:00
{
2024-10-12 20:20:29 +02:00
for(auto [combinedId, availableArts] : arts)
{
2024-10-12 20:20:29 +02:00
const auto combinedArt = combinedId.toArtifact();
2024-05-21 19:00:13 +02:00
MetaString info;
info.appendEOL();
info.appendEOL();
info.appendRawString("{");
info.appendName(combinedArt->getId());
info.appendRawString("}");
info.appendRawString(" (%d/%d)");
2024-10-12 20:20:29 +02:00
info.replaceNumber(availableArts.size());
2024-05-21 19:00:13 +02:00
info.replaceNumber(combinedArt->getConstituents().size());
for(const auto part : combinedArt->getConstituents())
{
2024-10-12 20:20:29 +02:00
const auto found = std::find_if(availableArts.begin(), availableArts.end(), [part](const auto & availablePart) -> bool
{
return availablePart == part->getId() ? true : false;
});
2024-05-21 19:00:13 +02:00
info.appendEOL();
2024-10-12 20:20:29 +02:00
if(found < availableArts.end())
2024-05-21 19:00:13 +02:00
{
info.appendName(part->getId());
2024-10-12 20:20:29 +02:00
availableArts.erase(found);
2024-05-21 19:00:13 +02:00
}
else
{
info.appendRawString("{#A9A9A9|");
info.appendName(part->getId());
info.appendRawString("}");
}
}
2024-05-21 19:00:13 +02:00
text += info.toString();
}
}
2024-10-20 14:27:21 +02:00
CSecSkillPlace::CSecSkillPlace(const Point & position, const SecondarySkill & skillId)
: CComponentHolder(Rect(position, Point(44, 44)), Point())
{
OBJECT_CONSTRUCTION;
image = std::make_shared<CAnimImage>(AnimationPath::builtin("SECSKILL"), 0);
setSkill(skillId);
}
void CSecSkillPlace::setSkill(const SecondarySkill & skillId)
{
//skillId.toSkill()->getIconIndex();
}