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.

133 lines
4.2 KiB

using System;
using System.Collections.Generic;
using SkiaSharp;
using TheGameExtreme.viewmodel;
using TouchTracking;
using Xamarin.Essentials;
namespace TheGameExtreme.view
{
public class TouchManipulationBitmap
{
public SKPoint InitialPoint { get; set; }
SKPaint textPaint;
Dictionary<long, TouchManipulationInfo> touchDictionary = new Dictionary<long, TouchManipulationInfo>();
public CardVM Value;
public string display;
private float width = 0.01f * (float)DeviceDisplay.MainDisplayInfo.Width;
private float height = 0.01f * (float)DeviceDisplay.MainDisplayInfo.Width * 3f;
public TouchManipulationBitmap(SKPaint textPaint, CardVM value)
{
this.textPaint = textPaint;
Value = value;
display = Value.ToString();
if (!display.Contains(",") && !display.Contains(".") && !display.Contains("/"))
{
if (decimal.Parse(display) < 0 && decimal.Parse(display) > -10)
{
display = "-0" + Math.Abs(decimal.Parse(display)).ToString();
}
else if (decimal.Parse(display) >= 0 && decimal.Parse(display) < 10)
{
display = "0" + display;
}
}
Matrix = SKMatrix.MakeIdentity();
Mode = TouchManipulationMode.PanOnly;
}
public TouchManipulationMode Mode { set; get; }
public SKMatrix Matrix { set; get; }
public SKMatrix InitialMatrix { set; get; }
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 textBounds = new SKRect();
textPaint.MeasureText(display, ref textBounds);
SKRoundRect card = new SKRoundRect(textBounds, 1f, 1f);
card.Inflate(width, height);
canvas.DrawRoundRect(card, textPaint);
canvas.DrawText(display, 0, 0, textPaint);
canvas.Restore();
}
public void ChangeColor(SKColor color)
{
textPaint.Color = color;
}
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(-70, -110, textPaint.MeasureText(display) + 35, textPaint.TextSize);
return rect.Contains(transformedPoint);
}
return false;
}
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;
}
}
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);
}
}
}
}