// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "Widgets/Input/SComboBox.h" /** Typed SComboBox with a translator to FText. */ template class SMutableTextComboBox : public SComboBox { public: typedef typename SComboBox::FOnSelectionChanged FOnSelectionChanged; typedef typename SComboBox::NullableOptionType NullableOptionType; DECLARE_DELEGATE_RetVal_OneParam(FText, FTranslateDelegate, OptionType); SLATE_BEGIN_ARGS(SMutableTextComboBox) {} // Super arguments. SLATE_ARGUMENT(const TArray*, Options) SLATE_EVENT(FOnSelectionChanged, OnSelectionChanged) SLATE_ARGUMENT(NullableOptionType, InitiallySelectedItem) // Own arguments. /** Translates OptionType to FText. */ SLATE_EVENT(FTranslateDelegate, Translate) SLATE_END_ARGS() void Construct( const FArguments& InArgs) { Translate = InArgs._Translate; SComboBox::Construct( typename SComboBox::FArguments() .OptionsSource(InArgs._Options) .OnSelectionChanged(InArgs._OnSelectionChanged) .InitiallySelectedItem(InArgs._InitiallySelectedItem) .OnGenerateWidget_Lambda([this](OptionType InOption) { return SNew(STextBlock).Text(this, &SMutableTextComboBox::GetText, InOption); }) [ SNew(STextBlock).Text(this, &SMutableTextComboBox::GetSelectedItemText) ] ); } private: /** Translate delegate. */ FTranslateDelegate Translate; /** If the translate delegate is bounded, returns call it and get the FText. */ FText GetText(OptionType Option) const { if (Translate.IsBound()) { return Translate.Execute(Option); } else { return FText(); } } /** Get the the text if there is a selected item. */ FText GetSelectedItemText() const { SMutableTextComboBox* NonConstThis = const_cast*>(this); // GetSelectedItem is non constant if (NullableOptionType NullableSelected = NonConstThis->GetSelectedItem(); TListTypeTraits::IsPtrValid(NullableSelected)) { OptionType ActuallySelected = TListTypeTraits::NullableItemTypeConvertToItemType(NullableSelected); return GetText(ActuallySelected); } else { return FText(); } } };