Documentation for the Unreal C++ Plugin
Loading...
Searching...
No Matches
BaseWristMenu.cpp
Go to the documentation of this file.
1// Copyright(c) 2018 PixoVR, LLC. All Rights Reserved.
2
3
8#include "Kismet/GameplayStatics.h"
9
11
13{
14 PrimaryActorTick.bCanEverTick = true;
15
16 WristMenuButton = CreateDefaultSubobject<UChildActorComponent>(TEXT("WristMenuButton"));
17 SetRootComponent(WristMenuButton);
18
19 WristMenuWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("WristMenuWidget"));
20 WristMenuWidget->SetupAttachment(RootComponent);
21
22 PointingFingerCollisionComp = CreateDefaultSubobject<UBoxComponent>("PointingFingerCollision");
24
25 /*
26 PointingFingerCollisionComp->OnComponentBeginOverlap.AddDynamic(this,
27 &ABaseWristMenu::OnBeginOverlapPointingFingerCollision);
28 PointingFingerCollisionComp->OnComponentEndOverlap.AddDynamic(this,
29 &ABaseWristMenu::OnEndOverlapPointingFingerCollision);
30 */
31}
32
34{
35 Super::BeginPlay();
36
38
39 if (ABaseWristMenuButton* WristMenuButtonActor = Cast<ABaseWristMenuButton>(WristMenuButton->GetChildActor()))
40 {
41 WristMenuButtonActor->OnWristMenuButtonPressed.AddDynamic(this, &ABaseWristMenu::OnWristMenuButtonPressed);
42 }
43
44 if (APixoVRHand* HandOwner = Cast<APixoVRHand>(GetParentActor()))
45 {
46 if(HandOwner->HandMotionSource == EControllerHand::Left)
47 {
48 // Adjusting relative location and rotation for the left hand
49 WristMenuWidget->SetRelativeLocation(WristMenuWidget->GetRelativeLocation() * FVector(1.0f, -1.0f, 1.0f));
50 USceneComponent* RootParent = RootComponent->GetAttachParent();
51 if (RootParent)
52 {
53 FVector RelativeLocation = RootParent->GetRelativeLocation();
54 RootParent->SetRelativeLocation(RelativeLocation * FVector(1.0f, -1.0f, 1.0f));
55 RootParent->AddRelativeRotation(FRotator(0.0f, 0.0f, 180.0f));
56 }
57 }
58 }
59
60 if (GetWorld())
61 CameraManagerRef = UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0);
62}
63
64void ABaseWristMenu::Tick(float DeltaTime)
65{
66 Super::Tick(DeltaTime);
67
68 FVector WristMenuActivationDirection;
70 WristMenuActivationDirection = GetActorLocation() - CameraManagerRef->GetCameraLocation();
71 WristMenuActivationDirection.Normalize();
72
73 const float WristMenuToActivationAngleDifference = FMath::RadiansToDegrees(FMath::Acos(FVector::DotProduct(GetActorUpVector() * -1.0f, WristMenuActivationDirection)));
74
75 bool bIsWristDirectionBetweenTolerance = WristMenuToActivationAngleDifference < WristAngleTolerance;
76
77 if (bCanOpenWristMenu && bIsWristDirectionBetweenTolerance && !IsWristMenuOpened())
78 {
79 if (!WristMenuWidget->GetWidget()->GetClass()->ImplementsInterface(UWristMenuWidgetInterface::StaticClass())
80 || IWristMenuWidgetInterface::Execute_IsWidgetShouldBeDisplayed(WristMenuWidget->GetWidget()))
81 {
82 bCanOpenWristMenu = false;
84 }
85 }
86 else if (!bCanOpenWristMenu && !bIsWristDirectionBetweenTolerance && IsWristMenuOpened())
87 {
89 }
90
91}
92
94{
95 if (!WristMenuButton)
96 return;
97
98 WristMenuButton->SetHiddenInGame(!IsVisible);
99 WristMenuButton->GetChildActor()->SetActorEnableCollision(IsVisible);
100 const ABaseWristMenuButton* BaseWristMenuButton = Cast<ABaseWristMenuButton>(WristMenuButton->GetChildActor());
101 if (BaseWristMenuButton)
102 {
103 BaseWristMenuButton->PointingFingerCollisionComp->SetCollisionEnabled(IsVisible ?
104 ECollisionEnabled::QueryOnly : ECollisionEnabled::NoCollision);
105 }
106}
107
109{
110 if (!WristMenuWidget)
111 return;
112
113 WristMenuWidget->SetHiddenInGame(!IsVisible);
114 WristMenuWidget->SetCollisionEnabled(IsVisible ? ECollisionEnabled::QueryOnly : ECollisionEnabled::NoCollision);
115 PointingFingerCollisionComp->SetCollisionEnabled(IsVisible ?
116 ECollisionEnabled::QueryOnly : ECollisionEnabled::NoCollision);
117}
118
120{
121 if (!WristMenuButton)
122 return;
123
124 if (WristMenuButton->IsVisible())
125 {
126 SetButtonVisibility(false);
127 SetUIVisibility(true);
129 }
130}
131
133{
134 bCanOpenWristMenu = true;
135 SetButtonVisibility(false);
136 SetUIVisibility(false);
137 if (WristMenuWidget->GetWidget()->GetClass()->ImplementsInterface(UWristMenuWidgetInterface::StaticClass()))
138 {
139 IWristMenuWidgetInterface::Execute_ResetWidget(WristMenuWidget->GetWidget());
140 }
141}
142
144{
145 return !WristMenuButton->bHiddenInGame || !WristMenuWidget->bHiddenInGame;
146}
147
148void ABaseWristMenu::OnBeginOverlapPointingFingerCollision(UPrimitiveComponent* OverlappedComponent,
149 AActor* OtherActor, UPrimitiveComponent* OtherComp,
150 int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
151{
153}
154
155void ABaseWristMenu::OnEndOverlapPointingFingerCollision(UPrimitiveComponent* OverlappedComponent,
156 AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
157{
159}
160
161void ABaseWristMenu::OnPointingFingerCollisionOverlapChange(AActor* OtherActor, bool IsBeginOverlap) const
162{
163 const APixoVRHand* Hand = Cast<APixoVRHand>(OtherActor);
164 if (Hand)
165 {
166 /* If overlapping hand doesn't hold object */
167 if (!Hand->HandHoldingObject && (!Hand->LaserMesh || !Hand->LaserMesh->IsVisible()))
168 {
169 /* Change hand animation and allow widget interaction using touch */
170 if (IsBeginOverlap)
171 {
172 IHandAnimationInterface::Execute_SetAnimationGripState(
173 Hand->HandMesh->GetAnimInstance(), EPixoVRGripState::PointFinger);
174 Hand->TouchComponent->Activate();
175 }
176 else
177 {
178 IHandAnimationInterface::Execute_SetAnimationGripState(
179 Hand->HandMesh->GetAnimInstance(), EPixoVRGripState::Open);
180 Hand->TouchComponent->Deactivate();
181 }
182 }
183 }
184}
Represents a wrist menu button actor.
UBoxComponent * PointingFingerCollisionComp
UPROPERTY(EditDefaultsOnly)
void SetButtonVisibility(bool IsVisible)
UFUNCTION(BlueprintCallable, Category = "WristMenu")
virtual void Tick(float DeltaTime) override
void SetUIVisibility(bool IsVisible)
UFUNCTION(BlueprintCallable, Category = "WristMenu")
void OnBeginOverlapPointingFingerCollision(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
Change hand animation to pointing finger.
APlayerCameraManager * CameraManagerRef
void OnWristMenuButtonPressed()
UFUNCTION(BlueprintCallable, Category = "WristMenu")
FOnWristMenuButtonPressedDelegate OnWristMenuButtonPressedDelegate
UPROPERTY(BlueprintAssignable, Category = "WristMenu")
UChildActorComponent * WristMenuButton
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "WristMenu")
void HideWristMenu()
UFUNCTION(BlueprintCallable, Category = "WristMenu")
static bool bCanOpenWristMenu
UWidgetComponent * WristMenuWidget
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "WristMenu")
bool IsWristMenuOpened()
UFUNCTION(BlueprintCallable, Category = "WristMenu")
UBoxComponent * PointingFingerCollisionComp
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly)
void OnEndOverlapPointingFingerCollision(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
Change hand animation to default.
virtual void BeginPlay() override
void OnPointingFingerCollisionOverlapChange(AActor *OtherActor, bool IsBeginOverlap) const
Change hand animation.
float WristAngleTolerance
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "WristMenu")
APixoVRHand is an actor class that represents a hand in the VR environment.
Definition PixoVRHand.h:99
UWidgetInteractionComponent * TouchComponent
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
Definition PixoVRHand.h:172
UStaticMeshComponent * LaserMesh
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Components")
Definition PixoVRHand.h:136
USkeletalMeshComponent * HandMesh
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Components")
Definition PixoVRHand.h:130
UObject * HandHoldingObject
UPROPERTY(BlueprintReadOnly, Replicated, Category = "PixoVR")
Definition PixoVRHand.h:250