A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VRGlobalSettings.cpp
Go to the documentation of this file.
1
2#include "VRGlobalSettings.h"
4
5#if WITH_CHAOS
6#include "Chaos/ChaosConstraintSettings.h"
7#endif
8
9UVRGlobalSettings::UVRGlobalSettings(const FObjectInitializer& ObjectInitializer)
10 : Super(ObjectInitializer),
11 bLerpHybridWithSweepGrips(false),
12 bOnlyLerpHybridRotation(false),
13 HybridWithSweepLerpDuration(0.2f),
14 bUseGlobalLerpToHand(false),
15 bSkipLerpToHandIfHeld(false),
16 MinDistanceForLerp(10.0f),
17 LerpDuration(0.25f),
18 MinSpeedForLerp(100.f),
19 MaxSpeedForLerp(500.f),
20 LerpInterpolationMode(EVRLerpInterpolationMode::QuatInterp),
21 bUseCurve(false),
22 MaxCCDPasses(1),
23 OneEuroMinCutoff(0.1f),
24 OneEuroCutoffSlope(10.0f),
25 OneEuroDeltaCutoff(10.0f),
26 CurrentControllerProfileInUse(NAME_None),
27 CurrentControllerProfileTransform(FTransform::Identity),
28 bUseSeperateHandTransforms(false),
29 CurrentControllerProfileTransformRight(FTransform::Identity)
30{
31 DefaultGrippableCharacterMeshComponentClass = UGrippableSkeletalMeshComponent::StaticClass();
32
33#if WITH_CHAOS
34 LinearDriveStiffnessScale = Chaos::ConstraintSettings::LinearDriveStiffnessScale();
35 LinearDriveDampingScale = Chaos::ConstraintSettings::LinearDriveDampingScale();
36 AngularDriveStiffnessScale = Chaos::ConstraintSettings::AngularDriveStiffnessScale();
37 AngularDriveDampingScale = Chaos::ConstraintSettings::AngularDriveDampingScale();
38#endif
39}
40
41TSubclassOf<class UGrippableSkeletalMeshComponent> UVRGlobalSettings::GetDefaultGrippableCharacterMeshComponentClass()
42{
43 const UVRGlobalSettings* VRSettings = GetDefault<UVRGlobalSettings>();
44
45 if (VRSettings)
46 {
47 // Using a getter to stay safe from bricking peoples projects if they set it to none somehow
48 if (VRSettings->DefaultGrippableCharacterMeshComponentClass != nullptr)
49 {
51 }
52 }
53
54 return UGrippableSkeletalMeshComponent::StaticClass();
55}
56
57
59{
60 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
61 return VRSettings.bUseGlobalLerpToHand;
62}
63
64FTransform UVRGlobalSettings::AdjustTransformByControllerProfile(FName OptionalControllerProfileName, const FTransform& SocketTransform, bool bIsRightHand)
65{
66 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
67
68 if (OptionalControllerProfileName == NAME_None)
69 {
70 if (VRSettings.CurrentControllerProfileInUse != NAME_None)
71 {
72 // Use currently loaded transform
73 return SocketTransform * (((bIsRightHand && VRSettings.bUseSeperateHandTransforms) ? VRSettings.CurrentControllerProfileTransformRight : VRSettings.CurrentControllerProfileTransform));
74 }
75
76 // No override and no default, return base transform back
77 return SocketTransform;
78 }
79
80 // Had an override, find it if possible and use its transform
81 const FBPVRControllerProfile* FoundProfile = VRSettings.ControllerProfiles.FindByPredicate([OptionalControllerProfileName](const FBPVRControllerProfile& ArrayItem)
82 {
83 return ArrayItem.ControllerName == OptionalControllerProfileName;
84 });
85
86 if (FoundProfile)
87 {
88 return SocketTransform * (((bIsRightHand && VRSettings.bUseSeperateHandTransforms) ? FoundProfile->SocketOffsetTransformRightHand : FoundProfile->SocketOffsetTransform));
89 }
90
91 // Couldn't find it, return base transform
92 return SocketTransform;
93}
94
95
96FTransform UVRGlobalSettings::AdjustTransformByGivenControllerProfile(UPARAM(ref) FBPVRControllerProfile& ControllerProfile, const FTransform& SocketTransform, bool bIsRightHand)
97{
98 // Use currently loaded transform
99 return SocketTransform * (((bIsRightHand && ControllerProfile.bUseSeperateHandOffsetTransforms) ? ControllerProfile.SocketOffsetTransformRightHand : ControllerProfile.SocketOffsetTransform));
100
101 // Couldn't find it, return base transform
102 return SocketTransform;
103}
104
105TArray<FBPVRControllerProfile> UVRGlobalSettings::GetControllerProfiles()
106{
107 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
108
109 return VRSettings.ControllerProfiles;
110}
111
112void UVRGlobalSettings::OverwriteControllerProfile(UPARAM(ref)FBPVRControllerProfile& OverwritingProfile, bool bSaveOutToConfig)
113{
114 UVRGlobalSettings& VRSettings = *GetMutableDefault<UVRGlobalSettings>();
115
116 for (int i = 0; i < VRSettings.ControllerProfiles.Num(); ++i)
117 {
118 if (VRSettings.ControllerProfiles[i].ControllerName == OverwritingProfile.ControllerName)
119 {
120 VRSettings.ControllerProfiles[i] = OverwritingProfile;
121 }
122 }
123
124 if (bSaveOutToConfig)
126}
127
128void UVRGlobalSettings::AddControllerProfile(UPARAM(ref)FBPVRControllerProfile& NewProfile, bool bSaveOutToConfig)
129{
130 UVRGlobalSettings& VRSettings = *GetMutableDefault<UVRGlobalSettings>();
131
132 VRSettings.ControllerProfiles.Add(NewProfile);
133
134 if (bSaveOutToConfig)
136}
137
138void UVRGlobalSettings::DeleteControllerProfile(FName ControllerProfileName, bool bSaveOutToConfig)
139{
140 UVRGlobalSettings& VRSettings = *GetMutableDefault<UVRGlobalSettings>();
141
142 for (int i = VRSettings.ControllerProfiles.Num() - 1; i >= 0; --i)
143 {
144 if (VRSettings.ControllerProfiles[i].ControllerName == ControllerProfileName)
145 {
146 VRSettings.ControllerProfiles.RemoveAt(i);
147 }
148 }
149
150 if (bSaveOutToConfig)
152}
153
155{
156 UVRGlobalSettings& VRSettings = *GetMutableDefault<UVRGlobalSettings>();
157 VRSettings.SaveConfig();
158
159 //VRSettings.SaveConfig(CPF_Config, *VRSettings.GetGlobalUserConfigFilename());//VRSettings.GetDefaultConfigFilename());
160}
161
162
163FName UVRGlobalSettings::GetCurrentProfileName(bool& bHadLoadedProfile)
164{
165 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
166
167 bHadLoadedProfile = VRSettings.CurrentControllerProfileInUse != NAME_None;
168 return VRSettings.CurrentControllerProfileInUse;
169}
170
172{
173 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
174
175 FName ControllerProfileName = VRSettings.CurrentControllerProfileInUse;
176 const FBPVRControllerProfile* FoundProfile = VRSettings.ControllerProfiles.FindByPredicate([ControllerProfileName](const FBPVRControllerProfile& ArrayItem)
177 {
178 return ArrayItem.ControllerName == ControllerProfileName;
179 });
180
181 bHadLoadedProfile = FoundProfile != nullptr;
182
183 if (bHadLoadedProfile)
184 {
185 return *FoundProfile;
186 }
187 else
188 return FBPVRControllerProfile();
189}
190
191bool UVRGlobalSettings::GetControllerProfile(FName ControllerProfileName, FBPVRControllerProfile& OutProfile)
192{
193 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
194
195 const FBPVRControllerProfile* FoundProfile = VRSettings.ControllerProfiles.FindByPredicate([ControllerProfileName](const FBPVRControllerProfile& ArrayItem)
196 {
197 return ArrayItem.ControllerName == ControllerProfileName;
198 });
199
200 if (FoundProfile)
201 {
202 OutProfile = *FoundProfile;
203 return true;
204 }
205
206 return false;
207}
208
209bool UVRGlobalSettings::LoadControllerProfileByName(FName ControllerProfileName, bool bSetAsCurrentProfile)
210{
211 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
212
213 const FBPVRControllerProfile* FoundProfile = VRSettings.ControllerProfiles.FindByPredicate([ControllerProfileName](const FBPVRControllerProfile& ArrayItem)
214 {
215 return ArrayItem.ControllerName == ControllerProfileName;
216 });
217
218 if (FoundProfile)
219 {
220 return LoadControllerProfile(*FoundProfile, bSetAsCurrentProfile);
221 }
222
223
224 UE_LOG(LogTemp, Warning, TEXT("Could not find controller profile!: %s"), *ControllerProfileName.ToString());
225 return false;
226}
227
228bool UVRGlobalSettings::LoadControllerProfile(const FBPVRControllerProfile& ControllerProfile, bool bSetAsCurrentProfile)
229{
230
231 /*
232 UInputSettings* InputSettings = GetMutableDefault<UInputSettings>();
233 if (false)//InputSettings != nullptr)
234 {
235 if (ControllerProfile.ActionOverrides.Num() > 0)
236 {
237 // Load button mappings
238 for (auto& Elem : ControllerProfile.ActionOverrides)
239 {
240 FName ActionName = Elem.Key;
241 FActionMappingDetails Mapping = Elem.Value;
242
243 // We allow for 0 mapped actions here in case you want to delete one
244 if (ActionName == NAME_None)
245 continue;
246
247 const TArray<FInputActionKeyMapping>& ActionMappings = InputSettings->GetActionMappings();
248 for (int32 ActionIndex = ActionMappings.Num() - 1; ActionIndex >= 0; --ActionIndex)
249 {
250 if (ActionMappings[ActionIndex].ActionName == ActionName)
251 {
252 InputSettings->RemoveActionMapping(ActionMappings[ActionIndex], false);
253 // we don't break because the mapping may have been in the array twice
254 }
255 }
256
257 // Then add the new bindings
258 for (FInputActionKeyMapping &KeyMapping : Mapping.ActionMappings)
259 {
260 // By default the key mappings don't have an action name, add them here
261 KeyMapping.ActionName = ActionName;
262 InputSettings->AddActionMapping(KeyMapping, false);
263 }
264 }
265 }
266
267 if (ControllerProfile.AxisOverrides.Num() > 0)
268 {
269 // Load axis mappings
270 for (auto& Elem : ControllerProfile.AxisOverrides)
271 {
272 FName AxisName = Elem.Key;
273 FAxisMappingDetails Mapping = Elem.Value;
274
275 // We allow for 0 mapped Axis's here in case you want to delete one
276 if (AxisName == NAME_None)
277 continue;
278
279 const TArray<FInputAxisKeyMapping>& AxisMappings = InputSettings->GetAxisMappings();
280
281 // Clear all Axis's that use our Axis name first
282 for (int32 AxisIndex = AxisMappings.Num() - 1; AxisIndex >= 0; --AxisIndex)
283 {
284 if (AxisMappings[AxisIndex].AxisName == AxisName)
285 {
286 InputSettings->RemoveAxisMapping(AxisMappings[AxisIndex], false);
287 // we don't break because the mapping may have been in the array twice
288 }
289 }
290
291 // Then add the new bindings
292 for (FInputAxisKeyMapping &KeyMapping : Mapping.AxisMappings)
293 {
294 // By default the key mappings don't have an Axis name, add them here
295 KeyMapping.AxisName = AxisName;
296 InputSettings->AddAxisMapping(KeyMapping, false);
297 }
298 }
299 }
300
301 if (ControllerProfile.ActionOverrides.Num() > 0 || ControllerProfile.AxisOverrides.Num() > 0)
302 {
303 // Tell all players to use the new keymappings
304 InputSettings->ForceRebuildKeymaps();
305 }
306 }*/
307
308 if (bSetAsCurrentProfile)
309 {
310 UVRGlobalSettings* VRSettings = GetMutableDefault<UVRGlobalSettings>();
311 if (VRSettings)
312 {
313 VRSettings->CurrentControllerProfileInUse = ControllerProfile.ControllerName;
314 VRSettings->CurrentControllerProfileTransform = ControllerProfile.SocketOffsetTransform;
315 ensure(!VRSettings->CurrentControllerProfileTransform.ContainsNaN());
316 VRSettings->bUseSeperateHandTransforms = ControllerProfile.bUseSeperateHandOffsetTransforms;
318 ensure(!VRSettings->CurrentControllerProfileTransformRight.ContainsNaN());
319 VRSettings->OnControllerProfileChangedEvent.Broadcast();
320 }
321 else
322 return false;
323 }
324
325
326 // Not saving key mapping in purpose, app will revert to default on next load and profiles will load custom changes
327 return true;
328}
329
331{
332#if WITH_EDITOR
333 // Not doing this currently, loading defaults is cool, and I may go back to it later when i get
334 // controller offsets for vive/touch/MR vs each other.
335 /*if (ControllerProfiles.Num() == 0)
336 {
337 ControllerProfiles.Add(FBPVRControllerProfile(TEXT("Vive_Wands")));
338 ControllerProfiles.Add(FBPVRControllerProfile(TEXT("Oculus_Touch"), ControllerProfileStatics::OculusTouchStaticOffset));
339 this->SaveConfig(CPF_Config, *this->GetDefaultConfigFilename());
340 }*/
341#endif
342 Super::PostInitProperties();
343}
344
345void UVRGlobalSettings::GetMeleeSurfaceGlobalSettings(TArray<FBPHitSurfaceProperties>& OutMeleeSurfaceSettings)
346{
347 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
348 OutMeleeSurfaceSettings = VRSettings.MeleeSurfaceSettings;
349}
350
352{
353 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
354
356 OutVirtualStockSettings.StockSnapDistance = VRSettings.VirtualStockSettings.StockSnapDistance;
357 OutVirtualStockSettings.StockSnapOffset = VRSettings.VirtualStockSettings.StockSnapOffset;
358 OutVirtualStockSettings.bSmoothStockHand = VRSettings.VirtualStockSettings.bSmoothStockHand;
359 OutVirtualStockSettings.SmoothingValueForStock = VRSettings.VirtualStockSettings.SmoothingValueForStock;
360}
361
363{
364 UVRGlobalSettings& VRSettings = *GetMutableDefault<UVRGlobalSettings>();
365 VRSettings.VirtualStockSettings = NewVirtualStockSettings;
366
367 VRSettings.SaveConfig();
368}
EVRLerpInterpolationMode
UENUM(BlueprintType)
UCLASS(config = Engine, defaultconfig)
static bool LoadControllerProfileByName(FName ControllerProfileName, bool bSetAsCurrentProfile=true)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles")
float LinearDriveDampingScale
UPROPERTY(config, BlueprintReadWrite, EditAnywhere, Category = "ChaosPhysics")
static void SaveControllerProfiles()
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles|Operations")
float AngularDriveDampingScale
UPROPERTY(config, BlueprintReadWrite, EditAnywhere, Category = "ChaosPhysics")
TArray< FBPHitSurfaceProperties > MeleeSurfaceSettings
UPROPERTY(config, EditAnywhere, Category = "MeleeSettings")
UVRGlobalSettings(const FObjectInitializer &ObjectInitializer)
FVRControllerProfileChangedEvent OnControllerProfileChangedEvent
static TSubclassOf< class UGrippableSkeletalMeshComponent > GetDefaultGrippableCharacterMeshComponentClass()
FBPVirtualStockSettings VirtualStockSettings
UPROPERTY(config, EditAnywhere, Category = "GunSettings")
static FTransform AdjustTransformByControllerProfile(FName OptionalControllerProfileName, const FTransform &SocketTransform, bool bIsRightHand=false)
UFUNCTION(BlueprintPure, Category = "VRControllerProfiles")
static FBPVRControllerProfile GetCurrentProfile(bool &bHadLoadedProfile)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles")
static bool GetControllerProfile(FName ControllerProfileName, FBPVRControllerProfile &OutProfile)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles")
TArray< FBPVRControllerProfile > ControllerProfiles
UPROPERTY(config, EditAnywhere, Category = "ControllerProfiles")
static void SaveVirtualStockGlobalSettings(FBPVirtualStockSettings NewVirtualStockSettings)
UFUNCTION(BlueprintCallable, Category = "GunSettings|VirtualStock")
static FTransform AdjustTransformByGivenControllerProfile(UPARAM(ref) FBPVRControllerProfile &ControllerProfile, const FTransform &SocketTransform, bool bIsRightHand=false)
UFUNCTION(BlueprintPure, Category = "VRControllerProfiles")
static void GetMeleeSurfaceGlobalSettings(TArray< FBPHitSurfaceProperties > &OutMeleeSurfaceSettings)
UFUNCTION(BlueprintCallable, Category = "MeleeSettings")
static void GetVirtualStockGlobalSettings(FBPVirtualStockSettings &OutVirtualStockSettings)
UFUNCTION(BlueprintCallable, Category = "GunSettings|VirtualStock")
FTransform CurrentControllerProfileTransform
float AngularDriveStiffnessScale
UPROPERTY(config, BlueprintReadWrite, EditAnywhere, Category = "ChaosPhysics")
bool bUseGlobalLerpToHand
UPROPERTY(config, BlueprintReadWrite, EditAnywhere, Category = "GlobalLerpToHand")
static bool IsGlobalLerpEnabled()
UFUNCTION(BlueprintPure, Category = "GlobalLerpToHand")
static void DeleteControllerProfile(FName ControllerProfileName, bool bSaveOutToConfig=true)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles|Operations")
TSubclassOf< class UGrippableSkeletalMeshComponent > DefaultGrippableCharacterMeshComponentClass
UPROPERTY(config, BlueprintReadWrite, EditAnywhere, Category = "Misc")
static bool LoadControllerProfile(const FBPVRControllerProfile &ControllerProfile, bool bSetAsCurrentProfile=true)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles")
static void AddControllerProfile(UPARAM(ref) FBPVRControllerProfile &NewProfile, bool bSaveOutToConfig=true)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles|Operations")
float LinearDriveStiffnessScale
UPROPERTY(config, BlueprintReadWrite, EditAnywhere, Category = "ChaosPhysics")
static TArray< FBPVRControllerProfile > GetControllerProfiles()
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles")
virtual void PostInitProperties() override
static FName GetCurrentProfileName(bool &bHadLoadedProfile)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles")
static void OverwriteControllerProfile(UPARAM(ref) FBPVRControllerProfile &OverwritingProfile, bool bSaveOutToConfig=true)
UFUNCTION(BlueprintCallable, Category = "VRControllerProfiles|Operations")
FTransform CurrentControllerProfileTransformRight
USTRUCT(BlueprintType, Category = "ControllerProfiles")
FTransform_NetQuantize SocketOffsetTransformRightHand
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ControllerProfiles", meta = (editcondition = ...
FName ControllerName
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ControllerProfiles")
FTransform_NetQuantize SocketOffsetTransform
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ControllerProfiles")
bool bUseSeperateHandOffsetTransforms
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ControllerProfiles")
USTRUCT(BlueprintType, Category = "GunSettings")
Definition GS_GunTools.h:22
bool bSmoothStockHand
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VirtualStock|Smoothing")
Definition GS_GunTools.h:81
FVector_NetQuantize100 StockSnapOffset
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VirtualStock")
Definition GS_GunTools.h:65
float StockSnapDistance
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VirtualStock")
Definition GS_GunTools.h:40
bool bUseDistanceBasedStockSnapping
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VirtualStock")
Definition GS_GunTools.h:32
float SmoothingValueForStock
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VirtualStock|Smoothing", meta = (editconditio...
Definition GS_GunTools.h:90