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

315 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(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;
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;
component.subType = artInst->getTypeId();
2023-04-23 13:09:49 +02:00
}
2023-10-19 20:22:26 +02:00
image->enable();
2023-04-23 13:09:49 +02:00
text = artInst->getDescription();
}
2023-11-04 22:15:03 +02:00
CArtPlace::CArtPlace(Point position, const CArtifactInstance * art)
: ourArt(art)
, locked(false)
{
2023-04-23 13:09:49 +02:00
pos += position;
pos.w = pos.h = 44;
2023-11-04 22:15:03 +02:00
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
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();
selection = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), ArtifactID::ART_SELECTION, 0, -1, -1);
2023-11-04 22:15:03 +02:00
selection->visible = false;
}
2023-04-23 13:09:49 +02:00
const CArtifactInstance * CArtPlace::getArt()
{
2023-04-23 13:09:49 +02:00
return ourArt;
}
2023-11-04 22:15:03 +02:00
CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * art)
: CArtPlace(position, art),
2023-04-23 13:09:49 +02:00
commanderOwner(commanderOwner),
commanderSlotID(artSlot.num)
{
2023-11-04 22:15:03 +02:00
setArtifact(art);
2023-04-23 13:09:49 +02:00
}
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);
src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
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-11-04 22:15:03 +02:00
CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * art)
: CArtPlace(position, art)
2023-04-23 13:09:49 +02:00
{
}
2023-11-04 22:15:03 +02:00
void CArtPlace::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-11-04 22:15:03 +02:00
bool CArtPlace::isLocked() const
{
2023-04-23 13:09:49 +02:00
return locked;
}
2023-11-04 22:15:03 +02:00
void CArtPlace::selectSlot(bool on)
{
2023-11-04 22:15:03 +02:00
selection->visible = on;
}
2023-11-04 22:15:03 +02:00
bool CArtPlace::isSelected() const
{
2023-11-04 22:15:03 +02:00
return selection->visible;
2023-04-23 13:09:49 +02:00
}
void CArtPlace::clickPressed(const Point & cursorPosition)
2023-04-23 13:09:49 +02:00
{
if(clickPressedCallback)
clickPressedCallback(*this, cursorPosition);
2023-04-23 13:09:49 +02:00
}
void CArtPlace::showPopupWindow(const Point & cursorPosition)
2023-04-23 13:09:49 +02:00
{
2023-09-17 17:40:14 +02:00
if(showPopupCallback)
showPopupCallback(*this, cursorPosition);
}
2023-12-17 16:30:19 +02:00
void CArtPlace::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
{
if(!on)
return;
if(gestureCallback)
gestureCallback(*this, initialPosition);
}
2023-11-04 22:15:03 +02:00
void CArtPlace::showAll(Canvas & to)
{
2023-11-04 22:15:03 +02:00
CIntObject::showAll(to);
selection->showAll(to);
}
2023-11-04 22:15:03 +02:00
void CArtPlace::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
}
void CArtPlace::setClickPressedCallback(ClickFunctor callback)
{
clickPressedCallback = callback;
}
void CArtPlace::setShowPopupCallback(ClickFunctor callback)
{
showPopupCallback = callback;
}
2023-12-17 16:30:19 +02:00
void CArtPlace::setGestureCallback(ClickFunctor callback)
{
gestureCallback = callback;
}
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 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;
}