Exemple #1
0
        public TouchEffect(Grid self, Trace t, double minRange)
        {
            center = t.Center;
            InputDevice d = null;

            MainBall = new Ellipse {
                Fill = DefaultBrush,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Height = t.Radius * 2,
                Width  = t.Radius * 2,
                Margin = new Thickness {
                    Left = t.Center.X - t.Radius, Top = t.Center.Y - t.Radius
                }
            };
            Ball = new Ellipse {
                Fill   = DefaultBrush,
                Width  = t.Radius,
                Height = t.Radius,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
            };
            Line = new Line {
                StrokeThickness     = t.Radius / 11,
                HorizontalAlignment = HorizontalAlignment.Left,
                Stroke            = DefaultBrush,
                VerticalAlignment = VerticalAlignment.Top,
                IsHitTestVisible  = false
            };
            EventHandler <TouchEventArgs> start = (s, e) => {
                if (Disabled || d != null)
                {
                    return;
                }
                d = e.Device;
                MoveTo(e.GetTouchPoint(self).Position);
                t.Released = false;
                var sc = RandomColor();
                Ball.Fill   = MainBall.Fill = new RadialGradientBrush(sc);
                Line.Stroke = new LinearGradientBrush(sc);
                ReadyToMove?.Invoke(t.Clone());
            };

            MainBall.TouchDown += start;
            Ball.TouchDown     += start;
            Line.X1             = t.Center.X;
            Line.Y1             = t.Center.Y;
            MoveTo(t.Center);
            self.Children.Add(Line);
            self.Children.Add(Ball);
            self.Children.Add(MainBall);
            Func <TouchEventArgs, bool> tryMove = e => {
                if (e.Device != d)
                {
                    return(false);
                }
                var to = e.GetTouchPoint(self).Position;
                t.Direction = to - t.Center;
                MoveTo(to);
                return(true);
            };

            self.TouchMove += (s, e) => {
                if (tryMove(e))
                {
                    Moving?.Invoke(t.Clone());
                }
            };
            self.TouchLeave += (s, e) => {
                if (!tryMove(e))
                {
                    return;
                }
                d          = null;
                t.Released = true;
                if (t.Direction.Length < minRange)
                {
                    t.Direction = new Vector();
                    Reset();
                }
                else
                {
                    StopMoving?.Invoke(t.Clone());
                }
            };
        }
Exemple #2
0
            public TraceEffect(Grid parent, double radius, Point center, Vector direction, double minLength, int i)
            {
                Index           = i;
                Center          = center;
                Radius          = radius;
                Direction       = direction;
                MainBall.Height = MainBall.Width = radius * 2;
                MainBall.Margin = new Thickness {
                    Left = center.X - radius, Top = center.Y - radius
                };
                InputDevice d    = null;
                var         ball = new Ellipse {
                    Fill   = Brushes.pending,
                    Width  = radius,
                    Height = radius,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment   = VerticalAlignment.Top,
                };
                var line = new Line {
                    Stroke = Brushes.pending,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment   = VerticalAlignment.Top,
                    IsHitTestVisible    = false
                };
                Action <Point> moveTo = newTo => {
                    ball.Margin = new Thickness {
                        Left = newTo.X - ball.Width / 2, Top = newTo.Y - ball.Width / 2
                    };
                    line.X2 = newTo.X;
                    line.Y2 = newTo.Y;
                };
                EventHandler <TouchEventArgs> start = (s, e) => {
                    if (d != null)
                    {
                        return;
                    }
                    d             = e.Device;
                    MainBall.Fill = Brushes.picked;
                    moveTo(e.GetTouchPoint(parent).Position);
                    ReadyToMove?.Invoke(this, EventArgs.Empty);
                };

                MainBall.TouchDown += start;
                ball.TouchDown     += start;
                var to = center + Direction;

                line.X1 = center.X;
                line.Y1 = center.Y;
                moveTo(to);
                ball.Height = Width / 3;
                ball.Width  = Width / 3;
                parent.Children.Add(line);
                parent.Children.Add(MainBall);
                parent.Children.Add(ball);
                moveTo(to);
                Func <TouchEventArgs, bool> tryMove = e => {
                    if (e.Device != d)
                    {
                        return(false);
                    }
                    var newTo = e.GetTouchPoint(parent).Position;
                    Direction = newTo - Center;
                    moveTo(newTo);
                    return(true);
                };

                parent.TouchMove += (s, e) => {
                    if (tryMove(e))
                    {
                        Moving?.Invoke(this, EventArgs.Empty);
                    }
                };
                Func <GradientStopCollection> tempSc = () => {
                    var r  = new Random();
                    var b  = new byte[3];
                    var rr = new List <Color>();
                    for (var ii = 0; ii < 3; ii++)
                    {
                        r.NextBytes(b);
                        rr.Add(Color.FromArgb(255, b[0], b[1], b[2]));
                    }
                    var os = 0;
                    return(new GradientStopCollection(rr.Select(rrrr => new GradientStop {
                        Color = rrrr,
                        Offset = os++ / 2.0
                    })));
                };

                parent.TouchLeave += (s, e) => {
                    if (!tryMove(e))
                    {
                        return;
                    }
                    d        = null;
                    Released = true;
                    if (Direction.Length < minLength)
                    {
                        Direction = new Vector();
                        moveTo(center);
                    }
                    else
                    {
                        var sc = tempSc();
                        ball.Fill = MainBall.Fill = new RadialGradientBrush(sc);
                        line.Fill = new LinearGradientBrush(sc);
                    }
                    Moved?.Invoke(this, EventArgs.Empty);
                };
            }