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

249 lines
6.1 KiB
C++
Raw Normal View History

/*
2024-04-23 12:49:35 +02:00
* CArtPlace.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-04-23 12:49:35 +02:00
#include "CArtPlace.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/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"
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-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)
{
OBJECT_CONSTRUCTION;
2023-11-04 22:15:03 +02:00
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();
2024-08-03 16:04:27 +02:00
moveSelectionForeground();
}
2024-04-23 19:26:21 +02:00
const CArtifactInstance * CArtPlace::getArt() const
{
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;
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)
{
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-04-23 19:26:21 +02:00
if(getArt()->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
{
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())
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
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;
}
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::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
}
2024-04-23 19:26:21 +02:00
void CArtPlace::setClickPressedCallback(const ClickFunctor & callback)
{
clickPressedCallback = callback;
}
2024-04-23 19:26:21 +02:00
void CArtPlace::setShowPopupCallback(const ClickFunctor & callback)
{
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-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-05-21 19:00:13 +02:00
for(const auto & availableArts : arts)
{
2024-05-21 19:00:13 +02:00
const auto combinedArt = availableArts.first.toArtifact();
MetaString info;
info.appendEOL();
info.appendEOL();
info.appendRawString("{");
info.appendName(combinedArt->getId());
info.appendRawString("}");
info.appendRawString(" (%d/%d)");
info.replaceNumber(availableArts.second.size());
info.replaceNumber(combinedArt->getConstituents().size());
for(const auto part : combinedArt->getConstituents())
{
2024-05-21 19:00:13 +02:00
info.appendEOL();
if(vstd::contains(availableArts.second, part->getId()))
{
info.appendName(part->getId());
}
else
{
info.appendRawString("{#A9A9A9|");
info.appendName(part->getId());
info.appendRawString("}");
}
}
2024-05-21 19:00:13 +02:00
text += info.toString();
}
}