using Sandbox.UI; using SkiaSharp; namespace Sandbox; public partial class Bitmap { /// /// Draws a rectangle using the current pen settings. /// /// The rectangle to draw. public void DrawRect( Rect rect ) { var skRect = rect.ToSk(); _canvas.DrawRect( skRect, GetPen() ); } /// /// Draws a rectangle using the current pen settings. /// /// The x-coordinate of the top-left corner. /// The y-coordinate of the top-left corner. /// The width of the rectangle. /// The height of the rectangle. public void DrawRect( float x, float y, float width, float height ) { DrawRect( new Rect( x, y, x + width, y + height ) ); } /// /// Draws a rectangle using the current pen settings. /// public void DrawRoundRect( Rect rect, Margin margins ) { var rounded = new SKRoundRect( rect.ToSk() ); rounded.SetRectRadii( rect.ToSk(), new[] { new SKPoint( margins.Left, margins.Left ), new SKPoint( margins.Top, margins.Top ), new SKPoint( margins.Right, margins.Right ), new SKPoint( margins.Bottom, margins.Bottom ) } ); var skRect = rect.ToSk(); _canvas.DrawRoundRect( rounded, GetPen() ); } /// /// Draws a circle using the current pen settings. /// /// The center of the circle. /// The radius of the circle. public void DrawCircle( Vector2 center, float radius ) { _canvas.DrawCircle( center.ToSk(), radius, GetPen() ); } /// /// Draws a circle using the current pen settings. /// /// The x-coordinate of the circle's center. /// The y-coordinate of the circle's center. /// The radius of the circle. public void DrawCircle( float x, float y, float radius ) { DrawCircle( new Vector2( x, y ), radius ); } /// /// Draws a polygon using the current pen settings. /// /// The points of the polygon. public void DrawPolygon( Vector2[] points ) { using var path = new SKPath(); path.MoveTo( points[0].ToSk() ); for ( int i = 1; i < points.Length; i++ ) { path.LineTo( points[i].ToSk() ); } path.Close(); _canvas.DrawPath( path, GetPen() ); } /// /// Draws an arc using the current pen settings. /// /// The bounding rectangle of the arc. /// The starting angle of the arc, in degrees. /// The sweep angle of the arc, in degrees. public void DrawArc( Rect rect, float startAngle, float sweepAngle ) { _canvas.DrawArc( rect.ToSk(), startAngle, sweepAngle, false, GetPen() ); } /// /// Draws an arc using the current pen settings, with an option to connect to the center. /// /// The bounding rectangle of the arc. /// The starting angle of the arc, in degrees. /// The sweep angle of the arc, in degrees. /// If true, connects the arc endpoints to the center point, forming a pie shape. public void DrawArc( Rect rect, float startAngle, float sweepAngle, bool useCenter ) { _canvas.DrawArc( rect.ToSk(), startAngle, sweepAngle, useCenter, GetPen() ); } /// /// Draws another bitmap onto this bitmap. /// /// The bitmap to draw. /// The destination rectangle for the drawn bitmap. public void DrawBitmap( Bitmap bitmap, Rect destRect ) { _canvas.DrawBitmap( bitmap._bitmap, destRect.ToSk(), GetPen() ); } /// /// Draws a line using the current pen settings. /// /// The starting point of the line. /// The ending point of the line. public void DrawLine( Vector2 start, Vector2 end ) { _canvas.DrawLine( start.ToSk(), end.ToSk(), GetPen() ); } /// /// Draws connected lines through a series of points using the current pen settings. /// /// The points to connect with lines. public void DrawLines( params Vector2[] points ) { if ( points == null || points.Length < 2 ) throw new ArgumentException( "At least two points are required to draw lines.", nameof( points ) ); var pen = GetPen(); using var path = new SKPath(); path.MoveTo( points[0].ToSk() ); for ( int i = 1; i < points.Length; i++ ) { path.LineTo( points[i].ToSk() ); } _canvas.DrawPath( path, pen ); } }