UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SWidgetCarousel.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 SWidgetCarousel.h: Declares the SWidgetCarousel class.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
11#include "Types/SlateEnums.h"
12#include "Widgets/SWidget.h"
15#include "Widgets/SBoxPanel.h"
16#include "Widgets/SOverlay.h"
19
22{
23 enum Type
24 {
25 // Scroll the carousel left.
27 // Scroll the carousel right.
29 // Center the carousel
31 };
32}
33
35struct FCarouselDisplayItem : public TSharedFromThis<FCarouselDisplayItem>
36{
41 : OpacityValue( 0.f )
42 , SlideValue( 0.f )
43 , DesiredOpacityValue( 0.f )
44 , SlideValueLeftLimit( -1.0f )
45 , SlideValueRightLimit( 1.0f )
46 , PeakValueLeftLimit(-0.05f)
47 , PeakValueRightLimit(0.05f)
48 , OptimalSlideValue( 0.0f )
49 , DesiredSlideValue( 0.f )
51 , bPeak(false)
52 , bFade( false )
53 , MoveSpeed( 5.0f )
54 , PeakSpeed(.2f)
55 , PeakDistance(0.05f)
56 , FadeRate( 2.0f )
58 {
59 // Create default widget
61 .IgnoreClipping(false);
62
64 SlideInCurve.AddCurve(0.1f, 0.5f);
65 }
66
79
113
118 void Tick( float DeltaTime )
119 {
120 if ( bTransition )
121 {
124 }
125 else if ( bPeak )
126 {
129 }
130
131 if ( bFade )
132 {
135 }
136 }
137
146 bool BlendWidget( const float& DeltaTime, const float& DesiredValue, const float& Speed, float& OutCurrentValue )
147 {
148 bool bStillBlending = true;
149
150 float DesiredBlendSpeed = Speed * DeltaTime;
151 float BlendDif = OutCurrentValue - DesiredValue;
152
153 if (bPeak)
154 {
155 if (FMath::Abs(BlendDif) > PeakDistance + (float)FLOAT_NORMAL_THRESH)
156 {
159 }
160 float Lerp = FMath::Max(SlideInCurve.GetLerp(), 0.1f);
161 DesiredBlendSpeed = Speed * Lerp * DeltaTime;
162 }
163
164 if (FMath::Abs(DesiredBlendSpeed) > FMath::Abs(BlendDif))
165 {
166 OutCurrentValue = DesiredValue;
167 bStillBlending = false;
168 }
169 else
170 {
171 // Reverse Direction if sliding left
172 if ( BlendDif > 0.f )
173 {
174 DesiredBlendSpeed *= -1.f;
175 }
176
178 }
179
180 return bStillBlending;
181 }
182
188 {
190 .IgnoreClipping(false)
191 .VisualOffset( this, &FCarouselDisplayItem::GetSlide )
192 [
193// SNew(SOverlay)
194// + SOverlay::Slot()
195// [
197// ]
198// + SOverlay::Slot()
199// .HAlign(HAlign_Right)
200// [
201// SNew(STextBlock)
202// .Text(this, &FCarouselDisplayItem::GetPositionText)
203// ]
204// + SOverlay::Slot()
205// .HAlign(HAlign_Left)
206// [
207// SNew(STextBlock)
208// .Text(this, &FCarouselDisplayItem::GetPositionText)
209// ]
210 ];
211 }
212
214 {
215 if (FXWidget.IsValid())
216 {
217 FChildren* Children = FXWidget->GetChildren();
218 if (Children && Children->Num() > 0)
219 {
220 return Children->GetChildAt(0);
221 }
222 }
223
225 }
226
227// FText GetPositionText() const
228// {
229// return FText::FromString(FString::Printf(TEXT("%.2f, %.2f, %.2f"), SlideValue, DesiredSlideValue, SlideInCurve.GetLerp()));
230// }
231
240
245 void SetOpacity( float InOpacity )
246 {
247 OpacityValue = InOpacity;
249 }
250
256
262 void SetSlide( float InSlide, bool bClearTransition = false)
263 {
265
267 {
268 bPeak = false;
269 bTransition = false;
271 }
272 }
273
279 {
281 }
282
291
300
305
310
319
325 {
327 }
328
330 {
331 return bTransition || bFade;
332 }
333
334 const float GetSlideValue() const
335 {
336 return SlideValue;
337 }
338
339 const float GetMoveSpeed() const
340 {
341 return MoveSpeed;
342 }
343
344protected:
345
351 {
352 return FVector2D( SlideValue, 0.f );
353 }
354
355protected:
356 // Holds the Opacity Value. 0 == transparent
358
359 // The slide position
361
362 // The desired opacity value
364
365 // The max left position
367
368 // The max right position
370
371 // The left peak position
373
374 // The right peak position
376
377 // The slide value to display the widget on screen
379
380 // The desired slide value
382
383 // Holds if widget is in transition
385
386 // Hold if the widget is peaking
387 bool bPeak;
388
389 // Holds if the widget is fading in or out
390 bool bFade;
391
392 // Holds if the widget should fade in or out
394
395 // The maximum move speed of the widget
397
398 // The Peak speed
400
401 // The Peak Distance
403
404 // The amount by which a widget fades per frame
405 float FadeRate;
406
407 // Holds the FX widget that does the transitioning
409
410 // Holds the peak curve
412
413 // The current visibility of the display item
415};
416
422template <typename ItemType>
424 : public SCompoundWidget
425{
426public:
427
430
432 : _WidgetItemsSource( static_cast<const TArray<ItemType>*>(NULL) )
433 , _MoveSpeed( 1.f )
434 , _SlideValueLeftLimit( -1.f )
435 , _SlideValueRightLimit( 1.f )
436 , _PeakValueLeftLimit(-0.05f)
437 , _PeakValueRightLimit(0.05f)
438 , _FadeRate( 1.0f )
439 {
441 }
442
444 SLATE_EVENT( FOnGenerateWidget, OnGenerateWidget )
448 SLATE_ARGUMENT( const TArray<ItemType>*, WidgetItemsSource )
461
463
464public:
468 void Construct( const FArguments& InArgs )
469 {
470 OnGenerateWidget = InArgs._OnGenerateWidget;
471 OnPageChanged = InArgs._OnPageChanged;
472 ItemsSource = InArgs._WidgetItemsSource;
473 WidgetIndex = 0;
474 MoveSpeed = InArgs._MoveSpeed.Get();
475 SlideValueLeftLimit = InArgs._SlideValueLeftLimit.Get();
476 SlideValueRightLimit = InArgs._SlideValueRightLimit.Get();
477 PeakValueLeftLimit = InArgs._PeakValueLeftLimit.Get();
478 PeakValueRightLimit = InArgs._PeakValueRightLimit.Get();
479
480 FadeRate = InArgs._FadeRate.Get();
481
485
486 // Set up the Carousel Widget
491
496
501
502 // Create the widget
504 [
506 ];
507
509
510 OnPageChanged.ExecuteIfBound(WidgetIndex);
511 }
512
514 {
515 const TArray<ItemType>& ItemsSourceRef = (*ItemsSource);
516 if (ItemsSourceRef.Num() > 0)
517 {
518 if (WidgetIndex >= ItemsSourceRef.Num())
519 {
521 }
522
526
527 // Each carousel widget must have unique content since Slate Global Invalidation requires that widgets only exist in a single slot
532 // CenterCarouselWidget is the one that displays the active child. Needs to be populated last for the Widget Path to match the visible hierarchy.
534 }
535
536 if (ItemsSourceRef.Num() == 1)
537 {
540 }
541 else
542 {
545 }
546
549 }
550
556 {
557 return WidgetIndex;
558 }
559
566 {
567 if (!IsInTransition())
568 {
569 // Don't change if the widget index is the same
570 if (Index != WidgetIndex)
571 {
572 // Choose the scroll direction
575
577 }
578 }
579 }
580
591
602
604 {
605 if (!IsInTransition())
606 {
607 CenterCarouselWidget->PeakIn(Direction);
608 RightCarouselWidget->PeakIn(Direction);
609 LeftCarouselWidget->PeakIn(Direction);
610 }
611 }
612
618 {
620 if ( ItemsSource->Num() )
621 {
623 }
624 }
625
627 {
629 }
630
640
641 float GetMoveSpeed() const
642 {
644 }
645
653
654protected:
655
679
684 {
685 // Register for an active update every 2.5 seconds
686 RegisterActiveTimer(0.0f, FWidgetActiveTimerDelegate::CreateSP(this, &SWidgetCarousel::RegisteredTick));
687
688 const TArray<ItemType>& ItemsSourceRef = (*ItemsSource);
689
693
695
696 if (ItemsSourceRef.Num() > 0)
697 {
699 {
709 }
710 else
711 {
721 }
722 }
723
724 if (ItemsSourceRef.IsValidIndex(OverrideWidgetIndex))
725 {
726 // Need to find a way to clear left and right images
729 }
730
731 // Ensure the 3 CarouselWidgets here have unique widgets, if that isn't the case, prioritize the 2 widgets that will display during the transition
737
743
744 OnPageChanged.ExecuteIfBound(WidgetIndex);
745 }
746
767
769 {
771 if (LeftWidgetIndex < 0)
772 {
773 LeftWidgetIndex = FMath::Max(0, ItemsSource->Num() - 1);
774 }
775 return LeftWidgetIndex;
776 }
777
779 {
782 {
784 }
785 return RightWidgetIndex;
786 }
787
792
802
803protected:
804
805 // Holds the CarouselWidgets
809
810 // Pointer to the array of data items that we are observing
812
813 // Hold the delegate to be invoked when a widget changes.
815
816 // Hold the delegate to be invoked when the current page changes.
818
819 // Holds the widget display box.
821
822 // Holds the visible widget index.
824
825 // Holds the move speed
827
828 // Holds The left limit
830
831 // The right limit
833
834 // Holds The peak left limit
836
837 // The peak right limit
839
840 // The fade rate
841 float FadeRate;
842};
#define NULL
Definition oodle2base.h:134
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable(ObjectType *InObject)
Definition SharedPointer.h:1947
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define SAssignNew(ExposeAs, WidgetType,...)
Definition DeclarativeSyntaxSupport.h:41
#define SNew(WidgetType,...)
Definition DeclarativeSyntaxSupport.h:37
#define SLATE_ATTRIBUTE(AttrType, AttrName)
Definition DeclarativeSyntaxSupport.h:192
#define SLATE_EVENT(DelegateName, EventName)
Definition DeclarativeSyntaxSupport.h:458
#define SLATE_END_ARGS()
Definition DeclarativeSyntaxSupport.h:116
#define SLATE_ARGUMENT(ArgType, ArgName)
Definition DeclarativeSyntaxSupport.h:208
UE::Math::TVector2< double > FVector2D
Definition MathFwd.h:48
EActiveTimerReturnType
Definition SlateEnums.h:329
#define FLOAT_NORMAL_THRESH
Definition UnrealMathUtility.h:95
Definition ChildrenBase.h:27
SLATECORE_API void ClearChildren()
Definition SBoxPanel.cpp:216
Definition SCompoundWidget.h:22
virtual SLATECORE_API void SetVisibility(TAttribute< EVisibility > InVisibility) override final
Definition SCompoundWidget.cpp:91
FCompoundWidgetOneChildSlot ChildSlot
Definition SCompoundWidget.h:113
virtual SLATECORE_API FChildren * GetChildren() override
Definition SCompoundWidget.cpp:53
void SetColorAndOpacity(TAttribute< FLinearColor > InColorAndOpacity)
Definition SCompoundWidget.h:60
Definition SFxWidget.h:20
Definition SBoxPanel.h:171
FScopedWidgetSlotArguments AddSlot()
Definition SBoxPanel.h:285
static SLATECORE_API TSharedRef< class SWidget > NullWidget
Definition SNullWidget.h:22
Definition SOverlay.h:44
static SLATECORE_API FOverlaySlot::FSlotArguments Slot(int32 ZOrder=0)
Definition SOverlay.cpp:120
Definition SWidgetCarousel.h:425
void SetActiveWidgetIndex(int32 Index)
Definition SWidgetCarousel.h:565
int32 GetRightWidgetIndex(int32 Index)
Definition SWidgetCarousel.h:778
float GetPrimarySlide()
Definition SWidgetCarousel.h:626
int32 GetLeftWidgetIndex(int32 Index)
Definition SWidgetCarousel.h:768
int32 GetWidgetIndex()
Definition SWidgetCarousel.h:555
TSharedPtr< FCarouselDisplayItem > LeftCarouselWidget
Definition SWidgetCarousel.h:806
int32 WidgetIndex
Definition SWidgetCarousel.h:823
float GetMoveSpeed() const
Definition SWidgetCarousel.h:641
void SetItemSource(const TArray< ItemType > *InWidgetItemsSource)
Definition SWidgetCarousel.h:617
TSlateDelegates< ItemType >::FOnGenerateWidget FOnGenerateWidget
Definition SWidgetCarousel.h:429
void GenerateCurrentWidgets()
Definition SWidgetCarousel.h:513
void SetNextWidget()
Definition SWidgetCarousel.h:584
float PeakValueRightLimit
Definition SWidgetCarousel.h:838
float SlideValueRightLimit
Definition SWidgetCarousel.h:832
TSharedPtr< FCarouselDisplayItem > CenterCarouselWidget
Definition SWidgetCarousel.h:807
float FadeRate
Definition SWidgetCarousel.h:841
void ResetTransition()
Definition SWidgetCarousel.h:646
float MoveSpeed
Definition SWidgetCarousel.h:826
void SetPreviousWidget()
Definition SWidgetCarousel.h:595
TSharedPtr< FCarouselDisplayItem > RightCarouselWidget
Definition SWidgetCarousel.h:808
void Construct(const FArguments &InArgs)
Definition SWidgetCarousel.h:468
float SlideValueLeftLimit
Definition SWidgetCarousel.h:829
void SwapBuffer(EWidgetCarouselScrollDirection::Type ScrollDirection, int32 OverrideWidgetIndex=INDEX_NONE)
Definition SWidgetCarousel.h:683
bool IsInTransition()
Definition SWidgetCarousel.h:788
TSharedPtr< SHorizontalBox > WidgetDisplayBox
Definition SWidgetCarousel.h:820
float PeakValueLeftLimit
Definition SWidgetCarousel.h:835
void GenerateWidgets()
Definition SWidgetCarousel.h:659
FOnGenerateWidget OnGenerateWidget
Definition SWidgetCarousel.h:814
void Peak(EWidgetCarouselScrollDirection::Type Direction)
Definition SWidgetCarousel.h:603
void SetMoveSpeed(float InMoveSpeed)
Definition SWidgetCarousel.h:631
EActiveTimerReturnType RegisteredTick(double InCurrentTime, float InDeltaTime)
Definition SWidgetCarousel.h:793
FOnCarouselPageChanged OnPageChanged
Definition SWidgetCarousel.h:817
const TArray< ItemType > * ItemsSource
Definition SWidgetCarousel.h:811
void SetSliderLimits()
Definition SWidgetCarousel.h:747
DECLARE_DELEGATE_OneParam(FOnCarouselPageChanged, int32)
SLATE_BEGIN_ARGS(SWidgetCarousel< ItemType >)
Definition SWidgetCarousel.h:431
SLATECORE_API TSharedRef< FActiveTimerHandle > RegisterActiveTimer(float TickPeriod, FWidgetActiveTimerDelegate TickFunction)
Definition SWidget.cpp:1821
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition SharedPointer.h:1640
TSharedRef< ObjectType, Mode > ToSharedRef() const &
Definition SharedPointer.h:1028
UE_FORCEINLINE_HINT const bool IsValid() const
Definition SharedPointer.h:1085
Definition SharedPointer.h:153
Definition SlateDelegates.h:134
Definition SWidgetCarousel.h:22
Type
Definition SWidgetCarousel.h:24
@ Carousel_Right
Definition SWidgetCarousel.h:28
@ Carousel_Left
Definition SWidgetCarousel.h:26
@ Carousel_Center
Definition SWidgetCarousel.h:30
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
Definition Visibility.h:12
static SLATECORE_API const EVisibility Visible
Definition Visibility.h:14
static SLATECORE_API const EVisibility Collapsed
Definition Visibility.h:17
Definition SWidgetCarousel.h:36
FCurveSequence SlideInCurve
Definition SWidgetCarousel.h:411
void SetSliderOptimalPostion(float DesiredOptimalSlideValue)
Definition SWidgetCarousel.h:315
void SetSliderRightLimit(float DesiredRightLimit)
Definition SWidgetCarousel.h:296
bool bPeak
Definition SWidgetCarousel.h:387
const float GetSlideValue() const
Definition SWidgetCarousel.h:334
EVisibility Visibility
Definition SWidgetCarousel.h:414
float PeakValueLeftLimit
Definition SWidgetCarousel.h:372
void SetFadeRate(float DesiredFadeRate)
Definition SWidgetCarousel.h:324
FCarouselDisplayItem()
Definition SWidgetCarousel.h:40
float OpacityValue
Definition SWidgetCarousel.h:357
void SetSliderLeftLimit(float DesiredLeftLimit)
Definition SWidgetCarousel.h:287
float MoveSpeed
Definition SWidgetCarousel.h:396
void ScrollIn(EWidgetCarouselScrollDirection::Type ScrollDirection)
Definition SWidgetCarousel.h:71
float PeakDistance
Definition SWidgetCarousel.h:402
float SlideValueRightLimit
Definition SWidgetCarousel.h:369
float DesiredSlideValue
Definition SWidgetCarousel.h:381
float OptimalSlideValue
Definition SWidgetCarousel.h:378
float PeakSpeed
Definition SWidgetCarousel.h:399
void SetOpacity(float InOpacity)
Definition SWidgetCarousel.h:245
float SlideValue
Definition SWidgetCarousel.h:360
void SetSliderPeakRightLimit(float DesiredLimit)
Definition SWidgetCarousel.h:306
float SlideValueLeftLimit
Definition SWidgetCarousel.h:366
TSharedPtr< SFxWidget > FXWidget
Definition SWidgetCarousel.h:408
bool BlendWidget(const float &DeltaTime, const float &DesiredValue, const float &Speed, float &OutCurrentValue)
Definition SWidgetCarousel.h:146
float FadeRate
Definition SWidgetCarousel.h:405
FVector2D GetSlide() const
Definition SWidgetCarousel.h:350
void SetSlide(float InSlide, bool bClearTransition=false)
Definition SWidgetCarousel.h:262
bool IsInTransition()
Definition SWidgetCarousel.h:329
bool bFade
Definition SWidgetCarousel.h:390
void SetVisibility(EVisibility InVisibility)
Definition SWidgetCarousel.h:251
TSharedPtr< SFxWidget > GetSFXWidget()
Definition SWidgetCarousel.h:236
void SetSliderPeakLeftLimit(float DesiredLimit)
Definition SWidgetCarousel.h:301
void Tick(float DeltaTime)
Definition SWidgetCarousel.h:118
const float GetMoveSpeed() const
Definition SWidgetCarousel.h:339
void SetWidgetContent(TSharedRef< SWidget > InContent)
Definition SWidgetCarousel.h:187
bool bFadeInAndOut
Definition SWidgetCarousel.h:393
TSharedRef< SWidget > GetWidgetContent() const
Definition SWidgetCarousel.h:213
float PeakValueRightLimit
Definition SWidgetCarousel.h:375
void PeakIn(EWidgetCarouselScrollDirection::Type ScrollDirection)
Definition SWidgetCarousel.h:84
void SetMoveSpeed(float DesiredMoveSpeed)
Definition SWidgetCarousel.h:278
float DesiredOpacityValue
Definition SWidgetCarousel.h:363
bool bTransition
Definition SWidgetCarousel.h:384
Definition CurveSequence.h:26
SLATECORE_API FCurveHandle AddCurve(const float InStartTimeSeconds, const float InDurationSeconds, const ECurveEaseFunction InEaseFunction=ECurveEaseFunction::Linear)
Definition CurveSequence.cpp:49
SLATECORE_API void PlayReverse(const TSharedRef< SWidget > &InOwnerWidget, bool bPlayLooped=false, const float StartAtTime=0.0f, bool bRequiresActiveTimer=true)
Definition CurveSequence.cpp:118
SLATECORE_API void JumpToEnd()
Definition CurveSequence.cpp:229
SLATECORE_API float GetLerp() const
Definition CurveSequence.cpp:254
Definition Color.h:48