Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
DirectRectangle.cs
1using System.Drawing;
2using System.Runtime.CompilerServices;
9using SkiaSharp;
10
12
68public class DirectRectangle : DirectDrawingMovableBase
69{
73 public enum ImageFillMode
74 {
77
80
83
86
89
92 }
93
94 private readonly SKPaint _fillPaint; // cached fill
95 private readonly SKPaint _strokePaint; // cached stroke
96
97 private readonly SKPaint _imagePaint = new()
98 {
99 IsAntialias = true,
100 FilterQuality = SKFilterQuality.Medium,
101 BlendMode = SKBlendMode.SrcOver
102 };
103
104 private SKColor? _borderColor; // optional distinct border color
105 private SKShader? _fillShader; // optional fill shader
106 private SKImage? _fillImage;
107 private SKBitmap? _fillBitmap;
108 private ImageFillMode _imageFillMode = ImageFillMode.Stretch;
109 private float _imageScale = 1f;
110 private SKPoint _imageOffsetPx;
111 private bool _resourcesDisposed;
112
113 private bool _isFilled;
114 private float _cornerRadius;
115 private float[]? _dashPattern;
116 private StrokeAlign _strokeAlign = StrokeAlign.Center;
117 private bool _needsRebuildPaints = true; // mark when properties change
118
119 // --- Pulse settings ---
120 private bool _pulseFillEnabled;
121 private SKColor _pulseFillFrom, _pulseFillTo;
122 private float _pulseFillPeriodSec = 1f;
123
124 private bool _pulseBorderEnabled;
125 private SKColor _pulseBorderFrom, _pulseBorderTo;
126 private float _pulseBorderPeriodSec = 1f;
127
128 private enum PulseWave
129 {
133 Sine,
134
138 Triangle
139 }
140
141 private PulseWave _pulseFillWave = PulseWave.Sine;
142 private PulseWave _pulseBorderWave = PulseWave.Sine;
143
144 // --- Time keeping for Update(tick) ---
145 private long _pulseLastTick = 0;
146 private float _timeSec; // accumulated seconds
147
148 private DirectRectangle(
149 Color color,
150 RenderSurfaceHostBase renderSurfaceHost,
152 SceneLayer? sceneLayer,
153 View? view,
154 Rectangle? screenBounds,
155 Rectangle? worldBounds,
156 string? nickname = null)
157 : base(
158 renderSurfaceHost,
159 mode,
160 sceneLayer,
161 view,
162 screenBounds,
163 worldBounds,
164 nickname)
165 {
166 // initialize with defaults; actual paints built lazily
167 _fillPaint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Fill };
168 _strokePaint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Stroke, StrokeWidth = 1 };
169 SetColor(color); // sets base color and marks rebuild
170 SetBlendMode(SKBlendMode.SrcOver);
171 SetFilled(false);
172 }
173
195 Color color,
196 RenderSurfaceHostBase renderSurfaceHost,
197 SceneLayer sceneLayer,
198 Rectangle worldBounds,
199 string? nickname = null)
200 : this(
201 color,
202 renderSurfaceHost,
204 sceneLayer,
205 null,
206 null,
207 worldBounds,
208 nickname)
209 {
210 }
211
233 Color color,
234 RenderSurfaceHostBase renderSurfaceHost,
235 View view,
236 Rectangle screenBounds,
237 string? nickname = null)
238 : this(
239 color,
240 renderSurfaceHost,
242 null,
243 view,
244 screenBounds,
245 null,
246 nickname)
247 {
248 }
249
266 public DirectRectangle SetColor(Color color)
267 {
268 // store as stroke/ fill base via rebuild
269 var sk = color.ToSKColor();
270 // Set on one paint; rebuild will propagate
271 _fillPaint.Color = sk;
272 _strokePaint.Color = sk;
273 _needsRebuildPaints = true;
274 return this;
275 }
276
299 public DirectRectangle SetBorderColor(Color color)
300 {
301 _borderColor = color.ToSKColor();
302 _needsRebuildPaints = true;
303 return this;
304 }
305
326 public DirectRectangle SetAlpha(int alpha)
327 {
328 var c = _fillPaint.Color;
329
330 var withA = new SKColor(
331 c.Red,
332 c.Green,
333 c.Blue,
334 (byte)alpha);
335
336 _fillPaint.Color = withA;
337
338 // Only change stroke when it's not using a distinct border color
339 if (_borderColor is null)
340 _strokePaint.Color = withA;
341
342 _needsRebuildPaints = true;
343 return this;
344 }
345
366 public DirectRectangle SetFilled(bool isFilled)
367 {
368 _isFilled = isFilled;
369 _needsRebuildPaints = true;
370 return this;
371 }
372
392 public DirectRectangle SetStrokeWidth(float width)
393 {
394 _strokePaint.StrokeWidth = width;
395 _needsRebuildPaints = true;
396 return this;
397 }
398
423 public DirectRectangle SetCornerRadius(float radius)
424 {
425 _cornerRadius = radius;
426 return this;
427 }
428
452 public DirectRectangle SetDashPattern(float dashLength, float gapLength)
453 {
454 _dashPattern = new[] { dashLength, gapLength };
455 // path effect is applied on draw; no rebuild needed
456 return this;
457 }
458
473 public DirectRectangle ClearDashPattern()
474 {
475 _dashPattern = null;
476 return this;
477 }
478
504 public DirectRectangle SetBlendMode(SKBlendMode mode)
505 {
506 // apply to both paints
507 _fillPaint.BlendMode = mode;
508 _strokePaint.BlendMode = mode;
509 _imagePaint.BlendMode = mode;
510 // no rebuild needed
511 return this;
512 }
513
547 public DirectRectangle SetStrokeAlign(StrokeAlign align)
548 {
549 _strokeAlign = align;
550 return this;
551 }
552
583 public DirectRectangle PulseFill(
584 Color from,
585 Color to,
586 float periodSec,
587 bool enabled = true,
588 bool triangle = false)
589 {
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;
595 return this;
596 }
597
628 public DirectRectangle PulseBorder(
629 Color from,
630 Color to,
631 float periodSec,
632 bool enabled = true,
633 bool triangle = false)
634 {
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;
640 return this;
641 }
642
658 public DirectRectangle StopPulses()
659 {
660 _pulseFillEnabled = _pulseBorderEnabled = false;
661 RebuildPaints(); // restore fill/border to SetColor/SetBorderColor values
662 ForceRefresh();
663 return this;
664 }
665
703 public DirectRectangle SetFillPattern(
704 SKBitmap bitmap,
705 SKShaderTileMode tileX = SKShaderTileMode.Repeat,
706 SKShaderTileMode tileY = SKShaderTileMode.Repeat,
707 float scale = 1f,
708 SKPoint? offsetPx = null,
709 SKFilterQuality filterQuality = SKFilterQuality.None)
710 {
711 ArgumentNullException.ThrowIfNull(bitmap);
712
713 scale = float.IsFinite(scale) && scale > 0f
714 ? scale
715 : throw new ArgumentOutOfRangeException(
716 nameof(scale),
717 "Pattern scale must be finite and greater than zero.");
718
719 var m = SKMatrix.CreateScale(scale, scale);
720
721 if (offsetPx is { } o)
722 m = m.PostConcat(SKMatrix.CreateTranslation(o.X, o.Y));
723
724 SKShader newShader = SKShader.CreateBitmap(
725 bitmap,
726 tileX,
727 tileY,
728 m);
729
730 _fillPaint.Shader = null;
731 _fillShader?.Dispose();
732 _fillShader = newShader;
733
734 _fillImage = null;
735 _fillBitmap = null;
736
737 _fillPaint.Shader = _fillShader;
738 _fillPaint.FilterQuality = filterQuality; // or Low/Medium/High
739
740 // Ensure we're in filled mode for visibility
741 _isFilled = true;
742 ForceRefresh();
743 return this;
744 }
745
762 public DirectRectangle SetFillImage(
763 SKBitmap bitmap,
764 ImageFillMode mode = ImageFillMode.Stretch,
765 float scale = 1f,
766 SKPoint? offsetPx = null,
767 SKFilterQuality filterQuality = SKFilterQuality.Medium)
768 {
769 ArgumentNullException.ThrowIfNull(bitmap);
770
771 SetFillImageCore(
772 image: null,
773 bitmap: bitmap,
774 mode: mode,
775 scale: scale,
776 offsetPx: offsetPx,
777 filterQuality: filterQuality);
778
779 return this;
780 }
781
798 public DirectRectangle SetFillImage(
799 SKImage image,
800 ImageFillMode mode = ImageFillMode.Stretch,
801 float scale = 1f,
802 SKPoint? offsetPx = null,
803 SKFilterQuality filterQuality = SKFilterQuality.Medium)
804 {
805 ArgumentNullException.ThrowIfNull(image);
806
807 SetFillImageCore(
808 image: image,
809 bitmap: null,
810 mode: mode,
811 scale: scale,
812 offsetPx: offsetPx,
813 filterQuality: filterQuality);
814
815 return this;
816 }
817
822 public DirectRectangle ClearFillImage()
823 {
824 _fillImage = null;
825 _fillBitmap = null;
826 ForceRefresh();
827 return this;
828 }
829
843 public DirectRectangle ClearFillPattern()
844 {
845 _fillPaint.Shader = null;
846 _fillShader?.Dispose();
847 _fillShader = null;
848 ForceRefresh();
849 return this;
850 }
851
879 public override void Update(long tick)
880 {
881 if (tick <= _lastTick)
882 return;
883
884 // no previous tick assume first frame, so skip dt-based updates
885 if (_pulseLastTick > 0)
886 {
887 float dt = HighResTimer.GetDuration(_pulseLastTick, tick);
888 _timeSec += dt;
889 }
890
891 _pulseLastTick = tick;
892
893 bool changed = false;
894
895 if (_pulseFillEnabled)
896 {
897 float t = PulseT(_timeSec, _pulseFillPeriodSec, _pulseFillWave);
898 var c = LerpColor(_pulseFillFrom, _pulseFillTo, t);
899
900 if (_fillPaint.Color != c)
901 {
902 _fillPaint.Color = c;
903 changed = true;
904 }
905 }
906
907 if (_pulseBorderEnabled)
908 {
909 float t = PulseT(_timeSec, _pulseBorderPeriodSec, _pulseBorderWave);
910 var c = LerpColor(_pulseBorderFrom, _pulseBorderTo, t);
911
912 if (_strokePaint.Color != c)
913 {
914 _strokePaint.Color = c; // paint-only override
915 ForceRefresh();
916 }
917 }
918
919 if (changed)
920 {
921 _needsRebuildPaints = false; // we already set paints directly
922 ForceRefresh(); // request redraw
923 }
924
925 base.Update(tick);
926 }
927
955 protected override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
956 {
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.");
960
961 var canvas = backbuffer.Canvas;
962
963 if (_needsRebuildPaints)
964 RebuildPaints();
965
966 var fillRect = destRectScreen.ToSKRect();
967 var strokeRect = fillRect;
968
969 bool willDrawStroke = !_isFilled || _borderColor.HasValue || _strokePaint.StrokeWidth > 0.01f;
970 float half = _strokePaint.StrokeWidth * 0.5f;
971
972 // 1) APPLY STROKE ALIGNMENT (use HALF the width; path is centered)
973 if (willDrawStroke && _strokeAlign != StrokeAlign.Center)
974 {
975 if (_strokeAlign == StrokeAlign.Inside)
976 strokeRect.Inflate(-half, -half);
977 else if (_strokeAlign == StrokeAlign.Outside)
978 strokeRect.Inflate(half, half);
979 }
980
981 // 1.5) corner radius for the stroke path, adjusted to keep the inner/outer arcs aligned to the fill
982 float strokeCornerRadius;
983
984 switch (_strokeAlign)
985 {
986 case StrokeAlign.Outside:
987 strokeCornerRadius = MathF.Max(0f, _cornerRadius - half);
988 break;
989 case StrokeAlign.Inside:
990 strokeCornerRadius = _cornerRadius + half;
991 break;
992 case StrokeAlign.Center:
993 default:
994 strokeCornerRadius = _cornerRadius;
995 break;
996 }
997
998 // 2) Dash for stroke only
999 _strokePaint.PathEffect = _dashPattern is { Length: > 0 }
1000 ? SKPathEffect.CreateDash(_dashPattern, 0)
1001 : null;
1002
1003 // 3) Draw fill (unmodified rect)
1004 if (_isFilled)
1005 {
1006 if (_fillImage is not null || _fillBitmap is not null)
1007 {
1008 DrawImageFill(canvas, fillRect);
1009 }
1010 else if (_cornerRadius > 0)
1011 {
1012 canvas.DrawRoundRect(
1013 fillRect,
1014 strokeCornerRadius,
1015 strokeCornerRadius,
1016 _fillPaint);
1017 }
1018 else
1019 {
1020 canvas.DrawRect(fillRect, _fillPaint);
1021 }
1022 }
1023
1024 // 4) Draw stroke on its aligned rect (pure/opaque so it stays white)
1025 if (willDrawStroke)
1026 {
1027 var prevBlend = _strokePaint.BlendMode;
1028 var prevColor = _strokePaint.Color;
1029 var prevAA = _strokePaint.IsAntialias;
1030
1031 _strokePaint.IsAntialias = false;
1032 _strokePaint.BlendMode = SKBlendMode.Src;
1033
1034 // If pulsing, keep color from Update(),
1035 // else use base border color (or previous).
1036 var strokeColor = _pulseBorderEnabled
1037 ? _strokePaint.Color
1038 : (_borderColor ?? prevColor);
1039
1040 _strokePaint.Color = strokeColor.WithAlpha(255);
1041
1042 if (_cornerRadius > 0)
1043 canvas.DrawRoundRect(
1044 strokeRect,
1045 _cornerRadius,
1046 _cornerRadius,
1047 _strokePaint);
1048 else
1049 canvas.DrawRect(strokeRect, _strokePaint);
1050
1051 _strokePaint.IsAntialias = prevAA;
1052 _strokePaint.BlendMode = prevBlend;
1053 _strokePaint.Color = prevColor;
1054 _strokePaint.StrokeJoin = SKStrokeJoin.Round;
1055 _strokePaint.StrokeCap = SKStrokeCap.Round;
1056 }
1057 }
1058
1060 protected override void Dispose(bool disposing)
1061 {
1062 if (disposing && !_resourcesDisposed)
1063 {
1064 _resourcesDisposed = true;
1065 _fillPaint.Shader = null;
1066 _fillShader?.Dispose();
1067 _fillShader = null;
1068 _fillPaint.Dispose();
1069 _strokePaint.Dispose();
1070 _imagePaint.Dispose();
1071 }
1072
1073 base.Dispose(disposing);
1074 }
1075
1076 private void SetFillImageCore(
1077 SKImage? image,
1078 SKBitmap? bitmap,
1079 ImageFillMode mode,
1080 float scale,
1081 SKPoint? offsetPx,
1082 SKFilterQuality filterQuality)
1083 {
1084 if (!float.IsFinite(scale) || scale <= 0f)
1085 throw new ArgumentOutOfRangeException(nameof(scale), "Image scale must be finite and greater than zero.");
1086
1087 if (!Enum.IsDefined(mode))
1088 throw new ArgumentOutOfRangeException(nameof(mode));
1089
1090 _fillPaint.Shader = null;
1091 _fillShader?.Dispose();
1092 _fillShader = null;
1093
1094 _fillImage = image;
1095 _fillBitmap = bitmap;
1096 _imageFillMode = mode;
1097 _imageScale = scale;
1098 _imageOffsetPx = offsetPx ?? SKPoint.Empty;
1099 _imagePaint.FilterQuality = filterQuality;
1100 _isFilled = true;
1101
1102 ForceRefresh();
1103 }
1104
1105 private void DrawImageFill(
1106 SKCanvas canvas,
1107 SKRect fillRect)
1108 {
1109 SKImage? image = _fillImage;
1110 SKBitmap? bitmap = _fillBitmap;
1111
1112 int sourceWidth = image?.Width ?? bitmap?.Width ?? 0;
1113 int sourceHeight = image?.Height ?? bitmap?.Height ?? 0;
1114
1115 if (sourceWidth <= 0 || sourceHeight <= 0)
1116 return;
1117
1118 canvas.Save();
1119
1120 try
1121 {
1122 using var clipPath = new SKPath();
1123
1124 if (_cornerRadius > 0f)
1125 clipPath.AddRoundRect(fillRect, _cornerRadius, _cornerRadius);
1126 else
1127 clipPath.AddRect(fillRect);
1128
1129 canvas.ClipPath(
1130 clipPath,
1131 SKClipOperation.Intersect,
1132 antialias: _cornerRadius > 0f);
1133
1134 var source = new SKRect(
1135 0f,
1136 0f,
1137 sourceWidth,
1138 sourceHeight);
1139
1140 if (_imageFillMode == ImageFillMode.Repeat)
1141 {
1142 if (image is not null)
1143 {
1144 DrawRepeatedImage(
1145 canvas,
1146 image,
1147 source,
1148 fillRect);
1149 }
1150 else
1151 {
1152 DrawRepeatedBitmap(
1153 canvas,
1154 bitmap!,
1155 source,
1156 fillRect);
1157 }
1158 }
1159 else
1160 {
1161 SKRect destination = ComputeImageDestination(
1162 fillRect,
1163 source,
1164 _imageFillMode);
1165
1166 if (image is not null)
1167 {
1168 canvas.DrawImage(
1169 image,
1170 source,
1171 destination,
1172 _imagePaint);
1173 }
1174 else
1175 {
1176 canvas.DrawBitmap(
1177 bitmap!,
1178 source,
1179 destination,
1180 _imagePaint);
1181 }
1182 }
1183 }
1184 finally
1185 {
1186 canvas.Restore();
1187 }
1188 }
1189
1190 private void DrawRepeatedImage(
1191 SKCanvas canvas,
1192 SKImage image,
1193 SKRect source,
1194 SKRect fillRect)
1195 {
1196 float tileWidth = image.Width * _imageScale;
1197 float tileHeight = image.Height * _imageScale;
1198
1199 float offsetX = PositiveModulo(_imageOffsetPx.X, tileWidth);
1200 float offsetY = PositiveModulo(_imageOffsetPx.Y, tileHeight);
1201
1202 float startX = fillRect.Left + offsetX;
1203 float startY = fillRect.Top + offsetY;
1204
1205 if (startX > fillRect.Left)
1206 startX -= tileWidth;
1207
1208 if (startY > fillRect.Top)
1209 startY -= tileHeight;
1210
1211 for (float y = startY; y < fillRect.Bottom; y += tileHeight)
1212 {
1213 for (float x = startX; x < fillRect.Right; x += tileWidth)
1214 {
1215 canvas.DrawImage(
1216 image,
1217 source,
1218 new SKRect(
1219 x,
1220 y,
1221 x + tileWidth,
1222 y + tileHeight),
1223 _imagePaint);
1224 }
1225 }
1226 }
1227
1228 private void DrawRepeatedBitmap(
1229 SKCanvas canvas,
1230 SKBitmap bitmap,
1231 SKRect source,
1232 SKRect fillRect)
1233 {
1234 float tileWidth = bitmap.Width * _imageScale;
1235 float tileHeight = bitmap.Height * _imageScale;
1236
1237 float offsetX = PositiveModulo(_imageOffsetPx.X, tileWidth);
1238 float offsetY = PositiveModulo(_imageOffsetPx.Y, tileHeight);
1239
1240 float startX = fillRect.Left + offsetX;
1241 float startY = fillRect.Top + offsetY;
1242
1243 if (startX > fillRect.Left)
1244 startX -= tileWidth;
1245
1246 if (startY > fillRect.Top)
1247 startY -= tileHeight;
1248
1249 for (float y = startY; y < fillRect.Bottom; y += tileHeight)
1250 {
1251 for (float x = startX; x < fillRect.Right; x += tileWidth)
1252 {
1253 canvas.DrawBitmap(
1254 bitmap,
1255 source,
1256 new SKRect(
1257 x,
1258 y,
1259 x + tileWidth,
1260 y + tileHeight),
1261 _imagePaint);
1262 }
1263 }
1264 }
1265
1266 private static SKRect ComputeImageDestination(
1267 SKRect bounds,
1268 SKRect source,
1269 ImageFillMode mode)
1270 {
1271 if (mode == ImageFillMode.Stretch)
1272 return bounds;
1273
1274 if (mode == ImageFillMode.Center)
1275 {
1276 float x = bounds.MidX - source.Width * 0.5f;
1277 float y = bounds.MidY - source.Height * 0.5f;
1278
1279 return new SKRect(
1280 x,
1281 y,
1282 x + source.Width,
1283 y + source.Height);
1284 }
1285
1286 float scaleX = bounds.Width / source.Width;
1287 float scaleY = bounds.Height / source.Height;
1288
1289 float scale = mode switch
1290 {
1291 ImageFillMode.Fit => MathF.Min(scaleX, scaleY),
1292 ImageFillMode.Fill => MathF.Max(scaleX, scaleY),
1293 ImageFillMode.PixelPerfect => MathF.Max(
1294 1f,
1295 MathF.Floor(MathF.Min(scaleX, scaleY))),
1296 _ => 1f
1297 };
1298
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;
1303
1304 return new SKRect(
1305 left,
1306 top,
1307 left + width,
1308 top + height);
1309 }
1310
1311 private static float PositiveModulo(float value, float modulus)
1312 {
1313 float remainder = value % modulus;
1314
1315 return remainder < 0f
1316 ? remainder + modulus
1317 : remainder;
1318 }
1319
1323 private void RebuildPaints()
1324 {
1325 // Ensure styles/AA set
1326 _fillPaint.IsAntialias = true;
1327 _fillPaint.Style = SKPaintStyle.Fill;
1328
1329 _strokePaint.IsAntialias = true;
1330 _strokePaint.Style = SKPaintStyle.Stroke;
1331
1332 // Only rebuild stroke color if not pulsing border
1333 if (!_pulseBorderEnabled)
1334 {
1335 // If border color set, use it for stroke; else match fill/base color
1336 if (_borderColor.HasValue)
1337 {
1338 _ = _strokePaint.Color; // preserve alpha if you want; otherwise:
1339 _strokePaint.Color = _borderColor.Value;
1340 }
1341 else
1342 {
1343 _strokePaint.Color = _fillPaint.Color; // same as fill if no explicit border
1344 }
1345
1346 _needsRebuildPaints = false;
1347 }
1348 }
1349
1357 public enum StrokeAlign
1358 {
1364
1370
1376 }
1377
1378 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1379 private static float PulseT(float timeSec, float periodSec, PulseWave wave)
1380 {
1381 float phase = (timeSec / periodSec) % 1f;
1382
1383 if (wave == PulseWave.Sine)
1384 {
1385 // (sin(2πx)+1)/2 in [0..1]
1386 return 0.5f * (1f + MathF.Sin(phase * MathF.PI * 2f));
1387 }
1388 else // Triangle: ramp up 0..1 then down 1..0
1389 {
1390 return phase < 0.5f ? (phase * 2f) : (1f - ((phase - 0.5f) * 2f));
1391 }
1392 }
1393
1394 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1395 private static SKColor LerpColor(SKColor a, SKColor b, float t01)
1396 {
1397 t01 = Math.Clamp(t01, 0f, 1f);
1398
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);
1403
1404 return new SKColor(
1405 r,
1406 g,
1407 bch,
1408 aA);
1409 }
1410}
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...
Definition View.cs:14
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.