using System; using System.Collections.Generic; using SkiaSharp; using TheGameExtreme.model.card.cardType; using TheGameExtreme.viewmodel; using TouchTracking; using Xamarin.Essentials; namespace TheGameExtreme.view { /** * Classe permettant de gérer un objet dessiné dans un canvas */ public class TouchManipulationCard { public SKPoint InitialPoint { get; set; } SKPaint textPaint; SKPaint textPaint1; SKPaint textPaint2; Dictionary touchDictionary = new Dictionary(); public CardVM Value; public string display; private float width; private float height; /** * Objet dessiné * Carte qui est dessiné par le textPaint */ public TouchManipulationCard(SKPaint textPaint, CardVM value, float width) { this.textPaint = textPaint; Value = value; if (Value.View.GetType() == typeof(FractionCard)) { textPaint1 = new SKPaint(); textPaint2 = new SKPaint(); textPaint1.TextSize = textPaint.TextSize; textPaint2.TextSize = textPaint.TextSize; } display = Value.ToString(); this.width = width; height = 2f * width; if (!display.Contains(",") && !display.Contains(".") && !display.Contains("/")) { if (Value.Value.CompareTo(-10m) <= 0) { this.width -= textPaint.MeasureText("0") * 0.5f; } if (Value.Value.CompareTo(0m) >= 0 && Value.Value.CompareTo(10) < 0) { this.width += textPaint.MeasureText("0") * 0.5f; } } Matrix = SKMatrix.MakeIdentity(); Mode = TouchManipulationMode.PanOnly; } public TouchManipulationMode Mode { set; get; } public SKMatrix Matrix { set; get; } public SKMatrix InitialMatrix { set; get; } /** * Fonction permettant de dessiner l'objet que contient l'instance dans un canvas donné avec une couleur donnée * Canvas où l'on souhaite dessiner l'objet contenu dans l'instance * Couleur qui sera prise par l'objet */ public void Paint(SKCanvas canvas, SKColor color) { canvas.Save(); SKMatrix matrix = Matrix; canvas.Concat(ref matrix); textPaint.Color = color; textPaint.StrokeWidth = 5; textPaint.Style = SKPaintStyle.Stroke; SKRect card = new SKRect(); if (Value.View.GetType() == typeof(FractionCard)) { textPaint.MeasureText("00", ref card); textPaint1.Color = color; textPaint1.StrokeWidth = 5; textPaint1.Style = SKPaintStyle.Stroke; textPaint2.Color = color; textPaint2.StrokeWidth = 5; textPaint2.Style = SKPaintStyle.Stroke; } else { textPaint.MeasureText(display, ref card); } card.Inflate(width, height); if (Value.View.GetType() == typeof(FractionCard)) { canvas.DrawRect(card, textPaint1); if (((FractionCard)Value.View).Fraction.Numerateur < 10 && ((FractionCard)Value.View).Fraction.Numerateur > 0) // Gros prob { canvas.DrawText(((FractionCard)Value.View).Fraction.Numerateur.ToString(), textPaint.MeasureText(((FractionCard)Value.View).Fraction.Numerateur.ToString()) * 0.5f, -50, textPaint); } else { canvas.DrawText(((FractionCard)Value.View).Fraction.Numerateur.ToString(), 0, -50, textPaint); } canvas.DrawText("__", 0, -textPaint.TextSize * 0.5f, textPaint1); if (((FractionCard)Value.View).Fraction.Denominateur < 10 && ((FractionCard)Value.View).Fraction.Denominateur > 0) { canvas.DrawText(((FractionCard)Value.View).Fraction.Denominateur.ToString(), textPaint.MeasureText(((FractionCard)Value.View).Fraction.Denominateur.ToString()) * 0.5f, 50, textPaint2); } else { canvas.DrawText(((FractionCard)Value.View).Fraction.Denominateur.ToString(), 0, 50, textPaint2); } } else { canvas.DrawRect(card, textPaint); canvas.DrawText(display, 0, 0, textPaint); } canvas.Restore(); } /** * Fonction permettant de changer la couleur de l'objet contenu dans l'instance * Nouvelle couleur que l'on souhaite attribué à l'objet contenu dans l'instance */ public void ChangeColor(SKColor color) { textPaint.Color = color; } /** * Fonction permettant de savoir si le point qu'on lui passe est dans la zone où l'objet est dessiné * Point que l'on souhaite tester * Booléen qui indique si le point donné est dans la zone où l'objet en instance est dessiné */ public bool HitTest(SKPoint location) { // Invert the matrix SKMatrix inverseMatrix; if (Matrix.TryInvert(out inverseMatrix)) { // Transform the point using the inverted matrix SKPoint transformedPoint = inverseMatrix.MapPoint(location); // Check if it's in the untransformed bitmap rectangle SKRect rect = new SKRect(-width, -height - textPaint.TextSize + 10, width + textPaint.MeasureText(display), height + 10); return rect.Contains(transformedPoint); } return false; } /** * Evénement qui permet de gérer le Drag and Drop. * Il permet de traiter le clic, le mouvement, le relachement du clic et la fin de traitement d'un objet. * Index de la position que l'on souhaite traiter * Type d'évenement lancé * Nouveau point que l'on souhaite traiter */ public void ProcessTouchEvent(long id, TouchActionType type, SKPoint location) { switch (type) { case TouchActionType.Pressed: touchDictionary.Add(id, new TouchManipulationInfo { PreviousPoint = location, NewPoint = location }); break; case TouchActionType.Moved: TouchManipulationInfo info = touchDictionary[id]; info.NewPoint = location; Manipulate(); info.PreviousPoint = info.NewPoint; break; case TouchActionType.Released: case TouchActionType.Cancelled: touchDictionary.Remove(id); break; } } /** * Fonction permettant de traiter un déplacement de l'objet en instance */ private void Manipulate() { TouchManipulationInfo[] infos = new TouchManipulationInfo[touchDictionary.Count]; touchDictionary.Values.CopyTo(infos, 0); if (Mode == TouchManipulationMode.PanOnly) { Matrix = SKMatrix.MakeTranslation(infos[0].NewPoint.X, infos[0].NewPoint.Y); } } } }