I have tried my best to figure this out but my lack of experience with slate and my inability to find related questions or forum posts has left me needing help. The best learning resource I found was Reuben Wards youtube tutorial on designing a UI using slate. I have also gone though all related header files and available Unreal Documentation.
My UI looks and works exactly as intended after writing it in slate, and I understand what the code is doing. I just don’t know how to manually set focus on a SButton either at creation or when a button is pressed on either keyboard or gamepad with slate, something I can do with UButton in c++ with no issue.
I can see that in SWidget there is a boolean bIsHovered but I don’t know how to access the SButton after creating it in the ChildSlot. Here’s my not working attempt.
ChildSlot
[
SNew(SOverlay)
+ SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
[
SNew(SConstraintCanvas)
+ SConstraintCanvas::Slot()
.Anchors(FAnchors(0.05f, 0.05f, .9f, .35f))
.AutoSize(true)
.Alignment(FVector2D(5.f,.5f))
[
SNew(SScaleBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
.Stretch(EStretch::ScaleToFill)
[
SNew(STextBlock)
.ColorAndOpacity(FColor::White)
.Font(TitleTextStyle)
.Margin(FMargin(12.f))
.Text(TitleText)
.Justification(ETextJustify::Left)
]
]
// PlayButton
+ SConstraintCanvas::Slot()
.Anchors(FAnchors(.1f, .4f, .3f, .55f))
[
SNew(SScaleBox)
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Stretch(EStretch::ScaleToFit)
[
SNew(SButton)
.ButtonColorAndOpacity(Transparency)
.OnClicked(this, &SMainMenuWidget::OnPlayClicked)
[
SNew(STextBlock)
.ColorAndOpacity(FLinearColor(1,1,1,1))
.Font(ButtonTextStyle)
.Text(PlayText)
.Justification(ETextJustify::Left)
]
]
]
];
TSharedPtr<SWidget, ESPMode::NotThreadSafe> ActiveButton = ChildSlot.GetChildAt(2);
UWidget* ActiveWidget = Cast<UWidget>(ActiveButton.Get());
if(!ActiveWidget){UE_LOG(LogTemp, Error, TEXT("ActiveWidget Cast Failed!!")); return;}
ActiveWidget->SetFocus();
Removing the last 4 lines results in a successful compile. If more of my code is needed please let me know, I just didn’t want to make this too long as I’m sure that someone that knows slate will have no issue explaining how to do this.
If anyone can show me how to make a SButton hovered by code I would greatly appreciate it!