7using Microsoft.Extensions.Logging;
34 private static readonly ILogger<SplashScreen> Logger =
35 EngineLogger.GetLogger<SplashScreen>();
68 private readonly
View _view;
69 private Timer? _holdTimer;
70 private TaskCompletionSource? _holdDurationCompletionSource;
71 private bool _disposed;
94 public float HoldSec {
get;
private set; }
101 #endregion properties
103 #region factory / constructor
110 Action? onHoldingSync,
111 Func<Task>? onHoldingAsync,
112 Action? onSplashCompleted)
122 ??
throw new InvalidOperationException(
"Splash image must be attached to a view.");
124 _view.Viewport.TargetRectChanged += OnViewportTargetRectChanged;
205 float fadeInSec = 0.45f,
207 float fadeOutSec = 0.45f,
208 Action? onHoldingSync =
null,
209 Func<Task>? onHoldingAsync =
null,
210 Action? onSplashCompleted =
null)
215 using var bitmap = SKBitmap.Decode(imageStream);
217 var sourceImage = bitmap is
null
219 : SKImage.FromBitmap(bitmap);
221 if (sourceImage is
null)
226 var screenBounds = viewport;
237 image.Disposing += (_, _) => sourceImage.
Dispose();
239 image.ZOrder =
int.MaxValue;
242 return new SplashScreen(
252 #endregion factory / constructor
264 _view.Viewport.TargetRectChanged -= OnViewportTargetRectChanged;
266 _holdTimer?.Dispose();
269 _holdDurationCompletionSource?.TrySetCanceled();
270 _holdDurationCompletionSource =
null;
280 Image.ScreenBounds = args.
NewRect;
283 #region splash control
285 private void StartFadeIn(Action? onHoldingSync, Func<Task>? onHoldingAsync, Action? onSplashCompleted)
287 Logger.LogDebug(
"Starting splash screen fade-in");
291 EventHandler<DirectDrawingBase>? fadeInHandler =
null;
293 fadeInHandler = (_, _) =>
295 Image.FadeToCompleted -= fadeInHandler;
302 _ = RunHoldPhaseAsync(
308 Image.FadeToCompleted += fadeInHandler;
313 private async Task RunHoldPhaseAsync(
314 Action? onHoldingSync,
315 Func<Task>? onHoldingAsync,
316 Action? onSplashCompleted)
318 Logger.LogDebug(
"Starting splash screen hold phase");
322 var holdDurationTask = WaitForHoldDurationAsync();
324 var delegateTask = ExecuteHoldDelegatesAsync(onHoldingSync, onHoldingAsync);
330 catch (OperationCanceledException)
337 Logger.LogError(ex,
"Unexpected error during SplashScreen hold phase");
341 _holdTimer?.Dispose();
344 _holdDurationCompletionSource =
null;
350 Engine.Instance.EngineDispatcher.Post(() =>
358 StartFadeOut(onSplashCompleted);
362 private Task WaitForHoldDurationAsync()
364 var completionSource =
new TaskCompletionSource(
365 TaskCreationOptions.RunContinuationsAsynchronously);
367 _holdDurationCompletionSource =
370 var timer = Timer.
Add(
377 void OnHoldTimerElapsed()
379 timer.Tick -= OnHoldTimerElapsed;
381 completionSource.TrySetResult();
384 timer.Tick += OnHoldTimerElapsed;
386 return completionSource.Task;
389 private static async Task ExecuteHoldDelegatesAsync(
390 Action? onHoldingSync,
391 Func<Task>? onHoldingAsync)
393 if (onHoldingSync is
null &&
394 onHoldingAsync is
null)
400 Engine.Instance.EngineDispatcher;
404 await dispatcher.PostAsync(async () =>
412 if (onHoldingAsync is not
null)
414 Logger.LogDebug(
"Calling onHoldingAsync in SplashScreen");
418 asyncTask = onHoldingAsync() ?? Task.CompletedTask;
422 asyncTask = Task.FromException(ex);
426 if (onHoldingSync is not
null)
428 Logger.LogDebug(
"Calling onHoldingSync in SplashScreen");
436 syncTask = Task.FromException(ex);
440 await Task.WhenAll(asyncTask, syncTask);
445 Logger.LogError(ex,
"Error executing SplashScreen hold delegates");
449 private void StartFadeOut(Action? onSplashCompleted)
451 Logger.LogDebug(
"Starting splash screen fade-out");
455 EventHandler<DirectDrawingBase>? fadeOutHandler =
null;
457 fadeOutHandler = (_, _) =>
459 Image.FadeToCompleted -= fadeOutHandler;
462 onSplashCompleted?.Invoke();
465 Image.FadeToCompleted += fadeOutHandler;
469 #endregion splash control
RenderSurfaceHostBase RenderSurfaceHost
Gets the render surface host shared by the composite and its children.
View? View
Gets the view shared by all children in view mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
void Dispose()
Releases all resources used by this DirectDrawingBase instance.
DirectDrawingBase FadeIn(float durationSec)
Fades this direct drawing from its current opacity to fully opaque over the specified duration.
Renders a bitmap or image to a rectangular region with support for scaling modes, source clipping,...
DirectImage SetScaleMode(ScaleMode mode)
Sets how the image is scaled to fit within the destination rectangle.
ScaleMode
Defines how an image is scaled to fit within the destination rectangle.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
ViewManager ViewManager
Gets the view manager that controls camera positions, viewports, and multi-view rendering for this re...
ReadOnlyCollection< View > Views
Gets a read-only collection of all views currently managed by this view manager. Views are sorted by ...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Viewport Viewport
Gets the viewport that defines the screen-space rectangle, zoom level, and other rendering parameters...
Provides data for the Viewport.TargetRectChanged event, which is raised when a viewport's target rect...
Rectangle NewRect
Gets the new target rectangle after the resize or reposition operation.
Rectangle TargetRectPx
Screen-space rectangle (in SCREEN / RenderSurface pixels) where this view is drawn....
Represents a high-resolution timer that can trigger events at specified intervals and supports variou...
static Timer Add(string timerID, TimerType type, TimerCycles cycles, double length)
Creates a new timer with the specified parameters and adds it to the collection of active timers.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
TimerType
Specifies when a Timer should have its Timer.Tick event raised relative to the engine's main update a...
TimerCycles
Specifies the cycle behavior of a Timer, determining whether it executes once or repeatedly at its co...