2025-09-10 12:45:17 +02:00

382 lines
12 KiB
C++

/******************************************************************************
* Spine Runtimes License Agreement
* Last updated April 5, 2025. Replaces all prior versions.
*
* Copyright (c) 2013-2025, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include "SpineSkeletonComponent.h"
#include "SpineSkeletonRendererComponent.h"
#include "SpineAtlasAsset.h"
#include "spine/spine.h"
#define LOCTEXT_NAMESPACE "Spine"
using namespace spine;
USpineSkeletonComponent::USpineSkeletonComponent() {
PrimaryComponentTick.bCanEverTick = true;
bTickInEditor = true;
bAutoActivate = true;
}
bool USpineSkeletonComponent::SetSkins(UPARAM(ref) TArray<FString> &SkinNames) {
CheckState();
if (skeleton) {
spine::Skin *newSkin = new spine::Skin("__spine-ue3_custom_skin");
for (auto &skinName : SkinNames) {
spine::Skin *skin = skeleton->getData().findSkin(TCHAR_TO_UTF8(*skinName));
if (!skin) {
delete newSkin;
return false;
}
newSkin->addSkin(*skin);
}
skeleton->setSkin(newSkin);
if (customSkin != nullptr) {
delete customSkin;
}
customSkin = newSkin;
return true;
} else
return false;
}
bool USpineSkeletonComponent::SetSkin(const FString skinName) {
CheckState();
if (skeleton) {
Skin *skin = skeleton->getData().findSkin(TCHAR_TO_UTF8(*skinName));
if (!skin) return false;
skeleton->setSkin(skin);
return true;
} else
return false;
}
void USpineSkeletonComponent::GetSkins(TArray<FString> &Skins) {
CheckState();
if (skeleton) {
for (size_t i = 0, n = skeleton->getData().getSkins().size(); i < n; i++) {
Skins.Add(skeleton->getData().getSkins()[i]->getName().buffer());
}
}
}
bool USpineSkeletonComponent::HasSkin(const FString skinName) {
CheckState();
if (skeleton) {
return skeleton->getData().findSkin(TCHAR_TO_UTF8(*skinName)) != nullptr;
}
return false;
}
bool USpineSkeletonComponent::SetAttachment(const FString slotName, const FString attachmentName) {
CheckState();
if (skeleton) {
if (attachmentName.IsEmpty()) {
skeleton->setAttachment(TCHAR_TO_UTF8(*slotName), NULL);
return true;
}
if (!skeleton->getAttachment(TCHAR_TO_UTF8(*slotName), TCHAR_TO_UTF8(*attachmentName))) return false;
skeleton->setAttachment(TCHAR_TO_UTF8(*slotName), TCHAR_TO_UTF8(*attachmentName));
return true;
}
return false;
}
FTransform USpineSkeletonComponent::GetBoneWorldTransform(const FString &BoneName) {
CheckState();
if (skeleton) {
Bone *bone = skeleton->findBone(TCHAR_TO_UTF8(*BoneName));
if (!bone) return FTransform();
// Need to fetch the renderer component to get world transform of actor plus
// offset by renderer component and its parent component(s). If no renderer
// component is found, this components owner's transform is used as a fallback
FTransform baseTransform;
AActor *owner = GetOwner();
if (owner) {
USpineSkeletonRendererComponent *rendererComponent = static_cast<USpineSkeletonRendererComponent *>(
owner->GetComponentByClass(USpineSkeletonRendererComponent::StaticClass()));
if (rendererComponent)
baseTransform = rendererComponent->GetComponentTransform();
else
baseTransform = owner->GetActorTransform();
}
FVector position(bone->getAppliedPose().getWorldX(), 0, bone->getAppliedPose().getWorldY());
FMatrix localTransform;
localTransform.SetIdentity();
localTransform.SetAxis(2, FVector(bone->getAppliedPose().getA(), 0, bone->getAppliedPose().getC()));
localTransform.SetAxis(0, FVector(bone->getAppliedPose().getB(), 0, bone->getAppliedPose().getD()));
localTransform.SetOrigin(FVector(bone->getAppliedPose().getWorldX(), 0, bone->getAppliedPose().getWorldY()));
localTransform = localTransform * baseTransform.ToMatrixWithScale();
FTransform result;
result.SetFromMatrix(localTransform);
return result;
}
return FTransform();
}
void USpineSkeletonComponent::SetBoneWorldPosition(const FString &BoneName, const FVector &position) {
CheckState();
if (skeleton) {
Bone *bone = skeleton->findBone(TCHAR_TO_UTF8(*BoneName));
if (!bone) return;
// Need to fetch the renderer component to get world transform of actor plus
// offset by renderer component and its parent component(s). If no renderer
// component is found, this components owner's transform is used as a fallback
FTransform baseTransform;
AActor *owner = GetOwner();
if (owner) {
USpineSkeletonRendererComponent *rendererComponent = static_cast<USpineSkeletonRendererComponent *>(
owner->GetComponentByClass(USpineSkeletonRendererComponent::StaticClass()));
if (rendererComponent)
baseTransform = rendererComponent->GetComponentTransform();
else
baseTransform = owner->GetActorTransform();
}
baseTransform = baseTransform.Inverse();
FVector localPosition = baseTransform.TransformPosition(position);
float localX = 0, localY = 0;
if (bone->getParent()) {
bone->getParent()->getAppliedPose().worldToLocal(localPosition.X, localPosition.Z, localX, localY);
} else {
bone->getAppliedPose().worldToLocal(localPosition.X, localPosition.Z, localX, localY);
}
bone->getAppliedPose().setX(localX);
bone->getAppliedPose().setY(localY);
}
}
void USpineSkeletonComponent::UpdateWorldTransform() {
CheckState();
if (skeleton) {
skeleton->updateWorldTransform(Physics_Update);
}
}
void USpineSkeletonComponent::SetupPose() {
CheckState();
if (skeleton) skeleton->setupPose();
}
void USpineSkeletonComponent::SetupPoseBones() {
CheckState();
if (skeleton) skeleton->setupPoseBones();
}
void USpineSkeletonComponent::SetupPoseSlots() {
CheckState();
if (skeleton) skeleton->setupPoseSlots();
}
void USpineSkeletonComponent::SetScaleX(float scaleX) {
CheckState();
if (skeleton) skeleton->setScaleX(scaleX);
}
float USpineSkeletonComponent::GetScaleX() {
CheckState();
if (skeleton) return skeleton->getScaleX();
return 1;
}
void USpineSkeletonComponent::SetScaleY(float scaleY) {
CheckState();
if (skeleton) skeleton->setScaleY(scaleY);
}
float USpineSkeletonComponent::GetScaleY() {
CheckState();
if (skeleton) return skeleton->getScaleY();
return 1;
}
void USpineSkeletonComponent::GetBones(TArray<FString> &Bones) {
CheckState();
if (skeleton) {
for (size_t i = 0, n = skeleton->getBones().size(); i < n; i++) {
Bones.Add(skeleton->getBones()[i]->getData().getName().buffer());
}
}
}
bool USpineSkeletonComponent::HasBone(const FString BoneName) {
CheckState();
if (skeleton) {
return skeleton->getData().findBone(TCHAR_TO_UTF8(*BoneName)) != nullptr;
}
return false;
}
void USpineSkeletonComponent::GetSlots(TArray<FString> &Slots) {
CheckState();
if (skeleton) {
for (size_t i = 0, n = skeleton->getSlots().size(); i < n; i++) {
Slots.Add(skeleton->getSlots()[i]->getData().getName().buffer());
}
}
}
bool USpineSkeletonComponent::HasSlot(const FString SlotName) {
CheckState();
if (skeleton) {
return skeleton->getData().findSlot(TCHAR_TO_UTF8(*SlotName)) != nullptr;
}
return false;
}
void USpineSkeletonComponent::SetSlotColor(const FString SlotName, const FColor color) {
CheckState();
if (skeleton) {
Slot *slot = skeleton->findSlot(TCHAR_TO_UTF8(*SlotName));
if (slot) {
slot->getPose().getColor().set(color.R / 255.f, color.G / 255.f, color.B / 255.f, color.A / 255.f);
}
}
}
void USpineSkeletonComponent::GetAnimations(TArray<FString> &Animations) {
CheckState();
if (skeleton) {
for (size_t i = 0, n = skeleton->getData().getAnimations().size(); i < n; i++) {
Animations.Add(skeleton->getData().getAnimations()[i]->getName().buffer());
}
}
}
bool USpineSkeletonComponent::HasAnimation(FString AnimationName) {
CheckState();
if (skeleton) {
return skeleton->getData().findAnimation(TCHAR_TO_UTF8(*AnimationName)) != nullptr;
}
return false;
}
float USpineSkeletonComponent::GetAnimationDuration(FString AnimationName) {
CheckState();
if (skeleton) {
Animation *animation = skeleton->getData().findAnimation(TCHAR_TO_UTF8(*AnimationName));
if (animation == nullptr)
return 0;
else
return animation->getDuration();
}
return 0;
}
void USpineSkeletonComponent::PhysicsTranslate(float x, float y) {
CheckState();
if (skeleton) {
skeleton->physicsTranslate(x, y);
}
}
void USpineSkeletonComponent::PhysicsRotate(float x, float y, float degrees) {
CheckState();
if (skeleton) {
skeleton->physicsRotate(x, y, degrees);
}
}
void USpineSkeletonComponent::ResetPhysicsConstraints() {
CheckState();
if (skeleton) {
Array<PhysicsConstraint *> &constraints = skeleton->getPhysicsConstraints();
for (int i = 0, n = (int) constraints.size(); i < n; i++) {
constraints[i]->reset(*skeleton);
}
}
}
void USpineSkeletonComponent::BeginPlay() {
Super::BeginPlay();
}
void USpineSkeletonComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) {
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
InternalTick(DeltaTime);
}
void USpineSkeletonComponent::InternalTick(float DeltaTime, bool CallDelegates, bool Preview) {
CheckState();
if (skeleton) {
if (CallDelegates) BeforeUpdateWorldTransform.Broadcast(this);
skeleton->updateWorldTransform(Physics_Update);
if (CallDelegates) AfterUpdateWorldTransform.Broadcast(this);
}
}
void USpineSkeletonComponent::CheckState() {
bool needsUpdate = lastAtlas != Atlas || lastData != SkeletonData;
if (!needsUpdate) {
// Are we doing a re-import? Then check if the underlying spine-cpp data
// has changed.
if (lastAtlas && lastAtlas == Atlas && lastData && lastData == SkeletonData) {
spine::Atlas *atlas = Atlas->GetAtlas();
if (lastSpineAtlas != atlas) {
needsUpdate = true;
}
if (skeleton && &skeleton->getData() != SkeletonData->GetSkeletonData(atlas)) {
needsUpdate = true;
}
}
}
if (needsUpdate) {
DisposeState();
if (Atlas && SkeletonData) {
spine::SkeletonData *data = SkeletonData->GetSkeletonData(Atlas->GetAtlas());
skeleton = new (__FILE__, __LINE__) Skeleton(*data);
}
lastAtlas = Atlas;
lastSpineAtlas = Atlas ? Atlas->GetAtlas() : nullptr;
lastData = SkeletonData;
}
}
void USpineSkeletonComponent::DisposeState() {
if (skeleton) {
delete skeleton;
skeleton = nullptr;
}
}
void USpineSkeletonComponent::FinishDestroy() {
DisposeState();
Super::FinishDestroy();
}
#undef LOCTEXT_NAMESPACE