PointCloudComponent.cpp 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2018 Michal Cieciura. All Rights Reserved.

#include "PointCloudComponent.h"
#include "Engine/CollisionProfile.h"
#include "PointCloud.h"

UPointCloudComponent::UPointCloudComponent( const FObjectInitializer& ObjectInitializer )
	: Super( ObjectInitializer )
{
	PrimaryComponentTick.bCanEverTick = false;
cg's avatar
cg committed
11
	Mobility = EComponentMobility::Movable;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
}

int32 UPointCloudComponent::GetNumMaterials() const
{
	return PointCloud ? PointCloud->GetNumMaterials() : 0;
}

UMaterialInterface* UPointCloudComponent::GetMaterial(int32 ElementIndex) const
{
	return PointCloud ? PointCloud->GetMaterial(ElementIndex) : nullptr;
}

FBoxSphereBounds UPointCloudComponent::CalcBounds(const FTransform& LocalToWorld) const
{
	return PointCloud != NULL ? PointCloud->GetBounds().TransformBy(LocalToWorld) : USceneComponent::CalcBounds(LocalToWorld);
}

void UPointCloudComponent::AttachPointCloudListener()
{
	if (PointCloud)
	{
		PointCloud->OnPointCloudRebuilt().AddUObject(this, &UPointCloudComponent::OnPointCloudRebuilt);
	}
}

void UPointCloudComponent::RemovePointCloudListener()
{
	if (PointCloud)
	{
		PointCloud->OnPointCloudRebuilt().RemoveAll(this);
	}
}

void UPointCloudComponent::OnPointCloudRebuilt()
{
	MarkRenderStateDirty();
	UpdateBounds();
}

void UPointCloudComponent::SetPointCloud(UPointCloud *InPointCloud)
{
	if (PointCloud != InPointCloud)
	{
		RemovePointCloudListener();
		PointCloud = InPointCloud;
		AttachPointCloudListener();	
		OnPointCloudRebuilt();
	}
}

void UPointCloudComponent::AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector)
{
	UPointCloudComponent* This = CastChecked<UPointCloudComponent>(InThis);
	Super::AddReferencedObjects(This, Collector);
}

#if WITH_EDITOR
void UPointCloudComponent::PostLoad()
{
	Super::PostLoad();
	AttachPointCloudListener();
}

void UPointCloudComponent::PreEditChange(UProperty* PropertyThatWillChange)
{
	Super::PreEditChange(PropertyThatWillChange);

	if (PropertyThatWillChange && PropertyThatWillChange->GetName().Equals("PointCloud"))
	{
		RemovePointCloudListener();
	}
}

void UPointCloudComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);

	if (PropertyChangedEvent.MemberProperty)
	{
		if (PropertyChangedEvent.MemberProperty->GetName().Equals("PointCloud"))
		{
			AttachPointCloudListener();
		}
	}
}
#endif