Added text to new debug system

This commit is contained in:
bernatx 2019-07-12 17:43:32 +02:00
parent 0b8837f34c
commit f820826f7f
4 changed files with 109 additions and 8 deletions

View File

@ -6,6 +6,7 @@
#include "Carla.h"
#include "Carla/Game/CarlaGameModeBase.h"
#include "Carla/Game/CarlaHUD.h"
#include <compiler/disable-ue4-macros.h>
#include <carla/rpc/WeatherParameters.h>
@ -22,6 +23,9 @@ ACarlaGameModeBase::ACarlaGameModeBase(const FObjectInitializer& ObjectInitializ
Recorder = CreateDefaultSubobject<ACarlaRecorder>(TEXT("Recorder"));
// HUD
HUDClass = ACarlaHUD::StaticClass();
TaggerDelegate = CreateDefaultSubobject<UTaggerDelegate>(TEXT("TaggerDelegate"));
CarlaSettingsDelegate = CreateDefaultSubobject<UCarlaSettingsDelegate>(TEXT("CarlaSettingsDelegate"));
}

View File

@ -0,0 +1,55 @@
// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "CarlaHUD.h"
void ACarlaHUD::DrawHUD()
{
Super::DrawHUD();
double Now = FPlatformTime::Seconds();
int ScreenWidth = 0;
int ScreenHeight = 0;
auto Player = GetOwningPlayerController();
if (Player == nullptr)
{
UE_LOG(LogCarla, Error, TEXT("Can't find player controller!"));
return;
}
// get viewport size for culling
Player->GetViewportSize(ScreenWidth, ScreenHeight);
int i = 0;
while (i < StringList.Num())
{
// project position from camera
FVector2D Screen;
Player->ProjectWorldLocationToScreen(StringList[i].Location, Screen, true);
if (Screen.X >= 0 && Screen.Y >= 0 && Screen.X < ScreenWidth && Screen.Y < ScreenHeight)
{
// draw text
DrawText(StringList[i].Str, StringList[i].Color, Screen.X, Screen.Y, nullptr, 1.0f, false);
}
// check to remove the string
if (Now >= StringList[i].TimeToDie)
{
// remove the string
StringList.RemoveAt(i);
}
else
++i;
}
}
void ACarlaHUD::AddHUDString(FString Str, FVector Location, FColor Color, double LifeTime)
{
double Now = FPlatformTime::Seconds();
HUDString Obj { Str, Location, Color, Now + LifeTime };
StringList.Add(std::move(Obj));
}

View File

@ -0,0 +1,42 @@
// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#pragma once
#include "Containers/Array.h"
#include "GameFramework/HUD.h"
#include "CarlaHUD.generated.h"
struct HUDString
{
FString Str { "" };
FVector Location;
FColor Color;
double TimeToDie;
};
/// Class to draw on HUD
UCLASS()
class CARLA_API ACarlaHUD : public AHUD
{
GENERATED_BODY()
public:
ACarlaHUD(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryActorTick.bCanEverTick = false;
}
virtual void DrawHUD() override;
void AddHUDString(FString Str, FVector Location, FColor Color, double LifeTime);
private:
TArray<HUDString> StringList;
};

View File

@ -159,14 +159,14 @@ struct FShapeVisitor
void operator()(const Shape::String &Str) const
{
DrawDebugString(
World,
Str.location,
carla::rpc::ToFString(Str.text),
nullptr,
Color,
LifeTime,
Str.draw_shadow);
auto PlayerController = UGameplayStatics::GetPlayerController(World, 0);
if (PlayerController == nullptr)
{
UE_LOG(LogCarla, Error, TEXT("Can't find player controller!"));
return;
}
ACarlaHUD *Hud = Cast<ACarlaHUD>(PlayerController->GetHUD());
Hud->AddHUDString(carla::rpc::ToFString(Str.text), Str.location, Color, LifeTime);
}
private: