A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
HighlightComponent.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
4#include "Kismet/GameplayStatics.h"
5
6// Sets default values for this component's properties
8{
9 // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
10 // off to improve performance if you don't need them.
11 PrimaryComponentTick.bCanEverTick = false;
12
13 ColorMap.Add(EHighlightColor::None, FLinearColor(0.0f, 0.0f, 0.0f));
14 ColorMap.Add(EHighlightColor::Blue, FLinearColor(0.0f, 0.866382f, 1.0f));
15 ColorMap.Add(EHighlightColor::Green, FLinearColor(0.061116f, 1.0f, 0.0f));
16 ColorMap.Add(EHighlightColor::Yellow, FLinearColor(1.0f, 0.850555f, 0.0f));
17 ColorMap.Add(EHighlightColor::Red, FLinearColor(1.0f, 0.0f, 0.0f));
18}
19
20// Called when the game starts
22{
23 Super::BeginPlay();
24}
25
27{
29 {
30 CurrentHighlight = HighlightColor;
31 OnHighlightValueChanged.Broadcast();
32 auto Color = [this]()->FVector{return FVector(*ColorMap.Find(CurrentHighlight));};
34 }
35}
36
38{
39 if (!Activate)
40 {
41 if (PulseHighlightTimerHandle.IsValid())
42 {
43 HighlightTime = 0.0;
44 PulseDirection = 0.0;
45 PulseHighlightTimerHandle.Invalidate();
46 BlockPulsing = true;
47 }
49 }
50 else
51 {
52 HighlightTime = 1.0;
53 PulseDirection = -1.0;
55 }
56}
57
59{
60 auto Color = [this]()->FVector
61 {
62 return FVector(FMath::Lerp(*ColorMap.Find(EHighlightColor::None), *ColorMap.Find(EHighlightColor::Blue), HighlightTime));;
63 };
66 if (HighlightTime > 1.0)
67 {
68 HighlightTime = 1.0;
69 PulseDirection = -1.0;
70 }
71 else if (HighlightTime < 0.0)
72 {
73 HighlightTime = 0.0;
74 PulseDirection = 1.0;
75 }
76 if (BlockPulsing)
77 {
78 BlockPulsing = false;
79 return;
80 }
81 GetWorld()->GetTimerManager().SetTimer( PulseHighlightTimerHandle,this, &UHighlightComponent::PerformPulseHighlight, 0.1);
82}
83
84void UHighlightComponent::ChangeHighlightMaterialColor(const TFunctionRef<FVector()> Color)
85{
86 TArray<UStaticMeshComponent*> StaticMeshes;
87 GetOwner()->GetComponents<UStaticMeshComponent>(StaticMeshes);
88 TArray<USkeletalMeshComponent*> SkeletalMeshes;
89 GetOwner()->GetComponents<USkeletalMeshComponent>(SkeletalMeshes);
90
91 for (UStaticMeshComponent* staticMesh : StaticMeshes)
92 {
93 if (UGameplayStatics::GetPlatformName() == TEXT("Android") || AlwaysEmissiveHighlight)
94 staticMesh->SetVectorParameterValueOnMaterials(TEXT("EmissiveColor"), Color());
95 else
96 {
98 staticMesh->SetRenderCustomDepth(false);
99 else
100 staticMesh->SetRenderCustomDepth(true);
101
102 staticMesh->SetCustomDepthStencilValue((uint8)CurrentHighlight);
103 }
104 }
105
106 for (USkeletalMeshComponent* skeletalMesh : SkeletalMeshes)
107 {
108 if (UGameplayStatics::GetPlatformName() == TEXT("Android") || AlwaysEmissiveHighlight)
109 skeletalMesh->SetVectorParameterValueOnMaterials(TEXT("EmissiveColor"), FVector(*ColorMap.Find(CurrentHighlight)));
110 else
111 {
113 skeletalMesh->SetRenderCustomDepth(false);
114 else
115 skeletalMesh->SetRenderCustomDepth(true);
116
117 skeletalMesh->SetCustomDepthStencilValue((uint8)CurrentHighlight);
118 }
119 }
120}
121
EHighlightColor
UENUM()
TMap< EHighlightColor, FLinearColor > ColorMap
bool AlwaysEmissiveHighlight
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
void PerformPulseHighlight()
UFUNCTION()
bool IsShowHighlight
UPROPERTY()
FTimerHandle PulseHighlightTimerHandle
UPROPERTY(BlueprintReadWrite)
virtual void BeginPlay() override
EHighlightColor CurrentHighlight
UPROPERTY()
void UpdateHighlightValue(EHighlightColor HighlightColor)
Updates the highlight color of the object.
void ChangeHighlightMaterialColor(const TFunctionRef< FVector()> Color)
void ActivatePulseHighlight(bool Activate)
Activates or deactivates the pulse highlight effect.
FOnHighlightValueChanged OnHighlightValueChanged
UPROPERTY(BlueprintAssignable)