diff --git a/Source/WidgetInteraction/Private/WidgetInteraction.cpp b/Source/WidgetInteraction/Private/WidgetInteraction.cpp index 9c6ae60ac9c3145cb839ecdcd6354d1e6ce4b1e2..68fd513ee0d0f5f75eb6c45a64710e9e52dc5972 100644 --- a/Source/WidgetInteraction/Private/WidgetInteraction.cpp +++ b/Source/WidgetInteraction/Private/WidgetInteraction.cpp @@ -2,14 +2,16 @@ #include "WidgetInteraction.h" #include "HeadMountedDisplayFunctionLibrary.h" +#include "IDisplayCluster.h" +#include "IDisplayClusterClusterManager.h" #include "Engine.h" #define LOCTEXT_NAMESPACE "FWidgetInteractionModule" void FWidgetInteractionModule::StartupModule() { - on_world_tick_start_delegate_.BindRaw(this, &FWidgetInteractionModule::OnWorldTickStart); - FWorldDelegates::OnWorldTickStart.Add(on_world_tick_start_delegate_); + on_world_tick_start_delegate_.BindRaw(this, &FWidgetInteractionModule::OnWorldTickStart); + FWorldDelegates::OnWorldTickStart.Add(on_world_tick_start_delegate_); } void FWidgetInteractionModule::ShutdownModule() @@ -19,47 +21,66 @@ void FWidgetInteractionModule::ShutdownModule() void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val) { - auto worlds = GEngine->GetWorldContexts(); - - if (widget_interaction_cmp_ != nullptr && widget_interaction_cmp_->IsValidLowLevel() == true) - return; - - - for (auto & world_context : worlds) - { - auto world = world_context.World(); - auto player_controller = world->GetFirstPlayerController(); - if (player_controller == nullptr) - continue; - - auto vr_pawn = dynamic_cast<AVirtualRealityPawn*>(player_controller->AcknowledgedPawn); - if (vr_pawn == nullptr) - continue; - - FString name = FString("flystick"); - auto component_class = UMotionControllerComponent::StaticClass(); - if (UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled()) - { - name = FString("RightMotionController"); - component_class = UMotionControllerComponent::StaticClass(); - } - - - auto parent_vec = vr_pawn->GetComponentsByClass(component_class); - for (auto parent : parent_vec) - if (parent->GetName() == FString(name)) - CreateWidgetInteraction(dynamic_cast<USceneComponent*>(parent), vr_pawn); - } + auto worlds = GEngine->GetWorldContexts(); + + if (widget_interaction_cmp_ != nullptr) { + if (widget_interaction_cmp_->IsValidLowLevel() == true) { + return; + } + else { + widget_interaction_cmp_ = nullptr; + } + } + + for (auto & world_context : worlds) + { + auto world = world_context.World(); + auto player_controller = world->GetFirstPlayerController(); + if (player_controller == nullptr) + continue; + + auto vr_pawn = dynamic_cast<AVirtualRealityPawn*>(player_controller->AcknowledgedPawn); + if (vr_pawn == nullptr) + continue; + + FString name = ""; + UClass* component_class = UMotionControllerComponent::StaticClass(); + + + if (IDisplayCluster::Get().GetClusterMgr()->IsStandalone()) { + //if this is a standalone setup ... + if (UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled()) { + //.. with an HMD, we attach the intercation component to the right hand + name = FString("RightMotionController"); + component_class = UMotionControllerComponent::StaticClass(); + } + else { + //... without an HMD, we also attach it to the virtual right hand, since it exists in this case + name = TEXT("RightMotionController"); + component_class = UMotionControllerComponent::StaticClass(); + } + } + else { + //if this is a cluster setup we attach it to the flystick + name = TEXT("flystick"); + component_class = UMotionControllerComponent::StaticClass(); + } + + auto parent_vec = vr_pawn->GetComponentsByClass(component_class); + for (auto parent : parent_vec) + if (parent->GetName() == FString(name)) + CreateWidgetInteraction(dynamic_cast<USceneComponent*>(parent), vr_pawn); + } } void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent * parent, AVirtualRealityPawn* outer) { - widget_interaction_cmp_ = NewObject<URwthComponent>(outer, URwthComponent::StaticClass()); - widget_interaction_cmp_->AttachToComponent(parent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false)); - widget_interaction_cmp_->Init(); + widget_interaction_cmp_ = NewObject<URwthComponent>(outer, URwthComponent::StaticClass()); + widget_interaction_cmp_->AttachToComponent(parent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false)); + widget_interaction_cmp_->Init(); } #undef LOCTEXT_NAMESPACE - -IMPLEMENT_MODULE(FWidgetInteractionModule, WidgetInteraction) \ No newline at end of file + +IMPLEMENT_MODULE(FWidgetInteractionModule, WidgetInteraction)