2using System.Runtime.CompilerServices;
94 private readonly SKPaint _fillPaint;
95 private readonly SKPaint _strokePaint;
97 private readonly SKPaint _imagePaint =
new()
100 FilterQuality = SKFilterQuality.Medium,
101 BlendMode = SKBlendMode.SrcOver
104 private SKColor? _borderColor;
105 private SKShader? _fillShader;
106 private SKImage? _fillImage;
107 private SKBitmap? _fillBitmap;
109 private float _imageScale = 1f;
110 private SKPoint _imageOffsetPx;
111 private bool _resourcesDisposed;
113 private bool _isFilled;
114 private float _cornerRadius;
115 private float[]? _dashPattern;
117 private bool _needsRebuildPaints =
true;
120 private bool _pulseFillEnabled;
121 private SKColor _pulseFillFrom, _pulseFillTo;
122 private float _pulseFillPeriodSec = 1f;
124 private bool _pulseBorderEnabled;
125 private SKColor _pulseBorderFrom, _pulseBorderTo;
126 private float _pulseBorderPeriodSec = 1f;
128 private enum PulseWave
141 private PulseWave _pulseFillWave = PulseWave.Sine;
142 private PulseWave _pulseBorderWave = PulseWave.Sine;
145 private long _pulseLastTick = 0;
146 private float _timeSec;
148 private DirectRectangle(
154 Rectangle? screenBounds,
155 Rectangle? worldBounds,
156 string? nickname =
null)
167 _fillPaint =
new SKPaint { IsAntialias =
true, Style = SKPaintStyle.Fill };
168 _strokePaint =
new SKPaint { IsAntialias =
true, Style = SKPaintStyle.Stroke, StrokeWidth = 1 };
198 Rectangle worldBounds,
199 string? nickname =
null)
236 Rectangle screenBounds,
237 string? nickname =
null)
269 var sk = color.ToSKColor();
271 _fillPaint.Color = sk;
272 _strokePaint.Color = sk;
273 _needsRebuildPaints =
true;
301 _borderColor = color.ToSKColor();
302 _needsRebuildPaints =
true;
328 var c = _fillPaint.Color;
330 var withA =
new SKColor(
336 _fillPaint.Color = withA;
339 if (_borderColor is
null)
340 _strokePaint.Color = withA;
342 _needsRebuildPaints =
true;
368 _isFilled = isFilled;
369 _needsRebuildPaints =
true;
394 _strokePaint.StrokeWidth = width;
395 _needsRebuildPaints =
true;
425 _cornerRadius = radius;
454 _dashPattern =
new[] { dashLength, gapLength };
507 _fillPaint.BlendMode = mode;
508 _strokePaint.BlendMode = mode;
509 _imagePaint.BlendMode = mode;
549 _strokeAlign = align;
588 bool triangle =
false)
590 _pulseFillEnabled = enabled;
591 _pulseFillFrom = from.ToSKColor();
592 _pulseFillTo = to.ToSKColor();
593 _pulseFillPeriodSec = MathF.Max(0.0001f, periodSec);
594 _pulseFillWave = triangle ? PulseWave.Triangle : PulseWave.Sine;
633 bool triangle =
false)
635 _pulseBorderEnabled = enabled;
636 _pulseBorderFrom = from.ToSKColor();
637 _pulseBorderTo = to.ToSKColor();
638 _pulseBorderPeriodSec = MathF.Max(0.0001f, periodSec);
639 _pulseBorderWave = triangle ? PulseWave.Triangle : PulseWave.Sine;
660 _pulseFillEnabled = _pulseBorderEnabled =
false;
705 SKShaderTileMode tileX = SKShaderTileMode.Repeat,
706 SKShaderTileMode tileY = SKShaderTileMode.Repeat,
708 SKPoint? offsetPx =
null,
709 SKFilterQuality filterQuality = SKFilterQuality.None)
711 ArgumentNullException.ThrowIfNull(bitmap);
713 scale =
float.IsFinite(scale) && scale > 0f
715 :
throw new ArgumentOutOfRangeException(
717 "Pattern scale must be finite and greater than zero.");
719 var m = SKMatrix.CreateScale(scale, scale);
721 if (offsetPx is { } o)
722 m = m.PostConcat(SKMatrix.CreateTranslation(o.X, o.Y));
724 SKShader newShader = SKShader.CreateBitmap(
730 _fillPaint.Shader =
null;
731 _fillShader?.Dispose();
732 _fillShader = newShader;
737 _fillPaint.Shader = _fillShader;
738 _fillPaint.FilterQuality = filterQuality;
766 SKPoint? offsetPx =
null,
767 SKFilterQuality filterQuality = SKFilterQuality.Medium)
769 ArgumentNullException.ThrowIfNull(bitmap);
777 filterQuality: filterQuality);
802 SKPoint? offsetPx =
null,
803 SKFilterQuality filterQuality = SKFilterQuality.Medium)
805 ArgumentNullException.ThrowIfNull(image);
813 filterQuality: filterQuality);
845 _fillPaint.Shader =
null;
885 if (_pulseLastTick > 0)
887 float dt = HighResTimer.GetDuration(_pulseLastTick, tick);
891 _pulseLastTick = tick;
893 bool changed =
false;
895 if (_pulseFillEnabled)
897 float t = PulseT(_timeSec, _pulseFillPeriodSec, _pulseFillWave);
898 var c = LerpColor(_pulseFillFrom, _pulseFillTo, t);
900 if (_fillPaint.Color != c)
902 _fillPaint.Color = c;
907 if (_pulseBorderEnabled)
909 float t = PulseT(_timeSec, _pulseBorderPeriodSec, _pulseBorderWave);
910 var c = LerpColor(_pulseBorderFrom, _pulseBorderTo, t);
912 if (_strokePaint.Color != c)
914 _strokePaint.Color = c;
921 _needsRebuildPaints =
false;
957 if (backbuffer is RecordingBackbuffer && _fillImage?.IsTextureBacked ==
true)
958 throw new NotSupportedException(
959 "Desktop frame recording requires a CPU image, not a context-bound texture.");
961 var canvas = backbuffer.
Canvas;
963 if (_needsRebuildPaints)
966 var fillRect = destRectScreen.ToSKRect();
967 var strokeRect = fillRect;
969 bool willDrawStroke = !_isFilled || _borderColor.HasValue || _strokePaint.StrokeWidth > 0.01f;
970 float half = _strokePaint.StrokeWidth * 0.5f;
973 if (willDrawStroke && _strokeAlign !=
StrokeAlign.Center)
976 strokeRect.Inflate(-half, -half);
978 strokeRect.Inflate(half, half);
982 float strokeCornerRadius;
984 switch (_strokeAlign)
987 strokeCornerRadius = MathF.Max(0f, _cornerRadius - half);
990 strokeCornerRadius = _cornerRadius + half;
994 strokeCornerRadius = _cornerRadius;
999 _strokePaint.PathEffect = _dashPattern is { Length: > 0 }
1000 ? SKPathEffect.CreateDash(_dashPattern, 0)
1006 if (_fillImage is not
null || _fillBitmap is not
null)
1008 DrawImageFill(canvas, fillRect);
1010 else if (_cornerRadius > 0)
1012 canvas.DrawRoundRect(
1020 canvas.DrawRect(fillRect, _fillPaint);
1027 var prevBlend = _strokePaint.BlendMode;
1028 var prevColor = _strokePaint.Color;
1029 var prevAA = _strokePaint.IsAntialias;
1031 _strokePaint.IsAntialias =
false;
1032 _strokePaint.BlendMode = SKBlendMode.Src;
1036 var strokeColor = _pulseBorderEnabled
1037 ? _strokePaint.Color
1038 : (_borderColor ?? prevColor);
1040 _strokePaint.Color = strokeColor.WithAlpha(255);
1042 if (_cornerRadius > 0)
1043 canvas.DrawRoundRect(
1049 canvas.DrawRect(strokeRect, _strokePaint);
1051 _strokePaint.IsAntialias = prevAA;
1052 _strokePaint.BlendMode = prevBlend;
1053 _strokePaint.Color = prevColor;
1054 _strokePaint.StrokeJoin = SKStrokeJoin.Round;
1055 _strokePaint.StrokeCap = SKStrokeCap.Round;
1062 if (disposing && !_resourcesDisposed)
1064 _resourcesDisposed =
true;
1065 _fillPaint.Shader =
null;
1066 _fillShader?.Dispose();
1068 _fillPaint.Dispose();
1069 _strokePaint.Dispose();
1070 _imagePaint.Dispose();
1073 base.Dispose(disposing);
1076 private void SetFillImageCore(
1082 SKFilterQuality filterQuality)
1084 if (!
float.IsFinite(scale) || scale <= 0f)
1085 throw new ArgumentOutOfRangeException(nameof(scale),
"Image scale must be finite and greater than zero.");
1087 if (!Enum.IsDefined(mode))
1088 throw new ArgumentOutOfRangeException(nameof(mode));
1090 _fillPaint.Shader =
null;
1091 _fillShader?.Dispose();
1095 _fillBitmap = bitmap;
1096 _imageFillMode = mode;
1097 _imageScale = scale;
1098 _imageOffsetPx = offsetPx ?? SKPoint.Empty;
1099 _imagePaint.FilterQuality = filterQuality;
1105 private void DrawImageFill(
1109 SKImage? image = _fillImage;
1110 SKBitmap? bitmap = _fillBitmap;
1112 int sourceWidth = image?.Width ?? bitmap?.Width ?? 0;
1113 int sourceHeight = image?.Height ?? bitmap?.Height ?? 0;
1115 if (sourceWidth <= 0 || sourceHeight <= 0)
1122 using var clipPath =
new SKPath();
1124 if (_cornerRadius > 0f)
1125 clipPath.AddRoundRect(fillRect, _cornerRadius, _cornerRadius);
1127 clipPath.AddRect(fillRect);
1131 SKClipOperation.Intersect,
1132 antialias: _cornerRadius > 0f);
1134 var source =
new SKRect(
1142 if (image is not
null)
1161 SKRect destination = ComputeImageDestination(
1166 if (image is not
null)
1190 private void DrawRepeatedImage(
1196 float tileWidth = image.Width * _imageScale;
1197 float tileHeight = image.Height * _imageScale;
1199 float offsetX = PositiveModulo(_imageOffsetPx.X, tileWidth);
1200 float offsetY = PositiveModulo(_imageOffsetPx.Y, tileHeight);
1202 float startX = fillRect.Left + offsetX;
1203 float startY = fillRect.Top + offsetY;
1205 if (startX > fillRect.Left)
1206 startX -= tileWidth;
1208 if (startY > fillRect.Top)
1209 startY -= tileHeight;
1211 for (
float y = startY; y < fillRect.Bottom; y += tileHeight)
1213 for (
float x = startX; x < fillRect.Right; x += tileWidth)
1228 private void DrawRepeatedBitmap(
1234 float tileWidth = bitmap.Width * _imageScale;
1235 float tileHeight = bitmap.Height * _imageScale;
1237 float offsetX = PositiveModulo(_imageOffsetPx.X, tileWidth);
1238 float offsetY = PositiveModulo(_imageOffsetPx.Y, tileHeight);
1240 float startX = fillRect.Left + offsetX;
1241 float startY = fillRect.Top + offsetY;
1243 if (startX > fillRect.Left)
1244 startX -= tileWidth;
1246 if (startY > fillRect.Top)
1247 startY -= tileHeight;
1249 for (
float y = startY; y < fillRect.Bottom; y += tileHeight)
1251 for (
float x = startX; x < fillRect.Right; x += tileWidth)
1266 private static SKRect ComputeImageDestination(
1276 float x = bounds.MidX - source.Width * 0.5f;
1277 float y = bounds.MidY - source.Height * 0.5f;
1286 float scaleX = bounds.Width / source.Width;
1287 float scaleY = bounds.Height / source.Height;
1289 float scale = mode
switch
1295 MathF.Floor(MathF.Min(scaleX, scaleY))),
1299 float width = source.Width * scale;
1300 float height = source.Height * scale;
1301 float left = bounds.MidX - width * 0.5f;
1302 float top = bounds.MidY - height * 0.5f;
1311 private static float PositiveModulo(
float value,
float modulus)
1313 float remainder = value % modulus;
1315 return remainder < 0f
1316 ? remainder + modulus
1323 private void RebuildPaints()
1326 _fillPaint.IsAntialias =
true;
1327 _fillPaint.Style = SKPaintStyle.Fill;
1329 _strokePaint.IsAntialias =
true;
1330 _strokePaint.Style = SKPaintStyle.Stroke;
1333 if (!_pulseBorderEnabled)
1336 if (_borderColor.HasValue)
1338 _ = _strokePaint.Color;
1339 _strokePaint.Color = _borderColor.Value;
1343 _strokePaint.Color = _fillPaint.Color;
1346 _needsRebuildPaints =
false;
1378 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1379 private static float PulseT(
float timeSec,
float periodSec, PulseWave wave)
1381 float phase = (timeSec / periodSec) % 1f;
1383 if (wave == PulseWave.Sine)
1386 return 0.5f * (1f + MathF.Sin(phase * MathF.PI * 2f));
1390 return phase < 0.5f ? (phase * 2f) : (1f - ((phase - 0.5f) * 2f));
1394 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1395 private static SKColor LerpColor(SKColor a, SKColor b,
float t01)
1397 t01 = Math.Clamp(t01, 0f, 1f);
1399 byte r = (byte)(a.Red + (b.Red - a.Red) * t01);
1400 byte g = (byte)(a.Green + (b.Green - a.Green) * t01);
1401 byte bch = (byte)(a.Blue + (b.Blue - a.Blue) * t01);
1402 byte aA = (byte)(a.Alpha + (b.Alpha - a.Alpha) * t01);
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.
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
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 OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Renders the rectangle to the backbuffer with all configured properties and effects applied.
DirectRectangle SetFilled(bool isFilled)
Sets whether the rectangle interior is filled with the base color (or pattern).
DirectRectangle StopPulses()
Stops all active color pulsing animations (fill and border), reverting to static colors.
DirectRectangle ClearDashPattern()
Removes any existing dash pattern, reverting to a solid stroke.
DirectRectangle SetBorderColor(Color color)
Sets a distinct border (stroke) color, independent of the fill color.
StrokeAlign
Defines how the stroke (outline) is positioned relative to the rectangle's boundary.
@ Outside
Draws the stroke entirely outside the rectangle's bounds. The inner edge of the stroke aligns with th...
@ Inside
Draws the stroke entirely inside the rectangle's bounds. The outer edge of the stroke aligns with the...
DirectRectangle SetDashPattern(float dashLength, float gapLength)
Sets a simple repeating dash pattern using dash and gap lengths, in pixels.
DirectRectangle SetColor(Color color)
Sets the base color used for both fill and stroke (unless a distinct border color is specified).
DirectRectangle SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
DirectRectangle SetFillPattern(SKBitmap bitmap, SKShaderTileMode tileX=SKShaderTileMode.Repeat, SKShaderTileMode tileY=SKShaderTileMode.Repeat, float scale=1f, SKPoint? offsetPx=null, SKFilterQuality filterQuality=SKFilterQuality.None)
Sets the rectangle's fill to use a tiled bitmap pattern instead of a solid color.
DirectRectangle SetAlpha(int alpha)
Sets the alpha (transparency) channel of the base color.
DirectRectangle SetFillImage(SKImage image, ImageFillMode mode=ImageFillMode.Stretch, float scale=1f, SKPoint? offsetPx=null, SKFilterQuality filterQuality=SKFilterQuality.Medium)
Fills the rectangle with an image using the requested scaling or repeat behavior.
DirectRectangle SetCornerRadius(float radius)
Sets the radius for rounded corners in pixels.
DirectRectangle(Color color, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? nickname=null)
Initializes a new instance of the DirectRectangle class for scene-layer (world-space) rendering.
override void Update(long tick)
Performs per-frame update logic, including pulse animation advancement and physics integration.
DirectRectangle PulseFill(Color from, Color to, float periodSec, bool enabled=true, bool triangle=false)
Initiates an animated color transition (pulse) for the rectangle's fill between two colors.
DirectRectangle SetBlendMode(SKBlendMode mode)
Sets the blend mode used when rendering this rectangle onto the canvas.
ImageFillMode
Defines how an image fills the rectangle interior.
@ Fit
Fits the entire image inside the rectangle while preserving aspect ratio.
@ PixelPerfect
Uses the largest whole-number scale that fits, never scaling below native size.
@ Center
Draws the image at native size, centered and clipped to the rectangle.
@ Repeat
Repeats the image as tiles across the rectangle.
@ Fill
Covers the rectangle while preserving aspect ratio and clips any overflow.
@ Stretch
Stretches the image to the full rectangle without preserving aspect ratio.
override void Dispose(bool disposing)
Releases resources used by this DirectDrawingBase instance.When disposing is true,...
DirectRectangle SetFillImage(SKBitmap bitmap, ImageFillMode mode=ImageFillMode.Stretch, float scale=1f, SKPoint? offsetPx=null, SKFilterQuality filterQuality=SKFilterQuality.Medium)
Fills the rectangle with a bitmap using the requested scaling or repeat behavior.
DirectRectangle(Color color, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? nickname=null)
Initializes a new instance of the DirectRectangle class for view (screen-space) rendering.
DirectRectangle PulseBorder(Color from, Color to, float periodSec, bool enabled=true, bool triangle=false)
Initiates an animated color transition (pulse) for the rectangle's border between two colors.
DirectRectangle ClearFillPattern()
Removes the current pattern fill and reverts to solid color rendering.
DirectRectangle SetStrokeAlign(StrokeAlign align)
Sets how the rectangle's stroke is positioned relative to its bounds.
DirectRectangle ClearFillImage()
Removes the current bitmap or image fill and returns to solid-color filling.
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 ...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.