A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VRButtonComponent.h
Go to the documentation of this file.
1// Fill out your copyright notice in the Description page of Project Settings.
2
3#pragma once
4
5#include "CoreMinimal.h"
7#include "MotionControllerComponent.h"
8#include "VRGripInterface.h"
9#include "Components/StaticMeshComponent.h"
11//#include "VRBPDatatypes.h"
12//#include "VRExpansionFunctionLibrary.h"
13#include "VRButtonComponent.generated.h"
14
19// VR Button Types
20UENUM(Blueprintable)
21enum class EVRButtonType : uint8
22{
26};
27
28// VR Button SyncOptions
29UENUM(Blueprintable)
30enum class EVRStateChangeAuthorityType : uint8
31{
32 /* Button state can be changed on all connections */
34 /* Button state can be changed only on the server */
36 /* Button state can be changed only on the owner of the interacting primitive */
38};
39
41DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FVRButtonStateChangedSignature, bool, ButtonState, AActor *, InteractingActor, UPrimitiveComponent *, InteractingComponent);
42
44DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FVRButtonStartedInteractionSignature, AActor *, InteractingActor, UPrimitiveComponent *, InteractingComponent);
45
46UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = (VRExpansionPlugin))
47class VREXPANSIONPLUGIN_API UVRButtonComponent : public UStaticMeshComponent
48{
49 GENERATED_BODY()
51public:
52 UVRButtonComponent(const FObjectInitializer& ObjectInitializer);
53
54
57 UFUNCTION()
58 void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
59
60 UFUNCTION()
61 void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
62
63 virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
64 virtual void BeginPlay() override;
65
66 UFUNCTION(BlueprintPure, Category = "VRButtonComponent")
67 bool IsButtonInUse();
68
69 // Should be called after the button is moved post begin play
70 UFUNCTION(BlueprintCallable, Category = "VRButtonComponent")
71 void ResetInitialButtonLocation();
72
73 // Sets the button state outside of interaction, bSnapIntoPosition is for Toggle_Stay mode, it will lerp into the new position if this is false
74 UFUNCTION(BlueprintCallable, Category = "VRButtonComponent")
75 void SetButtonState(bool bNewButtonState, bool bCallButtonChangedEvent = true, bool bSnapIntoPosition = false);
76
77 // Resets the button to its resting location (mostly for Toggle_Stay)
78 UFUNCTION(BlueprintCallable, Category = "VRButtonComponent")
79 void SetButtonToRestingPosition(bool bLerpToPosition = false);
80
81 // On the button state changing, keep in mind that InteractingActor can be invalid if manually setting the state
82 UPROPERTY(BlueprintAssignable, Category = "VRButtonComponent")
83 FVRButtonStateChangedSignature OnButtonStateChanged;
84
85 // On the button state changing, keep in mind that InteractingActor can be invalid if manually setting the state
86 UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Button State Changed"))
87 void ReceiveButtonStateChanged(bool bCurButtonState, AActor * LastInteractingActor, UPrimitiveComponent * InteractingComponent);
88
89 // On Button beginning interaction (may spam a bit depending on if overlap is jittering)
90 UPROPERTY(BlueprintAssignable, Category = "VRButtonComponent")
91 FVRButtonStartedInteractionSignature OnButtonBeginInteraction;
92
93 // On Button ending interaction (may spam a bit depending on if overlap is jittering)
94 UPROPERTY(BlueprintAssignable, Category = "VRButtonComponent")
95 FVRButtonStartedInteractionSignature OnButtonEndInteraction;
96
97 // On Button beginning interaction (may spam a bit depending on if overlap is jittering)
98 UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Button Started Interaction"))
99 void ReceiveButtonBeginInteraction(AActor * InteractingActor, UPrimitiveComponent * InteractingComponent);
100
101 // On Button ending interaction (may spam a bit depending on if overlap is jittering)
102 UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Button Ended Interaction"))
103 void ReceiveButtonEndInteraction(AActor * LastInteractingActor, UPrimitiveComponent * LastInteractingComponent);
104
105 // On the button state changing, keep in mind that InteractingActor can be invalid if manually setting the state
106 UPROPERTY(BlueprintReadOnly, Category = "VRButtonComponent")
107 TWeakObjectPtr<UPrimitiveComponent> LocalInteractingComponent;
108
109 UPROPERTY(BlueprintReadOnly, Category = "VRButtonComponent")
110 TWeakObjectPtr<AActor> LocalLastInteractingActor;
112 UPROPERTY(BlueprintReadOnly, Category = "VRButtonComponent")
113 TWeakObjectPtr<UPrimitiveComponent> LocalLastInteractingComponent;
114
115 // Whether the button is enabled or not (can be interacted with)
116 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
117 bool bIsEnabled;
119 // Current state of the button, writable to set initial value
120 UPROPERTY(EditAnywhere,BlueprintReadWrite, Replicated, Category = "VRButtonComponent")
121 bool bButtonState;
122
123 // Who is allowed to change the button state
124 UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "VRButtonComponent|Replication")
125 EVRStateChangeAuthorityType StateChangeAuthorityType;
126
127 // Speed that the button de-presses when no longer interacted with
128 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
129 float DepressSpeed;
130
131 // Distance that the button depresses
132 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
133 float DepressDistance;
134
135 // Type of button this is
136 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
138
139 // Negative on this axis is the depress direction
140 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
141 EVRInteractibleAxis ButtonAxis;
142
143 // Depth at which the button engages (switches)
144 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
145 float ButtonEngageDepth;
147 // Minimum time before the button can be switched again
148 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
149 float MinTimeBetweenEngaging;
150
151 // Skips filtering overlaps on the button and lets you manage it yourself, this is the alternative to overriding IsValidOverlap
152 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
153 bool bSkipOverlapFiltering;
154
155 UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "VRButtonComponent")
156 bool IsValidOverlap(UPrimitiveComponent * OverlapComponent);
157
158 // Sets the Last interacting actor variable
159 void SetLastInteractingActor();
160
161 virtual FVector GetTargetRelativeLocation();
162
163 // Overrides the default of : true and allows for controlling it like in an actor, should be default of off normally with grippable components
164 UPROPERTY(EditAnywhere, Replicated, BlueprintReadWrite, Category = "VRGripInterface|Replication")
165 bool bReplicateMovement;
166
167 virtual void PreReplication(IRepChangedPropertyTracker & ChangedPropertyTracker) override;
168
169 // Resetting the initial transform here so that it comes in prior to BeginPlay and save loading.
170 virtual void OnRegister() override;
171
172 // Now replicating this so that it works correctly over the network
173 UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_InitialRelativeTransform, Category = "VRButtonComponent")
174 FTransform_NetQuantize InitialRelativeTransform;
175
176 UFUNCTION()
177 virtual void OnRep_InitialRelativeTransform()
178 {
179 SetButtonToRestingPosition();
180 }
181
182protected:
183
184 // Control variables
185 FVector InitialLocation;
186 bool bToggledThisTouch;
187 FVector InitialComponentLoc;
188 float LastToggleTime;
189
190 inline float GetAxisValue(FVector CheckLocation)
192 switch (ButtonAxis)
193 {
195 return CheckLocation.X; break;
197 return CheckLocation.Y; break;
199 return CheckLocation.Z; break;
200 default:return 0.0f; break;
201 }
202 }
203
204 inline FVector SetAxisValue(float SetValue)
205 {
206 FVector vec = FVector::ZeroVector;
208 switch (ButtonAxis)
209 {
211 vec.X = SetValue; break;
213 vec.Y = SetValue; break;
215 vec.Z = SetValue; break;
216 }
217
218 return vec;
219 }
220
221};
ButtonType
UENUM()
EVRButtonType
UENUM(Blueprintable)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FVRButtonStartedInteractionSignature, AActor *, InteractingActor, UPrimitiveComponent *, InteractingComponent)
EVRStateChangeAuthorityType
UENUM(Blueprintable)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FVRButtonStateChangedSignature, bool, ButtonState, AActor *, InteractingActor, UPrimitiveComponent *, InteractingComponent)
EVRInteractibleAxis
UENUM(Blueprintable)
UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = (VRExpansionPlugin))
TWeakObjectPtr< AActor > LocalLastInteractingActor
UPROPERTY(BlueprintReadOnly, Category = "VRButtonComponent")
EVRStateChangeAuthorityType StateChangeAuthorityType
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "VRButtonComponent|Replication")
bool bReplicateMovement
UPROPERTY(EditAnywhere, Replicated, BlueprintReadWrite, Category = "VRGripInterface|Replication")
float DepressDistance
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
FVRButtonStartedInteractionSignature OnButtonBeginInteraction
UPROPERTY(BlueprintAssignable, Category = "VRButtonComponent")
FTransform_NetQuantize InitialRelativeTransform
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_InitialRelativeTransform, Category = "VRButtonCo...
TWeakObjectPtr< UPrimitiveComponent > LocalInteractingComponent
UPROPERTY(BlueprintReadOnly, Category = "VRButtonComponent")
FVRButtonStateChangedSignature OnButtonStateChanged
UPROPERTY(BlueprintAssignable, Category = "VRButtonComponent")
virtual void OnRep_InitialRelativeTransform()
UFUNCTION()
float DepressSpeed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
EVRInteractibleAxis ButtonAxis
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
float ButtonEngageDepth
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
bool bIsEnabled
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
void ReceiveButtonStateChanged(bool bCurButtonState, AActor *LastInteractingActor, UPrimitiveComponent *InteractingComponent)
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Button State Changed"))
float GetAxisValue(FVector CheckLocation)
TWeakObjectPtr< UPrimitiveComponent > LocalLastInteractingComponent
UPROPERTY(BlueprintReadOnly, Category = "VRButtonComponent")
bool bSkipOverlapFiltering
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
FVRButtonStartedInteractionSignature OnButtonEndInteraction
UPROPERTY(BlueprintAssignable, Category = "VRButtonComponent")
float MinTimeBetweenEngaging
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
void ReceiveButtonBeginInteraction(AActor *InteractingActor, UPrimitiveComponent *InteractingComponent)
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Button Started Interaction"))
void ReceiveButtonEndInteraction(AActor *LastInteractingActor, UPrimitiveComponent *LastInteractingComponent)
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Button Ended Interaction"))
EVRButtonType ButtonType
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRButtonComponent")
FVector SetAxisValue(float SetValue)
bool IsValidOverlap(UPrimitiveComponent *OverlapComponent)
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "VRButtonComponent")
bool bButtonState
UPROPERTY(EditAnywhere,BlueprintReadWrite, Replicated, Category = "VRButtonComponent")
USTRUCT(BlueprintType, Category = "VRExpansionLibrary|TransformNetQuantize", meta = (HasNativeMake = ...