UEノート---バインドエージェントイベント

3667 ワード

一、UUserWidgetサブクラスにおけるSwidgetエージェントイベントコードのバインド例:両方の方法でマクロ展開可能#define BIND_UOBJECT_DELEGATE(Type, Function)\Type::CreateUObject( this, &ThisClass::Function )
void UUMGContentBrowser::ConstructCategoryTree()
{
	TSharedPtr LevelScrollBoxTree = StaticCastSharedPtr(ScrollBox_Level->GetCachedWidget());

	TSharedPtr NewEntry =
		SNew(SCategoryButton)
		.Text(FText::FromString(TEXT(" ")))
		.CategoryTreeLevel(ECategoryTreeLevel::CTL_ALL)
		.Level1Index(-1)
		.Level2Index(-1)
		//.OnCategoryTreeClicked(BIND_UOBJECT_DELEGATE(FOnCategoryTreeClickedDelegate, Callback_OnCategoryTreeClicked));
		.OnCategoryTreeClicked(FOnCategoryTreeClickedDelegate::CreateUObject(this, &UUMGContentBrowser::Callback_OnCategoryTreeClicked));

	LevelScrollBoxTree->AddSlot()
		.Padding(FMargin(0, 5, 0, 0))
		[
			NewEntry.ToSharedRef()
		];
}

二、SLateサブクラスにおけるバインドエージェントイベントコードの例:UE SEditorTutorialsから.cppクラスバインドの方法はすべてSwidgetサブクラスの方法OnBackClicked(FSimpleDelegate::CreateSP(this,&SEditorTutorials::HandleBackClicked))
void SEditorTutorials::RebuildCurrentContent()
{
	UEditorTutorial* CurrentTutorial = OnGetCurrentTutorial.Execute();
	int32 CurrentTutorialStage = OnGetCurrentTutorialStage.Execute();

	OverlayContent = nullptr;
	ContentBox->ClearChildren();
	if(CurrentTutorial != nullptr && CurrentTutorialStage < CurrentTutorial->Stages.Num())
	{
		ContentBox->AddSlot()
		[
			SAssignNew(OverlayContent, STutorialOverlay, CurrentTutorial, &CurrentTutorial->Stages[CurrentTutorialStage])
			.OnClosed(FSimpleDelegate::CreateSP(this, &SEditorTutorials::HandleCloseClicked))
			.IsStandalone(CurrentTutorial->bIsStandalone)
			.ParentWindow(ParentWindow)
			.AllowNonWidgetContent(bIsNavigationWindow)
			.OnBackClicked(FSimpleDelegate::CreateSP(this, &SEditorTutorials::HandleBackClicked))
			.OnHomeClicked(FSimpleDelegate::CreateSP(this, &SEditorTutorials::HandleHomeClicked))
			.OnNextClicked(FSimpleDelegate::CreateSP(this, &SEditorTutorials::HandleNextClicked))
			.IsBackEnabled(this, &SEditorTutorials::IsBackButtonEnabled)
			.IsHomeEnabled(this, &SEditorTutorials::IsHomeButtonEnabled)
			.IsNextEnabled(this, &SEditorTutorials::IsNextButtonEnabled)
			.OnWidgetWasDrawn(OnWidgetWasDrawn)
			.OnWasWidgetDrawn(OnWasWidgetDrawn)
		];
	}
	else
	{
		// create 'empty' overlay, as we may need this for picking visualization
		ContentBox->AddSlot()
		[
			SAssignNew(OverlayContent, STutorialOverlay, nullptr, nullptr)
			.OnClosed(FSimpleDelegate::CreateSP(this, &SEditorTutorials::HandleCloseClicked))
			.IsStandalone(false)
			.ParentWindow(ParentWindow)
			.AllowNonWidgetContent(false)
		];	
	}
}

三、カスタムメソッドにおけるバインドイベントメソッドパラメータが代理イベントFOnCheckStateChangedである
	// Check
	void AddCheckBoxBlock(const FString& InBlockID, EWidgetType::Type InType, const FText& InText, const FOnCheckStateChanged& InOnCheckStateChanged = FOnCheckStateChanged())
	{
		TSharedRef< FDetailEntryBlock > EntryBlock(new FDetailEntryBlock());
		EntryBlock->BlockID = InBlockID;
		EntryBlock->Type = InType;
		EntryBlock->Text = InText;
		EntryBlock->OnCheckStateChanged = InOnCheckStateChanged;
		DetailBuilderList.Add(EntryBlock);
	}

	FDetailBuilder DetailBuilder;
	DetailBuilder.AddCheckBoxBlock(
		TEXT("Check1"),
		EWidgetType::WT_TEXTBLOCK_CHECK,
		FText::FromString(TEXT(" ")),
		FOnCheckStateChanged::CreateUObject(this, &ATutorialsPlayerController::OnCheckStateChangedEvent1)
	);