UE4C++UI平滑曲线图绘制
- 前言
UE4的曲线在UMG上面绘制的时候总是和麻烦的,这里我们采用UE4曲线FRichCurve中的绘制方法,传入对应的Key值,然后差值获取俩个点之间的数据,然后将所有点连接则是对应的平滑曲线。(此处没计算,用的FRichCurve中所自带点的计算),逻辑比较简单,再次不过多赘述。
附源码:
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "SmoothedLineWidget.generated.h"
UCLASS()
class SPECIALCHART_API USmoothedLineWidget : public UUserWidget
{
GENERATED_BODY()
public:
USmoothedLineWidget(const FObjectInitializer& ObjectInitializer);
UFUNCTION(BlueprintCallable)
void SetValues(TArray<float> InValues);
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FVector2D Size= FVector2D(100,100);
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FVector2D Locationn = FVector2D(100, 100);
UPROPERTY(EditAnywhere,BlueprintReadWrite)
float BrushSize=5;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FColor Color;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<float> Values;
virtual void NativePreConstruct() override;
protected:
virtual int32 NativePaint(
const FPaintArgs& Args,
const FGeometry& AllottedGeometry,
const FSlateRect& MyCullingRect,
FSlateWindowElementList& OutDrawElements,
int32 LayerId,
const FWidgetStyle& InWidgetStyle,
bool bParentEnabled) const override;
private:
void DrawSmoothedLine(
FSlateWindowElementList& OutDrawElement,
int InLayerId,
const FGeometry& InAllottedGeometry,
TArray<FVector2D> InPoints,
float InThickness,
FColor InColor
)const;
TArray<FVector2D> MultiplyPoint;
};
#include "SmoothedLineWidget.h"
#include "Components/CanvasPanelSlot.h"
USmoothedLineWidget::USmoothedLineWidget(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{
}
void USmoothedLineWidget::SetValues(TArray<float> InValues)
{
if (InValues.Num() < 2)
return;
MultiplyPoint.Empty();
float WidgetWidth = Size.X;
float WidgetHeight = Size.Y;
float Space = WidgetWidth / (InValues.Num() - 1);
for (int32 Index = 0; Index < InValues.Num(); Index++)
{
FVector2D KeyPosition(Space * Index, WidgetHeight - InValues[Index]);
MultiplyPoint.Add(KeyPosition);
}
}
void USmoothedLineWidget::NativePreConstruct()
{
Super::NativePreConstruct();
SetValues(Values);
}
int32 USmoothedLineWidget::NativePaint(const FPaintArgs& Args,
const FGeometry& AllottedGeometry,
const FSlateRect& MyCullingRect,
FSlateWindowElementList& OutDrawElements,
int32 LayerId,
const FWidgetStyle& InWidgetStyle,
bool bParentEnabled) const
{
DrawSmoothedLine(
OutDrawElements,
LayerId,
AllottedGeometry,
MultiplyPoint,
BrushSize,
Color);
return LayerId++;
}
void USmoothedLineWidget::DrawSmoothedLine(
FSlateWindowElementList& OutDrawElement,
int InLayerId,
const FGeometry& InAllottedGeometry,
TArray<FVector2D> InPoints,
float InThickness,
FColor InColor) const
{
if (InPoints.Num() < 2)
return;
FRichCurve* RichCurve = new FRichCurve();
for (FVector2D InPoint : InPoints)
{
FKeyHandle KeyHandle = RichCurve->AddKey(InPoint.X, InPoint.Y);
RichCurve->SetKeyInterpMode(KeyHandle, ERichCurveInterpMode::RCIM_Cubic);
}
UCanvasPanelSlot* CanvsdPanelSlot = Cast<UCanvasPanelSlot>(this->Slot);
TArray<FVector2D> ResultPoints;
float WidgetWidth = Size.X;
float WidgetHeight = Size.Y;
int32 Begin = 0;
int32 End = (int32)WidgetWidth;
for (int32 X = Begin; X < End; X++)
{
float Y = RichCurve->Eval(X);
FVector2D ResultPoint(X+ Locationn.X, Y+ Locationn.Y);
ResultPoints.Add(ResultPoint);
}
delete RichCurve;
FSlateDrawElement::MakeLines(
OutDrawElement,
InLayerId,
InAllottedGeometry.ToPaintGeometry(),
ResultPoints,
ESlateDrawEffect::None,
InColor,
true,
InThickness
);
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/b4c9633dc4324e99932c6e07a8007514.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5re36L-55Lit55qE5ZK46bG8,size_20,color_FFFFFF,t_70,g_se,x_16)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)