Пример #1
0
    private void DrawIndicators(SKCanvas canvas, float radius)
    {
        var threashHold = radius.Scale(0.05f);

        var previousDirection = Direction;

        canvas.Save();

        var baseRadius = radius.Scale(0.80f);

        var size = radius.Scale(0.08f);

        _indicatorPath = SKPath.ParseSvgPathData($"M-{size} 0L0 {size}L{size} 0L-{size} 0Z");

        _indicatorPaint.ImageFilter = SKImageFilter.CreateDropShadow(0, 1, baseRadius.Scale(0.005f), baseRadius.Scale(0.005f), SKColors.Gray);


        // Top

        canvas.Save();

        var isForward = _position.Y > threashHold;

        _indicatorPaint.Color = isForward ? IndicatorsColor.ToSKColor() : BackgroundColor.ToEngravedGradientColor(IsEnabled);

        Direction = isForward ? Direction & (~JoystickDirection.None) | JoystickDirection.Forward : Direction & (~JoystickDirection.Forward);

        canvas.Translate(0, baseRadius);

        canvas.RotateDegrees(0);

        canvas.DrawPath(_indicatorPath, _indicatorPaint);

        canvas.Restore();

        // Rigth

        canvas.Save();

        var isRigth = _position.X > threashHold;

        _indicatorPaint.Color = isRigth ? IndicatorsColor.ToSKColor() : BackgroundColor.ToEngravedGradientColor(IsEnabled);

        Direction = isRigth ? Direction & (~JoystickDirection.None) | JoystickDirection.Right : Direction & (~JoystickDirection.Right);

        canvas.Translate(baseRadius, 0);

        canvas.RotateDegrees(-90);

        canvas.DrawPath(_indicatorPath, _indicatorPaint);

        canvas.Restore();

        // Backward

        canvas.Save();

        var isBackward = _position.Y < -threashHold;

        _indicatorPaint.Color = isBackward ? IndicatorsColor.ToSKColor() : BackgroundColor.ToEngravedGradientColor(IsEnabled);

        Direction = isBackward ? Direction & (~JoystickDirection.None) | JoystickDirection.Backward : Direction & (~JoystickDirection.Backward);

        canvas.Translate(0, -baseRadius);

        canvas.RotateDegrees(180);

        canvas.DrawPath(_indicatorPath, _indicatorPaint);

        canvas.Restore();


        // Left

        canvas.Save();

        var isLeft = _position.X < -threashHold;

        _indicatorPaint.Color = isLeft ? IndicatorsColor.ToSKColor() : BackgroundColor.ToEngravedGradientColor(IsEnabled);

        Direction = isLeft ? Direction & (~JoystickDirection.None) | JoystickDirection.Left : Direction & (~JoystickDirection.Left);

        canvas.Translate(-baseRadius, 0);

        canvas.RotateDegrees(90);

        canvas.DrawPath(_indicatorPath, _indicatorPaint);

        canvas.Restore();

        if (!isForward && !isBackward && !isRigth && !isLeft)
        {
            Direction = JoystickDirection.None;
        }

        if (Direction != previousDirection)
        {
            DigitalMoved?.Invoke(this, new EventArgs());
            Execute(Command);
        }

        canvas.Restore();
    }