Prompt widget is pretty good now

This commit is contained in:
2026-05-04 15:52:05 -04:00
parent 3e7e6a2ae4
commit 78c85660c9
3 changed files with 38 additions and 20 deletions

Binary file not shown.

View File

@@ -109,8 +109,12 @@ void UlxPromptWidget::SynchronizeProperties()
MyBrush.SetUVRegion(GetIconUVs(Icon)); MyBrush.SetUVRegion(GetIconUVs(Icon));
MyImage->InvalidateImage(); MyImage->InvalidateImage();
MyImage->SetDesiredSizeOverride(Size); MyBox->SetWidthOverride(Size.X);
MyGlyphSlot->SetPadding(GetScaledMargins()); MyBox->SetHeightOverride(Size.Y);
const float Extra = Depressed ? 0.05f : 0.0f;
const FMargin ExtraPadding(Extra * Size.X, Extra * Size.Y, Extra * Size.X, Extra * Size.Y);
MyImageSlot->SetPadding(ExtraPadding);
MyGlyphSlot->SetPadding(GetScaledMargins() + ExtraPadding);
MyGlyph->SetColorAndOpacity(GlyphColor); MyGlyph->SetColorAndOpacity(GlyphColor);
if (!Glyph.IsEmpty()) if (!Glyph.IsEmpty())
@@ -151,6 +155,15 @@ void UlxPromptWidget::SetGlyphColor(FLinearColor InColor)
} }
} }
void UlxPromptWidget::SetDepressed(bool InDepressed)
{
if (Depressed != InDepressed)
{
Depressed = InDepressed;
SynchronizeProperties();
}
}
void UlxPromptWidget::SetKeys(FKey InGamepadKey, FKey InKeyboardKey) void UlxPromptWidget::SetKeys(FKey InGamepadKey, FKey InKeyboardKey)
{ {
if ((GamepadKey != InGamepadKey) || (KeyboardKey != InKeyboardKey)) if ((GamepadKey != InGamepadKey) || (KeyboardKey != InKeyboardKey))
@@ -200,32 +213,26 @@ TSharedRef<SWidget> UlxPromptWidget::RebuildWidget()
} }
} }
int32 Icon = 0;
FString Glyph;
ChooseAppearance(Icon, Glyph);
MyBrush.SetResourceObject(ButtonAtlas.Get()); MyBrush.SetResourceObject(ButtonAtlas.Get());
MyBrush.ImageSize = Size;
MyBrush.SetUVRegion(GetIconUVs(Icon));
SAssignNew(MyImage, SImage).Image(&MyBrush); SAssignNew(MyImage, SImage).Image(&MyBrush);
EVisibility GlyphVisibility = Glyph.IsEmpty() ? EVisibility::Collapsed : EVisibility::HitTestInvisible; SAssignNew(MyGlyph, STextBlock);
SAssignNew(MyGlyph, STextBlock)
.Text(FText::FromString(Glyph))
.ColorAndOpacity(GlyphColor)
.Visibility(GlyphVisibility);
SAssignNew(MyScaleBox, SScaleBox) SAssignNew(MyScaleBox, SScaleBox)
.Stretch(EStretch::ScaleToFit) .Stretch(EStretch::ScaleToFit)
[ MyGlyph.ToSharedRef() ]; [ MyGlyph.ToSharedRef() ];
MyOverlay = SNew(SOverlay) MyOverlay = SNew(SOverlay);
+ SOverlay::Slot() [ MyImage.ToSharedRef() ]; MyOverlay->AddSlot().Expose(MyImageSlot)
[ MyImage.ToSharedRef() ];
MyOverlay->AddSlot().Padding(GetScaledMargins()).HAlign(HAlign_Fill).VAlign(VAlign_Fill).Expose(MyGlyphSlot) MyOverlay->AddSlot().HAlign(HAlign_Fill).VAlign(VAlign_Fill).Expose(MyGlyphSlot)
[ MyScaleBox.ToSharedRef() ]; [ MyScaleBox.ToSharedRef() ];
return MyOverlay.ToSharedRef(); SAssignNew(MyBox, SBox)
[ MyOverlay.ToSharedRef() ];
SynchronizeProperties();
return MyBox.ToSharedRef();
} }
void UlxPromptWidget::ReleaseSlateResources(bool bReleaseChildren) void UlxPromptWidget::ReleaseSlateResources(bool bReleaseChildren)
@@ -238,9 +245,11 @@ void UlxPromptWidget::ReleaseSlateResources(bool bReleaseChildren)
IDS->OnInputHardwareDeviceChanged.RemoveDynamic(this, &UlxPromptWidget::OnHardwareDeviceChanged); IDS->OnInputHardwareDeviceChanged.RemoveDynamic(this, &UlxPromptWidget::OnHardwareDeviceChanged);
} }
} }
MyBox.Reset();
MyOverlay.Reset(); MyOverlay.Reset();
MyImage.Reset(); MyImage.Reset();
MyScaleBox.Reset(); MyScaleBox.Reset();
MyGlyph.Reset(); MyGlyph.Reset();
MyImageSlot = nullptr;
MyGlyphSlot = nullptr; MyGlyphSlot = nullptr;
} }

