先說一下繪制的坐標系 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坐標下