A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
SteamVRKeyboardComponent.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"
6//#include "EngineMinimal.h"
7//#include "VRBPDatatypes.h"
9//#include "GripMotionControllerComponent.h"
10#include "Engine/Engine.h"
11
12#include "IXRTrackingSystem.h"
13#include "IHeadMountedDisplay.h"
14
15#include "SteamVRKeyboardComponent.generated.h"
16
17
18DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FVRKeyboardStringCallbackSignature, FString, Text);
19DECLARE_DYNAMIC_MULTICAST_DELEGATE(FVRKeyboardNullCallbackSignature);
20
25UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent))
26class OPENVREXPANSIONPLUGIN_API USteamVRKeyboardComponent : public USceneComponent
27{
28
29public:
30
31 GENERATED_BODY()
32
33public:
34 USteamVRKeyboardComponent(const FObjectInitializer& ObjectInitializer);
35
37
38 virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
39 virtual void OnUnregister() override;
40 // Keyboard Functions //
41
42#if STEAMVR_SUPPORTED_PLATFORM
43 FBPOpenVRKeyboardHandle KeyboardHandle;
44#endif
45
46 UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
47 FVRKeyboardStringCallbackSignature OnKeyboardDone;
48
49 UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
50 FVRKeyboardNullCallbackSignature OnKeyboardClosed;
51
52 UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
53 FVRKeyboardStringCallbackSignature OnKeyboardCharInput;
54
55
56 // Opens the vrkeyboard, can fail if already open or in use
57 UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true", ExpandEnumAsExecs = "Result"))
58 void OpenVRKeyboard(bool bIsForPassword, bool bIsMultiline, bool bUseMinimalMode, bool bIsRightHand, int32 MaxCharacters, FString Description, FString StartingString, EBPOVRResultSwitch & Result)
59 {
60#if !STEAMVR_SUPPORTED_PLATFORM
62 return;
63#else
64 if ( KeyboardHandle.IsValid())
65 {
67 return;
68 }
69
70 if (KeyboardHandle.IsValid())
71 {
73 return;
74 }
75
76 if (!GEngine->XRSystem.IsValid() || (GEngine->XRSystem->GetSystemName() != SteamVRSystemName))
77 {
79 return;
80 }
81
82 vr::IVROverlay* VROverlay = vr::VROverlay();
83
84 if (!VROverlay)
85 {
87 return;
88 }
89
90 vr::EVROverlayError OverlayError;
91 OverlayError = VROverlay->CreateOverlay("KeyboardOverlay", "Keyboard Overlay", &KeyboardHandle.VRKeyboardHandle);
92
93 if (OverlayError != vr::EVROverlayError::VROverlayError_None || !KeyboardHandle.IsValid())
94 {
95 KeyboardHandle.VRKeyboardHandle = vr::k_ulOverlayHandleInvalid;
97 return;
98 }
99
100 vr::EGamepadTextInputMode Inputmode = bIsForPassword ? vr::EGamepadTextInputMode::k_EGamepadTextInputModePassword : vr::EGamepadTextInputMode::k_EGamepadTextInputModeNormal;
101 vr::EGamepadTextInputLineMode LineInputMode = bIsMultiline ? vr::EGamepadTextInputLineMode::k_EGamepadTextInputLineModeMultipleLines : vr::EGamepadTextInputLineMode::k_EGamepadTextInputLineModeSingleLine;
102 uint32 HandInteracting = bIsRightHand ? 0 : 1;
103
104 if (bIsForPassword)
105 OverlayError = VROverlay->ShowKeyboardForOverlay(KeyboardHandle.VRKeyboardHandle, Inputmode, LineInputMode, TCHAR_TO_ANSI(*Description), MaxCharacters, TCHAR_TO_ANSI(*StartingString), bUseMinimalMode, HandInteracting);
106 else
107 OverlayError = VROverlay->ShowKeyboardForOverlay(KeyboardHandle.VRKeyboardHandle, Inputmode, LineInputMode, TCHAR_TO_ANSI(*Description), MaxCharacters, TCHAR_TO_ANSI(*StartingString), bUseMinimalMode, HandInteracting);
108
109 if (OverlayError != vr::EVROverlayError::VROverlayError_None)
110 {
111 VROverlay->DestroyOverlay(KeyboardHandle.VRKeyboardHandle);
112 KeyboardHandle.VRKeyboardHandle = vr::k_ulOverlayHandleInvalid;
114 return;
115 }
116
117 //VROverlay->SetOverlayAlpha(KeyboardHandle.VRKeyboardHandle, 0.0f); // Might need to remove this, keyboard would be invis?
118 VROverlay->ShowOverlay(KeyboardHandle.VRKeyboardHandle);
119
120 // // Set the position of the keyboard in world space
121 //virtual void SetKeyboardTransformAbsolute(ETrackingUniverseOrigin eTrackingOrigin, const HmdMatrix34_t *pmatTrackingOriginToKeyboardTransform) = 0;
122
123
124 // // Set the position of the keyboard in overlay space by telling it to avoid a rectangle in the overlay. Rectangle coords have (0,0) in the bottom left
125 //virtual void SetKeyboardPositionForOverlay(VROverlayHandle_t ulOverlayHandle, HmdRect2_t avoidRect) = 0;
126 //VROverlay->SetOverlayTransformAbsolute(,ETrackingUniverseOrigin::TrackingUniverseStanding,HMDMatrix)
127
128 //const float WorldToMeterScale = FMath::Max(GetWorldToMetersScale(), 0.1f);
129 //OVR_VERIFY(VROverlay->SetOverlayWidthInMeters(Layer.OverlayHandle, Layer.LayerDesc.QuadSize.X / WorldToMeterScale));
130 //OVR_VERIFY(VROverlay->SetOverlayTexelAspect(Layer.OverlayHandle, Layer.LayerDesc.QuadSize.X / Layer.LayerDesc.QuadSize.Y));
131 //OVR_VERIFY(VROverlay->SetOverlaySortOrder(Layer.OverlayHandle, Layer.LayerDesc.Priority));
132
133 this->SetComponentTickEnabled(true);
135#endif
136 }
137
138
139 // Closes the vrkeyboard, can fail if not already open
140 UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true", ExpandEnumAsExecs = "Result"))
141 void CloseVRKeyboard(EBPOVRResultSwitch & Result)
142 {
143#if !STEAMVR_SUPPORTED_PLATFORM
145 return;
146#else
147 if ( !KeyboardHandle.IsValid())
148 {
150 return;
151 }
152
153 if (!GEngine->XRSystem.IsValid() || (GEngine->XRSystem->GetSystemName() != SteamVRSystemName))
154 {
156 return;
157 }
158
159 vr::IVROverlay* VROverlay = vr::VROverlay();
160
161 if (!VROverlay)
162 {
164 return;
165 }
166
167 VROverlay->HideKeyboard();
168 VROverlay->HideOverlay(KeyboardHandle.VRKeyboardHandle);
169
170 vr::EVROverlayError OverlayError;
171 OverlayError = VROverlay->DestroyOverlay(KeyboardHandle.VRKeyboardHandle);
172 KeyboardHandle.VRKeyboardHandle = vr::k_ulOverlayHandleInvalid;
173 this->SetComponentTickEnabled(false);
175#endif
176 }
177
178
179 // Re-Opens the vr keyboard that is currently active, can be used for switching interacting hands and the like.
180 UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true", ExpandEnumAsExecs = "Result"))
181 void ReOpenVRKeyboardForUser(bool bIsForPassword, bool bIsMultiline, bool bUseMinimalMode, bool bIsRightHand, int32 MaxCharacters, FString Description, FString StartingString, EBPOVRResultSwitch & Result)
182 {
183#if !STEAMVR_SUPPORTED_PLATFORM
185 return;
186#else
187 if ( !KeyboardHandle.IsValid())
188 {
190 return;
191 }
192
193 if (!GEngine->XRSystem.IsValid() || (GEngine->XRSystem->GetSystemName() != SteamVRSystemName))
194 {
196 return;
197 }
198
199 vr::IVROverlay* VROverlay = vr::VROverlay();
200
201 if (!VROverlay)
204 return;
205 }
206
207 vr::EVROverlayError OverlayError;
208
209 VROverlay->HideKeyboard();
210
211 vr::EGamepadTextInputMode Inputmode = bIsForPassword ? vr::EGamepadTextInputMode::k_EGamepadTextInputModePassword : vr::EGamepadTextInputMode::k_EGamepadTextInputModeNormal;
212 vr::EGamepadTextInputLineMode LineInputMode = bIsMultiline ? vr::EGamepadTextInputLineMode::k_EGamepadTextInputLineModeMultipleLines : vr::EGamepadTextInputLineMode::k_EGamepadTextInputLineModeSingleLine;
213 uint32 HandInteracting = bIsRightHand ? 0 : 1;
214
215 if (bIsForPassword)
216 OverlayError = VROverlay->ShowKeyboardForOverlay(KeyboardHandle.VRKeyboardHandle, Inputmode, LineInputMode, TCHAR_TO_ANSI(*Description), MaxCharacters, TCHAR_TO_ANSI(*StartingString), bUseMinimalMode, HandInteracting);
217 else
218 OverlayError = VROverlay->ShowKeyboardForOverlay(KeyboardHandle.VRKeyboardHandle, Inputmode, LineInputMode, TCHAR_TO_ANSI(*Description), MaxCharacters, TCHAR_TO_ANSI(*StartingString), bUseMinimalMode, HandInteracting);
219
220 if (OverlayError != vr::EVROverlayError::VROverlayError_None)
221 {
222 VROverlay->DestroyOverlay(KeyboardHandle.VRKeyboardHandle);
223 KeyboardHandle.VRKeyboardHandle = vr::k_ulOverlayHandleInvalid;
225 return;
226 }
227
229#endif
230 }
231
232
233 // Closes the vrkeyboard, can fail if not already open
234 UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true", ExpandEnumAsExecs = "Result"))
235 void GetVRKeyboardText(FString & Text, EBPOVRResultSwitch & Result)
236 {
237#if !STEAMVR_SUPPORTED_PLATFORM
239 return;
240#else
241 if ( !KeyboardHandle.IsValid())
242 {
244 return;
245 }
246
247 if (!GEngine->XRSystem.IsValid() || (GEngine->XRSystem->GetSystemName() != SteamVRSystemName))
248 {
250 return;
251 }
252
253 vr::IVROverlay* VROverlay = vr::VROverlay();
254
255 if (!VROverlay)
256 {
258 return;
260
261 char OutString[512];
262 uint32 TextLen = VROverlay->GetKeyboardText((char*)&OutString, 512);
263
264 Text = FString(ANSI_TO_TCHAR(OutString));
266#endif
267 }
268
269
270};
static FName SteamVRSystemName(TEXT("SteamVR"))
EBPOVRResultSwitch
UENUM()
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FVRKeyboardNullCallbackSignature)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FVRKeyboardStringCallbackSignature, FString, Text)
UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent))
FVRKeyboardStringCallbackSignature OnKeyboardCharInput
UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
void CloseVRKeyboard(EBPOVRResultSwitch &Result)
UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true",...
void ReOpenVRKeyboardForUser(bool bIsForPassword, bool bIsMultiline, bool bUseMinimalMode, bool bIsRightHand, int32 MaxCharacters, FString Description, FString StartingString, EBPOVRResultSwitch &Result)
UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true",...
void GetVRKeyboardText(FString &Text, EBPOVRResultSwitch &Result)
UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true",...
FVRKeyboardNullCallbackSignature OnKeyboardClosed
UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
FVRKeyboardStringCallbackSignature OnKeyboardDone
UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
void OpenVRKeyboard(bool bIsForPassword, bool bIsMultiline, bool bUseMinimalMode, bool bIsRightHand, int32 MaxCharacters, FString Description, FString StartingString, EBPOVRResultSwitch &Result)
UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true",...