UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ProgressCancel.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of geometry3cpp ProgressCancel
4
5#pragma once
6
7#include "Containers/Array.h"
9#include "HAL/Platform.h"
11#include "MathUtil.h"
12#include "Misc/DateTime.h"
13#include "Misc/ScopeLock.h"
14#include "Templates/Function.h"
16
17#include <atomic>
18
19class FProgressCancel;
20
21
22namespace UE
23{
24namespace Geometry
25{
26
27
50
74
75
80{
82 Success = 0,
84 InProgress = 1,
86 Cancelled = 2,
88 PartialResult = 3,
90 Failure = 4
91};
92
93
170
171
172
173} // end namespace UE::Geometry
174} // end namespace UE
175
176
177
178
179
187{
188private:
189 bool WasCancelled = false; // will be set to true if CancelF() ever returns true
190
191 // Progress tracking data
192 struct FProgressData
193 {
194 // Active range of progress tracking -- progress should not go out of this range
195 float CurrentMin = 0;
196 float CurrentMax = 1;
197 // Number of nested scopes currently affecting the active range
198 int ScopeDepth = 0;
199 // Message describing the current work being done
200 FText Message;
201
202 float Range() const
203 {
204 return CurrentMax - CurrentMin;
205 }
206
207 // Advance progress without going beyond the current range
208 float SafeAdvance(float CurrentFraction, float AdvanceAmount) const
209 {
210 return FMathf::Min(CurrentMax, CurrentFraction + Range() * AdvanceAmount);
211 }
212 };
213 FProgressData Progress;
214 FText ProgressMessage;
215 int MaxMessageDepth = -1; // If set to a positive value, message will not be updated by scopes below this depth
216
217 // Current total progress, in range [0, 1]
218 std::atomic<float> ProgressFraction = 0;
219
220 // critical section for accesses to the Progress Message
221 mutable FCriticalSection MessageCS;
222
223 GEOMETRYCORE_API void StartWorkScope(FProgressData& SaveProgressFrameOut, float StepSize, const FText& Message);
224
225 GEOMETRYCORE_API void EndWorkScope(const FProgressData& SavedProgressFrame);
226
227public:
228 TFunction<bool()> CancelF = []() { return false; };
229
230 void SetMaxMessageDepth(int32 ScopeDepth)
231 {
232 MaxMessageDepth = ScopeDepth;
233 }
234
236 {
237 MaxMessageDepth = -1;
238 }
239
244 {
245 if (WasCancelled)
246 {
247 return true;
248 }
249 WasCancelled = CancelF();
250 return WasCancelled;
251 }
252
253 friend class FProgressScope;
254
255 // Simple helper to track progress in a local scope on an optional FProgressCancel
256 // Will still work if the ProgressCancel is null (just does nothing in that case)
258 {
259 FProgressCancel* ProgressCancel;
260 FProgressData SavedProgressData;
261 bool bEnded = false;
262
263 public:
264
269 GEOMETRYCORE_API FProgressScope(FProgressCancel* ProgressCancel, float ProgressAmount, const FText& Message = FText());
270
274 FProgressScope() : ProgressCancel(nullptr)
275 {}
276
279 FProgressScope(FProgressScope&& Other) noexcept : ProgressCancel(Other.ProgressCancel), SavedProgressData(MoveTemp(Other.SavedProgressData)), bEnded(Other.bEnded)
280 {
281 Other.ProgressCancel = nullptr;
282 Other.bEnded = true;
283 }
285 {
286 if (this != &Other)
287 {
288 ProgressCancel = Other.ProgressCancel;
289 SavedProgressData = MoveTemp(Other.SavedProgressData);
290 bEnded = Other.bEnded;
291 Other.ProgressCancel = nullptr;
292 Other.bEnded = true;
293 }
294 return *this;
295 }
296
298 {
299 if (!bEnded)
300 {
301 Done();
302 }
303 }
304
308 GEOMETRYCORE_API void Done();
309
313 inline void AdvanceProgressBy(float Amount)
314 {
315 if (ProgressCancel)
316 {
317 ProgressCancel->AdvanceCurrentScopeProgressBy(Amount);
318 }
319 }
320
327 {
328 if (ProgressCancel)
329 {
331 }
332 }
333
338 {
339 if (ProgressCancel)
340 {
341 return ProgressCancel->GetCurrentScopeDistanceTo(TargetProgressFrac);
342 }
343
344 return 0;
345 }
346
350 inline float GetProgress()
351 {
352 if (ProgressCancel)
353 {
354 return ProgressCancel->GetCurrentScopeProgress();
355 }
356
357 return 1;
358 }
359
367 {
368 if (ProgressCancel)
369 {
371 }
372 }
373 };
374
381 static GEOMETRYCORE_API FProgressScope CreateScopeTo(FProgressCancel* ProgressCancel, float ProgressTo, const FText& Message = FText());
382
383 float GetProgress() const
384 {
385 return FMathf::Clamp(ProgressFraction, 0, 1);
386 }
387
389 {
390 FScopeLock MessageLock(&MessageCS);
391 return ProgressMessage;
392 }
393
394 void SetProgressMessage(const FText& Message)
395 {
396 if (MaxMessageDepth == -1 || Progress.ScopeDepth < MaxMessageDepth)
397 {
398 Progress.Message = Message;
399 FScopeLock MessageLock(&MessageCS);
400 ProgressMessage = Message;
401 }
402 }
403
408 {
409 ProgressFraction = Progress.SafeAdvance(ProgressFraction, Amount);
410 }
411
418 {
419 float TargetProgressFraction = FMathf::Max(ProgressFraction, Progress.CurrentMin + Progress.Range() * TargetProgressFrac);
420 ProgressFraction = Progress.SafeAdvance(ProgressFraction, FMathf::Lerp(ProgressFraction, TargetProgressFraction, FractionToward));
421 }
422
427 {
428 float Range = Progress.Range();
429 return Range > 0 ? (ProgressFraction - Progress.CurrentMin) / Range : 1;
430 }
431
440
448 {
449 ProgressFraction = FMathf::Clamp(Progress.CurrentMin + Progress.Range() * NewProgressFrac, ProgressFraction, Progress.CurrentMax);
450 }
451
452public:
453
454 enum class EMessageLevel
455 {
456 // Note: Corresponds to EToolMessageLevel in InteractiveToolsFramework/ToolContextInterfaces.h
457
459 Internal = 0,
461 UserMessage = 1,
465 UserWarning = 3,
467 UserError = 4
468 };
469
476
477 void AddWarning(const FText& MessageText, EMessageLevel MessageLevel)
478 {
479 Warnings.Add(FMessageInfo{ MessageText , MessageLevel, FDateTime::Now() });
480 }
481
483};
484
485
486
487
489{
490 if (Progress && Progress->Cancelled())
491 {
493 }
494 else
495 {
497 }
498}
499
500
502{
504 Errors.Add(FGeometryError(ResultCode, ErrorMessage));
505}
506
507
509{
510 if (Progress && Progress->Cancelled())
511 {
513 return true;
514 }
515 return false;
516}
bool bSuccess
Definition ConvexDecomposition3.cpp:819
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::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
const bool
Definition NetworkReplayStreaming.h:178
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition ProgressCancel.h:258
FProgressScope(FProgressScope &&Other) noexcept
Definition ProgressCancel.h:279
void SetProgressTo(float NewProgressFrac)
Definition ProgressCancel.h:366
float GetDistanceTo(float TargetProgressFrac)
Definition ProgressCancel.h:337
FProgressScope(const FProgressScope &Other)=delete
~FProgressScope()
Definition ProgressCancel.h:297
GEOMETRYCORE_API void Done()
Definition ProgressCancel.cpp:73
FProgressScope & operator=(FProgressScope &&Other) noexcept
Definition ProgressCancel.h:284
FProgressScope()
Definition ProgressCancel.h:274
void AdvanceProgressToward(float TargetProgressFrac, float FractionToward)
Definition ProgressCancel.h:326
void AdvanceProgressBy(float Amount)
Definition ProgressCancel.h:313
FProgressScope & operator=(const FProgressScope &Other)=delete
float GetProgress()
Definition ProgressCancel.h:350
Definition ProgressCancel.h:187
void SetCurrentScopeProgressTo(float NewProgressFrac)
Definition ProgressCancel.h:447
bool Cancelled()
Definition ProgressCancel.h:243
void SetProgressMessage(const FText &Message)
Definition ProgressCancel.h:394
EMessageLevel
Definition ProgressCancel.h:455
void ClearMaxMessageDepth()
Definition ProgressCancel.h:235
static GEOMETRYCORE_API FProgressScope CreateScopeTo(FProgressCancel *ProgressCancel, float ProgressTo, const FText &Message=FText())
Definition ProgressCancel.cpp:55
float GetCurrentScopeProgress()
Definition ProgressCancel.h:426
FText GetProgressMessage() const
Definition ProgressCancel.h:388
TFunction< bool()> CancelF
Definition ProgressCancel.h:228
void AdvanceCurrentScopeProgressToward(float TargetProgressFrac, float FractionToward)
Definition ProgressCancel.h:417
void AdvanceCurrentScopeProgressBy(float Amount)
Definition ProgressCancel.h:407
void AddWarning(const FText &MessageText, EMessageLevel MessageLevel)
Definition ProgressCancel.h:477
float GetCurrentScopeDistanceTo(float TargetProgressFrac)
Definition ProgressCancel.h:436
float GetProgress() const
Definition ProgressCancel.h:383
void SetMaxMessageDepth(int32 ScopeDepth)
Definition ProgressCancel.h:230
TArray< FMessageInfo > Warnings
Definition ProgressCancel.h:482
Definition ScopeLock.h:141
Definition Text.h:385
Definition Array.h:670
Definition AndroidPlatformMisc.h:14
static RealType Lerp(const RealType A, const RealType B, RealType Alpha)
Definition MathUtil.h:422
static RealType Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax)
Definition MathUtil.h:222
static RealType Max(const RealType A, const RealType B)
Definition MathUtil.h:246
static RealType Min(const RealType A, const RealType B)
Definition MathUtil.h:271
Definition ByteSwap.h:14
EGeometryResultType
Definition ProgressCancel.h:80
Definition AdvancedWidgetsModule.cpp:13
Definition DateTime.h:76
static CORE_API FDateTime Now()
Definition DateTime.cpp:377
Definition ProgressCancel.h:471
FDateTime Timestamp
Definition ProgressCancel.h:474
FText MessageText
Definition ProgressCancel.h:472
EMessageLevel MessageLevel
Definition ProgressCancel.h:473
Definition ProgressCancel.h:34
FGeometryError()
Definition ProgressCancel.h:40
FGeometryError(int32 Code, const FText &MessageIn)
Definition ProgressCancel.h:45
FText Message
Definition ProgressCancel.h:36
FDateTime Timestamp
Definition ProgressCancel.h:37
TArray< unsigned char > CustomData
Definition ProgressCancel.h:38
int32 ErrorCode
Definition ProgressCancel.h:35
Definition ProgressCancel.h:99
bool HasFailed() const
Definition ProgressCancel.h:160
TArray< FGeometryWarning > Warnings
Definition ProgressCancel.h:102
void SetFailed()
Definition ProgressCancel.h:120
void AddWarning(FGeometryWarning Warning)
Definition ProgressCancel.h:152
void SetSuccess()
Definition ProgressCancel.h:123
EGeometryResultType Result
Definition ProgressCancel.h:100
FGeometryResult()
Definition ProgressCancel.h:105
FGeometryResult(EGeometryResultType ResultType)
Definition ProgressCancel.h:110
bool CheckAndSetCancelled(FProgressCancel *Progress)
Definition ProgressCancel.h:508
static FGeometryResult Failed()
Definition ProgressCancel.h:167
static FGeometryResult Cancelled()
Definition ProgressCancel.h:168
void UpdateResultType(EGeometryResultType NewType)
Definition ProgressCancel.h:115
bool HasResult() const
Definition ProgressCancel.h:165
void SetCancelled()
Definition ProgressCancel.h:121
void AddError(FGeometryError Error)
Definition ProgressCancel.h:144
TArray< FGeometryError > Errors
Definition ProgressCancel.h:101
Definition ProgressCancel.h:57
FGeometryWarning()
Definition ProgressCancel.h:64
FDateTime Timestamp
Definition ProgressCancel.h:60
int32 WarningCode
Definition ProgressCancel.h:58
FGeometryWarning(int32 Code, const FText &MessageIn)
Definition ProgressCancel.h:69
FText Message
Definition ProgressCancel.h:59
TArray< unsigned char > CustomData
Definition ProgressCancel.h:61