1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-29 22:45:36 +00:00

Use string_view in animation code

This commit is contained in:
unknown 2022-08-21 18:53:38 +02:00
parent 537c6e96ab
commit 827a2f0b77
14 changed files with 63 additions and 63 deletions

View file

@ -60,7 +60,7 @@ ActorAnimation::ActorAnimation(const MWWorld::Ptr& ptr, osg::ref_ptr<osg::Group>
ActorAnimation::~ActorAnimation() = default;
PartHolderPtr ActorAnimation::attachMesh(const std::string& model, const std::string& bonename, bool enchantedGlow, osg::Vec4f* glowColor)
PartHolderPtr ActorAnimation::attachMesh(const std::string& model, std::string_view bonename, bool enchantedGlow, osg::Vec4f* glowColor)
{
osg::Group* parent = getBoneByName(bonename);
if (!parent)
@ -71,22 +71,22 @@ PartHolderPtr ActorAnimation::attachMesh(const std::string& model, const std::st
const NodeMap& nodeMap = getNodeMap();
NodeMap::const_iterator found = nodeMap.find(bonename);
if (found == nodeMap.end())
return PartHolderPtr();
return {};
if (enchantedGlow)
mGlowUpdater = SceneUtil::addEnchantedGlow(instance, mResourceSystem, *glowColor);
return PartHolderPtr(new PartHolder(instance));
return std::make_unique<PartHolder>(instance);
}
osg::ref_ptr<osg::Node> ActorAnimation::attach(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool isLight)
osg::ref_ptr<osg::Node> ActorAnimation::attach(const std::string& model, std::string_view bonename, std::string_view bonefilter, bool isLight)
{
osg::ref_ptr<const osg::Node> templateNode = mResourceSystem->getSceneManager()->getTemplate(model);
const NodeMap& nodeMap = getNodeMap();
auto found = nodeMap.find(bonename);
if (found == nodeMap.end())
throw std::runtime_error("Can't find attachment node " + bonename);
throw std::runtime_error("Can't find attachment node " + std::string{bonename});
if(isLight)
{
osg::Quat rotation(osg::DegreesToRadians(-90.f), osg::Vec3f(1,0,0));
@ -109,7 +109,7 @@ std::string ActorAnimation::getShieldMesh(const MWWorld::ConstPtr& shield, bool
if (part.mPart != ESM::PRT_Shield)
continue;
std::string bodypartName;
std::string_view bodypartName;
if (female && !part.mFemale.empty())
bodypartName = part.mFemale;
else if (!part.mMale.empty())
@ -212,7 +212,7 @@ void ActorAnimation::updateHolsteredShield(bool showCarriedLeft)
if (mesh.empty())
return;
std::string boneName = "Bip01 AttachShield";
std::string_view boneName = "Bip01 AttachShield";
osg::Vec4f glowColor = shield->getClass().getEnchantmentColor(*shield);
std::string holsteredName = mesh;
holsteredName = holsteredName.replace(holsteredName.size()-4, 4, "_sh.nif");
@ -275,22 +275,21 @@ bool ActorAnimation::useShieldAnimations() const
return false;
}
osg::Group* ActorAnimation::getBoneByName(const std::string& boneName) const
osg::Group* ActorAnimation::getBoneByName(std::string_view boneName) const
{
if (!mObjectRoot)
return nullptr;
SceneUtil::FindByNameVisitor findVisitor (boneName);
SceneUtil::FindByNameVisitor findVisitor(boneName);
mObjectRoot->accept(findVisitor);
return findVisitor.mFoundNode;
}
std::string ActorAnimation::getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon)
std::string_view ActorAnimation::getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon)
{
std::string boneName;
if(weapon.isEmpty())
return boneName;
return {};
auto type = weapon.getClass().getType();
if(type == ESM::Weapon::sRecordId)
@ -300,7 +299,7 @@ std::string ActorAnimation::getHolsteredWeaponBoneName(const MWWorld::ConstPtr&
return MWMechanics::getWeaponType(weaponType)->mSheathingBone;
}
return boneName;
return {};
}
void ActorAnimation::resetControllers(osg::Node* node)
@ -336,7 +335,7 @@ void ActorAnimation::updateHolsteredWeapon(bool showHolsteredWeapons)
std::string mesh = weapon->getClass().getModel(*weapon);
std::string scabbardName = mesh;
std::string boneName = getHolsteredWeaponBoneName(*weapon);
std::string_view boneName = getHolsteredWeaponBoneName(*weapon);
if (mesh.empty() || boneName.empty())
return;
@ -405,7 +404,7 @@ void ActorAnimation::updateQuiver()
return;
std::string mesh = weapon->getClass().getModel(*weapon);
std::string boneName = getHolsteredWeaponBoneName(*weapon);
std::string_view boneName = getHolsteredWeaponBoneName(*weapon);
if (mesh.empty() || boneName.empty())
return;

View file

@ -43,20 +43,20 @@ class ActorAnimation : public Animation, public MWWorld::ContainerStoreListener
void removeFromScene() override;
protected:
osg::Group* getBoneByName(const std::string& boneName) const;
osg::Group* getBoneByName(std::string_view boneName) const;
virtual void updateHolsteredWeapon(bool showHolsteredWeapons);
virtual void updateHolsteredShield(bool showCarriedLeft);
virtual void updateQuiver();
std::string getShieldMesh(const MWWorld::ConstPtr& shield, bool female) const;
virtual std::string getSheathedShieldMesh(const MWWorld::ConstPtr& shield) const;
virtual std::string getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon);
virtual PartHolderPtr attachMesh(const std::string& model, const std::string& bonename, bool enchantedGlow, osg::Vec4f* glowColor);
virtual PartHolderPtr attachMesh(const std::string& model, const std::string& bonename)
virtual std::string_view getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon);
virtual PartHolderPtr attachMesh(const std::string& model, std::string_view bonename, bool enchantedGlow, osg::Vec4f* glowColor);
virtual PartHolderPtr attachMesh(const std::string& model, std::string_view bonename)
{
osg::Vec4f stubColor = osg::Vec4f(0,0,0,0);
return attachMesh(model, bonename, false, &stubColor);
};
osg::ref_ptr<osg::Node> attach(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool isLight);
osg::ref_ptr<osg::Node> attach(const std::string& model, std::string_view bonename, std::string_view bonefilter, bool isLight);
PartHolderPtr mScabbard;
PartHolderPtr mHolsteredShield;

