UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
KeyFrameManipulator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Curves/KeyHandle.h"
8
9template<typename TimeType> struct TKeyTimeIterator;
10
15template<typename TimeType>
17{
18public:
19 // Pass by value/ref parameter type
21
26 : KeyTimes(KeyTimesParam)
27 , KeyHandleLUT(ExternalKeyHandleLUT)
28 {
29 check(KeyTimes);
30
31 if (!KeyHandleLUT)
32 {
33 KeyHandleLUT = &TemporaryKeyHandleLUT;
34 for (int32 TimeIndex = 0; TimeIndex < KeyTimes->Num(); ++TimeIndex)
35 {
37 }
38 }
39 }
40
42 : KeyTimes(Rhs.KeyTimes)
43 , KeyHandleLUT(Rhs.KeyHandleLUT == &Rhs.TemporaryKeyHandleLUT ? &TemporaryKeyHandleLUT : Rhs.KeyHandleLUT)
44 , TemporaryKeyHandleLUT(Rhs.TemporaryKeyHandleLUT)
45 {}
46
48 {
49 if (&Rhs != this)
50 {
51 KeyTimes = Rhs.KeyTimes;
52 KeyHandleLUT = Rhs.KeyHandleLUT == &Rhs.TemporaryKeyHandleLUT ? &TemporaryKeyHandleLUT : Rhs.KeyHandleLUT;
53 TemporaryKeyHandleLUT = Rhs.TemporaryKeyHandleLUT;
54 }
55 return *this;
56 }
57
59 : KeyTimes(Rhs.KeyTimes)
60 , KeyHandleLUT(Rhs.KeyHandleLUT == &Rhs.TemporaryKeyHandleLUT ? &TemporaryKeyHandleLUT : Rhs.KeyHandleLUT)
61 , TemporaryKeyHandleLUT(MoveTemp(Rhs.TemporaryKeyHandleLUT))
62 {}
63
65 {
66 if (&Rhs != this)
67 {
68 KeyTimes = Rhs.KeyTimes;
69 KeyHandleLUT = Rhs.KeyHandleLUT == &Rhs.TemporaryKeyHandleLUT ? &TemporaryKeyHandleLUT : Rhs.KeyHandleLUT;
70 TemporaryKeyHandleLUT = MoveTemp(Rhs.TemporaryKeyHandleLUT);
71 }
72 return *this;
73 }
74
76
77private:
78
80 virtual void OnKeyAdded(int32 Index) {}
82 virtual void OnKeyRelocated(int32 OldIndex, int32 NewIndex) {}
84 virtual void OnKeyRemoved(int32 Index) {}
86 virtual void OnReset() {}
87
88private:
89
90 virtual FKeyHandle AddKeyImpl(TimeTypeRef InTime) override { return AddKey(InTime); }
91 virtual void SetKeyTimeImpl(FKeyHandle KeyHandle, TimeTypeRef NewTime) override { SetKeyTime(KeyHandle, NewTime); }
92 virtual void RemoveKeyImpl(FKeyHandle KeyHandle) override { RemoveKey(KeyHandle); }
93 virtual TOptional<TimeType> GetKeyTimeImpl(FKeyHandle KeyHandle) const override { return GetKeyTime(KeyHandle); }
94 virtual TOptional<FKeyHandle> FindKeyImpl(const TFunctionRef<bool(TimeTypeRef)>& InPredicate) const override { return FindKey(InPredicate); }
95 virtual TKeyTimeIterator<TimeType> IterateKeysImpl() const override { return IterateKeys(); }
96
97public:
98
106 {
107 int32 InsertIndex = ComputeInsertIndex(InTime);
108
109 FKeyHandle Handle = InsertKeyImpl(InTime, InsertIndex);
110 OnKeyAdded(InsertIndex);
111
112 return Handle;
113 }
114
121 void SetKeyTime(FKeyHandle KeyHandle, TimeTypeRef NewTime)
122 {
123 const int32 ExistingIndex = KeyHandleLUT->GetIndex(KeyHandle);
125 {
126 return;
127 }
128
129 (*KeyTimes)[ExistingIndex] = NewTime;
130
131 int32 NewIndex = ComputeInsertIndex(NewTime, ExistingIndex);
132 if (NewIndex != ExistingIndex)
133 {
134 // Need to move the key
136 OnKeyRelocated(ExistingIndex, NewIndex);
137 }
138 }
139
140
146 void RemoveKey(FKeyHandle KeyHandle)
147 {
148 int32 RemoveAtIndex = GetIndex(KeyHandle);
149 if (RemoveAtIndex != INDEX_NONE)
150 {
151 KeyTimes->RemoveAtSwap(RemoveAtIndex, EAllowShrinking::No);
152 KeyHandleLUT->DeallocateHandle(RemoveAtIndex);
153
154 OnKeyRemoved(RemoveAtIndex);
155 }
156 }
157
161 void Reset()
162 {
163 KeyTimes->Empty();
164 KeyHandleLUT->Reset();
165 OnReset();
166 }
167
168public:
169
177 {
178 const int32 Index = KeyHandleLUT->GetIndex(KeyHandle);
179 return Index == INDEX_NONE ? TOptional<TimeType>() : (*KeyTimes)[Index];
180 }
181
189 {
190 for (int32 Index = 0; Index < KeyTimes->Num(); ++Index)
191 {
192 if (InPredicate((*KeyTimes)[Index]))
193 {
194 return KeyHandleLUT->FindOrAddKeyHandle(Index);
195 }
196 }
197
198 return TOptional<FKeyHandle>();
199 }
200
210
211protected:
212
217 {
218 for(; StartAtIndex < KeyTimes->Num() && (*KeyTimes)[StartAtIndex] < InTime; ++StartAtIndex);
219 return StartAtIndex;
220 }
221
226 {
227 KeyTimes->Insert(Time, InsertIndex);
228 return KeyHandleLUT->AllocateHandle(InsertIndex);
229 }
230
235 {
236 return (*KeyTimes)[KeyIndex];
237 }
238
242 int32 GetIndex(FKeyHandle KeyHandle) const
243 {
244 return KeyHandleLUT->GetIndex(KeyHandle);
245 }
246
251 {
252 return KeyTimes->IsValidIndex(Index) ? KeyHandleLUT->FindOrAddKeyHandle(Index) : FKeyHandle();
253 }
254
258 void RelocateKeyImpl(int32 OldIndex, int32 NewIndex)
259 {
260 if (OldIndex == NewIndex || OldIndex < 0 || NewIndex < 0)
261 {
262 return;
263 }
264
265 check(KeyTimes->IsValidIndex(OldIndex));
266
267 TimeType Time = (*KeyTimes)[OldIndex];
268
269 KeyTimes->RemoveAtSwap(OldIndex, EAllowShrinking::No);
270 KeyTimes->Insert(Time, NewIndex);
271
272 KeyHandleLUT->MoveHandle(OldIndex, NewIndex);
273 }
274
275private:
276
278
280 TArray<TimeType>* KeyTimes;
281
283 FKeyHandleLookupTable* KeyHandleLUT;
284
286 mutable FKeyHandleLookupTable TemporaryKeyHandleLUT;
287};
#define check(expr)
Definition AssertionMacros.h:314
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition IKeyFrameManipulator.h:17
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_FORCEINLINE_HINT void RemoveAtSwap(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2185
UE_NODEBUG UE_FORCEINLINE_HINT bool IsValidIndex(SizeType Index) const
Definition Array.h:1122
SizeType Insert(std::initializer_list< ElementType > InitList, const SizeType InIndex)
Definition Array.h:1875
void Empty(SizeType Slack=0)
Definition Array.h:2273
Definition AssetRegistryState.h:50
Definition KeyFrameManipulator.h:17
TKeyFrameManipulator(TKeyFrameManipulator &&Rhs)
Definition KeyFrameManipulator.h:58
TimeType GetKeyTimeChecked(int32 KeyIndex) const
Definition KeyFrameManipulator.h:234
FKeyHandle GetKeyHandleFromIndex(int32 Index) const
Definition KeyFrameManipulator.h:250
TKeyFrameManipulator(TArray< TimeType > *KeyTimesParam, FKeyHandleLookupTable *ExternalKeyHandleLUT=nullptr)
Definition KeyFrameManipulator.h:25
FKeyHandle AddKey(TimeTypeRef InTime)
Definition KeyFrameManipulator.h:105
void Reset()
Definition KeyFrameManipulator.h:161
TOptional< FKeyHandle > FindKey(const TFunctionRef< bool(TimeTypeRef)> &InPredicate) const
Definition KeyFrameManipulator.h:188
void RemoveKey(FKeyHandle KeyHandle)
Definition KeyFrameManipulator.h:146
int32 GetIndex(FKeyHandle KeyHandle) const
Definition KeyFrameManipulator.h:242
void RelocateKeyImpl(int32 OldIndex, int32 NewIndex)
Definition KeyFrameManipulator.h:258
TKeyFrameManipulator(const TKeyFrameManipulator &Rhs)
Definition KeyFrameManipulator.h:41
FKeyHandle InsertKeyImpl(TimeTypeRef Time, int32 InsertIndex) const
Definition KeyFrameManipulator.h:225
TKeyFrameManipulator & operator=(TKeyFrameManipulator &&Rhs)
Definition KeyFrameManipulator.h:64
int32 ComputeInsertIndex(TimeTypeRef InTime, int32 StartAtIndex=0) const
Definition KeyFrameManipulator.h:216
virtual ~TKeyFrameManipulator()
Definition KeyFrameManipulator.h:75
TKeyTimeIterator< TimeType > IterateKeys() const
Definition KeyFrameManipulator.h:206
TOptional< TimeType > GetKeyTime(FKeyHandle KeyHandle) const
Definition KeyFrameManipulator.h:176
TCallTraits< TimeType >::ParamType TimeTypeRef
Definition KeyFrameManipulator.h:20
void SetKeyTime(FKeyHandle KeyHandle, TimeTypeRef NewTime)
Definition KeyFrameManipulator.h:121
TKeyFrameManipulator & operator=(const TKeyFrameManipulator &Rhs)
Definition KeyFrameManipulator.h:47
U16 Index
Definition radfft.cpp:71
Definition KeyHandle.h:138
ENGINE_API void Reset()
Definition KeyHandle.cpp:266
ENGINE_API void DeallocateHandle(int32 Index)
Definition KeyHandle.cpp:244
ENGINE_API void MoveHandle(int32 OldIndex, int32 NewIndex)
Definition KeyHandle.cpp:209
ENGINE_API FKeyHandle FindOrAddKeyHandle(int32 Index)
Definition KeyHandle.cpp:193
ENGINE_API int32 GetIndex(FKeyHandle KeyHandle)
Definition KeyHandle.cpp:187
ENGINE_API FKeyHandle AllocateHandle(int32 Index)
Definition KeyHandle.cpp:229
Definition KeyHandle.h:15
TCallTraitsParamTypeHelper< T, PassByValue >::ParamType ParamType
Definition UnrealTypeTraits.h:275
Definition IKeyFrameManipulator.h:135
Definition Optional.h:131