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"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/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
|
|
|
|
2024-10-19 15:25:26 +02:00
|
|
|
CArtPlace::CArtPlace(Point position, const ArtifactID & artId, const SpellID & spellId)
|
|
|
|
: SelectableSlot(Rect(position, Point(44, 44)), Point(1, 1))
|
|
|
|
, locked(false)
|
|
|
|
, imageIndex(0)
|
|
|
|
{
|
|
|
|
OBJECT_CONSTRUCTION;
|
|
|
|
|
|
|
|
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)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2024-10-19 15:25:26 +02:00
|
|
|
this->artId = artId;
|
|
|
|
if(artId == ArtifactID::NONE)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
|
|
|
image->disable();
|
|
|
|
text.clear();
|
2024-10-19 15:25:26 +02:00
|
|
|
lockSlot(false);
|
2023-04-23 13:09:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-09-25 22:58:59 +02:00
|
|
|
|
2024-10-19 15:25:26 +02:00
|
|
|
const auto artType = artId.toArtifact();
|
|
|
|
imageIndex = artType->getIconIndex();
|
|
|
|
if(artId == ArtifactID::SPELL_SCROLL)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
2024-10-19 15:25:26 +02:00
|
|
|
this->spellId = spellId;
|
|
|
|
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())
|
|
|
|
{
|
2024-10-19 15:25:26 +02:00
|
|
|
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;
|
2024-10-19 15:25:26 +02:00
|
|
|
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;
|
2024-10-19 15:25:26 +02:00
|
|
|
component.subType = artId;
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
2023-10-19 20:22:26 +02:00
|
|
|
image->enable();
|
2024-10-19 15:25:26 +02:00
|
|
|
lockSlot(locked);
|
2023-11-04 22:15:03 +02:00
|
|
|
|
2024-10-19 15:25:26 +02:00
|
|
|
text = artType->getDescriptionTranslated();
|
|
|
|
if(artType->isScroll())
|
|
|
|
ArtifactUtils::insertScrrollSpellName(text, spellId);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2024-10-19 15:25:26 +02:00
|
|
|
ArtifactID CArtPlace::getArtifactId() const
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2024-10-19 15:25:26 +02:00
|
|
|
return artId;
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2024-10-19 15:25:26 +02:00
|
|
|
CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot,
|
|
|
|
const ArtifactID & artId, const SpellID & spellId)
|
|
|
|
: CArtPlace(position, artId, spellId),
|
2023-04-23 13:09:49 +02:00
|
|
|
commanderOwner(commanderOwner),
|
|
|
|
commanderSlotID(artSlot.num)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCommanderArtPlace::returnArtToHeroCallback()
|
|
|
|
{
|
|
|
|
ArtifactPosition artifactPos = commanderSlotID;
|
2024-10-19 15:25:26 +02:00
|
|
|
ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArtifactId());
|
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-10-19 15:25:26 +02:00
|
|
|
if(getArtifactId().toArtifact()->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);
|
2024-10-19 15:25:26 +02:00
|
|
|
setArtifact(ArtifactID(ArtifactID::NONE));
|
2023-04-23 13:09:49 +02:00
|
|
|
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-10-19 15:25:26 +02:00
|
|
|
if(getArtifactId() != ArtifactID::NONE && 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-10-19 15:25:26 +02:00
|
|
|
if(getArtifactId() != ArtifactID::NONE && 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
|
|
|
void CArtPlace::lockSlot(bool on)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
locked = on;
|
|
|
|
if(on)
|
2024-10-19 15:25:26 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
image->setFrame(ArtifactID::ART_LOCK);
|
2024-10-19 15:25:26 +02:00
|
|
|
hoverText = CGI->generaltexth->allTexts[507];
|
|
|
|
}
|
|
|
|
else if(artId != ArtifactID::NONE)
|
|
|
|
{
|
2023-10-19 20:22:26 +02:00
|
|
|
image->setFrame(imageIndex);
|
2024-10-19 15:25:26 +02:00
|
|
|
auto hoverText = MetaString::createFromRawString(CGI->generaltexth->heroscrn[1]);
|
|
|
|
hoverText.replaceName(artId);
|
|
|
|
this->hoverText = hoverText.toString();
|
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
else
|
2024-10-19 15:25:26 +02:00
|
|
|
{
|
|
|
|
hoverText = CGI->generaltexth->allTexts[507];
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
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-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-10-12 20:20:29 +02:00
|
|
|
for(auto [combinedId, availableArts] : arts)
|
2016-01-21 19:23:45 +02:00
|
|
|
{
|
2024-10-12 20:20:29 +02:00
|
|
|
const auto combinedArt = combinedId.toArtifact();
|
2024-05-21 19:00:13 +02:00
|
|
|
MetaString info;
|
|
|
|
info.appendEOL();
|
|
|
|
info.appendEOL();
|
|
|
|
info.appendRawString("{");
|
|
|
|
info.appendName(combinedArt->getId());
|
|
|
|
info.appendRawString("}");
|
|
|
|
info.appendRawString(" (%d/%d)");
|
2024-10-12 20:20:29 +02:00
|
|
|
info.replaceNumber(availableArts.size());
|
2024-05-21 19:00:13 +02:00
|
|
|
info.replaceNumber(combinedArt->getConstituents().size());
|
|
|
|
for(const auto part : combinedArt->getConstituents())
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2024-10-12 20:20:29 +02:00
|
|
|
const auto found = std::find_if(availableArts.begin(), availableArts.end(), [part](const auto & availablePart) -> bool
|
|
|
|
{
|
|
|
|
return availablePart == part->getId() ? true : false;
|
|
|
|
});
|
|
|
|
|
2024-05-21 19:00:13 +02:00
|
|
|
info.appendEOL();
|
2024-10-12 20:20:29 +02:00
|
|
|
if(found < availableArts.end())
|
2024-05-21 19:00:13 +02:00
|
|
|
{
|
|
|
|
info.appendName(part->getId());
|
2024-10-12 20:20:29 +02:00
|
|
|
availableArts.erase(found);
|
2024-05-21 19:00:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.appendRawString("{#A9A9A9|");
|
|
|
|
info.appendName(part->getId());
|
|
|
|
info.appendRawString("}");
|
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
2024-05-21 19:00:13 +02:00
|
|
|
text += info.toString();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|