2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2024-04-23 12:49:35 +02:00
|
|
|
* CArtPlace.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "StdInc.h"
|
2024-04-23 12:49:35 +02:00
|
|
|
#include "CArtPlace.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-04-27 19:21:06 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "CComponent.h"
|
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../windows/GUIClasses.h"
|
2023-06-02 15:42:18 +02:00
|
|
|
#include "../render/Canvas.h"
|
|
|
|
#include "../render/Colors.h"
|
2023-09-25 22:58:59 +02:00
|
|
|
#include "../render/IRenderHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
#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"
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2023-10-23 15:38:05 +02:00
|
|
|
#include "../../lib/networkPacks/ArtifactLocation.h"
|
2023-09-25 22:58:59 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CArtPlace::setInternals(const CArtifactInstance * artInst)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
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;
|
2023-10-31 11:09:56 +02:00
|
|
|
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)
|
2023-10-31 11:09:56 +02:00
|
|
|
component.type = ComponentType::SPELL_SCROLL;
|
|
|
|
component.subType = spellID;
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-31 11:09:56 +02:00
|
|
|
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));
|
|
|
|
}
|
2023-10-31 11:09:56 +02:00
|
|
|
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();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 19:49:59 +02:00
|
|
|
CArtPlace::CArtPlace(Point position, const CArtifactInstance * art)
|
|
|
|
: SelectableSlot(Rect(position, Point(44, 44)), Point(1, 1))
|
|
|
|
, ourArt(art)
|
2023-11-04 22:15:03 +02:00
|
|
|
, locked(false)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
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();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 19:26:21 +02:00
|
|
|
const CArtifactInstance * CArtPlace::getArt() const
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
return ourArt;
|
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
|
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;
|
2024-04-23 19:26:21 +02:00
|
|
|
ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArt()->getTypeId());
|
2023-04-23 13:09:49 +02:00
|
|
|
if(freeSlot == ArtifactPosition::PRE_FIRST)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
else
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-10-14 21:00:39 +02:00
|
|
|
ArtifactLocation src(commanderOwner->id, artifactPos);
|
2023-10-23 18:37:18 +02:00
|
|
|
src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
|
2023-10-14 21:00:39 +02:00
|
|
|
ArtifactLocation dst(commanderOwner->id, freeSlot);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2024-04-23 19:26:21 +02:00
|
|
|
if(getArt()->canBePutAt(commanderOwner, freeSlot, true))
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
LOCPLINT->cb->swapArtifacts(src, dst);
|
|
|
|
setArtifact(nullptr);
|
|
|
|
parent->redraw();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
|
2016-01-23 14:20:51 +02:00
|
|
|
{
|
2024-04-23 19:26:21 +02:00
|
|
|
if(getArt() && 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-04-23 19:26:21 +02:00
|
|
|
if(getArt() && text.size())
|
2023-07-08 13:33:04 +02:00
|
|
|
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-03-18 12:44:01 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 22:15:03 +02:00
|
|
|
void CArtPlace::lockSlot(bool on)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
if(locked == on)
|
|
|
|
return;
|
2014-07-05 12:58:42 +03:00
|
|
|
|
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);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-11-04 22:15:03 +02:00
|
|
|
bool CArtPlace::isLocked() const
|
2022-12-18 14:34:38 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
return locked;
|
2022-12-18 14:34:38 +02:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:43:02 +02:00
|
|
|
void CArtPlace::clickPressed(const Point & cursorPosition)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
2023-11-13 17:43:02 +02:00
|
|
|
if(clickPressedCallback)
|
|
|
|
clickPressedCallback(*this, cursorPosition);
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:43:02 +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)
|
2023-11-13 17:43:02 +02:00
|
|
|
showPopupCallback(*this, cursorPosition);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
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::setArtifact(const CArtifactInstance * art)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
setInternals(art);
|
|
|
|
if(art)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-10-19 20:22:26 +02:00
|
|
|
image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
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
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2024-04-23 19:26:21 +02:00
|
|
|
void CArtPlace::setClickPressedCallback(const ClickFunctor & callback)
|
2023-11-13 17:43:02 +02:00
|
|
|
{
|
|
|
|
clickPressedCallback = callback;
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:26:21 +02:00
|
|
|
void CArtPlace::setShowPopupCallback(const ClickFunctor & callback)
|
2023-11-13 17:43:02 +02:00
|
|
|
{
|
|
|
|
showPopupCallback = callback;
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:26:21 +02:00
|
|
|
void CArtPlace::setGestureCallback(const ClickFunctor & callback)
|
2023-12-17 16:30:19 +02:00
|
|
|
{
|
|
|
|
gestureCallback = callback;
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:26:21 +02:00
|
|
|
void CHeroArtPlace::addCombinedArtInfo(const std::map<const CArtifact*, int> & arts)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
2023-05-01 13:45:32 +02:00
|
|
|
for(const auto & combinedArt : arts)
|
2016-01-21 19:23:45 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
std::string artList;
|
|
|
|
text += "\n\n";
|
|
|
|
text += "{" + combinedArt.first->getNameTranslated() + "}";
|
|
|
|
if(arts.size() == 1)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
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();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
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;
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2023-09-27 17:44:08 +02:00
|
|
|
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);
|
2023-09-27 17:44:08 +02:00
|
|
|
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;
|
|
|
|
}
|