You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
8.0 KiB

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<long, TouchManipulationInfo> touchDictionary = new Dictionary<long, TouchManipulationInfo>();
public CardVM Value;
public string display;
private float width;
private float height;
/**
* <param name="textPaint">Objet dessiné</param>
* <param name="value">Carte qui est dessiné par le textPaint</param>
*/
public TouchManipulationCard(SKPaint textPaint, CardVM value, float width)
{
this.textPaint = textPaint;
Value = value;
this.width = width;
height = 2f * this.width;
if (Value.View.GetType() == typeof(FractionCard))
{
textPaint1 = new SKPaint();
textPaint2 = new SKPaint();
textPaint1.TextSize = textPaint.TextSize;
textPaint2.TextSize = textPaint.TextSize;
}
display = Value.ToString();
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
* <param name="canvas">Canvas où l'on souhaite dessiner l'objet contenu dans l'instance</param>
* <param name="color">Couleur qui sera prise par l'objet</param>
*/
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(0, 0, 100, 100);
SKRect card;
if (Value.View.GetType() == typeof(FractionCard))
{
card = new SKRect(-width, -height - textPaint.TextSize, width + textPaint.MeasureText("00"), height);
textPaint1.Color = color;
textPaint1.StrokeWidth = 5;
textPaint1.Style = SKPaintStyle.Stroke;
textPaint2.Color = color;
textPaint2.StrokeWidth = 5;
textPaint2.Style = SKPaintStyle.Stroke;
}
else
{
card = new SKRect(-width, -height - textPaint.TextSize, width + textPaint.MeasureText(display), height);
}
if (Value.View.GetType() == typeof(FractionCard))
{
canvas.DrawRect(card, textPaint1);
if (((FractionCard)Value.View).Fraction.Numerateur < 10 && ((FractionCard)Value.View).Fraction.Numerateur > 0)
{
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("__", textPaint.MeasureText("__") * 0.1f, -textPaint.TextSize * 0.4f, 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
* <param name="color">Nouvelle couleur que l'on souhaite attribué à l'objet contenu dans l'instance</param>
*/
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é
* <param name="location">Point que l'on souhaite tester</param>
* <returns>Booléen qui indique si le point donné est dans la zone où l'objet en instance est dessiné</returns>
*/
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, width + textPaint.MeasureText(display), height);
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.
* <param name="id">Index de la position que l'on souhaite traiter</param>
* <param name="type">Type d'évenement lancé</param>
* <param name="location">Nouveau point que l'on souhaite traiter</param>
*/
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);
}
}
}
}