A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VRGameViewportClient.h
Go to the documentation of this file.
1// This class is intended to provide support for Local Mixed play between a mouse and keyboard player
2// and a VR player. It is not needed outside of that use.
3
4#pragma once
5#include "Engine/GameViewportClient.h"
6#include "Engine/Engine.h"
7#include "CoreMinimal.h"
8
9#include "VRGameViewportClient.generated.h"
10
11UENUM(Blueprintable)
12enum class EVRGameInputMethod : uint8
13{
21
26UCLASS(Blueprintable)
27class VREXPANSIONPLUGIN_API UVRGameViewportClient : public UGameViewportClient
28{
29 GENERATED_UCLASS_BODY()
30
31public:
32
33 // Event thrown when the window is closed
34 UPROPERTY(BlueprintAssignable, Category = "VRExpansionPlugin")
35 FVROnWindowCloseRequested BPOnWindowCloseRequested;
36
37 // If true then forced window closing will be canceled (alt-f4, ect)
38 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
39 bool bIgnoreWindowCloseCommands;
40
41 // Input Method for the viewport
42 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
43 EVRGameInputMethod GameInputMethod;
45 // If true we will also shuffle gamepad input according to the GameInputMethod
46 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
47 bool bAlsoChangeGamepPadInput;
48
49 // A List of input categories to consider as valid gamepad ones if bIsGamepad is true on the input event
50 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
51 TArray<FName> GamepadInputCategories;
53 bool IsValidGamePadKey(const FKey & InputKey)
54 {
55 if (!bAlsoChangeGamepPadInput)
56 return false;
57
58 FName KeyCategory = InputKey.GetMenuCategory();
59
60 return GamepadInputCategories.Contains(KeyCategory);
61 }
62
63 UFUNCTION()
64 bool EventWindowClosing()
65 {
66 if (BPOnWindowCloseRequested.IsBound())
67 {
68 BPOnWindowCloseRequested.Broadcast();
69 }
70
71 if (bIgnoreWindowCloseCommands)
72 {
73 return false;
74 }
75
76 return true;
77 }
79 virtual void PostInitProperties() override
80 {
81 Super::PostInitProperties();
82
83 if (GamepadInputCategories.Num() < 1)
84 {
85 GamepadInputCategories.Add(FName(TEXT("Gamepad")));
86 GamepadInputCategories.Add(FName(TEXT("PS4")));
87 GamepadInputCategories.Add(FName(TEXT("XBox One")));
88 GamepadInputCategories.Add(FName(TEXT("Touch")));
89 GamepadInputCategories.Add(FName(TEXT("Gesture")));
90 }
92 OnWindowCloseRequested().BindUObject(this, &UVRGameViewportClient::EventWindowClosing);
93 }
94
95
96 virtual bool InputKey(const FInputKeyEventArgs& EventArgs) override
97 {
98 // Early out if a gamepad event or ignoring input or is default setup / no GEngine
99 if(GameInputMethod == EVRGameInputMethod::GameInput_Default || IgnoreInput() || (EventArgs.IsGamepad() && !IsValidGamePadKey(EventArgs.Key)))
100 return Super::InputKey(EventArgs);
101
102 const int32 NumLocalPlayers = World->GetGameInstance()->GetNumLocalPlayers();
103
104 // Also early out if number of players is less than 2
105 if (NumLocalPlayers < 2)
106 return Super::InputKey(EventArgs);
107
108 // Its const so have to copy and send a new one in now that the function signature has changed
109 FInputKeyEventArgs NewStruct = EventArgs;
110
112 {
113 // keyboard / mouse always go to player 0, so + 1 will be player 2
114 NewStruct.ControllerId++;
115 return Super::InputKey(NewStruct);
116 }
117 else // Shared keyboard and mouse
118 {
119 bool bRetVal = false;
120 for (int32 i = 0; i < NumLocalPlayers; i++)
121 {
122 NewStruct.ControllerId = i;
123 bRetVal = Super::InputKey(NewStruct) || bRetVal;
124 }
125
126 return bRetVal;
127 }
128 }
129
130 virtual bool InputAxis(FViewport* tViewport, int32 ControllerId, FKey Key, float Delta, float DeltaTime, int32 NumSamples = 1, bool bGamepad = false) override
131 {
132
133 const int32 NumLocalPlayers = World->GetGameInstance()->GetNumLocalPlayers();
134
135 // Early out if a gamepad or not a mouse event (vr controller) or ignoring input or is default setup / no GEngine
136 if (((!Key.IsMouseButton() && !bGamepad) || (bGamepad && !IsValidGamePadKey(Key))) || NumLocalPlayers < 2 || GameInputMethod == EVRGameInputMethod::GameInput_Default || IgnoreInput())
137 return Super::InputAxis(tViewport, ControllerId, Key, Delta, DeltaTime, NumSamples, bGamepad);
138
140 {
141 // keyboard / mouse always go to player 0, so + 1 will be player 2
142 ++ControllerId;
143 return Super::InputAxis(tViewport, ControllerId, Key, Delta, DeltaTime, NumSamples, bGamepad);
144 }
145 else // Shared keyboard and mouse
146 {
147 bool bRetVal = false;
148 for (int32 i = 0; i < NumLocalPlayers; i++)
149 {
150 bRetVal = Super::InputAxis(tViewport, i, Key, Delta, DeltaTime, NumSamples, bGamepad) || bRetVal;
151 }
152
153 return bRetVal;
154 }
155
156 }
158
159UVRGameViewportClient::UVRGameViewportClient(const FObjectInitializer& ObjectInitializer)
160 : Super(ObjectInitializer)
161{
164}
EVRGameInputMethod
UENUM(Blueprintable)
@ GameInput_KeyboardAndMouseToPlayer2
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FVROnWindowCloseRequested)
UCLASS(Blueprintable)
virtual bool InputAxis(FViewport *tViewport, int32 ControllerId, FKey Key, float Delta, float DeltaTime, int32 NumSamples=1, bool bGamepad=false) override
bool bIgnoreWindowCloseCommands
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
bool bAlsoChangeGamepPadInput
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
bool IsValidGamePadKey(const FKey &InputKey)
EVRGameInputMethod GameInputMethod
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
virtual bool InputKey(const FInputKeyEventArgs &EventArgs) override
TArray< FName > GamepadInputCategories
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "VRExpansionPlugin")
bool EventWindowClosing()
UFUNCTION()
FVROnWindowCloseRequested BPOnWindowCloseRequested
UPROPERTY(BlueprintAssignable, Category = "VRExpansionPlugin")
virtual void PostInitProperties() override