UE4 Slate 柱状图

  • Post author:
  • Post category:其他


// Fill out your copyright notice in the Description page of Project Settings.


#include "ZZTWidget.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
#include <windows.h>
#include "Windows/PostWindowsApi.h"

int32 UZZTWidget::NativePaint(const FPaintArgs& Args,
                              const FGeometry& AllottedGeometry,
                              const FSlateRect& MyCullingRect,
                              FSlateWindowElementList& OutDrawElements,
                              int32 LayerId,
                              const FWidgetStyle& InWidgetStyle,
                              bool bParentEnabled) const
{
	if(Gap <= SizeXRec)
	{
		// if(MessageBox(0, TEXT("Out Of Range!!!"), TEXT("ZZTWarning!!!"), MB_ICONWARNING) == IDOK)
		// {
		// 	
		// }
	}
	float MaxHeight = 0.f;
	for(int Index = 0; Index < SizeYRec.Num(); Index++)
	{
		MaxHeight = FMath::Max(MaxHeight, SizeYRec[Index]);
	}
	if(isDIYColor)
	{
		int32 MinLength = FMath::Min(SizeYRec.Num(), ColorsRect.Num());
		for(int Index = 0; Index < MinLength; Index++)
		{
			DrawRect(OutDrawElements,
				AllottedGeometry,
				LayerId,
				FVector2D(Gap * Index, MaxHeight),
				FVector2D(SizeXRec, SizeYRec[Index]),
				ColorsRect[Index]);
		}
	}
	else
	{
		for(int Index = 0; Index < SizeYRec.Num(); Index++)
		{
			DrawRect(OutDrawElements,
				AllottedGeometry,
				LayerId,
				FVector2D(Gap * Index, MaxHeight),
				FVector2D(SizeXRec, SizeYRec[Index]),
				ColorRec);
		}
	}
	
	//if(PositionRec.Num() > 0)	DrawRect(OutDrawElements, AllottedGeometry, LayerId, PositionRec[0], SizeRec, ColorRec);
	
	return LayerId++;
}

void UZZTWidget::DrawRect(FSlateWindowElementList& OutDrawElements,
	const FGeometry& AllocttedGeometry,
	int32 LayerId,
	FVector2D Position,
	FVector2D Size,
	FColor Color) const
{
	FSlateVertex LeftDownVertex;
	FSlateVertex RightDownVertex;
	FSlateVertex RightUpVertex;
	FSlateVertex LeftUpVertex;

	LeftDownVertex.Color = Color;
	RightDownVertex.Color = Color;
	RightUpVertex.Color = Color;
	LeftUpVertex.Color = Color;

	FVector2D LeftDownPosition = Position;
	FVector2D RightDownPosition = FVector2D(Position.X + Size.X, Position.Y);
	FVector2D RightUpPosition = FVector2D(Position.X + Size.X, Position.Y - Size.Y);
	FVector2D LeftUpPosition = FVector2D(Position.X, Position.Y - Size.Y);

	const FSlateRenderTransform& SlateRenderTransform = AllocttedGeometry.ToPaintGeometry().GetAccumulatedRenderTransform();
	LeftDownPosition = SlateRenderTransform.TransformPoint(LeftDownPosition);
	RightDownPosition = SlateRenderTransform.TransformPoint(RightDownPosition);
	RightUpPosition = SlateRenderTransform.TransformPoint(RightUpPosition);
	LeftUpPosition = SlateRenderTransform.TransformPoint(LeftUpPosition);

	LeftDownVertex.Position = LeftDownPosition;
	RightDownVertex.Position = RightDownPosition;
	RightUpVertex.Position = RightUpPosition;
	LeftUpVertex.Position = LeftUpPosition;

	TArray<FSlateVertex> SlateVertexes;
	
	int32 IndexLeftDown = SlateVertexes.Add(LeftDownVertex);
	int32 IndexRightDown = SlateVertexes.Add(RightDownVertex);
	int32 IndexRightUp = SlateVertexes.Add(RightUpVertex);
	int32 IndexLeftUp = SlateVertexes.Add(LeftUpVertex);

	TArray<SlateIndex>	Indexes;
	Indexes.Add(IndexLeftDown);
	Indexes.Add(IndexRightDown);
	Indexes.Add(IndexRightUp);
	
	Indexes.Add(IndexLeftUp);
	Indexes.Add(IndexRightUp);
	Indexes.Add(IndexLeftDown);

	for(FSlateVertex& Vertex : SlateVertexes)
	{
		Vertex.TexCoords[0] = 0.f;
		Vertex.TexCoords[1] = 0.f;
	}

	const FSlateBrush* Brush = FCoreStyle::Get().GetBrush("BrushName");
	const FSlateResourceHandle ResourceHandle = FSlateApplication::Get().GetRenderer()->GetResourceHandle(*Brush);
	FSlateDrawElement::MakeCustomVerts(
		OutDrawElements,
		LayerId,
		ResourceHandle,
		SlateVertexes,
		Indexes,
		nullptr,
		0,
		0
	);
}

void UZZTWidget::SetValues(TArray<float> Values)
{
	if(!Values.Num())	return ;

	SizeYRec = Values;
}
	
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "ZZTWidget.generated.h"

/**
 * 
 */
UCLASS()
class MYPROJECT_API UZZTWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable)
		void SetValues(TArray<float> Values);
	
protected:
	virtual int32 NativePaint(const FPaintArgs& Args,
		const FGeometry& AllottedGeometry,
		const FSlateRect& MyCullingRect,
		FSlateWindowElementList& OutDrawElements,
		int32 LayerId, 
		const FWidgetStyle& InWidgetStyle, 
		bool bParentEnabled) const override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool isDIYColor;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "isDIYColor"))
		TArray<FColor> ColorsRect;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float Gap;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TArray<float> SizeYRec;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float SizeXRec;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FColor ColorRec;
	
private:
	void DrawRect(FSlateWindowElementList& OutDrawElements,
		const FGeometry& AllocttedGeometry,
		int32 LayerId,
		FVector2D Position,
		FVector2D Size,
		FColor Color)	const;
};



版权声明:本文为qqQQqsadfj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。