From 31749dd382d99cb0b176a99e411945238794c8d5 Mon Sep 17 00:00:00 2001 From: cldupland Date: Tue, 19 Nov 2019 16:39:11 +0100 Subject: [PATCH] Drag and Drop foireux --- TheGameExtreme/model/Card/Card.cs | 3 +- TheGameExtreme/view/MainPage.xaml.cs | 53 +++++++++++++------ .../view/TouchManipulationBitmap.cs | 32 +++++++++-- TheGameExtreme/viewmodel/CardVM.cs | 30 +---------- TheGameExtreme/viewmodel/Main.cs | 12 +++-- 5 files changed, 78 insertions(+), 52 deletions(-) diff --git a/TheGameExtreme/model/Card/Card.cs b/TheGameExtreme/model/Card/Card.cs index cd06e34..c257e10 100644 --- a/TheGameExtreme/model/Card/Card.cs +++ b/TheGameExtreme/model/Card/Card.cs @@ -11,7 +11,8 @@ namespace TheGameExtreme.model.card public int Value { get { return value; } - set { + set + { this.value = value; //OnPropertyChange("Value"); } diff --git a/TheGameExtreme/view/MainPage.xaml.cs b/TheGameExtreme/view/MainPage.xaml.cs index bc2f195..ee800de 100644 --- a/TheGameExtreme/view/MainPage.xaml.cs +++ b/TheGameExtreme/view/MainPage.xaml.cs @@ -10,6 +10,7 @@ using System.Reflection; using SkiaSharp.Views.Forms; using TouchTracking; using Xamarin.Essentials; +using TheGameExtreme.model.card; namespace TheGameExtreme.view { @@ -124,6 +125,15 @@ namespace TheGameExtreme.view break; case TouchActionType.Released: + if (textDictionary.ContainsKey(args.Id)) + { + TouchManipulationBitmap bitmap = textDictionary[args.Id]; + bitmap.ProcessTouchEvent(args.Id, args.Type, point, stackCollection); + canvasView.InvalidateSurface(); + viewmodel.CurrentHand.RemoveAt((int)args.Id); + } + break; + case TouchActionType.Cancelled: if (textDictionary.ContainsKey(args.Id)) { @@ -150,21 +160,29 @@ namespace TheGameExtreme.view { stackCollection.Clear(); - SKPoint position = new SKPoint(150, (float)((DeviceDisplay.MainDisplayInfo.Height * 0.1) + (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.01)); + SKPaint textPaint = new SKPaint(); + float textWidth = textPaint.MeasureText("01"); + float textSize = 0.05f * (float)DeviceDisplay.MainDisplayInfo.Width * textPaint.TextSize / textWidth; + SKPoint position = new SKPoint((float)((DeviceDisplay.MainDisplayInfo.Width * 0.05) - (textWidth * 0.5)), (float)((DeviceDisplay.MainDisplayInfo.Height * 0.1) + (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.01 + 2 * textSize)); + textPaint.TextSize = textSize; + stackCollection.Add(new TouchManipulationBitmap(textPaint, 1) + { + Matrix = SKMatrix.MakeTranslation(position.X, position.Y), + InitialPoint = position + }); - for (int i = 0; i < 5; i++) + for (int i = 0; i < 4; i++) { - SKPaint textPaint = new SKPaint(); - float textWidth = textPaint.MeasureText("1"); - textPaint.TextSize = 0.05f * (float)DeviceDisplay.MainDisplayInfo.Width * textPaint.TextSize / textWidth; + textPaint = new SKPaint(); + textPaint.TextSize = textSize; + + position.X += (float)(DeviceDisplay.MainDisplayInfo.Width * 0.2); - stackCollection.Add(new TouchManipulationBitmap(textPaint, "1") + stackCollection.Add(new TouchManipulationBitmap(textPaint, 1) { - Matrix = SKMatrix.MakeTranslation(position.X, position.Y + textPaint.TextSize), + Matrix = SKMatrix.MakeTranslation(position.X, position.Y), InitialPoint = position }); - - position.X += textPaint.MeasureText("1") + (float)(DeviceDisplay.MainDisplayInfo.Width * 0.15); } } @@ -172,21 +190,24 @@ namespace TheGameExtreme.view { textCollection.Clear(); - SKPoint position = new SKPoint(100, (float)((DeviceDisplay.MainDisplayInfo.Height * 0.9) - (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.2)); + SKPaint textPaint = new SKPaint(); + float textWidth = textPaint.MeasureText("01"); + float textSize = 0.05f * (float)DeviceDisplay.MainDisplayInfo.Width * textPaint.TextSize / textWidth; + SKPoint position = new SKPoint((float)(DeviceDisplay.MainDisplayInfo.Width * 0.05), (float)((DeviceDisplay.MainDisplayInfo.Height * 0.9) - (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.2 - textSize)); for (int i = 0; i < viewmodel.CurrentHand.Count; i++) { - SKPaint textPaint = new SKPaint(); - float textWidth = textPaint.MeasureText(viewmodel.CurrentHand[i].Value.ToString()); - textPaint.TextSize = 0.05f * (float)DeviceDisplay.MainDisplayInfo.Width * textPaint.TextSize / textWidth; + textPaint = new SKPaint(); + textPaint.TextSize = textSize; + position.X -= (float)(textWidth * 0.5); - textCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.CurrentHand[i].Value.ToString()) + textCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.CurrentHand[i].Value) { - Matrix = SKMatrix.MakeTranslation(position.X, position.Y - textPaint.TextSize), + Matrix = SKMatrix.MakeTranslation(position.X, position.Y), InitialPoint = position }); - position.X += textPaint.MeasureText(viewmodel.CurrentHand[i].Value.ToString()) + 50; + position.X += (float)((DeviceDisplay.MainDisplayInfo.Width * 0.9) / viewmodel.CurrentHand.Count); } } diff --git a/TheGameExtreme/view/TouchManipulationBitmap.cs b/TheGameExtreme/view/TouchManipulationBitmap.cs index 6df5945..28e138a 100644 --- a/TheGameExtreme/view/TouchManipulationBitmap.cs +++ b/TheGameExtreme/view/TouchManipulationBitmap.cs @@ -12,10 +12,18 @@ namespace TheGameExtreme.view string value; SKCanvas canvas; - public TouchManipulationBitmap(SKPaint textPaint, string value) + public TouchManipulationBitmap(SKPaint textPaint, int value) { this.textPaint = textPaint; - this.value = value; + if (value > 0 && value < 10) + { + this.value = "0" + value.ToString(); + } + else + { + this.value = value.ToString(); + } + Matrix = SKMatrix.MakeIdentity(); TouchManager = new TouchManipulationManager @@ -32,12 +40,24 @@ namespace TheGameExtreme.view { this.canvas = canvas; canvas.Save(); + SKMatrix matrix = Matrix; canvas.Concat(ref matrix); - textPaint.Color = SKColors.Red; + + textPaint.Color = SKColors.DarkRed; + textPaint.StrokeWidth = 5; + textPaint.Style = SKPaintStyle.Stroke; + SKRect textBounds = new SKRect(); textPaint.MeasureText(value, ref textBounds); + + SKRoundRect card = new SKRoundRect(textBounds); + card.Inflate(35, 35); + + + canvas.DrawRoundRect(card, textPaint); canvas.DrawText(value, 0, 0, textPaint); + canvas.Restore(); } @@ -83,7 +103,12 @@ namespace TheGameExtreme.view { if (stack.HitTest(location)) { + touchDictionary[id].NewPoint = stack.InitialPoint; + Manipulate(); + touchDictionary[id].PreviousPoint = touchDictionary[id].NewPoint; stackCollection[stackCollection.IndexOf(stack)] = this; + //canvas.Flush(); + find = true; break; } } @@ -92,7 +117,6 @@ namespace TheGameExtreme.view touchDictionary[id].NewPoint = InitialPoint; Manipulate(); touchDictionary[id].PreviousPoint = touchDictionary[id].NewPoint; - touchDictionary.Remove(id); } break; diff --git a/TheGameExtreme/viewmodel/CardVM.cs b/TheGameExtreme/viewmodel/CardVM.cs index 9f4adac..8743ec4 100644 --- a/TheGameExtreme/viewmodel/CardVM.cs +++ b/TheGameExtreme/viewmodel/CardVM.cs @@ -12,25 +12,14 @@ namespace TheGameExtreme.viewmodel public event PropertyChangedEventHandler PropertyChanged; public Card View { get; set; } - protected string image; - - public string Image - { - get { return image; } - set - { - image = value; - OnPropertyChanged("Image"); - } - } - protected int value; - private Image bmp; + protected int value; public int Value { get { return value; } set { this.value = value; + View.Value = value; OnPropertyChanged("Value"); } } @@ -45,21 +34,6 @@ namespace TheGameExtreme.viewmodel { View = view; Value = view.Value; - - /*SKBitmap bit = new SKBitmap(); - RectangleF rectangle = new RectangleF(50, 90, 90, 50); - - Graphics g = Graphics.FromImage(bmp); - - g.SmoothingMode = SmoothingMode.AntiAlias; - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - g.PixelOffsetMode = PixelOffsetMode.HighQuality; - g.DrawString("yourText", new Font("Tahoma", 8), Brushes.Black, rectangle); - - g.Flush();*/ - - - } diff --git a/TheGameExtreme/viewmodel/Main.cs b/TheGameExtreme/viewmodel/Main.cs index 26c26e4..9fb8b08 100644 --- a/TheGameExtreme/viewmodel/Main.cs +++ b/TheGameExtreme/viewmodel/Main.cs @@ -48,7 +48,7 @@ namespace TheGameExtreme.viewmodel } private GameManager gameManager; - public List CurrentHand { get; set; } + public List CurrentHand { get; set; } = new List(); //private ObservableCollection> ListOrderedStacks; @@ -63,7 +63,10 @@ namespace TheGameExtreme.viewmodel gameManager.PlayerChanged += OnPlayerChanged; - CurrentHand = gameManager.CurrentHand; + gameManager.CurrentHand.ForEach( card => + { + CurrentHand.Add(new CardVM(card)); + }); Pseudo = players[0].Pseudo; @@ -72,7 +75,10 @@ namespace TheGameExtreme.viewmodel protected internal void OnPlayerChanged(object source, PlayerChangedEventArgs args) { - CurrentHand = args.NewCurrentHand; + args.NewCurrentHand.ForEach( card => + { + CurrentHand.Add(new CardVM(card)); + }); Pseudo = args.Pseudo; //PlayerChanged?.Invoke(this, args); }