A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
SteamVRKeyboardComponent.cpp
Go to the documentation of this file.
1// Fill out your copyright notice in the Description page of Project Settings.
2
4#include "Engine/Engine.h"
6#include "GameFramework/PlayerController.h"
7#include "Engine/LocalPlayer.h"
8//#include "GripMotionControllerComponent.h"
9
10
11//=============================================================================
12USteamVRKeyboardComponent::USteamVRKeyboardComponent(const FObjectInitializer& ObjectInitializer)
13 : Super(ObjectInitializer)
14{
15 PrimaryComponentTick.bCanEverTick = true;
16 PrimaryComponentTick.TickGroup = TG_PrePhysics;
17 PrimaryComponentTick.bStartWithTickEnabled = false;
18}
19
20//=============================================================================
24
26{
27#if STEAMVR_SUPPORTED_PLATFORM
28 if (KeyboardHandle.IsValid())
29 {
30 EBPOVRResultSwitch Result;
31 CloseVRKeyboard(Result);
32 }
33#endif
34
35 Super::OnUnregister();
36}
37
38
39void USteamVRKeyboardComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
40{
41Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
42
43#if !STEAMVR_SUPPORTED_PLATFORM
44return;
45#else
46 if ( !KeyboardHandle.IsValid())
47 {
48 return;
49 }
50
51 if (!GEngine->XRSystem.IsValid() || (GEngine->XRSystem->GetSystemName() != SteamVRSystemName))
52 {
53 return;
54 }
55
56 vr::IVROverlay* VROverlay = vr::VROverlay();
57
58
59 vr::IVRInput* VRInput = vr::VRInput();
60
61 if (!VROverlay)
62 {
63 return;
64 }
65
66 FTransform PlayerTransform = FTransform::Identity;
67
68 // Get first local player controller
69 /*APlayerController* PC = nullptr;
70 for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
71 {
72 if (Iterator->Get()->IsLocalPlayerController())
73 {
74 PC = Iterator->Get();
75 break;
76 }
77 }*/
78 APlayerController* PC = nullptr;
79 if (UWorld * CurWorld = GetWorld())
80 {
81 const ULocalPlayer* FirstPlayer = GEngine->GetFirstGamePlayer(CurWorld);
82 PC = FirstPlayer ? FirstPlayer->GetPlayerController(CurWorld) : nullptr;
83 }
84
85 if (PC)
86 {
87 APawn * mpawn = PC->GetPawnOrSpectator();
88 //bTextureNeedsUpdate = true;
89 if (mpawn)
90 {
91 // Set transform to this relative transform
92 PlayerTransform = mpawn->GetTransform();
93 }
94 }
95
96 float WorldToMetersScale = UHeadMountedDisplayFunctionLibrary::GetWorldToMetersScale(GetWorld());
97
98 // HMD Matrix
99 FTransform RelTransform = this->GetComponentTransform();
100 RelTransform = RelTransform.GetRelativeTransform(PlayerTransform);
101
102 FQuat Rot = RelTransform.GetRotation();
103 RelTransform.SetRotation(FQuat(Rot.Y, Rot.Z, -Rot.X, -Rot.W));
104
105 FVector pos = RelTransform.GetTranslation();
106 RelTransform.SetTranslation(FVector(pos.Y, pos.Z, -pos.X) / WorldToMetersScale);
107
108 FVector scale = RelTransform.GetScale3D();
109 RelTransform.SetScale3D(FVector(scale.Y, scale.Z, scale.X) / WorldToMetersScale);
110
111 vr::HmdMatrix34_t NewTransform = UOpenVRExpansionFunctionLibrary::ToHmdMatrix34(RelTransform.ToMatrixNoScale());
112 VROverlay->SetKeyboardTransformAbsolute(vr::ETrackingUniverseOrigin::TrackingUniverseStanding, &NewTransform);
113
114 // Poll SteamVR events
115 vr::VREvent_t VREvent;
116
117 while (KeyboardHandle.IsValid() && VROverlay->PollNextOverlayEvent(KeyboardHandle.VRKeyboardHandle, &VREvent, sizeof(VREvent)))
118 {
119
120 //VRKeyboardEvent_None = 0,
121 //VRKeyboardEvent_OverlayFocusChanged = 307, // data is overlay, global event
122 //VRKeyboardEvent_OverlayShown = 500,
123 //VRKeyboardEvent_OverlayHidden = 501,
124 //VRKeyboardEvent_ShowKeyboard = 509, // Sent to keyboard renderer in the dashboard to invoke it
125 //VRKeyboardEvent_HideKeyboard = 510, // Sent to keyboard renderer in the dashboard to hide it
126 //VRKeyboardEvent_OverlayGamepadFocusGained = 511, // Sent to an overlay when IVROverlay::SetFocusOverlay is called on it
127 //VRKeyboardEvent_OverlayGamepadFocusLost = 512, // Send to an overlay when it previously had focus and IVROverlay::SetFocusOverlay is called on something else
128 //VRKeyboardEvent_OverlaySharedTextureChanged = 513,
129 //VRKeyboardEvent_KeyboardClosed = 1200,
130 //VRKeyboardEvent_KeyboardCharInput = 1201,
131 //VRKeyboardEvent_KeyboardDone = 1202, // Sent when DONE button clicked on keyboard
132
133 switch (VREvent.eventType)
134 {
135 case vr::VREvent_KeyboardCharInput:
136 {
137 char OutString[512];
138 uint32 TextLen = VROverlay->GetKeyboardText((char*)&OutString, 512);
139 OnKeyboardCharInput.Broadcast(FString(ANSI_TO_TCHAR(OutString)));
140 }break;
141 case vr::VREvent_KeyboardClosed:
142 {
143 if (KeyboardHandle.IsValid())
144 {
145 VROverlay->DestroyOverlay(KeyboardHandle.VRKeyboardHandle);
146 KeyboardHandle.VRKeyboardHandle = vr::k_ulOverlayHandleInvalid;
147 }
148 OnKeyboardClosed.Broadcast();
149 }break;
150 case vr::VREvent_KeyboardDone:
151 {
152 char OutString[512];
153 uint32 TextLen = VROverlay->GetKeyboardText((char*)&OutString, 512);
154 OnKeyboardDone.Broadcast(FString(ANSI_TO_TCHAR(OutString)));
155
156 if (KeyboardHandle.IsValid())
157 {
158 VROverlay->HideKeyboard();
159 VROverlay->DestroyOverlay(KeyboardHandle.VRKeyboardHandle);
160 KeyboardHandle.VRKeyboardHandle = vr::k_ulOverlayHandleInvalid;
161 }
162 }break;
163
164 default:break;
165 }
166 }
167
168#endif
169}
static FName SteamVRSystemName(TEXT("SteamVR"))
EBPOVRResultSwitch
UENUM()
USteamVRKeyboardComponent(const FObjectInitializer &ObjectInitializer)
FVRKeyboardStringCallbackSignature OnKeyboardCharInput
UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
void CloseVRKeyboard(EBPOVRResultSwitch &Result)
UFUNCTION(BlueprintCallable, Category = "VRExpansionFunctions|SteamVR", meta = (bIgnoreSelf = "true",...
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
FVRKeyboardNullCallbackSignature OnKeyboardClosed
UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")
virtual void OnUnregister() override
FVRKeyboardStringCallbackSignature OnKeyboardDone
UPROPERTY(BlueprintAssignable, Category = "VRExpansionFunctions|SteamVR")