当前位置: 首页 > news >正文

UE5 C++ Slate 画曲线

一.直接上代码

头文件

#pragma once#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Rendering/DrawElementTypes.h"
#include "SmoothLineWidget.generated.h"/*** */
UCLASS()
class GWXPJ_API USmoothLineWidget : public UUserWidget
{GENERATED_BODY()
public:UFUNCTION(BlueprintCallable)void SetValues(TArray<float> InValues);
protected:virtual int32 NativePaint(const FPaintArgs& Args,const FGeometry& AllottedGeometry,const FSlateRect& MyCullingRect,FSlateWindowElementList& OutDrawElements,int32 LayerId,const FWidgetStyle& InWidgetStyle,bool bParentEnabled) const;void DrawSmoothedLine(FSlateWindowElementList& OutDrawElement,uint32 LayerId,const FGeometry& InAllottedGeometry,TArray<FVector2D> InPoints,float InThickness,FColor InColor)const;TArray<FVector2D> MultiplyPoint;
};

CPP

// Fill out your copyright notice in the Description page of Project Settings.#include "GWXWidget.h"
#include "Components/CanvasPanelSlot.h"void UGWXWidget::SetValues(TArray<float> InValues)
{if (InValues.Num() < 2){return;}MultiplyPoint.Empty();UCanvasPanelSlot* CanvsdPanelSlot = Cast<UCanvasPanelSlot>(this->Slot);if (CanvsdPanelSlot == nullptr)return;float WidgetWidth = CanvsdPanelSlot->GetSize().X;float WidgetHeight = CanvsdPanelSlot->GetSize().Y;float Space = WidgetWidth / (InValues.Num() - 1);float HeightSpace = WidgetHeight / 2000;  //值域 + - 2000for (int32 Index = 0; Index < InValues.Num(); Index++)  //数据输入{FVector2D KeyPosition(Space * Index, WidgetHeight - InValues[Index]*HeightSpace);MultiplyPoint.Add(KeyPosition);}
}int32 UGWXWidget::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,1.0f,FColor::Red);//float Numx = 10;  //X轴的取值点数float Numy = 10;	//Y轴的取值点数UCanvasPanelSlot* CanvsdPanelSlot = Cast<UCanvasPanelSlot>(this->Slot);if (!CanvsdPanelSlot){return LayerId++;}float WidgetWidth = CanvsdPanelSlot->GetSize().X;float WidgetHeight = CanvsdPanelSlot->GetSize().Y;float Space = WidgetWidth / (Numx);//X 轴刻度for (int32 Index = 0;Index <= Numx;Index++){FVector2D TextPosition = FVector2D(Space *Index,WidgetHeight);FSlateFontInfo SlateFontInfo = FCoreStyle::GetDefaultFontStyle("Regular",10);FSlateDrawElement::MakeText(OutDrawElements,LayerId,AllottedGeometry.ToOffsetPaintGeometry(TextPosition),FText::FromString(FString::FromInt(Index * 10)),SlateFontInfo,ESlateDrawEffect::None,FLinearColor::White);}// Y轴刻度 float HeightSpace = WidgetHeight / 2000*200;  //值域 + - 2000for (int32 Index = 0; Index <= Numy; Index++){FVector2D TextPosition = FVector2D(-30,HeightSpace*Index-10);FSlateFontInfo SlateFontInfo = FCoreStyle::GetDefaultFontStyle("Regular", 10);FSlateDrawElement::MakeText(OutDrawElements,LayerId,AllottedGeometry.ToOffsetPaintGeometry(TextPosition),FText::FromString(FString::FromInt((Numy-Index) * 200)),SlateFontInfo,ESlateDrawEffect::None,FLinearColor::White);}return LayerId++;
} //变化void UGWXWidget::DrawSmoothedLine(FSlateWindowElementList& OutDrawElement, uint32 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* CanvasPanelSlot = Cast<UCanvasPanelSlot>(this->Slot);if (CanvasPanelSlot == nullptr)return;TArray<FVector2D> ResultPoints;float WidgetWidth = CanvasPanelSlot->GetSize().X;int32 Begin = 0;int32 End = (int32)WidgetWidth;for (int32 X = Begin; X < End; X++){float Y = RichCurve->Eval(X);FVector2D ResultPoint(X, Y);ResultPoints.Add(ResultPoint);}delete RichCurve;FSlateDrawElement::MakeLines(OutDrawElement,InLayerId,InAllottedGeometry.ToPaintGeometry(),ResultPoints,ESlateDrawEffect::None,InColor,true,InThickness);  
}

二. 首先我们必须包含 UMG 模块,因为要在C++里继承UserWidget。其次我们需要包好Slate和SlateCore 模块。因为这样才能使用Slate编程里的函数API

三.思路

1.这里我们SetValues用于外部调用。

2.NativePaint更像是一个循环。

这里我们看到。它更是UserWidget里用来画最大LayerID的,当LayerID增加,它就不停刷新。

这里也绘制了X轴,Y轴,使用的是FSlateDrawElement::MakeText

3.将数组两两放入曲线中,得到更顺滑的数组。最终核心的Slate 还是FSlateDrawElement::MakeLines函数,进行绘画。

三.效果

http://www.dtcms.com/a/574322.html

相关文章:

  • 【机器学习15】强化学习入门、Q-Learning、贝尔曼方程
  • 解释seo网站推广北京十大科技公司
  • 基于电鱼 AI 工控机的塔吊与升降机安全监测方案——实时采集传感器数据,AI智能判断异常并报警
  • 如何看一个网站用什么程序做的南开做网站
  • 京东云双11活动-云产品特惠热卖中
  • 企业网站如何seowordpress touch
  • 怎样进行网站板块建设支付宝网站登录入口
  • Harbor 私有镜像仓库安装教程
  • 批发/贸易企业数字化转型:PHP开发的B2B订货系统
  • ACMMM2025 |TGSI+SATL:不改模型架构也能提升预测性能,破解几何结构评估与建模难题!
  • 【笔记】Windows系统安装SAM-2(Segment Anything Model 2)
  • 宁波网站制作报价主题设置wordpress
  • 嵌入式Linux安全启动全解析:从原理到实战
  • wordpress培训类网站模板厦门软件开发培训机构
  • 16000+字!Java集合笔记
  • 中国免费域名申请网站色彩搭配的网站
  • 传播网站建设龙泉驿最新消息
  • 2.2 python中带参数的装饰器与类装饰器
  • Java程序导致CPU打满如何排查
  • 建设网站天河区.net网站开发流程
  • 海外设计网站建设网站怎样运营
  • 两学一做微网站交流扬州网站建设费用
  • 网站开发 xmind长春建站网站模板
  • WebView 调试工具全解析,解决“看不见的移动端问题”
  • 论坛网站建设流程wordpress 页面压缩
  • 如果做京东优惠卷的网站七台河新闻联播视频
  • 永久免费自助建站源代码一个企业网站ppt怎么做
  • 工商网站官网入口seo顾问是干什么
  • 国产化云桌面有哪些有实力的厂家?
  • MediaPipe+OpenCV的python实现交互式贪吃蛇小游戏