UE4 Slate自定义开发记录

先说一下绘制的坐标系 UE4  Slate坐标系 Y轴向下的,其中MakeBox也是向下绘制

FSlateDrawElement::MakeBox(
        OutDrawElements,
        LayerId,
        AllottedGeometry.ToPaintGeometry(FVector2D(0,600), FVector2D(200,300)),
        &SlateBrush,
        ESlateDrawEffect::None,
        FColor::Green
    );

 

 LocalSize 和PainterSize 是固定大小,不随缩放改变,AbsoluteSize 随缩放改变

 如果已经是absolutepos了,就不需要转位置了  AllottedGeometry.ToPaintGeometry().GetAccumulatedRenderTransform().TransformPoint(APos);

AllottedGeometry.ToPaintGeometry().GetLocalSize();   获得显示UI大小

AllottedGeometry.GetAbsoluteSize(); 这个获得的大小不正确,带缩放的

AllottedGeometry.GetAbsolutePosition();  这个也是变化的,带缩放

 AllottedGeometry.Scale;      umg里面越小这个值越小

 

AllottedGeometry.ToPaintGeometry().GetAccumulatedRenderTransform().TransformPoint(APos);   转换顶点位置到UI控件空间

 

FSlateDrawElement::MakeText   这个绘制字体的函数把文字绘制到位置下面

 

 

为了使编辑器正常工作,我们需要在 Slate 小部件中的属性在 UMG 小部件中发生更改时更新它们。我们通过重写SynchronizeProperties函数来做到这一点:

void USlice::SynchronizeProperties()
{
  Super::SynchronizeProperties();
  MySlice->SetBrush(&Brush);
  MySlice->SetAngle(Angle);
  MySlice->SetArcSize(ArcSize);
}

Slate 小部件需要以下 setter 函数:

void SSlateSlice::SetBrush(FSlateBrush* InBrush)
{
  Brush.SetImage(*this, InBrush);
}

void SSlateSlice::SetAngle(float InAngle)
{
  Angle = InAngle;
}

void SSlateSlice::SetArcSize(float InArcSize)
{
  ArcSize = InArcSize;
}

现在,只要在编辑器中更改任何属性,切片就会立即更新。

 

设置Widget类别

除非另行指定,否则Widget在 UMG 设计器的Widget面板中显示为未分类。要设置类别,请添加对GetPaletteCategory函数的覆盖:

#if WITH_EDITOR
const FText USlice::GetPaletteCategory()
{
  return LOCTEXT("CustomPaletteCategory", "My custom category!");
}
#endif

 

获取字符串的绘制大小

const FString ValueString = PercentString; 
FSlateFontInfo ValueFont;
const TSharedRef< FSlateFontMeasure > FontMeasureService = FSlateApplication::Get().GetRenderer()->GetFontMeasureService(); FVector2D TextSize = FontMeasureService->Measure(ValueString, ValueFont);

 

做动画

RegisterActiveTimer

 TWeakPtr<FActiveTimerHandle> ActiveTimerHandle  = RegisterActiveTimer(0.5f, FWidgetActiveTimerDelegate::CreateSP(this, &SMyCompoundWidget::PinDragging));


EActiveTimerReturnType SMyCompoundWidget::PinDragging(double currentTime, float delateTime )
{
    GEngine->AddOnScreenDebugMessage(-1, 5.5f, FColor::Red, FString::Printf(TEXT("UTCPClientConnection Stop ")));


    return EActiveTimerReturnType::Continue;
}

 

FSlateDrawElement::MakeDrawSpaceSpline(
        DrawElementsList,//绘制的元素列表
        LayerId,//层级ID
        Start,//开始
        Delta,//开始的方向
        End,//结束
        DirDelta,//结束的方向
        Params.WireThickness,//线条粗细
        ESlateDrawEffect::None,//绘制效果
        Params.WireColor);//线条颜色

 

protected:
    virtual int32 NativePaint(
        const FPaintArgs& Args, 
        const FGeometry& AllottedGeometry,
        const FSlateRect& MyCullingRect, 
        FSlateWindowElementList& OutDrawElements,
        int32 LayerId, 
        const FWidgetStyle& InWidgetStyle, 
        bool bParentEnabled) const override;
    virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime)override;
    virtual void NativeConstruct()override;

virtual FReply NativeOnMouseMove(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)override;

 如果类是从  SCompoundWidget继承的,下面这些方法比较通用

void Construct(const FArguments& InArgs);

    virtual FReply OnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent);
    virtual FReply OnMouseButtonUp(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent);
    virtual FReply OnMouseMove(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent);
    virtual void OnMouseEnter(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent);
    virtual void OnMouseLeave(const FPointerEvent& MouseEvent);

    virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;

 

FSlateDrawElement::MakeCustomVerts  这个是在Absolute 坐标下的绘制的

FSlateDrawElement::MakeLines          这个是在local  做小下绘制的

AllottedGeometry.ToPaintGeometry().GetAccumulatedRenderTransform().TransformPoint(APos);   这个函数是把local坐标转换成Absolute坐标下

posted on 2022-02-21 17:03  c_dragon  阅读(1183)  评论(0编辑  收藏  举报

导航