UE4, 5 Slate Basic


# Moduleの組み込み
```c#:Playground51.Build.cs
    PrivateDependencyModuleNames.AddRange(new string[] {"Slate", "SlateCore"  });
```

# Slateの宣言
```c++:SExample.h
#pragma once
#include "Widgets/SCompoundWidget.h"

class PLAYGROUND51_API SExampleWidget : public SCompoundWidget
{
	SLATE_BEGIN_ARGS(SExampleWidget){}
	SLATE_ARGUMENT(FText, LabelText)
	SLATE_END_ARGS()

public:
	void Construct(const FArguments &InArgs);
	void SetText(FText NewText);
	
protected:
	FText Text;
	FSlateFontInfo Font;
	TSharedPtr<STextBlock> TextBlock;
};
```

```c++:SExample.cpp
#include "SExampleWidget.h"

void SExampleWidget::Construct(const FArguments& InArgs)
{
	bCanSupportFocus = true;
	
	Text = InArgs._LabelText;
	Font = FCoreStyle::Get().GetFontStyle(FName("EmbossedText"));
	Font.Size = 48;
	
	ChildSlot
	[
		SNew(SVerticalBox)
		+SVerticalBox::Slot()
		.VAlign(VAlign_Center)
		.HAlign(HAlign_Center)
		[
			SAssignNew(TextBlock, STextBlock)
			.Text(Text)
			.Font(Font)
			.ColorAndOpacity(FColor::Magenta)
		]
	];
}

void SExampleWidget::SetText(FText NewText)
{
	Text = NewText;
	TextBlock->SetText(Text);
}
```

# その１: HUDで表示させる
```c++:SlateSpawnHUD.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "SlateSpawnHUD.generated.h"

UCLASS()
class PLAYGROUND51_API ASlateSpawnHUD : public AHUD
{
	GENERATED_BODY()

protected:
	virtual void BeginPlay() override;
	
	TSharedPtr<class SExampleWidget> ExampleWidget;

public:
	UFUNCTION(BlueprintCallable)
	void ChangeText(FString NewText);
};
```

```c++:SlateSpawnHUD.cpp
#include "SlateSpawnHUD.h"
#include "SExampleWidget.h"

void ASlateSpawnHUD::BeginPlay()
{
	Super::BeginPlay();
	
	ExampleWidget = SNew(SExampleWidget).LabelText(FText::FromString("This is created by Slate"));
	UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
	ViewportClient->AddViewportWidgetContent(ExampleWidget.ToSharedRef());
}

void ASlateSpawnHUD::ChangeText(FString NewText)
{
	ExampleWidget->SetText(FText::FromString(NewText));
}
```

# GameModeを作って前述のHUDを指定
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24726/2a60ef1c-bd67-ca21-b05e-5eb508ebec88.png)

# 実行して表示
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24726/79fc330f-46b8-cd13-1d8c-da709597f03c.png)

# おまけ
### Level BlueprintでLボタンを押すとメッセージが変わるように設定
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24726/08744e61-9b91-a152-d374-940052a16918.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24726/695e1c47-6a42-6296-b06b-5b3d8d6d329a.png)
[Lebel Blueprint](https://blueprintue.com/render/zag-4g1i/)
<iframe src="https://blueprintue.com/render/zag-4g1i/" scrolling="no" allowfullscreen></iframe>

# その2: UWidget(UUserWidget)でラップして表示
```c++:SlateWrapperWidget.h
#pragma once

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


UCLASS()
class PLAYGROUND51_API USlateWrapperWidget : public UUserWidget
{
	GENERATED_BODY()

protected:
	virtual TSharedRef<SWidget> RebuildWidget() override;
	virtual void ReleaseSlateResources(bool bReleaseChildren) override;

public:
	UPROPERTY(BlueprintReadWrite, Category="Slate", meta=(ExposeOnSpawn="true"))
	FString TextForDisplay;

	UFUNCTION(BlueprintCallable)
	void ChangeText(FString NewText) const;

private:
	TSharedPtr<class SExampleWidget> SlateWidget;
};
```
```c++:SlateWrapperWidget.cpp
#include "SlateWrapperWidget.h"
#include "SExampleWidget.h"

TSharedRef<SWidget> USlateWrapperWidget::RebuildWidget()
{
	SlateWidget = SNew(SExampleWidget).LabelText(FText::FromString(TextForDisplay));
	return SlateWidget.ToSharedRef();
}

void USlateWrapperWidget::ChangeText(FString NewText) const
{
	SlateWidget->SetText(FText::FromString(NewText));
}

void USlateWrapperWidget::ReleaseSlateResources(bool bReleaseChildren)
{
	SlateWidget.Reset();
}
```
:::note info
下記2つをオーバーライドするよ
`protected:`
`	virtual TSharedRef<SWidget> RebuildWidget() override;`
`	virtual void ReleaseSlateResources(bool bReleaseChildren) override;`
:::

# Level Blueprintで上記Widgetを生成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24726/46f9906c-88e9-6730-66ae-dd75df432932.png)

# 参考リンク
[Custom widgets in Unreal](https://snorristurluson.github.io/CustomSlateWidgets/)