109 private readonly Random _rng =
new();
111 private readonly SKPaint _paint =
new()
114 BlendMode = SKBlendMode.SrcOver
117 private long _instanceLastTick;
161 public Func<Rectangle, Random, IEnumerable<ImageInstance>>?
Initializer {
get;
set; }
202 public Func<ImageInstance, Rectangle, Random, ImageInstance>?
RecycleInstance {
get;
set; }
238 Rectangle screenBounds,
239 string? nickname =
null)
245 screenBounds: screenBounds,
261 Rectangle worldBounds,
262 string? nickname =
null)
266 sceneLayer: sceneLayer,
269 worldBounds: worldBounds,
288 Rectangle screenBounds,
289 Func<Rectangle, Random, IEnumerable<ImageInstance>>? initializer,
290 Func<ImageInstance, Rectangle, bool>? shouldRecycle =
null,
291 Func<ImageInstance, Rectangle, Random, ImageInstance>? recycleInstance =
null,
292 Action<ImageInstance, float>? updateInstance =
null,
293 string? nickname =
null)
323 Rectangle worldBounds,
324 Func<Rectangle, Random, IEnumerable<ImageInstance>>? initializer,
325 Func<ImageInstance, Rectangle, bool>? shouldRecycle =
null,
326 Func<ImageInstance, Rectangle, Random, ImageInstance>? recycleInstance =
null,
327 Action<ImageInstance, float>? updateInstance =
null,
328 string? nickname =
null)
372 if (_instanceLastTick > 0)
373 dt = HighResTimer.GetDuration(_instanceLastTick, tick);
375 _instanceLastTick = tick;
387 for (
int i = 0; i <
Instances.Count; i++)
390 var oldRefreshBounds = GetRefreshBounds(instance);
392 instance.Bounds =
new RectangleF(
393 instance.Bounds.X + (instance.VelocityX * dt),
394 instance.Bounds.Y + (instance.VelocityY * dt),
395 instance.Bounds.Width,
396 instance.Bounds.Height);
398 instance.Rotation += instance.AngularVelocity * dt;
409 AddRefreshRect(oldRefreshBounds);
410 AddRefreshRect(GetRefreshBounds(recycled));
414 AddRefreshRect(oldRefreshBounds);
415 AddRefreshRect(GetRefreshBounds(instance));
429 var canvas = backbuffer.
Canvas;
435 float sx = srcBounds.Width > 0
436 ? destRectScreen.Width / srcBounds.Width
439 float sy = srcBounds.Height > 0
440 ? destRectScreen.Height / srcBounds.Height
444 canvas.ClipRect(destRectScreen.ToSKRect());
448 _paint.Color = instance.Tint;
450 var dst =
new SKRect(
451 destRectScreen.Left + ((instance.Bounds.Left - srcBounds.Left) * sx),
452 destRectScreen.Top + ((instance.Bounds.Top - srcBounds.Top) * sy),
453 destRectScreen.Left + ((instance.Bounds.Right - srcBounds.Left) * sx),
454 destRectScreen.Top + ((instance.Bounds.Bottom - srcBounds.Top) * sy));
456 if (instance.Rotation != 0f)
458 float cx = (dst.Left + dst.Right) * 0.5f;
459 float cy = (dst.Top + dst.Bottom) * 0.5f;
462 canvas.RotateDegrees(instance.Rotation, cx, cy);
463 canvas.DrawBitmap(instance.Bitmap, dst, _paint);
468 canvas.DrawBitmap(instance.Bitmap, dst, _paint);
479 protected override void Dispose(
bool disposing)
484 base.Dispose(disposing);
491 private void AddRefreshRect(RectangleF rect)
493 const float pad = 4f;
495 var expanded = Rectangle.Ceiling(RectangleF.Inflate(rect, pad, pad));
499 SceneLayer!.RefreshQueue.AddWorldRect(expanded);
504 sceneLayer.RefreshQueue.AddViewScreenRect(
View!, sceneLayer, expanded);
512 private static RectangleF GetRefreshBounds(ImageInstance instance)
514 if (instance.Rotation == 0f && instance.AngularVelocity == 0f)
515 return instance.Bounds;
517 float cx = instance.Bounds.Left + (instance.Bounds.Width * 0.5f);
518 float cy = instance.Bounds.Top + (instance.Bounds.Height * 0.5f);
520 float halfWidth = instance.Bounds.Width * 0.5f;
521 float halfHeight = instance.Bounds.Height * 0.5f;
522 float halfDiagonal = MathF.Sqrt((halfWidth * halfWidth) + (halfHeight * halfHeight));
524 return new RectangleF(
View? View
Gets the view to which this direct drawing is attached, or null if the drawing is in scene-layer mode...
long _lastTick
The last engine tick value used for delta-time calculations in Update.
RenderSurfaceHostBase RenderSurfaceHost
Gets the render surface host that manages this direct drawing's rendering pipeline.
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
Rectangle WorldBounds
Gets or sets the world-space bounds of this direct drawing in pixels.
DirectDrawingMode Mode
Gets the drawing mode that determines how this direct drawing is positioned and transformed.
Rectangle ScreenBounds
Gets or sets the screen-space bounds of this direct drawing in pixels.
DirectDrawingMovableBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle? screenBounds, Rectangle? worldBounds, string? name=null)
Initializes a new instance of the DirectDrawingMovableBase class with integrated movement capabilitie...
override void Dispose(bool disposing)
Disposes managed resources.
List< ImageInstance > Instances
Gets the collection of image instances managed by this layer.
override void Update(long tick)
Updates all instances, applying built-in motion, optional custom updates, and optional recycling beha...
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Renders all image instances to the backbuffer.
Action< ImageInstance, float >? UpdateInstance
Gets or sets an optional callback invoked after the built-in motion update for each instance.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, Func< Rectangle, Random, IEnumerable< ImageInstance > >? initializer, Func< ImageInstance, Rectangle, bool >? shouldRecycle=null, Func< ImageInstance, Rectangle, Random, ImageInstance >? recycleInstance=null, Action< ImageInstance, float >? updateInstance=null, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class and assigns optional hooks.
void InitializeInstances()
Clears the current instance collection and rebuilds it using Initializer, if one is assigned.
Func< ImageInstance, Rectangle, bool >? ShouldRecycle
Gets or sets an optional callback used to determine whether an instance should be recycled.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, Func< Rectangle, Random, IEnumerable< ImageInstance > >? initializer, Func< ImageInstance, Rectangle, bool >? shouldRecycle=null, Func< ImageInstance, Rectangle, Random, ImageInstance >? recycleInstance=null, Action< ImageInstance, float >? updateInstance=null, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class for scene-layer rendering and assigns opti...
Func< ImageInstance, Rectangle, Random, ImageInstance >? RecycleInstance
Gets or sets an optional callback used to create a replacement instance when recycling occurs.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class for scene-layer rendering.
Func< Rectangle, Random, IEnumerable< ImageInstance > >? Initializer
Gets or sets an optional callback used to populate the initial set of instances.
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
Scene Scene
Gets the source Scenes.Scene used for rendering operations.
ReadOnlyCollection< SceneLayer > SceneLayers
Gets a read-only collection of all layers in this scene, in their current order.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.