1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

CArtPlace now works with artifact ID

This commit is contained in:
SoundSSGood 2024-10-19 16:25:26 +03:00
parent 6dc6cc4cbf
commit 573bb6abc6
9 changed files with 83 additions and 99 deletions

View File

@ -29,26 +29,44 @@
#include "../../lib/networkPacks/ArtifactLocation.h" #include "../../lib/networkPacks/ArtifactLocation.h"
#include "../../lib/CConfigHandler.h" #include "../../lib/CConfigHandler.h"
void CArtPlace::setInternals(const CArtifactInstance * artInst) CArtPlace::CArtPlace(Point position, const ArtifactID & artId, const SpellID & spellId)
: SelectableSlot(Rect(position, Point(44, 44)), Point(1, 1))
, locked(false)
, imageIndex(0)
{ {
ourArt = artInst; OBJECT_CONSTRUCTION;
if(!artInst)
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)
{
this->artId = artId;
if(artId == ArtifactID::NONE)
{ {
image->disable(); image->disable();
text.clear(); text.clear();
hoverText = CGI->generaltexth->allTexts[507]; lockSlot(false);
return; return;
} }
imageIndex = artInst->artType->getIconIndex(); const auto artType = artId.toArtifact();
if(artInst->getTypeId() == ArtifactID::SPELL_SCROLL) imageIndex = artType->getIconIndex();
if(artId == ArtifactID::SPELL_SCROLL)
{ {
auto spellID = artInst->getScrollSpellID(); this->spellId = spellId;
assert(spellID.num >= 0); assert(spellId.num > 0);
if(settings["general"]["enableUiEnhancements"].Bool()) if(settings["general"]["enableUiEnhancements"].Bool())
{ {
imageIndex = spellID.num; imageIndex = spellId.num;
if(component.type != ComponentType::SPELL_SCROLL) if(component.type != ComponentType::SPELL_SCROLL)
{ {
image->setScale(Point(pos.w, 34)); image->setScale(Point(pos.w, 34));
@ -58,7 +76,7 @@ void CArtPlace::setInternals(const CArtifactInstance * artInst)
} }
// Add spell component info (used to provide a pic in r-click popup) // Add spell component info (used to provide a pic in r-click popup)
component.type = ComponentType::SPELL_SCROLL; component.type = ComponentType::SPELL_SCROLL;
component.subType = spellID; component.subType = spellId;
} }
else else
{ {
@ -69,47 +87,33 @@ void CArtPlace::setInternals(const CArtifactInstance * artInst)
image->moveTo(Point(pos.x, pos.y)); image->moveTo(Point(pos.x, pos.y));
} }
component.type = ComponentType::ARTIFACT; component.type = ComponentType::ARTIFACT;
component.subType = artInst->getTypeId(); component.subType = artId;
} }
image->enable(); image->enable();
text = artInst->getDescription(); lockSlot(locked);
text = artType->getDescriptionTranslated();
if(artType->isScroll())
ArtifactUtils::insertScrrollSpellName(text, spellId);
} }
CArtPlace::CArtPlace(Point position, const CArtifactInstance * art) ArtifactID CArtPlace::getArtifactId() const
: SelectableSlot(Rect(position, Point(44, 44)), Point(1, 1))
, ourArt(art)
, locked(false)
{ {
OBJECT_CONSTRUCTION; return artId;
imageIndex = 0;
if(locked)
imageIndex = ArtifactID::ART_LOCK;
else if(ourArt)
imageIndex = ourArt->artType->getIconIndex();
image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
image->disable();
moveSelectionForeground();
} }
const CArtifactInstance * CArtPlace::getArt() const CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot,
{ const ArtifactID & artId, const SpellID & spellId)
return ourArt; : CArtPlace(position, artId, spellId),
}
CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * art)
: CArtPlace(position, art),
commanderOwner(commanderOwner), commanderOwner(commanderOwner),
commanderSlotID(artSlot.num) commanderSlotID(artSlot.num)
{ {
setArtifact(art);
} }
void CCommanderArtPlace::returnArtToHeroCallback() void CCommanderArtPlace::returnArtToHeroCallback()
{ {
ArtifactPosition artifactPos = commanderSlotID; ArtifactPosition artifactPos = commanderSlotID;
ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArt()->getTypeId()); ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArtifactId());
if(freeSlot == ArtifactPosition::PRE_FIRST) if(freeSlot == ArtifactPosition::PRE_FIRST)
{ {
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152")); LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
@ -120,10 +124,10 @@ void CCommanderArtPlace::returnArtToHeroCallback()
src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER; src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
ArtifactLocation dst(commanderOwner->id, freeSlot); ArtifactLocation dst(commanderOwner->id, freeSlot);
if(getArt()->canBePutAt(commanderOwner, freeSlot, true)) if(getArtifactId().toArtifact()->canBePutAt(commanderOwner, freeSlot, true))
{ {
LOCPLINT->cb->swapArtifacts(src, dst); LOCPLINT->cb->swapArtifacts(src, dst);
setArtifact(nullptr); setArtifact(ArtifactID(ArtifactID::NONE));
parent->redraw(); parent->redraw();
} }
} }
@ -131,29 +135,35 @@ void CCommanderArtPlace::returnArtToHeroCallback()
void CCommanderArtPlace::clickPressed(const Point & cursorPosition) void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
{ {
if(getArt() && text.size()) if(getArtifactId() != ArtifactID::NONE && text.size())
LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {}); LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
} }
void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition) void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
{ {
if(getArt() && text.size()) if(getArtifactId() != ArtifactID::NONE && text.size())
CArtPlace::showPopupWindow(cursorPosition); CArtPlace::showPopupWindow(cursorPosition);
} }
void CArtPlace::lockSlot(bool on) void CArtPlace::lockSlot(bool on)
{ {
if(locked == on)
return;
locked = on; locked = on;
if(on) if(on)
{
image->setFrame(ArtifactID::ART_LOCK); image->setFrame(ArtifactID::ART_LOCK);
else if(ourArt) hoverText = CGI->generaltexth->allTexts[507];
}
else if(artId != ArtifactID::NONE)
{
image->setFrame(imageIndex); image->setFrame(imageIndex);
auto hoverText = MetaString::createFromRawString(CGI->generaltexth->heroscrn[1]);
hoverText.replaceName(artId);
this->hoverText = hoverText.toString();
}
else else
image->setFrame(0); {
hoverText = CGI->generaltexth->allTexts[507];
}
} }
bool CArtPlace::isLocked() const bool CArtPlace::isLocked() const
@ -182,24 +192,6 @@ void CArtPlace::gesture(bool on, const Point & initialPosition, const Point & fi
gestureCallback(*this, initialPosition); gestureCallback(*this, initialPosition);
} }
void CArtPlace::setArtifact(const CArtifactInstance * art)
{
setInternals(art);
if(art)
{
image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
if(locked) // Locks should appear as empty.
hoverText = CGI->generaltexth->allTexts[507];
else
hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
}
else
{
lockSlot(false);
}
}
void CArtPlace::setClickPressedCallback(const ClickFunctor & callback) void CArtPlace::setClickPressedCallback(const ClickFunctor & callback)
{ {
clickPressedCallback = callback; clickPressedCallback = callback;

View File

@ -20,11 +20,12 @@ public:
ArtifactPosition slot; ArtifactPosition slot;
CArtPlace(Point position, const CArtifactInstance * art = nullptr); CArtPlace(Point position, const ArtifactID & artId = ArtifactID::NONE, const SpellID & spellId = SpellID::NONE);
const CArtifactInstance * getArt() const; void setArtifact(const SpellID & spellId);
void setArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
ArtifactID getArtifactId() const;
void lockSlot(bool on); void lockSlot(bool on);
bool isLocked() const; bool isLocked() const;
void setArtifact(const CArtifactInstance * art);
void setClickPressedCallback(const ClickFunctor & callback); void setClickPressedCallback(const ClickFunctor & callback);
void setShowPopupCallback(const ClickFunctor & callback); void setShowPopupCallback(const ClickFunctor & callback);
void setGestureCallback(const ClickFunctor & callback); void setGestureCallback(const ClickFunctor & callback);
@ -34,16 +35,14 @@ public:
void addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts); void addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts);
private: private:
const CArtifactInstance * ourArt; ArtifactID artId;
SpellID spellId;
bool locked; bool locked;
int imageIndex; int32_t imageIndex;
std::shared_ptr<CAnimImage> image; std::shared_ptr<CAnimImage> image;
ClickFunctor clickPressedCallback; ClickFunctor clickPressedCallback;
ClickFunctor showPopupCallback; ClickFunctor showPopupCallback;
ClickFunctor gestureCallback; ClickFunctor gestureCallback;
protected:
void setInternals(const CArtifactInstance * artInst);
}; };
class CCommanderArtPlace : public CArtPlace class CCommanderArtPlace : public CArtPlace
@ -55,7 +54,8 @@ private:
void returnArtToHeroCallback(); void returnArtToHeroCallback();
public: public:
CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * art = nullptr); CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot,
const ArtifactID & artId = ArtifactID::NONE, const SpellID & spellId = SpellID::NONE);
void clickPressed(const Point & cursorPosition) override; void clickPressed(const Point & cursorPosition) override;
void showPopupWindow(const Point & cursorPosition) override; void showPopupWindow(const Point & cursorPosition) override;
}; };

