Iyon_Groznyy

Untitled

Mar 7th, 2022
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using System;
  2. using MvvmCross.Forms.Presenters.Attributes;
  3. using MvvmCross.Forms.Views;
  4. using SkiaSharp;
  5. using SkiaSharp.Views.Forms;
  6. using TestProject.Core.ViewModels.Home;
  7. using TestProject.UI.Extensions;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using BindExtensions = TestProject.UI.Extensions.BindableObjectExtensions;
  11.  
  12. namespace TestProject.UI.Pages
  13. {
  14.     [XamlCompilation(XamlCompilationOptions.Compile)]
  15.     [MvxContentPagePresentation(WrapInNavigationPage = true)]
  16.     public partial class HomePage : MvxContentPage<HomeViewModel>
  17.     {
  18.         private const float Cos35 = 0.82f;
  19.         private const float LowestPointY = 100f;
  20.         private const float LeftmostPointY = -126.7f;
  21.         private const float LeftmostPointX = -191f;
  22.         private const float OnePercentWight = 3.82f;
  23.         private const float Wight = 382f;
  24.         private const int StarsCount = 5;
  25.         private const int MaxFillValue = 100;
  26.  
  27.         public static readonly BindableProperty FillValueProperty = BindExtensions.Create<int, HomePage>(nameof(FillValue), p => p.IsFillValueChanged);
  28.         private readonly Point[] halfStarCoordinates = new[]
  29.         {
  30.             new Point(0 , -253),
  31.             new Point(46, -126.7f ),
  32.             new Point(191, -126.7f),
  33.             new Point(76, -38.7f),
  34.             new Point(132, 100),
  35.             new Point(0 , 7.3f),
  36.         };
  37.  
  38.         public HomePage()
  39.         {
  40.             InitializeComponent();
  41.         }
  42.  
  43.         public int FillValue
  44.         {
  45.             get => this.GetProperty<int>();
  46.             set => this.SetProperty(value);
  47.         }
  48.  
  49.         protected override void OnAppearing()
  50.         {
  51.             base.OnAppearing();
  52.         }
  53.  
  54.         private void DrawStar(SKPath starPath, Point[] coordinatesHalfObject, float offsetX = 0)
  55.         {
  56.             starPath.MoveTo((float)coordinatesHalfObject[0].X + offsetX, (float)coordinatesHalfObject[0].Y);
  57.            
  58.             for (var i = 1; i < coordinatesHalfObject.Length; i++)
  59.             {
  60.                 starPath.LineTo((float)coordinatesHalfObject[i].X + offsetX, (float)coordinatesHalfObject[i].Y);
  61.             }
  62.  
  63.             starPath.MoveTo((float)coordinatesHalfObject[0].X + offsetX, (float)coordinatesHalfObject[0].Y);
  64.  
  65.             for (var i = 0; i < coordinatesHalfObject.Length; i++)
  66.             {
  67.                 starPath.LineTo((float)coordinatesHalfObject[i].X * -1 + offsetX, (float)coordinatesHalfObject[i].Y);
  68.             }
  69.         }
  70.  
  71.         SKPath FillStars()
  72.         {
  73.             SKPath fillStars = new SKPath();
  74.  
  75.             var maxStarsCount = Math.Ceiling(FillValue / (double)(100 / StarsCount));
  76.  
  77.             for (var j = 0; j < maxStarsCount; j++)
  78.             {
  79.                 var offsetX = j * Wight;
  80.                 var currentLeftmostPositionX = LeftmostPointX + offsetX;
  81.                 var starFillValue = MaxFillValue;
  82.  
  83.                 if (j == maxStarsCount - 1)
  84.                 {
  85.                     starFillValue = (FillValue - MaxFillValue / StarsCount * j) * StarsCount;
  86.                 }
  87.  
  88.                 for (var i = 0; i < starFillValue; i++)
  89.                 {
  90.                     var lineToX = i * OnePercentWight + currentLeftmostPositionX;
  91.                     var lineToXDefault = i * OnePercentWight + LeftmostPointX;
  92.  
  93.                     if (lineToX <= -46 + offsetX || lineToX >= 46 + offsetX)
  94.                     {
  95.                         var x = Math.Abs(LeftmostPointX) - Math.Abs(lineToXDefault);
  96.                         var y = LeftmostPointY + (x * Cos35);
  97.                         fillStars.MoveTo(lineToX, LeftmostPointY);
  98.                         fillStars.LineTo(lineToX, y);
  99.                     }
  100.  
  101.                     if (lineToX >= -132 + offsetX && lineToX <= 132 + offsetX)
  102.                     {
  103.                         var heightToWeightUpperBound = 2.7f;
  104.                         var heightToWeightBottomBound = 0.7f;
  105.  
  106.                         var fromYLength = (132 - Math.Abs(lineToXDefault)) * heightToWeightUpperBound;
  107.                         var toYLength = (132 - Math.Abs(lineToXDefault)) * heightToWeightBottomBound;
  108.  
  109.                         var fromY = Math.Abs(LowestPointY) - fromYLength;
  110.                         var toY = Math.Abs(LowestPointY) - toYLength;
  111.  
  112.                         fillStars.MoveTo(lineToX, fromY);
  113.                         fillStars.LineTo(lineToX, toY);
  114.                     }
  115.                 }
  116.             }
  117.  
  118.             return fillStars;
  119.         }
  120.  
  121.         void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
  122.         {
  123.             SKPath emptyStars = new SKPath();
  124.  
  125.             for(var i = 0; i < StarsCount; i++)
  126.             {
  127.                 DrawStar(
  128.                     emptyStars,
  129.                     halfStarCoordinates,
  130.                     Math.Abs(LeftmostPointX) * i * 2);
  131.             }
  132.  
  133.             SKPaint paintFill = new SKPaint
  134.             {
  135.                 Style = SKPaintStyle.Fill,
  136.                 Color = SKColors.Gray,
  137.                 StrokeWidth = 5
  138.             };
  139.  
  140.             SKImageInfo info = args.Info;
  141.             SKSurface surface = args.Surface;
  142.             SKCanvas canvas = surface.Canvas;
  143.  
  144.             canvas.Clear();
  145.  
  146.             emptyStars.GetBounds(out var bounds);
  147.  
  148.             canvas.Translate(info.Width / 2, info.Height / 2);
  149.  
  150.             canvas.Scale(0.8f * Math.Min(info.Width / bounds.Width,
  151.                                          info.Height / bounds.Height));
  152.  
  153.             canvas.Translate(-bounds.MidX, -bounds.MidY);
  154.  
  155.             canvas.DrawPath(emptyStars, paintFill);
  156.  
  157.             var fillStarsPath = FillStars();
  158.  
  159.             SKPaint paintStroke = new SKPaint
  160.             {
  161.                 Style = SKPaintStyle.Stroke,
  162.                 Color = SKColors.Gold,
  163.                 StrokeWidth = 5
  164.             };
  165.  
  166.             canvas.DrawPath(fillStarsPath, paintStroke);
  167.         }
  168.  
  169.         private void IsFillValueChanged(int _, int newValue)
  170.         {
  171.             StarsCanvasView.InvalidateSurface();
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment