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

329 lines
7.9 KiB
C++
Raw Normal View History

/*
* CArtifactHolder.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 "CArtifactHolder.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "CComponent.h"
#include "../windows/GUIClasses.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/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"
2023-04-23 13:09:49 +02:00
void CArtPlace::setInternals(const CArtifactInstance * artInst)
{
2023-04-23 13:09:49 +02:00
ourArt = artInst;
if(!artInst)
{
image->disable();
text.clear();
hoverText = CGI->generaltexth->allTexts[507];
return;
}
2023-09-25 22:58:59 +02:00
2023-10-19 20:22:26 +02:00
imageIndex = artInst->artType->getIconIndex();
2023-04-23 13:09:49 +02:00
if(artInst->getTypeId() == ArtifactID::SPELL_SCROLL)
{
auto spellID = artInst->getScrollSpellID();
2023-10-19 20:22:26 +02:00
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())
{
imageIndex = spellID.num;
if(baseType != CComponent::spell)
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)
baseType = CComponent::spell;
type = spellID;
2023-04-23 13:09:49 +02:00
}
else
{
2023-10-19 20:22:26 +02:00
if(settings["general"]["enableUiEnhancements"].Bool() && baseType != CComponent::artifact)
{
image->setScale(Point());
image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
image->moveTo(Point(pos.x, pos.y));
}
2023-04-23 13:09:49 +02:00
baseType = CComponent::artifact;
type = artInst->getTypeId();
}
2023-10-19 20:22:26 +02:00
bonusValue = 0;
image->enable();
2023-04-23 13:09:49 +02:00
text = artInst->getDescription();
}
2023-04-23 13:09:49 +02:00
CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art)
: ourArt(Art)
{
2023-04-23 13:09:49 +02:00
image = nullptr;
pos += position;
pos.w = pos.h = 44;
}
2023-04-23 13:09:49 +02:00
const CArtifactInstance * CArtPlace::getArt()
{
2023-04-23 13:09:49 +02:00
return ourArt;
}
2023-04-23 13:09:49 +02:00
CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art)
: CArtPlace(position, Art),
commanderOwner(commanderOwner),
commanderSlotID(artSlot.num)
{
createImage();
setArtifact(Art);
}
2023-04-23 13:09:49 +02:00
void CCommanderArtPlace::createImage()
{
2023-04-23 13:09:49 +02:00
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
2023-10-19 20:22:26 +02:00
imageIndex = 0;
2023-04-23 13:09:49 +02:00
if(ourArt)
imageIndex = ourArt->artType->getIconIndex();
image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
2023-04-23 13:09:49 +02:00
if(!ourArt)
image->disable();
}
void CCommanderArtPlace::returnArtToHeroCallback()
{
ArtifactPosition artifactPos = commanderSlotID;
ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
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);
ArtifactLocation dst(commanderOwner->id, freeSlot);
if(ourArt->canBePutAt(commanderOwner, freeSlot, true))
{
2023-04-23 13:09:49 +02:00
LOCPLINT->cb->swapArtifacts(src, dst);
setArtifact(nullptr);
parent->redraw();
}
}
}
void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
2016-01-23 14:20:51 +02:00
{
if(ourArt && 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
void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
2023-04-23 13:09:49 +02:00
{
if(ourArt && text.size())
CArtPlace::showPopupWindow(cursorPosition);
2016-01-23 14:20:51 +02:00
}
2023-04-23 13:09:49 +02:00
void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
{
2023-04-23 13:09:49 +02:00
setInternals(art);
}
2023-04-23 13:09:49 +02:00
CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
: CArtPlace(position, Art),
locked(false),
marked(false)
{
createImage();
}
2023-04-23 13:09:49 +02:00
void CHeroArtPlace::lockSlot(bool on)
{
2023-04-23 13:09:49 +02:00
if(locked == on)
return;
2023-04-23 13:09:49 +02:00
locked = on;
if(on)
image->setFrame(ArtifactID::ART_LOCK);
else if(ourArt)
2023-10-19 20:22:26 +02:00
image->setFrame(imageIndex);
2023-04-23 13:09:49 +02:00
else
image->setFrame(0);
}
2023-04-23 13:09:49 +02:00
bool CHeroArtPlace::isLocked()
{
2023-04-23 13:09:49 +02:00
return locked;
}
2023-04-23 13:09:49 +02:00
void CHeroArtPlace::selectSlot(bool on)
{
2023-04-23 13:09:49 +02:00
if(marked == on)
return;
2023-04-23 13:09:49 +02:00
marked = on;
if(on)
selection->enable();
else
selection->disable();
}
2023-04-23 13:09:49 +02:00
bool CHeroArtPlace::isMarked() const
{
2023-04-23 13:09:49 +02:00
return marked;
}
void CHeroArtPlace::clickPressed(const Point & cursorPosition)
2023-04-23 13:09:49 +02:00
{
if(leftClickCallback)
leftClickCallback(*this);
}
void CHeroArtPlace::showPopupWindow(const Point & cursorPosition)
2023-04-23 13:09:49 +02:00
{
2023-09-17 17:40:14 +02:00
if(showPopupCallback)
showPopupCallback(*this);
}
void CHeroArtPlace::showAll(Canvas & to)
{
2023-04-23 13:09:49 +02:00
if(ourArt)
{
CIntObject::showAll(to);
}
if(marked && isActive())
to.drawBorder(pos, Colors::BRIGHT_YELLOW);
}
2023-04-23 13:09:49 +02:00
void CHeroArtPlace::setArtifact(const CArtifactInstance * art)
{
2023-04-23 13:09:49 +02:00
setInternals(art);
if(art)
{
2023-10-19 20:22:26 +02:00
image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
2023-04-23 13:09:49 +02:00
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
2023-04-13 19:40:36 +02:00
{
2023-04-23 13:09:49 +02:00
lockSlot(false);
2023-04-13 19:40:36 +02:00
}
2023-04-23 13:09:49 +02:00
}
2023-04-23 13:09:49 +02:00
void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
{
2023-05-01 13:45:32 +02:00
for(const auto & combinedArt : arts)
{
2023-04-23 13:09:49 +02:00
std::string artList;
text += "\n\n";
text += "{" + combinedArt.first->getNameTranslated() + "}";
if(arts.size() == 1)
{
2023-07-03 18:15:40 +02:00
for(const auto part : combinedArt.first->getConstituents())
2023-04-23 13:09:49 +02:00
artList += "\n" + part->getNameTranslated();
}
2023-04-23 13:09:49 +02:00
text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
2023-07-03 18:15:40 +02:00
boost::str(boost::format("%d") % combinedArt.first->getConstituents().size()) + ")" + artList;
}
}
2023-04-23 13:09:49 +02:00
void CHeroArtPlace::createImage()
{
2023-04-23 13:09:49 +02:00
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
2023-04-23 13:09:49 +02:00
si32 imageIndex = 0;
2023-04-23 13:09:49 +02:00
if(locked)
imageIndex = ArtifactID::ART_LOCK;
else if(ourArt)
imageIndex = ourArt->artType->getIconIndex();
image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
2023-10-19 20:22:26 +02:00
image->disable();
selection = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), ArtifactID::ART_SELECTION);
2023-04-23 13:09:49 +02:00
selection->disable();
}
2023-04-23 14:10:35 +02:00
bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
2023-04-23 13:17:27 +02:00
{
assert(hero);
const auto art = hero->getArt(slot);
assert(art);
2023-09-03 20:43:41 +02:00
if(hero->tempOwner != LOCPLINT->playerID)
return false;
2023-04-23 13:17:27 +02:00
2023-09-12 17:30:48 +02:00
auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId());
2023-09-03 20:43:41 +02:00
if(!assemblyPossibilities.empty())
{
auto askThread = new boost::thread([hero, art, slot, assemblyPossibilities]() -> void
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
2023-09-03 20:43:41 +02:00
for(const auto combinedArt : assemblyPossibilities)
{
bool assembleConfirmed = false;
CFunctionList<void()> onYesHandlers([&assembleConfirmed]() -> void {assembleConfirmed = true; });
onYesHandlers += std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId());
LOCPLINT->showArtifactAssemblyDialog(art->artType, combinedArt, onYesHandlers);
LOCPLINT->waitWhileDialog();
2023-09-03 20:43:41 +02:00
if(assembleConfirmed)
break;
}
});
askThread->detach();
2023-04-23 13:17:27 +02:00
return true;
}
return false;
}
2023-04-23 14:10:35 +02:00
bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
2023-04-23 13:17:27 +02:00
{
assert(hero);
const auto art = hero->getArt(slot);
assert(art);
2023-09-03 20:43:41 +02:00
if(hero->tempOwner != LOCPLINT->playerID)
return false;
2023-06-29 17:34:07 +02:00
if(art->isCombined())
2023-04-23 13:17:27 +02:00
{
2023-07-03 18:15:40 +02:00
if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
2023-04-23 13:17:27 +02:00
return false;
LOCPLINT->showArtifactAssemblyDialog(
art->artType,
nullptr,
std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
return true;
}
return false;
}