View file

@ -612,10 +612,9 @@ namespace MWRender
}
}
void Animation::addAnimSource(const std::string &model, const std::string& baseModel)
void Animation::addAnimSource(std::string_view model, const std::string& baseModel)
{
std::string kfname = model;
Misc::StringUtils::lowerCaseInPlace(kfname);
std::string kfname = Misc::StringUtils::lowerCase(model);
if(kfname.size() > 4 && kfname.compare(kfname.size()-4, 4, ".nif") == 0)
kfname.replace(kfname.size()-4, 4, ".kf");
@ -670,20 +669,20 @@ namespace MWRender
if (!mAccumRoot)
{
// Priority matters! bip01 is preferred.
static const std::array<std::string, 2> accumRootNames =
static const std::initializer_list<std::string_view> accumRootNames =
{
"bip01",
"root bone"
};
NodeMap::const_iterator found = nodeMap.end();
for (const std::string& name : accumRootNames)
for (const std::string_view& name : accumRootNames)
{
found = nodeMap.find(name);
if (found == nodeMap.end())
continue;
for (SceneUtil::KeyframeHolder::KeyframeControllerMap::const_iterator it = controllerMap.begin(); it != controllerMap.end(); ++it)
{
if (Misc::StringUtils::lowerCase(it->first) == name)
if (Misc::StringUtils::ciEqual(it->first, name))
{
mAccumRoot = found->second;
break;

View file

@ -330,7 +330,7 @@ protected:
* @param model The file to add the keyframes for. Note that the .nif file extension will be replaced with .kf.
* @param baseModel The filename of the mObjectRoot, only used for error messages.
*/
void addAnimSource(const std::string &model, const std::string& baseModel);
void addAnimSource(std::string_view model, const std::string& baseModel);
void addSingleAnimSource(const std::string &model, const std::string& baseModel);
/** Adds an additional light to the given node using the specified ESM record. */

View file

@ -114,7 +114,7 @@ void CreatureWeaponAnimation::updatePart(PartHolderPtr& scene, int slot)
}
MWWorld::ConstPtr item = *it;
std::string bonename;
std::string_view bonename;
std::string itemModel = item.getClass().getModel(item);
if (slot == MWWorld::InventoryStore::Slot_CarriedRight)
{

View file

@ -472,8 +472,8 @@ void NpcAnimation::updateNpcBase()
mHeadModel.clear();
mHairModel.clear();
std::string headName = isWerewolf ? "WerewolfHead" : mNpc->mHead;
std::string hairName = isWerewolf ? "WerewolfHair" : mNpc->mHair;
std::string_view headName = isWerewolf ? std::string_view{"WerewolfHead"} : mNpc->mHead;
std::string_view hairName = isWerewolf ? std::string_view{"WerewolfHair"} : mNpc->mHair;
if (!headName.empty())
{
@ -514,7 +514,7 @@ void NpcAnimation::updateNpcBase()
if(!is1stPerson)
{
const std::string base = Settings::Manager::getString("xbaseanim", "Models");
const std::string& base = Settings::Manager::getString("xbaseanim", "Models");
if (smodel != base && !isWerewolf)
addAnimSource(base, smodel);
@ -528,7 +528,7 @@ void NpcAnimation::updateNpcBase()
}
else
{
const std::string base = Settings::Manager::getString("xbaseanim1st", "Models");
const std::string& base = Settings::Manager::getString("xbaseanim1st", "Models");
if (smodel != base && !isWerewolf)
addAnimSource(base, smodel);
@ -701,13 +701,13 @@ void NpcAnimation::updateParts()
PartHolderPtr NpcAnimation::insertBoundedPart(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight)
PartHolderPtr NpcAnimation::insertBoundedPart(const std::string& model, std::string_view bonename, std::string_view bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight)
{
osg::ref_ptr<osg::Node> attached = attach(model, bonename, bonefilter, isLight);
if (enchantedGlow)
mGlowUpdater = SceneUtil::addEnchantedGlow(attached, mResourceSystem, *glowColor);
return PartHolderPtr(new PartHolder(attached));
return std::make_unique<PartHolder>(attached);
}
osg::Vec3f NpcAnimation::runAnimation(float timepassed)
@ -786,7 +786,7 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g
mPartPriorities[type] = priority;
try
{
std::string bonename = sPartList.at(type);
std::string_view bonename = sPartList.at(type);
if (type == ESM::PRT_Weapon)
{
const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
@ -794,7 +794,7 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g
if(weapon != inv.end() && weapon->getType() == ESM::Weapon::sRecordId)
{
int weaponType = weapon->get<ESM::Weapon>()->mBase->mData.mType;
const std::string weaponBonename = MWMechanics::getWeaponType(weaponType)->mAttachBone;
const std::string& weaponBonename = MWMechanics::getWeaponType(weaponType)->mAttachBone;
if (weaponBonename != bonename)
{
@ -807,7 +807,7 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g
}
// PRT_Hair seems to be the only type that breaks consistency and uses a filter that's different from the attachment bone
const std::string bonefilter = (type == ESM::PRT_Hair) ? "hair" : bonename;
const std::string_view bonefilter = (type == ESM::PRT_Hair) ? std::string_view{"hair"} : bonename;
mObjectParts[type] = insertBoundedPart(mesh, bonename, bonefilter, enchantedGlow, glowColor, isLight);
}
catch (std::exception& e)

View file

@ -82,8 +82,8 @@ private:
NpcType getNpcType() const;
PartHolderPtr insertBoundedPart(const std::string &model, const std::string &bonename,
const std::string &bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight);
PartHolderPtr insertBoundedPart(const std::string &model, std::string_view bonename,
std::string_view bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight);
void removeIndividualPart(ESM::PartReferenceType type);
void reserveIndividualPart(ESM::PartReferenceType type, int group, int priority);

View file

@ -689,7 +689,7 @@ namespace MWRender
SetupVisitor alphaFaderSetupVisitor(mPrecipitationAlpha);
mParticleEffect->accept(alphaFaderSetupVisitor);
SceneUtil::FindByClassVisitor findPSVisitor(std::string("ParticleSystem"));
SceneUtil::FindByClassVisitor findPSVisitor("ParticleSystem");
mParticleEffect->accept(findPSVisitor);
for (unsigned int i = 0; i < findPSVisitor.mFoundNodes.size(); ++i)

View file

@ -94,7 +94,7 @@ void WeaponAnimation::attachArrow(const MWWorld::Ptr& actor)
osg::ref_ptr<osg::Node> arrow = getResourceSystem()->getSceneManager()->getInstance(model, parent);
mAmmunition = PartHolderPtr(new PartHolder(arrow));
mAmmunition = std::make_unique<PartHolder>(arrow);
}
}

View file

@ -221,7 +221,8 @@ namespace MWWorld
std::ostringstream nodeName;
nodeName << "Dummy" << std::setw(2) << std::setfill('0') << iter;
const ESM::Weapon* weapon = MWBase::Environment::get().getWorld()->getStore().get<ESM::Weapon>().find (state.mIdMagic.at(iter));
SceneUtil::FindByNameVisitor findVisitor(nodeName.str());
std::string nameToFind = nodeName.str();
SceneUtil::FindByNameVisitor findVisitor(nameToFind);
attachTo->accept(findVisitor);
if (findVisitor.mFoundNode)
mResourceSystem->getSceneManager()->getInstance(

View file

@ -23,12 +23,11 @@ namespace SceneUtil
class CopyRigVisitor : public osg::NodeVisitor
{
public:
CopyRigVisitor(osg::ref_ptr<osg::Group> parent, const std::string& filter)
CopyRigVisitor(osg::ref_ptr<osg::Group> parent, std::string_view filter)
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
, mParent(parent)
, mFilter(Misc::StringUtils::lowerCase(filter))
, mFilter(std::move(filter))
{
mFilter2 = "tri " + mFilter;
}
void apply(osg::MatrixTransform& node) override
@ -71,19 +70,20 @@ namespace SceneUtil
private:
bool filterMatches(const std::string& name) const
bool filterMatches(std::string_view name) const
{
std::string lowerName = Misc::StringUtils::lowerCase(name);
return (lowerName.size() >= mFilter.size() && lowerName.compare(0, mFilter.size(), mFilter) == 0)
|| (lowerName.size() >= mFilter2.size() && lowerName.compare(0, mFilter2.size(), mFilter2) == 0);
if (Misc::StringUtils::ciCompareLen(name, mFilter, mFilter.size()) == 0)
return true;
if (Misc::StringUtils::ciCompareLen(name, "tri ", 4) == 0)
return Misc::StringUtils::ciCompareLen(name.substr(4), mFilter, mFilter.size()) == 0;
return false;
}
using NodeSet = std::set<osg::ref_ptr<const osg::Node>>;
NodeSet mToCopy;
osg::ref_ptr<osg::Group> mParent;
std::string mFilter;
std::string mFilter2;
std::string_view mFilter;
};
void mergeUserData(const osg::UserDataContainer* source, osg::Object* target)
@ -100,7 +100,7 @@ namespace SceneUtil
}
}
osg::ref_ptr<osg::Node> attach(osg::ref_ptr<const osg::Node> toAttach, osg::Node *master, const std::string &filter, osg::Group* attachNode, Resource::SceneManager* sceneManager, const osg::Quat* attitude)
osg::ref_ptr<osg::Node> attach(osg::ref_ptr<const osg::Node> toAttach, osg::Node* master, std::string_view filter, osg::Group* attachNode, Resource::SceneManager* sceneManager, const osg::Quat* attitude)
{
if (dynamic_cast<const SceneUtil::Skeleton*>(toAttach.get()))
{

View file

@ -1,7 +1,7 @@
#ifndef OPENMW_COMPONENTS_SCENEUTIL_ATTACH_H
#define OPENMW_COMPONENTS_SCENEUTIL_ATTACH_H
#include <string>
#include <string_view>
#include <osg/ref_ptr>
@ -24,7 +24,7 @@ namespace SceneUtil
/// Otherwise, just attach all of the toAttach scenegraph to the attachment node on the master scenegraph, with no filtering.
/// @note The master scene graph is expected to include a skeleton.
/// @return A newly created node that is directly attached to the master scene graph
osg::ref_ptr<osg::Node> attach(osg::ref_ptr<const osg::Node> toAttach, osg::Node* master, const std::string& filter, osg::Group* attachNode, Resource::SceneManager *sceneManager, const osg::Quat* attitude = nullptr);
osg::ref_ptr<osg::Node> attach(osg::ref_ptr<const osg::Node> toAttach, osg::Node* master, std::string_view filter, osg::Group* attachNode, Resource::SceneManager* sceneManager, const osg::Quat* attitude = nullptr);
}

View file

@ -26,7 +26,7 @@ namespace SceneUtil
void FindByClassVisitor::apply(osg::Node &node)
{
if (Misc::StringUtils::ciEqual(std::string_view(node.className()), mNameToFind))
if (Misc::StringUtils::ciEqual(node.className(), mNameToFind))
mFoundNodes.push_back(&node);
traverse(node);
@ -52,7 +52,7 @@ namespace SceneUtil
{
// Choose first found node in file
if (trans.libraryName() == std::string("osgAnimation"))
if (trans.libraryName() == std::string_view("osgAnimation"))
{
std::string nodeName = trans.getName();
// Convert underscores to whitespaces as a workaround for Collada (OpenMW's animation system uses whitespace-separated names)
@ -144,7 +144,7 @@ namespace SceneUtil
void RemoveTriBipVisitor::applyImpl(osg::Node& node)
{
const std::string toFind = "tri bip";
const std::string_view toFind = "tri bip";
if (Misc::StringUtils::ciCompareLen(node.getName(), toFind, toFind.size()) == 0)
{
osg::Group* parent = static_cast<osg::Group*>(*(getNodePath().end()-2));

View file

@ -4,6 +4,7 @@
#include <osg/MatrixTransform>
#include <osg/NodeVisitor>
#include <string_view>
#include <unordered_map>
#include <components/misc/strings/algorithm.hpp>
@ -17,9 +18,9 @@ namespace SceneUtil
class FindByNameVisitor : public osg::NodeVisitor
{
public:
FindByNameVisitor(const std::string& nameToFind)
FindByNameVisitor(std::string_view nameToFind)
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
, mNameToFind(nameToFind)
, mNameToFind(std::move(nameToFind))
, mFoundNode(nullptr)
{
}
@ -30,22 +31,22 @@ namespace SceneUtil
bool checkGroup(osg::Group& group);
std::string mNameToFind;
std::string_view mNameToFind;
osg::Group* mFoundNode;
};
class FindByClassVisitor : public osg::NodeVisitor
{
public:
FindByClassVisitor(const std::string& nameToFind)
FindByClassVisitor(std::string_view nameToFind)
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
, mNameToFind(nameToFind)
, mNameToFind(std::move(nameToFind))
{
}
void apply(osg::Node &node) override;
std::string mNameToFind;
std::string_view mNameToFind;
std::vector<osg::Node *> mFoundNodes;
};