From d5c30d7b11cc6f50c8ddc4349dcdbe5da384b2df Mon Sep 17 00:00:00 2001
From: Jonathan Wendt <wendt@vr.rwth-aachen.de>
Date: Thu, 5 Dec 2019 09:07:45 +0100
Subject: [PATCH] update attachement method

---
 .../Private/WidgetInteraction.cpp             | 102 +++++++-----------
 .../Public/WidgetInteraction.h                |   2 +
 2 files changed, 40 insertions(+), 64 deletions(-)

diff --git a/Source/WidgetInteraction/Private/WidgetInteraction.cpp b/Source/WidgetInteraction/Private/WidgetInteraction.cpp
index 9ab7b4c..cf653cc 100644
--- a/Source/WidgetInteraction/Private/WidgetInteraction.cpp
+++ b/Source/WidgetInteraction/Private/WidgetInteraction.cpp
@@ -8,6 +8,8 @@
 
 #define LOCTEXT_NAMESPACE "FWidgetInteractionModule"
 
+DEFINE_LOG_CATEGORY(WidgetIntLog);
+
 void FWidgetInteractionModule::StartupModule()
 {
   on_world_tick_start_delegate_.BindRaw(this, &FWidgetInteractionModule::OnWorldTickStart);
@@ -25,77 +27,49 @@ void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val
 
   auto worlds = GEngine->GetWorldContexts();
 
-  for (auto world_context : worlds) {
-
-    auto world = world_context.World();
-
-    if (last_world == world  && widget_interaction_cmp_ != nullptr) {
-      if (widget_interaction_cmp_->IsValidLowLevel() == true) {
-        continue;
-      }
-      else {
-        widget_interaction_cmp_ = nullptr;
-      }
-    }
-
-    if (world == nullptr)
-      continue;
-
-    auto player_controller = world->GetFirstPlayerController();
-    if (player_controller == nullptr)
-      continue;
-
-    auto vr_pawn = dynamic_cast<AVirtualRealityPawn*>(player_controller->AcknowledgedPawn);
-    if (vr_pawn == nullptr)
-      continue;
-
-    UE_LOG(LogTemp, Warning, TEXT("OnWorldTickStart called and interaction component will be updated"));
-
-    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("HMDRightMotionController");
-        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("HMDRightMotionController");
-        component_class = UMotionControllerComponent::StaticClass();
-      }
-    }
-    else {
-      //if this is a cluster setup we attach it to the flystick
-      name = TEXT("flystick");
-      component_class = UDisplayClusterSceneComponent::StaticClass();
-    }
-
-    auto parent_vec = vr_pawn->GetComponentsByClass(component_class);
-    bool success;
-    for (auto parent : parent_vec) {
-      if (parent->GetName() == FString(name)) {
-        CreateWidgetInteraction(dynamic_cast<USceneComponent*>(parent), vr_pawn);
-        success = true;
-        last_world = world;
-      }
-    }
-
-    if (!success)
-      UE_LOG(LogTemp, Error, TEXT("Failed to load widget asset \"%s\", cannot attach widget interaction component"), *name);
-  }
+	for (auto world_context : worlds) {
+
+		auto world = world_context.World();
+
+		if (last_world == world && widget_interaction_cmp_ != nullptr) {
+			if (widget_interaction_cmp_->IsValidLowLevel() == true) {
+				continue;
+			}
+			else {
+				widget_interaction_cmp_ = nullptr;
+			}
+		}
+
+		if (world == nullptr)
+			continue;
+
+		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();
+
+		auto right_hand = vr_pawn->GetRightHandComponent();
+		CreateWidgetInteraction(vr_pawn->GetRightHandComponent(), vr_pawn);
+		last_world = world;
+
+		UE_LOG(WidgetIntLog, Verbose, TEXT("VRInteractionComponent attached to right hand"));
+	}
 }
 
-URwthComponent* FWidgetInteractionModule::GetWidgetInteractionComponent() {
+UVRWidgetInteractionComponent* FWidgetInteractionModule::GetWidgetInteractionComponent() {
   return widget_interaction_cmp_;
 }
 
 void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent * parent, AVirtualRealityPawn* outer)
 {
-  widget_interaction_cmp_ = NewObject<URwthComponent>(outer, URwthComponent::StaticClass());
+  widget_interaction_cmp_ = NewObject<UVRWidgetInteractionComponent>(outer, UVRWidgetInteractionComponent::StaticClass());
   widget_interaction_cmp_->AttachToComponent(parent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
   widget_interaction_cmp_->Init();
 }
diff --git a/Source/WidgetInteraction/Public/WidgetInteraction.h b/Source/WidgetInteraction/Public/WidgetInteraction.h
index b970b95..08f46b5 100644
--- a/Source/WidgetInteraction/Public/WidgetInteraction.h
+++ b/Source/WidgetInteraction/Public/WidgetInteraction.h
@@ -7,6 +7,8 @@
 #include "VRWidgetInteractionComponent.h"
 #include "Modules/ModuleManager.h"
 
+DECLARE_LOG_CATEGORY_EXTERN(WidgetIntLog, Log, All);
+
 class WIDGETINTERACTION_API FWidgetInteractionModule : public IModuleInterface
 {
 public:
-- 
GitLab