View File

@ -83,7 +83,7 @@ void CArtifactsOfHeroBackpack::initAOHbackpack(size_t slots, bool slider)
slotSizeWithMargin * (artPlaceIdx / slotsColumnsMax)); slotSizeWithMargin * (artPlaceIdx / slotsColumnsMax));
backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos)); backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos));
artPlace = std::make_shared<CArtPlace>(pos); artPlace = std::make_shared<CArtPlace>(pos);
artPlace->setArtifact(nullptr); artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2)); artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2));
artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2)); artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
artPlaceIdx++; artPlaceIdx++;

View File

@ -66,13 +66,13 @@ void CArtifactsOfHeroBase::init(
for(auto artPlace : artWorn) for(auto artPlace : artWorn)
{ {
artPlace.second->slot = artPlace.first; artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr); artPlace.second->setArtifact(ArtifactID(ArtifactID::NONE));
artPlace.second->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2)); artPlace.second->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2));
artPlace.second->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2)); artPlace.second->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
} }
for(auto artPlace : backpack) for(auto artPlace : backpack)
{ {
artPlace->setArtifact(nullptr); artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2)); artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2));
artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2)); artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
} }
@ -260,7 +260,7 @@ void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosit
if(auto slotInfo = curHero->getSlot(slot)) if(auto slotInfo = curHero->getSlot(slot))
{ {
artPlace->lockSlot(slotInfo->locked); artPlace->lockSlot(slotInfo->locked);
artPlace->setArtifact(slotInfo->artifact); artPlace->setArtifact(slotInfo->artifact->getTypeId(), slotInfo->artifact->getScrollSpellID());
if(slotInfo->locked || slotInfo->artifact->isCombined()) if(slotInfo->locked || slotInfo->artifact->isCombined())
return; return;
@ -285,7 +285,7 @@ void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosit
} }
else else
{ {
artPlace->setArtifact(nullptr); artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
} }
} }

