diff --git a/Source/SenseGlove/Private/SenseGlove/Components/SGVirtualHandComponent.cpp b/Source/SenseGlove/Private/SenseGlove/Components/SGVirtualHandComponent.cpp index 6bc0696b..cf7c0984 100644 --- a/Source/SenseGlove/Private/SenseGlove/Components/SGVirtualHandComponent.cpp +++ b/Source/SenseGlove/Private/SenseGlove/Components/SGVirtualHandComponent.cpp @@ -41,8 +41,11 @@ #include "UObject/ConstructorHelpers.h" #include "SenseGlove/Animation/SGVirtualHandAnimInstance.h" +#include "SGConnect/SGConnect.h" +#include "SGCore/SGDeviceList.h" #include "SGCore/SGHandPose.h" #include "SGCore/SGHapticGlove.h" +#include "SGCore/SGHapticGloveHandProfiles.h" #include "SGLog/SGLog.h" struct USGVirtualHandComponent::FImpl @@ -115,6 +118,8 @@ struct USGVirtualHandComponent::FImpl const FReferenceSkeleton& GetRefSkeleton() const; bool CheckGlove() const; + + void SetVisibility(bool bVisible) const; }; const TArray>& USGVirtualHandComponent::GetLeftHandFingerBoneNames() @@ -203,6 +208,7 @@ USGVirtualHandComponent::USGVirtualHandComponent(const FObjectInitializer& Objec TEXT("Failed to load the skeletal mesh: '%s'"), *SkeletalMeshPath); Super::SetSkeletalMesh(SkeletalMesh.Object, true); + bSGConnectInitialized = false; Glove = nullptr; } @@ -252,6 +258,8 @@ void USGVirtualHandComponent::InitializeComponent() { Pimpl->SetDefaultMesh(); } + + (void) Pimpl->CheckGlove(); } void USGVirtualHandComponent::TickComponent( @@ -270,6 +278,13 @@ void USGVirtualHandComponent::TickComponent( (void) Pimpl->CheckGlove(); } +void USGVirtualHandComponent::BeginPlay() +{ + Super::BeginPlay(); + + (void) Pimpl->CheckGlove(); +} + void USGVirtualHandComponent::SetSkeletalMesh(USkeletalMesh* NewMesh, const bool bReinitPose) { if (IsValid(NewMesh)) @@ -481,6 +496,24 @@ const FReferenceSkeleton& USGVirtualHandComponent::FImpl::GetRefSkeleton() const bool USGVirtualHandComponent::FImpl::CheckGlove() const { + if (!Owner->bSGConnectInitialized) + { + // Ensure the device list is empty before doing anything. + FSGDeviceList::Dispose(); + FSGDeviceList::Reinitialize(); + + // Load the latest hand profiles. + FSGHapticGloveHandProfiles::TryLoadingFromDisk(); + + const int32_t Result = FSGConnect::Init(); + if (Result != 1) + { + return false; + } + + Owner->bSGConnectInitialized = true; + } + const bool bGloveWasPresent = IsValid(Owner->Glove); const bool bFoundGlove = USGHapticGlove::GetGlove(Owner, Owner->IsRight(), Owner->Glove); @@ -490,9 +523,20 @@ bool USGVirtualHandComponent::FImpl::CheckGlove() const (Owner->bRight ? TEXT("right-handed") : TEXT("left-handed")))); } + if (!IsEditor() && Owner->IsVisible() != bFoundGlove) + { + SetVisibility(bFoundGlove); + } + return bFoundGlove; } +void USGVirtualHandComponent::FImpl::SetVisibility(const bool bVisible) const +{ + Owner->SetVisibility(bVisible, true); + Owner->SetHiddenInGame(!bVisible, false); +} + void USGVirtualHandComponent::FImplDeleter::operator()(const FImpl* P) const { delete P; diff --git a/Source/SenseGlove/Public/SenseGlove/Components/SGVirtualHandComponent.h b/Source/SenseGlove/Public/SenseGlove/Components/SGVirtualHandComponent.h index 5faddf32..b8186770 100644 --- a/Source/SenseGlove/Public/SenseGlove/Components/SGVirtualHandComponent.h +++ b/Source/SenseGlove/Public/SenseGlove/Components/SGVirtualHandComponent.h @@ -86,6 +86,9 @@ private: uint8 bHiddenInGameIfNoGloveDetected : 1; private: + UPROPERTY(Transient) + uint8 bSGConnectInitialized : 1; + UPROPERTY(Transient) USGHapticGlove* Glove; @@ -125,6 +128,8 @@ public: enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; + virtual void BeginPlay() override; + virtual void SetSkeletalMesh(USkeletalMesh* NewMesh, bool bReinitPose = true) override; protected: