add possession in selection window

This commit is contained in:
Arnaud Jamin
2023-10-10 17:24:27 -04:00
parent 87acfc5a94
commit 83d756252a
43 changed files with 374 additions and 276 deletions
@@ -22,6 +22,7 @@ public class CogEngine : ModuleRules
new string[]
{
"Core",
"CogInterface",
"CogImgui",
"CogDebug",
"CogWindow",
@@ -12,24 +12,6 @@ void FCogEngineModule::ShutdownModule()
{
}
//--------------------------------------------------------------------------------------------------------------------------
ACogEngineReplicator* FCogEngineModule::GetLocalReplicator()
{
return LocalReplicator;
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineModule::SetLocalReplicator(ACogEngineReplicator* Value)
{
LocalReplicator = Value;
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineModule::AddRemoteReplicator(ACogEngineReplicator* Value)
{
RemoteReplicators.Add(Value);
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FCogEngineModule, CogEngine)
@@ -1,21 +1,53 @@
#include "CogEngineReplicator.h"
#include "CogDebugLogMacros.h"
#include "CogInterfacePossessor.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/WorldSettings.h"
#include "EngineUtils.h"
#include "Net/Core/PushModel/PushModel.h"
#include "Net/UnrealNetwork.h"
DEFINE_LOG_CATEGORY(LogCogEngine);
//--------------------------------------------------------------------------------------------------------------------------
void ACogEngineReplicator::Create(APlayerController* Controller)
ACogEngineReplicator* ACogEngineReplicator::Spawn(APlayerController* Controller)
{
if (Controller->GetWorld()->GetNetMode() != NM_Client)
if (Controller->GetWorld()->GetNetMode() == NM_Client)
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = Controller;
Controller->GetWorld()->SpawnActor<ACogEngineReplicator>(SpawnInfo);
return nullptr;
}
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = Controller;
ACogEngineReplicator* Replicator = Controller->GetWorld()->SpawnActor<ACogEngineReplicator>(SpawnInfo);
return Replicator;
}
//--------------------------------------------------------------------------------------------------------------------------
ACogEngineReplicator* ACogEngineReplicator::GetLocalReplicator(UWorld& World)
{
for (TActorIterator<ACogEngineReplicator> It(&World, ACogEngineReplicator::StaticClass()); It; ++It)
{
ACogEngineReplicator* Replicator = *It;
return Replicator;
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------------------------------
void ACogEngineReplicator::GetRemoteReplicators(UWorld& World, TArray<ACogEngineReplicator*>& Replicators)
{
for (TActorIterator<ACogEngineReplicator> It(&World, ACogEngineReplicator::StaticClass()); It; ++It)
{
ACogEngineReplicator* Replicator = Cast<ACogEngineReplicator>(*It);
Replicators.Add(Replicator);
}
}
//--------------------------------------------------------------------------------------------------------------------------
ACogEngineReplicator::ACogEngineReplicator(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
@@ -33,6 +65,8 @@ ACogEngineReplicator::ACogEngineReplicator(const FObjectInitializer& ObjectIniti
//--------------------------------------------------------------------------------------------------------------------------
void ACogEngineReplicator::BeginPlay()
{
COG_LOG_OBJECT(LogCogEngine, ELogVerbosity::Verbose, this, TEXT(""));
Super::BeginPlay();
UWorld* World = GetWorld();
@@ -42,15 +76,6 @@ void ACogEngineReplicator::BeginPlay()
bIsLocal = NetMode != NM_DedicatedServer;
OwnerPlayerController = Cast<APlayerController>(GetOwner());
if (OwnerPlayerController->IsLocalController())
{
FCogEngineModule::Get().SetLocalReplicator(this);
}
else
{
FCogEngineModule::Get().AddRemoteReplicator(this);
}
}
//--------------------------------------------------------------------------------------------------------------------------
@@ -129,3 +154,28 @@ void ACogEngineReplicator::OnRep_TimeDilation()
#endif // !UE_BUILD_SHIPPING
}
//--------------------------------------------------------------------------------------------------------------------------
void ACogEngineReplicator::Server_Possess_Implementation(APawn* Pawn)
{
#if !UE_BUILD_SHIPPING
if (ICogInterfacePossessor* Possessor = Cast<ICogInterfacePossessor>(OwnerPlayerController))
{
Possessor->SetPossession(Pawn);
}
#endif // !UE_BUILD_SHIPPING
}
//--------------------------------------------------------------------------------------------------------------------------
void ACogEngineReplicator::Server_ResetPossession_Implementation()
{
#if !UE_BUILD_SHIPPING
if (ICogInterfacePossessor* Possessor = Cast<ICogInterfacePossessor>(OwnerPlayerController))
{
Possessor->ResetPossession();
}
#endif // !UE_BUILD_SHIPPING
}
@@ -144,7 +144,7 @@ bool UCogEngineWindow_Selection::DrawSelectionCombo()
//------------------------
// Actor List
//------------------------
//ImGui::BeginChild("ActorList", ImVec2(-1, -1), false);
ImGui::BeginChild("ActorList", ImVec2(-1, -1), false);
TArray<AActor*> Actors;
for (TActorIterator<AActor> It(GetWorld(), SelectedSubClass); It; ++It)
@@ -194,7 +194,7 @@ bool UCogEngineWindow_Selection::DrawSelectionCombo()
}
}
Clipper.End();
//ImGui::EndChild();
ImGui::EndChild();
return SelectionChanged;
}
@@ -220,6 +220,34 @@ void UCogEngineWindow_Selection::DrawActorContextMenu(AActor* Actor)
ImGui::SetTooltip("Reset the selection to the controlled actor.");
}
if (APawn* Pawn = Cast<APawn>(Actor))
{
if (ImGui::Button("Possess", ImVec2(-1, 0)))
{
if (ACogEngineReplicator* Replicator = ACogEngineReplicator::GetLocalReplicator(*GetWorld()))
{
Replicator->Server_Possess(Pawn);
}
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("Possess this pawn.");
}
if (ImGui::Button("Reset Possession", ImVec2(-1, 0)))
{
if (ACogEngineReplicator* Replicator = ACogEngineReplicator::GetLocalReplicator(*GetWorld()))
{
Replicator->Server_ResetPossession();
}
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("Reset pawn.");
}
}
ImGui::EndPopup();
}
@@ -90,7 +90,7 @@ bool UCogEngineWindow_Spawns::RenderSpawnAsset(const FCogEngineSpawnEntry& Spawn
if (ImGui::Button(TCHAR_TO_ANSI(*EntryName), ImVec2(-1, 0)))
{
if (ACogEngineReplicator* Replicator = FCogEngineModule::Get().GetLocalReplicator())
if (ACogEngineReplicator* Replicator = ACogEngineReplicator::GetLocalReplicator(*GetWorld()))
{
Replicator->Server_Spawn(SpawnEntry);
}
@@ -31,7 +31,13 @@ void UCogEngineWindow_TimeScale::RenderContent()
{
Super::RenderContent();
ACogEngineReplicator* Replicator = FCogEngineModule::Get().GetLocalReplicator();
UWorld* World = GetWorld();
if (World == nullptr)
{
return;
}
ACogEngineReplicator* Replicator = ACogEngineReplicator::GetLocalReplicator(*World);
if (Replicator == nullptr)
{
return;
@@ -3,9 +3,6 @@
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
class ACogEngineReplicator;
class APlayerController;
class COGENGINE_API FCogEngineModule : public IModuleInterface
{
public:
@@ -16,15 +13,5 @@ public:
virtual void ShutdownModule() override;
ACogEngineReplicator* GetLocalReplicator();
void SetLocalReplicator(ACogEngineReplicator* Value);
TArray<TObjectPtr<ACogEngineReplicator>> GetRemoteReplicators() const { return RemoteReplicators; }
void AddRemoteReplicator(ACogEngineReplicator* Value);
private:
TObjectPtr<ACogEngineReplicator> LocalReplicator;
TArray<TObjectPtr<ACogEngineReplicator>> RemoteReplicators;
};
@@ -6,6 +6,8 @@
#include "UObject/ObjectMacros.h"
#include "CogEngineReplicator.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(LogCogEngine, Verbose, All);
class APlayerController;
using FCogEnineSpawnFunction = TFunction<void(const FCogEngineSpawnEntry& SpawnEntry)>;
@@ -17,7 +19,12 @@ class COGENGINE_API ACogEngineReplicator : public AActor
GENERATED_UCLASS_BODY()
public:
static void Create(APlayerController* Controller);
static ACogEngineReplicator* Spawn(APlayerController* Controller);
static ACogEngineReplicator* GetLocalReplicator(UWorld& World);
static void GetRemoteReplicators(UWorld& World, TArray<ACogEngineReplicator*>& Replicators);
virtual void BeginPlay() override;
@@ -35,6 +42,12 @@ public:
UFUNCTION(Server, Reliable)
void Server_SetTimeDilation(float Value);
UFUNCTION(Server, Reliable)
void Server_Possess(APawn* Pawn);
UFUNCTION(Server, Reliable)
void Server_ResetPossession();
protected:
UFUNCTION()
@@ -4,6 +4,8 @@
#include "CogWindow.h"
#include "CogEngineWindow_TimeScale.generated.h"
class ACogEngineReplicator;
UCLASS()
class COGENGINE_API UCogEngineWindow_TimeScale : public UCogWindow
{