View File

@ -29,14 +29,14 @@ CArtifactsOfHeroKingdom::CArtifactsOfHeroKingdom(ArtPlaceMap ArtWorn, std::vecto
for(auto artPlace : artWorn) for(auto artPlace : artWorn)
{ {
artPlace.second->slot = artPlace.first; artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr); artPlace.second->setArtifact(ArtifactID(ArtifactID::NONE));
artPlace.second->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2)); artPlace.second->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2));
artPlace.second->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2)); artPlace.second->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
} }
enableGesture(); enableGesture();
for(auto artPlace : backpack) for(auto artPlace : backpack)
{ {
artPlace->setArtifact(nullptr); artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2)); artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::clickPrassedArtPlace, this, _1, _2));
artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2)); artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
} }

View File

@ -432,7 +432,9 @@ CStackWindow::CommanderMainSection::CommanderMainSection(CStackWindow * owner, i
for(auto equippedArtifact : parent->info->commander->artifactsWorn) for(auto equippedArtifact : parent->info->commander->artifactsWorn)
{ {
Point artPos = getArtifactPos(equippedArtifact.first); Point artPos = getArtifactPos(equippedArtifact.first);
auto artPlace = std::make_shared<CCommanderArtPlace>(artPos, parent->info->owner, equippedArtifact.first, equippedArtifact.second.artifact); const auto commanderArt = equippedArtifact.second.artifact;
assert(commanderArt);
auto artPlace = std::make_shared<CCommanderArtPlace>(artPos, parent->info->owner, equippedArtifact.first, commanderArt->getTypeId());
artifacts.push_back(artPlace); artifacts.push_back(artPlace);
} }
@ -616,11 +618,11 @@ CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool s
auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT); auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
if(art) if(art)
{ {
parent->stackArtifactIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("ARTIFACT"), art->artType->getIconIndex(), 0, pos.x, pos.y); parent->stackArtifact = std::make_shared<CArtPlace>(pos, art->getTypeId());
parent->stackArtifactHelp = std::make_shared<LRClickableAreaWTextComp>(Rect(pos, Point(44, 44)), ComponentType::ARTIFACT); parent->stackArtifact->setShowPopupCallback([](CArtPlace & artPlace, const Point & cursorPosition)
parent->stackArtifactHelp->component.subType = art->artType->getId(); {
parent->stackArtifactHelp->text = art->getDescription(); artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
});
if(parent->info->owner) if(parent->info->owner)
{ {
parent->stackArtifactButton = std::make_shared<CButton>( parent->stackArtifactButton = std::make_shared<CButton>(
@ -987,8 +989,7 @@ void CStackWindow::removeStackArtifact(ArtifactPosition pos)
artLoc.creature = info->stackNode->armyObj->findStack(info->stackNode); artLoc.creature = info->stackNode->armyObj->findStack(info->stackNode);
LOCPLINT->cb->swapArtifacts(artLoc, ArtifactLocation(info->owner->id, slot)); LOCPLINT->cb->swapArtifacts(artLoc, ArtifactLocation(info->owner->id, slot));
stackArtifactButton.reset(); stackArtifactButton.reset();
stackArtifactHelp.reset(); stackArtifact.reset();
stackArtifactIcon.reset();
redraw(); redraw();
} }
} }

View File

@ -28,6 +28,7 @@ class CTabbedInt;
class CButton; class CButton;
class CMultiLineLabel; class CMultiLineLabel;
class CListBox; class CListBox;
class CArtPlace;
class CCommanderArtPlace; class CCommanderArtPlace;
class LRClickableArea; class LRClickableArea;
@ -156,8 +157,7 @@ class CStackWindow : public CWindowObject
MainSection(CStackWindow * owner, int yOffset, bool showExp, bool showArt); MainSection(CStackWindow * owner, int yOffset, bool showExp, bool showArt);
}; };
std::shared_ptr<CAnimImage> stackArtifactIcon; std::shared_ptr<CArtPlace> stackArtifact;
std::shared_ptr<LRClickableAreaWTextComp> stackArtifactHelp;
std::shared_ptr<CButton> stackArtifactButton; std::shared_ptr<CButton> stackArtifactButton;

View File

@ -134,14 +134,6 @@ std::string CArtifactInstance::nodeName() const
return "Artifact instance of " + (artType ? artType->getJsonKey() : std::string("uninitialized")) + " type"; return "Artifact instance of " + (artType ? artType->getJsonKey() : std::string("uninitialized")) + " type";
} }
std::string CArtifactInstance::getDescription() const
{
std::string text = artType->getDescriptionTranslated();
if(artType->isScroll())
ArtifactUtils::insertScrrollSpellName(text, getScrollSpellID());
return text;
}
ArtifactID CArtifactInstance::getTypeId() const ArtifactID CArtifactInstance::getTypeId() const
{ {
return artType->getId(); return artType->getId();

View File

@ -80,7 +80,6 @@ public:
CArtifactInstance(); CArtifactInstance();
void setType(const CArtifact * art); void setType(const CArtifact * art);
std::string nodeName() const override; std::string nodeName() const override;
std::string getDescription() const;
ArtifactID getTypeId() const; ArtifactID getTypeId() const;
ArtifactInstanceID getId() const; ArtifactInstanceID getId() const;
void setId(ArtifactInstanceID id); void setId(ArtifactInstanceID id);