View File

@@ -5,6 +5,7 @@
#include "Components/Widget.h" #include "Components/Widget.h"
#include "InputCoreTypes.h" #include "InputCoreTypes.h"
#include "Widgets/SOverlay.h" #include "Widgets/SOverlay.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Layout/SScaleBox.h" #include "Widgets/Layout/SScaleBox.h"
#include "PromptWidget.generated.h" #include "PromptWidget.generated.h"
@@ -39,6 +40,9 @@ public:
UPROPERTY(EditAnywhere, Setter, Category="Prompt") UPROPERTY(EditAnywhere, Setter, Category="Prompt")
FLinearColor GlyphColor = FLinearColor::White; FLinearColor GlyphColor = FLinearColor::White;
UPROPERTY(EditAnywhere, Setter, Category="Prompt")
bool Depressed = false;
public: public:
UFUNCTION(BlueprintCallable, Category="Prompt") UFUNCTION(BlueprintCallable, Category="Prompt")
void SetKeys(FKey InGamepadKey, FKey InKeyboardKey); void SetKeys(FKey InGamepadKey, FKey InKeyboardKey);
@@ -55,6 +59,9 @@ public:
UFUNCTION(BlueprintCallable, Category="Prompt") UFUNCTION(BlueprintCallable, Category="Prompt")
void SetSize(FVector2D InSize); void SetSize(FVector2D InSize);
UFUNCTION(BlueprintCallable, Category="Prompt")
void SetDepressed(bool InDepressed);
UFUNCTION() UFUNCTION()
void OnHardwareDeviceChanged(const FPlatformUserId UserId, const FInputDeviceId DeviceId); void OnHardwareDeviceChanged(const FPlatformUserId UserId, const FInputDeviceId DeviceId);
@@ -65,10 +72,12 @@ protected:
private: private:
FSlateBrush MyBrush; FSlateBrush MyBrush;
TSharedPtr<SBox> MyBox;
TSharedPtr<SOverlay> MyOverlay; TSharedPtr<SOverlay> MyOverlay;
TSharedPtr<SImage> MyImage; TSharedPtr<SImage> MyImage;
TSharedPtr<SScaleBox> MyScaleBox; TSharedPtr<SScaleBox> MyScaleBox;
TSharedPtr<STextBlock> MyGlyph; TSharedPtr<STextBlock> MyGlyph;
SOverlay::FOverlaySlot* MyImageSlot = nullptr;
SOverlay::FOverlaySlot* MyGlyphSlot = nullptr; SOverlay::FOverlaySlot* MyGlyphSlot = nullptr;
FBox2f GetIconUVs(int32 IconIndex); FBox2f GetIconUVs(int32 IconIndex);