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.

311 lines
11 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using TheGameExtreme.model.@event;
using TheGameExtreme.viewmodel;
using SkiaSharp;
using System.IO;
using System.Reflection;
using SkiaSharp.Views.Forms;
using TouchTracking;
using Xamarin.Essentials;
using TheGameExtreme.model.card;
namespace TheGameExtreme.view
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
private bool isFirst = true;
private Main viewmodel;
private List<SKRect> stacks = new List<SKRect>();
Button button;
List<string> playersNames;
TouchManipulationBitmap textPaint;
List<TouchManipulationBitmap> textCollection = new List<TouchManipulationBitmap>();
List<TouchManipulationBitmap> stackCollection = new List<TouchManipulationBitmap>();
Dictionary<long, TouchManipulationBitmap> textDictionary = new Dictionary<long, TouchManipulationBitmap>();
public MainPage(List<string> playersNames)
{
this.playersNames = playersNames;
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
InflateStack();
//button.Text = "Retry";
//button.Clicked += retry;
viewmodel = new Main(playersNames);
//viewmodel.BindingChanged += OnBindingChanged;
viewmodel.AlertChanged += OnAlertChanged;
pseudo.SetBinding(Label.TextProperty, new Binding("Pseudo", source: viewmodel));
InflateHand();
}
private void OnAlertChanged(object sender, EventArgs args)
{
if (viewmodel.Alert != null)
{
DisplayAlert("", viewmodel.Alert, "OK");
viewmodel.Alert = null;
}
}
public void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKCanvas canvas = args.Surface.Canvas;
canvas.Clear();
foreach (TouchManipulationBitmap textPaint in stackCollection)
{
textPaint.Paint(canvas);
}
foreach (TouchManipulationBitmap textPaint in textCollection)
{
textPaint.Paint(canvas);
}
}
public void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
// Convert Xamarin.Forms point to pixels
TouchTrackingPoint pt = args.Location;
SKPoint point =
new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width),
(float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height));
switch (args.Type)
{
case TouchActionType.Pressed:
for (int i = textCollection.Count - 1; i >= 0; i--)
{
TouchManipulationBitmap textPaint = textCollection[i];
if (textPaint.HitTest(point))
{
// Move bitmap to end of collection
textCollection.Remove(textPaint);
textCollection.Add(textPaint);
// Do the touch processing
textDictionary.Add(args.Id, textPaint);
textPaint.ProcessTouchEvent(args.Id, args.Type, point, stackCollection);
canvasView.InvalidateSurface();
break;
}
}
break;
case TouchActionType.Moved:
if (textDictionary.ContainsKey(args.Id))
{
TouchManipulationBitmap bitmap = textDictionary[args.Id];
bitmap.ProcessTouchEvent(args.Id, args.Type, point, stackCollection);
canvasView.InvalidateSurface();
}
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))
{
TouchManipulationBitmap bitmap = textDictionary[args.Id];
bitmap.ProcessTouchEvent(args.Id, args.Type, point, stackCollection);
textDictionary.Remove(args.Id);
canvasView.InvalidateSurface();
}
break;
}
}
//public void OnTouchModePickerSelectedIndexChanged(object sender, EventArgs args)
//{
// if (textPaint != null)
// {
// Picker picker = (Picker)sender;
// textPaint.TouchManager.Mode = (TouchManipulationMode)picker.SelectedItem;
// }
//}
private void InflateStack()
{
stackCollection.Clear();
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 < 4; i++)
{
textPaint = new SKPaint();
textPaint.TextSize = textSize;
position.X += (float)(DeviceDisplay.MainDisplayInfo.Width * 0.2);
stackCollection.Add(new TouchManipulationBitmap(textPaint, 1)
{
Matrix = SKMatrix.MakeTranslation(position.X, position.Y),
InitialPoint = position
});
}
}
private void InflateHand()
{
textCollection.Clear();
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++)
{
textPaint = new SKPaint();
textPaint.TextSize = textSize;
position.X -= (float)(textWidth * 0.5);
textCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.CurrentHand[i].Value)
{
Matrix = SKMatrix.MakeTranslation(position.X, position.Y),
InitialPoint = position
});
position.X += (float)((DeviceDisplay.MainDisplayInfo.Width * 0.9) / viewmodel.CurrentHand.Count);
}
}
private void OnBindingChanged(object sender, TopRangeChangedEventArgs args)
{
//switch (args.NumStackChanged)
//{
// case 0:
// pile0.Text = args.NewTopRangeCard.Value.ToString();
// break;
// case 1:
// pile1.Text = args.NewTopRangeCard.Value.ToString();
// break;
// case 2:
// pile2.Text = args.NewTopRangeCard.Value.ToString();
// break;
// case 3:
// pile3.Text = args.NewTopRangeCard.Value.ToString();
// break;
//}
//for (int i = 0; i < Hand.Children.Count; i++)
//{
// Button b = Hand.Children[i] as Button;
// if (String.Equals(b.Text,args.NewTopRangeCard.Value.ToString()))
// {
// Hand.Children.RemoveAt(i);
// break;
// }
//}
}
private void played(object sender, EventArgs args)
{
int numStack = 0;
bool hasFind = false;
stacks.ForEach(checkbox =>
{
//if (checkbox.IsChecked)
//{
// hasFind = true;
// if (viewmodel.played(numStack, Convert.ToInt32((sender as Button).Text)))
// {
// botPanel.Children.Add(button);
// }
//}
numStack += 1;
});
if (!hasFind)
{
viewmodel.Alert = "Aucune pile selectionné! Veuillez séléctionner une pile.";
return;
}
}
private void retry(object sender, EventArgs args)
{
viewmodel = new Main(playersNames);
viewmodel.BindingChanged += OnBindingChanged;
pseudo.SetBinding(Label.TextProperty, new Binding("Pseudo", source: viewmodel));
InflateStack();
InflateHand();
gameOption.Children.Clear();
gameOption.Children.Add(button);
}
private void EndTurn(object sender, EventArgs args)
{
if (viewmodel.endTurn())
{
button = (Button)gameOption.Children[0];
gameOption.Children.Clear();
Button retryButton = new Button();
retryButton.Text = "Retry";
retryButton.Clicked += retry;
gameOption.Children.Add(retryButton);
}
else
{
InflateHand();
}
}
private async void PlayToHome(object sender, EventArgs args)
{
await Navigation.PopToRootAsync();
}
}
}