Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using MvvmCross.Forms.Presenters.Attributes;
- using MvvmCross.Forms.Views;
- using SkiaSharp;
- using SkiaSharp.Views.Forms;
- using TestProject.Core.ViewModels.Home;
- using TestProject.UI.Extensions;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using BindExtensions = TestProject.UI.Extensions.BindableObjectExtensions;
- namespace TestProject.UI.Pages
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- [MvxContentPagePresentation(WrapInNavigationPage = true)]
- public partial class HomePage : MvxContentPage<HomeViewModel>
- {
- private const float Cos35 = 0.82f;
- private const float LowestPointY = 100f;
- private const float LeftmostPointY = -126.7f;
- private const float LeftmostPointX = -191f;
- private const float OnePercentWight = 3.82f;
- private const float Wight = 382f;
- private const int StarsCount = 5;
- private const int MaxFillValue = 100;
- public static readonly BindableProperty FillValueProperty = BindExtensions.Create<int, HomePage>(nameof(FillValue), p => p.IsFillValueChanged);
- private readonly Point[] halfStarCoordinates = new[]
- {
- new Point(0 , -253),
- new Point(46, -126.7f ),
- new Point(191, -126.7f),
- new Point(76, -38.7f),
- new Point(132, 100),
- new Point(0 , 7.3f),
- };
- public HomePage()
- {
- InitializeComponent();
- }
- public int FillValue
- {
- get => this.GetProperty<int>();
- set => this.SetProperty(value);
- }
- protected override void OnAppearing()
- {
- base.OnAppearing();
- }
- private void DrawStar(SKPath starPath, Point[] coordinatesHalfObject, float offsetX = 0)
- {
- starPath.MoveTo((float)coordinatesHalfObject[0].X + offsetX, (float)coordinatesHalfObject[0].Y);
- for (var i = 1; i < coordinatesHalfObject.Length; i++)
- {
- starPath.LineTo((float)coordinatesHalfObject[i].X + offsetX, (float)coordinatesHalfObject[i].Y);
- }
- starPath.MoveTo((float)coordinatesHalfObject[0].X + offsetX, (float)coordinatesHalfObject[0].Y);
- for (var i = 0; i < coordinatesHalfObject.Length; i++)
- {
- starPath.LineTo((float)coordinatesHalfObject[i].X * -1 + offsetX, (float)coordinatesHalfObject[i].Y);
- }
- }
- SKPath FillStars()
- {
- SKPath fillStars = new SKPath();
- var maxStarsCount = Math.Ceiling(FillValue / (double)(100 / StarsCount));
- for (var j = 0; j < maxStarsCount; j++)
- {
- var offsetX = j * Wight;
- var currentLeftmostPositionX = LeftmostPointX + offsetX;
- var starFillValue = MaxFillValue;
- if (j == maxStarsCount - 1)
- {
- starFillValue = (FillValue - MaxFillValue / StarsCount * j) * StarsCount;
- }
- for (var i = 0; i < starFillValue; i++)
- {
- var lineToX = i * OnePercentWight + currentLeftmostPositionX;
- var lineToXDefault = i * OnePercentWight + LeftmostPointX;
- if (lineToX <= -46 + offsetX || lineToX >= 46 + offsetX)
- {
- var x = Math.Abs(LeftmostPointX) - Math.Abs(lineToXDefault);
- var y = LeftmostPointY + (x * Cos35);
- fillStars.MoveTo(lineToX, LeftmostPointY);
- fillStars.LineTo(lineToX, y);
- }
- if (lineToX >= -132 + offsetX && lineToX <= 132 + offsetX)
- {
- var heightToWeightUpperBound = 2.7f;
- var heightToWeightBottomBound = 0.7f;
- var fromYLength = (132 - Math.Abs(lineToXDefault)) * heightToWeightUpperBound;
- var toYLength = (132 - Math.Abs(lineToXDefault)) * heightToWeightBottomBound;
- var fromY = Math.Abs(LowestPointY) - fromYLength;
- var toY = Math.Abs(LowestPointY) - toYLength;
- fillStars.MoveTo(lineToX, fromY);
- fillStars.LineTo(lineToX, toY);
- }
- }
- }
- return fillStars;
- }
- void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
- {
- SKPath emptyStars = new SKPath();
- for(var i = 0; i < StarsCount; i++)
- {
- DrawStar(
- emptyStars,
- halfStarCoordinates,
- Math.Abs(LeftmostPointX) * i * 2);
- }
- SKPaint paintFill = new SKPaint
- {
- Style = SKPaintStyle.Fill,
- Color = SKColors.Gray,
- StrokeWidth = 5
- };
- SKImageInfo info = args.Info;
- SKSurface surface = args.Surface;
- SKCanvas canvas = surface.Canvas;
- canvas.Clear();
- emptyStars.GetBounds(out var bounds);
- canvas.Translate(info.Width / 2, info.Height / 2);
- canvas.Scale(0.8f * Math.Min(info.Width / bounds.Width,
- info.Height / bounds.Height));
- canvas.Translate(-bounds.MidX, -bounds.MidY);
- canvas.DrawPath(emptyStars, paintFill);
- var fillStarsPath = FillStars();
- SKPaint paintStroke = new SKPaint
- {
- Style = SKPaintStyle.Stroke,
- Color = SKColors.Gold,
- StrokeWidth = 5
- };
- canvas.DrawPath(fillStarsPath, paintStroke);
- }
- private void IsFillValueChanged(int _, int newValue)
- {
- StarsCanvasView.InvalidateSurface();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment