@ -19,17 +19,13 @@ namespace TheGameExtreme.view
[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 > ( ) ;
private SKCanvas canvas ;
@ -40,19 +36,15 @@ namespace TheGameExtreme.view
InitializeComponent ( ) ;
NavigationPage . SetHasNavigationBar ( this , false ) ;
InflateStack ( ) ;
//button.Text = "Retry";
//button.Clicked += retry;
viewmodel = new Main ( playersNames ) ;
//viewmodel.BindingChanged += OnBindingChanged ;
viewmodel . EndGame + = OnEndGame ;
viewmodel . AlertChanged + = OnAlertChanged ;
pseudo . SetBinding ( Label . TextProperty , new Binding ( "Pseudo" , source : viewmodel ) ) ;
InflateStack ( ) ;
InflateHand ( ) ;
}
@ -70,8 +62,7 @@ namespace TheGameExtreme.view
public void OnCanvasViewPaintSurface ( object sender , SKPaintSurfaceEventArgs args )
{
SKCanvas canvas = args . Surface . Canvas ;
canvas = args . Surface . Canvas ;
canvas . Clear ( ) ;
foreach ( TouchManipulationBitmap textPaint in stackCollection )
@ -87,7 +78,6 @@ namespace TheGameExtreme.view
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 ) ,
@ -108,7 +98,7 @@ namespace TheGameExtreme.view
// Do the touch processing
textDictionary . Add ( args . Id , textPaint ) ;
textPaint . ProcessTouchEvent ( args . Id , args . Type , point , stackCollection );
textPaint . ProcessTouchEvent ( args . Id , args . Type , point );
canvasView . InvalidateSurface ( ) ;
break ;
}
@ -119,26 +109,44 @@ namespace TheGameExtreme.view
if ( textDictionary . ContainsKey ( args . Id ) )
{
TouchManipulationBitmap bitmap = textDictionary [ args . Id ] ;
bitmap . ProcessTouchEvent ( args . Id , args . Type , point , stackCollection );
bitmap . ProcessTouchEvent ( args . Id , args . Type , point );
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 ) ;
bool find = false ;
foreach ( TouchManipulationBitmap stack in stackCollection )
{
if ( stack . HitTest ( point ) )
{
int indexPile = stackCollection . IndexOf ( stack ) ;
if ( played ( indexPile , int . Parse ( bitmap . Value ) ) )
{
point = stack . InitialPoint ;
bitmap . ProcessTouchEvent ( args . Id , TouchActionType . Moved , point ) ;
canvasView . InvalidateSurface ( ) ;
bitmap . InitialPoint = stackCollection [ stackCollection . IndexOf ( stack ) ] . InitialPoint ;
stackCollection [ stackCollection . IndexOf ( stack ) ] = bitmap ;
textCollection . RemoveAt ( textCollection . Count - 1 ) ;
find = true ;
}
break ;
}
}
if ( ! find )
{
point = bitmap . InitialPoint ;
bitmap . ProcessTouchEvent ( args . Id , TouchActionType . Moved , point ) ;
canvasView . InvalidateSurface ( ) ;
}
bitmap . ProcessTouchEvent ( args . Id , args . Type , point ) ;
textDictionary . Remove ( args . Id ) ;
canvasView . InvalidateSurface ( ) ;
}
@ -164,32 +172,24 @@ namespace TheGameExtreme.view
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 + + )
for ( int i = 0 ; i < viewmodel . getListOrderedStacks ( ) . Count ; i + + )
{
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 , viewmodel . getListOrderedStacks ( ) [ i ] . Peek ( ) . Value )
{
Matrix = SKMatrix . MakeTranslation ( position . X , position . Y ) ,
InitialPoint = position
} ) ;
position . X + = ( float ) ( DeviceDisplay . MainDisplayInfo . Width * 0.2 ) ;
}
}
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 ;
@ -211,74 +211,38 @@ namespace TheGameExtreme.view
}
}
private void On BindingChanged ( object sender , TopRangeChanged EventArgs args )
private void On EndGame ( object sender , EventArgs 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;
// }
//}
button = ( Button ) gameOption . Children [ 0 ] ;
gameOption . Children . Clear ( ) ;
Button retryButton = new Button ( ) ;
retryButton . Text = "Retry" ;
retryButton . Clicked + = retry ;
retryButton . BackgroundColor = ( Color ) Application . Current . Resources [ "SkyBlueColor" ] ;
gameOption . Children . Add ( retryButton ) ;
}
private void played ( object sender , EventArgs args )
private bool played ( int numStack , int value )
{
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 )
if ( ! viewmodel . played ( numStack , value ) )
{
viewmodel . Alert = "Aucune pile selectionné! Veuillez séléctionner une pile." ;
return ;
return false ;
}
return true ;
}
private void retry ( object sender , EventArgs args )
{
viewmodel = new Main ( playersNames ) ;
viewmodel . BindingChanged + = OnBindingChanged ;
viewmodel . EndGame + = OnEndGame ;
pseudo . SetBinding ( Label . TextProperty , new Binding ( "Pseudo" , source : viewmodel ) ) ;
textCollection . Clear ( ) ;
textDictionary . Clear ( ) ;
canvasView . InvalidateSurface ( ) ;
InflateStack ( ) ;
InflateHand ( ) ;
gameOption . Children . Clear ( ) ;
@ -287,17 +251,13 @@ namespace TheGameExtreme.view
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
if ( ! viewmodel . endTurn ( ) )
{
textCollection . Clear ( ) ;
textDictionary . Clear ( ) ;
stackCollection . Clear ( ) ;
canvasView . InvalidateSurface ( ) ;
InflateStack ( ) ;
InflateHand ( ) ;
}
}