Files
UnrealEngineUWP/Engine/Source/Runtime/UMG/Private/Components/CheckBox.cpp
daren cheng 36cbbda965 Fix checkbox style on editor utility widgets.
#jira UE-117887
#rb Vincent.Gauthier
#preflight 6148f759315f54000134958f

[CL 17576768 by daren cheng in ue5-main branch]
2021-09-20 18:38:24 -04:00

310 lines
7.3 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Components/CheckBox.h"
#include "Widgets/SNullWidget.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/Input/SCheckBox.h"
#include "Slate/SlateBrushAsset.h"
#include "Styling/UMGCoreStyle.h"
#define LOCTEXT_NAMESPACE "UMG"
/////////////////////////////////////////////////////
// UCheckBox
static FCheckBoxStyle* DefaultCheckboxStyle = nullptr;
#if WITH_EDITOR
static FCheckBoxStyle* EditorCheckboxStyle = nullptr;
#endif
UCheckBox::UCheckBox(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
if (DefaultCheckboxStyle == nullptr)
{
DefaultCheckboxStyle = new FCheckBoxStyle(FUMGCoreStyle::Get().GetWidgetStyle<FCheckBoxStyle>("Checkbox"));
// Unlink UMG default colors.
DefaultCheckboxStyle->UnlinkColors();
}
WidgetStyle = *DefaultCheckboxStyle;
#if WITH_EDITOR
if (EditorCheckboxStyle == nullptr)
{
EditorCheckboxStyle = new FCheckBoxStyle(FCoreStyle::Get().GetWidgetStyle<FCheckBoxStyle>("Checkbox"));
// Unlink UMG Editor colors from the editor settings colors.
EditorCheckboxStyle->UnlinkColors();
}
if (IsEditorWidget())
{
WidgetStyle = *EditorCheckboxStyle;
// The CDO isn't an editor widget and thus won't use the editor style, call post edit change to mark difference from CDO
PostEditChange();
}
#endif // WITH_EDITOR
CheckedState = ECheckBoxState::Unchecked;
HorizontalAlignment = HAlign_Fill;
Padding_DEPRECATED = FMargin(0, 0, 0, 0);
BorderBackgroundColor_DEPRECATED = FLinearColor::White;
ClickMethod = EButtonClickMethod::DownAndUp;
TouchMethod = EButtonTouchMethod::DownAndUp;
IsFocusable = true;
#if WITH_EDITORONLY_DATA
AccessibleBehavior = ESlateAccessibleBehavior::Summary;
bCanChildrenBeAccessible = false;
#endif
}
void UCheckBox::ReleaseSlateResources(bool bReleaseChildren)
{
Super::ReleaseSlateResources(bReleaseChildren);
MyCheckbox.Reset();
}
TSharedRef<SWidget> UCheckBox::RebuildWidget()
{
MyCheckbox = SNew(SCheckBox)
.OnCheckStateChanged( BIND_UOBJECT_DELEGATE(FOnCheckStateChanged, SlateOnCheckStateChangedCallback) )
.Style(&WidgetStyle)
.HAlign( HorizontalAlignment )
.ClickMethod(ClickMethod)
.TouchMethod(TouchMethod)
.PressMethod(PressMethod)
.IsFocusable(IsFocusable)
;
if ( GetChildrenCount() > 0 )
{
MyCheckbox->SetContent(GetContentSlot()->Content ? GetContentSlot()->Content->TakeWidget() : SNullWidget::NullWidget);
}
return MyCheckbox.ToSharedRef();
}
void UCheckBox::SynchronizeProperties()
{
Super::SynchronizeProperties();
MyCheckbox->SetStyle(&WidgetStyle);
MyCheckbox->SetIsChecked( PROPERTY_BINDING(ECheckBoxState, CheckedState) );
}
void UCheckBox::OnSlotAdded(UPanelSlot* InSlot)
{
// Add the child to the live slot if it already exists
if ( MyCheckbox.IsValid() )
{
MyCheckbox->SetContent(InSlot->Content ? InSlot->Content->TakeWidget() : SNullWidget::NullWidget);
}
}
void UCheckBox::OnSlotRemoved(UPanelSlot* InSlot)
{
// Remove the widget from the live slot if it exists.
if ( MyCheckbox.IsValid() )
{
MyCheckbox->SetContent(SNullWidget::NullWidget);
}
}
bool UCheckBox::IsPressed() const
{
if ( MyCheckbox.IsValid() )
{
return MyCheckbox->IsPressed();
}
return false;
}
void UCheckBox::SetClickMethod(EButtonClickMethod::Type InClickMethod)
{
ClickMethod = InClickMethod;
if (MyCheckbox.IsValid())
{
MyCheckbox->SetClickMethod(ClickMethod);
}
}
void UCheckBox::SetTouchMethod(EButtonTouchMethod::Type InTouchMethod)
{
TouchMethod = InTouchMethod;
if (MyCheckbox.IsValid())
{
MyCheckbox->SetTouchMethod(TouchMethod);
}
}
void UCheckBox::SetPressMethod(EButtonPressMethod::Type InPressMethod)
{
PressMethod = InPressMethod;
if (MyCheckbox.IsValid())
{
MyCheckbox->SetPressMethod(PressMethod);
}
}
bool UCheckBox::IsChecked() const
{
if ( MyCheckbox.IsValid() )
{
return MyCheckbox->IsChecked();
}
return ( CheckedState == ECheckBoxState::Checked );
}
ECheckBoxState UCheckBox::GetCheckedState() const
{
if ( MyCheckbox.IsValid() )
{
return MyCheckbox->GetCheckedState();
}
return CheckedState;
}
void UCheckBox::SetIsChecked(bool InIsChecked)
{
CheckedState = InIsChecked ? ECheckBoxState::Checked : ECheckBoxState::Unchecked;
if ( MyCheckbox.IsValid() )
{
MyCheckbox->SetIsChecked(PROPERTY_BINDING(ECheckBoxState, CheckedState));
}
}
void UCheckBox::SetCheckedState(ECheckBoxState InCheckedState)
{
CheckedState = InCheckedState;
if ( MyCheckbox.IsValid() )
{
MyCheckbox->SetIsChecked(PROPERTY_BINDING(ECheckBoxState, CheckedState));
}
}
void UCheckBox::SlateOnCheckStateChangedCallback(ECheckBoxState NewState)
{
CheckedState = NewState;
//@TODO: Choosing to treat Undetermined as Checked
const bool bWantsToBeChecked = NewState != ECheckBoxState::Unchecked;
OnCheckStateChanged.Broadcast(bWantsToBeChecked);
}
void UCheckBox::PostLoad()
{
Super::PostLoad();
if ( GetLinkerUEVersion() < VER_UE4_DEPRECATE_UMG_STYLE_ASSETS )
{
if ( Style_DEPRECATED != nullptr )
{
const FCheckBoxStyle* StylePtr = Style_DEPRECATED->GetStyle<FCheckBoxStyle>();
if ( StylePtr != nullptr )
{
WidgetStyle = *StylePtr;
}
Style_DEPRECATED = nullptr;
}
if ( UncheckedImage_DEPRECATED != nullptr )
{
WidgetStyle.UncheckedImage = UncheckedImage_DEPRECATED->Brush;
UncheckedImage_DEPRECATED = nullptr;
}
if ( UncheckedHoveredImage_DEPRECATED != nullptr )
{
WidgetStyle.UncheckedHoveredImage = UncheckedHoveredImage_DEPRECATED->Brush;
UncheckedHoveredImage_DEPRECATED = nullptr;
}
if ( UncheckedPressedImage_DEPRECATED != nullptr )
{
WidgetStyle.UncheckedPressedImage = UncheckedPressedImage_DEPRECATED->Brush;
UncheckedPressedImage_DEPRECATED = nullptr;
}
if ( CheckedImage_DEPRECATED != nullptr )
{
WidgetStyle.CheckedImage = CheckedImage_DEPRECATED->Brush;
CheckedImage_DEPRECATED = nullptr;
}
if ( CheckedHoveredImage_DEPRECATED != nullptr )
{
WidgetStyle.CheckedHoveredImage = CheckedHoveredImage_DEPRECATED->Brush;
CheckedHoveredImage_DEPRECATED = nullptr;
}
if ( CheckedPressedImage_DEPRECATED != nullptr )
{
WidgetStyle.CheckedPressedImage = CheckedPressedImage_DEPRECATED->Brush;
CheckedPressedImage_DEPRECATED = nullptr;
}
if ( UndeterminedImage_DEPRECATED != nullptr )
{
WidgetStyle.UndeterminedImage = UndeterminedImage_DEPRECATED->Brush;
UndeterminedImage_DEPRECATED = nullptr;
}
if ( UndeterminedHoveredImage_DEPRECATED != nullptr )
{
WidgetStyle.UndeterminedHoveredImage = UndeterminedHoveredImage_DEPRECATED->Brush;
UndeterminedHoveredImage_DEPRECATED = nullptr;
}
if ( UndeterminedPressedImage_DEPRECATED != nullptr )
{
WidgetStyle.UndeterminedPressedImage = UndeterminedPressedImage_DEPRECATED->Brush;
UndeterminedPressedImage_DEPRECATED = nullptr;
}
}
if (GetLinkerUEVersion() < VER_UE4_DEPRECATE_UMG_STYLE_OVERRIDES)
{
WidgetStyle.Padding = Padding_DEPRECATED;
Padding_DEPRECATED = FMargin(0);
if (BorderBackgroundColor_DEPRECATED != FLinearColor::White)
{
WidgetStyle.BorderBackgroundColor = BorderBackgroundColor_DEPRECATED;
BorderBackgroundColor_DEPRECATED = FLinearColor::White;
}
}
}
#if WITH_ACCESSIBILITY
TSharedPtr<SWidget> UCheckBox::GetAccessibleWidget() const
{
return MyCheckbox;
}
#endif
#if WITH_EDITOR
const FText UCheckBox::GetPaletteCategory()
{
return LOCTEXT("Common", "Common");
}
#endif
/////////////////////////////////////////////////////
#undef LOCTEXT_NAMESPACE