public ApplicationCanvas()
        {

            avg = new Rectangle();

            avg.Fill = Brushes.Red;
            avg.AttachTo(this);
            avg.SizeTo(16, 16);
            avg.Opacity = 0.2;

            //Canvas.SetZIndex(avg, 1000);

            f.Fill = Brushes.Yellow;
            f.AttachTo(this);
            f.SizeTo(16, 16);
            f.Opacity = 0.5;
        }
        public OrcasAvalonApplicationCanvas()
        {
            Width = DefaultWidth;
            Height = DefaultHeight;
            //Background = 0xffc0c0c0.ToSolidColorBrush();
            Background = Brushes.Black;

            var InfoOverlay = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
            };


            var InfoOverlayShadow = new Rectangle
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
                Fill = Brushes.Black
            }.AttachTo(InfoOverlay);

            var InfoOverlayShadowOpacity = InfoOverlay.ToAnimatedOpacity();
            InfoOverlayShadowOpacity.Opacity = 1;

            var InfoOverlayText = new TextBox
            {
                AcceptsReturn = true,
                IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new Thickness(0),
                Width = DefaultWidth,
                Height = 180,
                TextAlignment = TextAlignment.Center,
                FontSize = 30,
                Foreground = Brushes.White,
                Text = "The winner is\n the first player\n to get an unbroken row\n of five stones",
                FontFamily = new FontFamily("Verdana")

            }.MoveTo(0, (DefaultHeight - 180) / 2).AttachTo(InfoOverlay);



            var TouchOverlay = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
                Background = Brushes.Yellow,
                Opacity = 0,
            };



            var Tiles = new Tile[Intersections * Intersections];

            #region WhereUnderAttack
            Func<int, int, IEnumerable<Tile>> WhereUnderAttack =
                (length, value) =>
                    Tiles.Where(
                        k =>
                        {
                            if (k.Value != 0)
                                return false;


                            return k.IsUnderAttack(value, length);
                        }
                    );
            #endregion

            #region AI
            Action AI =
                delegate
                {
                    // defensive rule:

                    var AttackWith4 = WhereUnderAttack(4, -1).FirstOrDefault();
                    if (AttackWith4 != null)
                    {
                        Console.WriteLine("AttackWith4");
                        AttackWith4.Value = -1;
                        return;
                    }

                    var AttackedBy4 = WhereUnderAttack(4, 1).FirstOrDefault();
                    if (AttackedBy4 != null)
                    {
                        Console.WriteLine("AttackedBy4");
                        AttackedBy4.Value = -1;
                        return;
                    }

                    var AttackWith3 = WhereUnderAttack(3, -1).FirstOrDefault();
                    if (AttackWith3 != null)
                    {
                        Console.WriteLine("AttackWith3");
                        AttackWith3.Value = -1;
                        return;
                    }

                    var AttackedBy3 = WhereUnderAttack(3, 1).FirstOrDefault();
                    if (AttackedBy3 != null)
                    {
                        Console.WriteLine("AttackedBy3");
                        AttackedBy3.Value = -1;
                        return;
                    }

                    var AttackWith2 = WhereUnderAttack(2, -1).FirstOrDefault();
                    if (AttackWith2 != null)
                    {
                        Console.WriteLine("AttackWith2");
                        AttackWith2.Value = -1;
                        return;
                    }

                    var AttackedBy2 = WhereUnderAttack(2, 1).FirstOrDefault();
                    if (AttackedBy2 != null)
                    {
                        Console.WriteLine("AttackedBy2");
                        AttackedBy2.Value = -1;
                        return;
                    }

                    var AttackedBy1 = WhereUnderAttack(1, 1).FirstOrDefault();
                    if (AttackedBy1 != null)
                    {
                        Console.WriteLine("AttackedBy1");
                        AttackedBy1.Value = -1;
                        return;
                    }



                    Console.WriteLine("Random");
                    Tiles.Where(k => k.Value == 0).Random().Value = -1;
                };
            #endregion

            var ResetOverlay = new Rectangle
            {
                Fill = Brushes.Yellow,
                Width = DefaultWidth,
                Height = DefaultHeight,
                Cursor = Cursors.Hand,
            };

            //9000.AtDelay(
            //    delegate
            //    {
            //        ResetOverlay.Orphanize();
            //        InfoOverlayShadowOpacity.Opacity = 0;
            //    }
            //);

            ResetOverlay.MouseLeftButtonUp +=
                delegate
                {
                    Tiles.ForEach(k => k.Value = 0);
                    ResetOverlay.Orphanize();
                    InfoOverlayShadowOpacity.Opacity = 0;
                };

            Action<Tile> Tiles_WithEvents =
                t =>
                {
                    #region add 2d awareness
                    t.ByOffset =
                        (ox, oy) =>
                        {
                            var x = t.X + ox;
                            var y = t.Y + oy;

                            if (x < 0)
                                return null;
                            if (y < 0)
                                return null;
                            if (x >= Intersections)
                                return null;
                            if (y >= Intersections)
                                return null;
                            return Tiles[x + y * Intersections];
                        };
                    #endregion

                    t.TouchOverlay.MouseLeftButtonUp +=
                        delegate
                        {
                            if (t.Value != 0)
                                return;

                            t.Value = 1;

                            if (AtClick != null)
                                AtClick();

                            // did we win?
                            if (Tiles.Any(
                                k =>
                                {
                                    if (k.Value != 1)
                                        return false;

                                    return k.IsUnderAttack(1, 4);
                                }
                            ))
                            {
                                InfoOverlayShadowOpacity.Opacity = 0.8;
                                InfoOverlayText.Text = "You won!\n\n:)";
                                ResetOverlay.AttachTo(TouchOverlay);


                                if (AtWin != null)
                                    AtWin();

                                return;
                            }

                            t.TouchOverlay.Hide();
                            100.AtDelay(
                                delegate
                                {
                                    AI();
                                    t.TouchOverlay.Show();


                                    if (Tiles.Any(
                                        k =>
                                        {
                                            if (k.Value != -1)
                                                return false;

                                            return k.IsUnderAttack(-1, 4);
                                        }
                                    ))
                                    {
                                        InfoOverlayShadowOpacity.Opacity = 0.8;
                                        InfoOverlayText.Text = "You lost!\n\n:(";
                                        ResetOverlay.AttachTo(TouchOverlay);


                                        if (AtLoss != null)
                                            AtLoss();

                                        return;
                                    }
                                }
                            );
                        };


                };

            var TileContainer = new Canvas().AttachTo(this);

            #region build board
            for (int x = 0; x < Intersections; x++)
                for (int y = 0; y < Intersections; y++)
                {

                    var t = new Tile(x, y);



                    t.Container.AttachTo(TileContainer);
                    t.TouchOverlay.AttachTo(TouchOverlay);

                    Tiles[x + y * Intersections] = t;
                    Tiles_WithEvents(t);
                }
            #endregion

            #region add logo
            var img = new com.abstractatech.gomoku.Avalon.Images.jsc
            {
            }.MoveTo(DefaultWidth - 96, DefaultHeight - 96).AttachTo(TileContainer);
            #endregion

            ResetOverlay.AttachTo(TouchOverlay);
            InfoOverlay.AttachTo(this);
            TouchOverlay.AttachTo(this);

            this.SizeChanged +=
                delegate
                {
                    TileContainer.MoveTo(
                        (this.Width - DefaultWidth) /2,
                        (this.Height - DefaultHeight) /2
                        );
                    TouchOverlay.MoveTo(
                     (this.Width - DefaultWidth) / 2,
                     (this.Height - DefaultHeight) / 2
                     );

                    ResetOverlay.SizeTo(this.Width, this.Height);
                    InfoOverlay.SizeTo(this.Width, this.Height);
                    InfoOverlayShadow.SizeTo(this.Width, this.Height);
                    //TouchOverlay.SizeTo(this.Width, this.Height);

                    InfoOverlayText.MoveTo(0, (this.Height - 180) / 2);
                    InfoOverlayText.SizeTo(this.Width, 180);


                };
        }
        public ApplicationCanvas()
        {
            r.Fill = Brushes.Red;
            r.AttachTo(this);
            r.MoveTo(0, 0);
            this.SizeChanged += (s, e) => r.SizeTo(this.Width, this.Height);



            space = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            space.SizeTo(64, 64 + 4 + 64);
            this.SizeChanged += (s, e) => space.MoveTo(this.Width - 64 - 4 - 64 - 4, this.Height - 64 - 4 - 64 - 4);


            up = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            up.SizeTo(64, 64);
            this.SizeChanged += (s, e) => up.MoveTo(this.Width - 64 - 4, this.Height - 64 - 4 - 64 - 4);

            down = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            down.SizeTo(64, 64);
            this.SizeChanged += (s, e) => down.MoveTo(this.Width - 64 - 4, this.Height - 64 - 4);




            control = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            control.SizeTo(64 + 4 + 64, 64);
            this.SizeChanged += (s, e) => control.MoveTo(4, this.Height - 64 - 4 - 64 - 4);

            left = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            left.SizeTo(64, 64);
            this.SizeChanged += (s, e) => left.MoveTo(4, this.Height - 64 - 4);

            right = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            right.SizeTo(64, 64);
            this.SizeChanged += (s, e) => right.MoveTo(4 + 64 + 4, this.Height - 64 - 4);

        }
        public ApplicationCanvas()
        {
            r.Fill = Brushes.Black;
            r.AttachTo(this);
            r.MoveTo(0, 0);
            this.SizeChanged += (s, e) => r.SizeTo(this.Width, this.Height);


             rtiltx = new Rectangle
            {
                Fill = Brushes.Red,
                Opacity = 0.5
            }.AttachTo(this);

             rtilty = new Rectangle
            {
                Fill = Brushes.Blue,
                Opacity = 0.5
            }.AttachTo(this);

            tilt_update = delegate
            {
                rtiltx.SizeTo(8, this.Height);
                rtiltx.MoveTo((this.Width - 8) * tiltx, 0);

                rtilty.SizeTo(this.Width, 8);
                rtilty.MoveTo(0, (this.Height - 8) * tilty);

                rtiltx.Opacity = tiltx;
                rtilty.Opacity = tilty;

            };
            this.SizeChanged +=
                (s, e) =>
                {
                    tilt_update();

                };

            enter = new Rectangle
            {
                Fill = Brushes.Green,
                Opacity = 0.5
            }.AttachTo(this);
            enter.SizeTo(fingersize + 4 + fingersize, fingersize + 4 + fingersize);
            this.SizeChanged += (s, e) => enter.MoveTo(this.Width - fingersize - 4 - fingersize - 4, this.Height - fingersize - 4 - fingersize - 4 - fingersize - 4);

            space = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            space.SizeTo(fingersize + 4 + fingersize, fingersize);
            this.SizeChanged += (s, e) => space.MoveTo(this.Width - fingersize - 4 - fingersize - 4, this.Height - fingersize - 4);


            //up = new Rectangle
            //{
            //    Fill = Brushes.White,
            //    Opacity = 0.5
            //}.AttachTo(this);
            //up.SizeTo(fingersize, fingersize);
            //this.SizeChanged += (s, e) => up.MoveTo(this.Width - fingersize - 4, this.Height - fingersize - 4 - fingersize - 4);

            //down = new Rectangle
            //{
            //    Fill = Brushes.White,
            //    Opacity = 0.5
            //}.AttachTo(this);
            //down.SizeTo(fingersize, fingersize);
            //this.SizeChanged += (s, e) => down.MoveTo(this.Width - fingersize - 4, this.Height - fingersize - 4);




            control = new Rectangle
            {
                Fill = Brushes.Red,
                Opacity = 0.5
            }.AttachTo(this);
            control.SizeTo(fingersize + 4 + fingersize, fingersize + 4 + fingersize);
            this.SizeChanged += (s, e) => control.MoveTo(4, this.Height - fingersize - 4 - fingersize - 4);

            alt = new Rectangle
            {
                Fill = Brushes.White,
                Opacity = 0.5
            }.AttachTo(this);
            alt.SizeTo(fingersize + 4 + fingersize, fingersize);
            this.SizeChanged += (s, e) => alt.MoveTo(4, this.Height - fingersize - 4 - fingersize - 4 - fingersize - 4);


            //left = new Rectangle
            //{
            //    Fill = Brushes.White,
            //    Opacity = 0.5
            //}.AttachTo(this);
            //left.SizeTo(fingersize, fingersize);
            //this.SizeChanged += (s, e) => left.MoveTo(4, this.Height - fingersize - 4);

            //right = new Rectangle
            //{
            //    Fill = Brushes.White,
            //    Opacity = 0.5
            //}.AttachTo(this);
            //right.SizeTo(fingersize, fingersize);
            //this.SizeChanged += (s, e) => right.MoveTo(4 + fingersize + 4, this.Height - fingersize - 4);

        }
        public ApplicationCanvas()
        {
            {
                var r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);
                r.MoveTo(0, 0);
                r.Opacity = 0.9;
                this.SizeChanged += (s, e) => r.SizeTo(this.Width, this.Height / 2);
            }

            {
                var r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);

                this.SizeChanged += (s, e) => r.MoveTo(0, this.Height / 2).SizeTo(this.Width, this.Height / 2);
            }

            var VocabularyLines = Vocabulary.Trim().Split('\n');

            var v = VocabularyLines.Select(
                k =>
                {
                    var verbs = k.Split(':');

                    return new { A = verbs[0].Trim(), B = verbs[1].Trim() };
                }
            ).Randomize().AsCyclicEnumerator();


            var az = 0.5;
            var ax = 0.0;

            var ABCanvas = new Canvas().AttachTo(this);

            var A = new TextBox
            {
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                TextAlignment = System.Windows.TextAlignment.Center,
                Foreground = Brushes.White,
                Text = "suur ettevõte",
                IsReadOnly = true,
                FontSize = 70
            };

            A.AttachTo(ABCanvas);

            var B = new TextBox
            {
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                TextAlignment = System.Windows.TextAlignment.Center,
                Foreground = Brushes.White,
                Text = "large-scale enterprise",
                IsReadOnly = true,
                FontSize = 70
            };

            var MoveNextDisabled = false;
            Action MoveNext = delegate
            {
                if (MoveNextDisabled)
                    return;

                MoveNextDisabled = true;
                v.MoveNext();
                A.Text = v.Current.A;
                B.Text = v.Current.B;

                600.AtDelay(() => MoveNextDisabled = false);
            };

            MoveNext();

            B.AttachTo(ABCanvas);
            B.MoveTo(0, 64);

            Action Update =
                delegate
                {
          

                    if (Math.Abs(ax) > 0.4)
                        MoveNext();

                    az = Math.Min(1, az);
                    az = Math.Max(0, az);

                    var max = this.Height / 6;
                    var min = this.Height / 3;

                    // az = 1 is 0 degrees
                    // az = 0 is 90 degrees

                    az = 1 - az;

                    az -= 0.05;
                    az *= 10;

                    az = 1 - az;

                    az = Math.Min(1, az);
                    az = Math.Max(0, az);

                    //Console.WriteLine(new { az });

                    A.Opacity = Math.Pow(az, 10);
                    var bz = 1 - az;

                    B.Opacity = Math.Pow(bz, 10);

                    A.MoveTo(0, min + az * max - 16).SizeTo(this.Width, 100);
                    B.MoveTo(0, min + (1 - az) * max - 16).SizeTo(this.Width, 100);

                    ABCanvas.MoveTo(this.Width * ax * 0.1, 0);

                };

            this.SizeChanged +=
                delegate
                {
                    Update();
                };

            this.MouseMove += (sender, args) =>
            {
                var p = args.GetPosition(this);

                az = (p.Y / this.Height);
                ax = -1 * ((p.X / this.Width) - 0.5);

                Update();
            };

            this.TouchMove += (sender, args) =>
            {
                var p = args.GetTouchPoint(this).Position;
                az = (p.Y / this.Height);
                ax = -1 * ((p.X / this.Width) - 0.5);

                Update();
            };


            this.Accelerate = (x, y, z) =>
            {
                az = z;
                ax = x;
                Update();
            };
        }
        public ApplicationCanvas()
        {

            var Container720X = new Canvas().AttachTo(this);

            Container720 = new Canvas().AttachTo(Container720X);
            //Container720A = Container720.ToAnimatedOpacity();

            i = new Avalon.Images.Promotion3D_controller_720p();
            i.AttachTo(Container720);
            iA = i.ToAnimatedOpacity();
            iA.Opacity = 0.8;

            var ShadowOverlay = new Rectangle { Fill = Brushes.Black };
            var ShadowOverlayA = ShadowOverlay.ToAnimatedOpacity();


            ShadowOverlay.AttachTo(Container720);
            ShadowOverlay.SizeTo(1280, 720);

            ii = new Avalon.Images.Promotion3D_controller_android_720();
            ii.AttachTo(Container720);
            var iiA = ii.ToAnimatedOpacity();


            var Title = new Avalon.Images.Title();
            Title.AttachTo(Container720X);





            this.SizeChanged += (s, e) =>
            {
                Container720X.MoveTo(
                        (this.Width - 1280) / 2,
                    (this.Height - 720) / 2
                );


                Console.WriteLine(new { this.Width, this.Height });


            };

            enter = new Avalon.Images.start
            {
                Cursor = Cursors.Hand
            }.AttachTo(this);

            entero = enter.ToAnimatedOpacity();


            this.SizeChanged += (s, e) => enter.MoveTo(
                (this.Width - 128) * 0.8 + 64,
                (this.Height - 128) / 2 + 32);

            #region Black
            {
                Rectangle r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);
                r.MoveTo(0, 0);

                this.SizeChanged += (s, e) =>
                {
                    r.Width = this.Width;
                    r.Height = ((this.Height - 720) / 2).Max(0);
                };
            }

            {
                Rectangle r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);
                r.MoveTo(0, 0);

                this.SizeChanged += (s, e) =>
                {
                    r.Width = this.Width;

                    //                NotImplementedException
                    //at ScriptCoreLib.ActionScript.BCLImplementation.System.Windows::__UIElement/InternalGetHeight_100663344()
                    //at ScriptCoreLib.ActionScript.BCLImplementation.System.Windows::__UIElement/VirtualGetHeight_100663342()
                    //at ScriptCoreLib.ActionScript.BCLImplementation.System.Windows::__FrameworkElement/get Height()

                    var Height = ((this.Height - 720) / 2).Max(0);

                    r.Height = Height;
                    r.MoveTo(0, this.Height - Height);
                };
            }
            #endregion

            var hotzone = new Rectangle { Fill = Brushes.Green, Opacity = 0 }.AttachTo(Container720X);

            hotzone.Cursor = Cursors.Hand;

            hotzone.MoveTo(1280 / 3, 720 * 2 / 3);
            hotzone.SizeTo(1280 / 3, 720 / 3);


            hotzone.MouseEnter +=
                delegate
                {
                    ShadowOverlayA.Opacity = 0.4;
                    iiA.Opacity = 1.0;
                    entero.Opacity = 0.2;

                    //Container720A.Opacity = 1;
                };


            hotzone.MouseLeave +=
                delegate
                {
                    ShadowOverlayA.Opacity = 0;
                    iiA.Opacity = 0;
                    entero.Opacity = 0.8;

                    //Container720A.Opacity = VideoPlayingOpacity;
                };


            hotzone.MouseLeftButtonUp +=
                delegate
                {
                    new Uri("http://young-beach-4377.herokuapp.com/android").NavigateTo();
                };

            #region enter
            enter.MouseEnter +=
                delegate
                {
                    entero.Opacity = 1;
                    ShadowOverlayA.Opacity = 0.4;

                    //Container720A.Opacity = 1;
                };


            enter.MouseLeave +=
             delegate
             {
                 entero.Opacity = 0.8;
                 ShadowOverlayA.Opacity = 0;

                 // got video?
                 //Container720A.Opacity = VideoPlayingOpacity;
                 //Container720A.Opacity = 1.0;
             };
            #endregion



            ShadowOverlayA.Opacity = 0;
            iiA.Opacity = 0;
            entero.Opacity = 0.8;

        }
        public ApplicationCanvas()
        {
            //return;

            Background = Brushes.Black;

            var OverlayWhite = new Rectangle
            {
                Fill = Brushes.White
            }.AttachTo(this);

            this.Overlay = new Rectangle
            {
                Fill = Brushes.Black
            }.AttachTo(this);

            var c = new Canvas().AttachTo(this);

            this.SizeChanged +=
                (s, e) =>
                {
                    //Console.WriteLine(new { Width, Height, this.ActualWidth, this.ActualHeight });
                    OverlayWhite.SizeTo(
                        this.Width,
                        this.Height
                    );
                    Overlay.SizeTo(
                        this.Width,
                        this.Height
                    );

                    c.MoveTo(
                        this.Width * 0.5,
                        this.Height * 0.5
                    );
                };

            Func<double, double, Image> f =
                (o, x) =>
                {
                    return new white_jsc
                    {
                        Opacity = o
                    }.AttachTo(c).MoveTo(
                        white_jsc.ImageDefaultWidth / -2 + x,
                        white_jsc.ImageDefaultHeight / -2
                    );
                };

            var a = new List<Action<bool>>();

            var ss = 640;
            var ia = 1;


            for (int i = -ss; i <= 0; i += ia)
            {
                ia += 2;

                {
                    var o = (ss + i + 64) / (double)(ss + 64);
                    var l = f(o, -i);
                    var r = f(o, i);

                    Action<bool> j =
                        n =>
                        {
                            l.Show(n);
                            r.Show(n);
                        };
                    j(false);
                    a.Add(j);
                }
            }



            {
                var l = f(1, 0);

                l.Hide();
                Action<bool> j =
                    n =>
                    {
                        if (n)
                        {
                            Overlay.Fill = Brushes.White;
                            l.Show();
                            return;
                        }

                        15.AtDelay(
                            delegate
                            {
                                Overlay.Fill = Brushes.Black;
                            }
                        );

                        Action<int, int, int> ShakeAt = null;

                        ShakeAt =
                            (delay, x, y) =>
                            {
                                delay.AtDelay(
                                    delegate
                                    {
                                        l.MoveTo(
                                            white_jsc.ImageDefaultWidth / -2 + x,
                                            white_jsc.ImageDefaultHeight / -2 + y
                                        );
                                    }
                                );
                            };

                        if (AnimationShake != null)
                            AnimationShake();


                        ShakeAt(30 * 2, -2, -4);
                        ShakeAt(30 * 3, 2, 3);
                        ShakeAt(30 * 4, -1, -2);
                        ShakeAt(30 * 5, 0, 3);
                        ShakeAt(30 * 6, 0, 4);

                        1000.AtDelay(
                            delegate
                            {
                                if (AnimationAllBlack != null)
                                    AnimationAllBlack();

                                1000.AtDelay(
                                    delegate
                                    {
                                        Overlay.FadeOut(
                                            delegate
                                            {
                                                l.Hide();
                                                OverlayWhite.FadeOut(
                                                    delegate
                                                    {
                                                        if (AnimationCompleted != null)
                                                            AnimationCompleted();
                                                    }
                                                );

                                                if (AnimationAllWhite != null)
                                                    AnimationAllWhite();
                                            }
                                        );
                                    }
                                );
                            }
                        );
                    };
                a.Add(j);
            }

            PrepareAnimation =
                delegate
                {
                    var aa = new Queue<Action<bool>>(a);

                    Action Next = delegate { };


                    Trigger = delegate
                    {
                        if (aa.Count == 0)
                            return;

                        Overlay.Opacity = 1;
                        OverlayWhite.Opacity = 1;

                        Action AnimationLoop = delegate
                        {
                            Next = delegate
                            {
                                var dd = aa.Dequeue();

                                dd(true);


                                (1000 / 24).AtDelay(
                                    () =>
                                    {
                                        dd(false);
                                        if (aa.Count > 0)
                                        {
                                            Next();
                                        }

                                    }
                                );
                            };

                            Next();
                        };

                        Next = delegate
                        {
                            AnimationStartDelay.AtDelay(
                               AnimationLoop
                           );


                        };

                        Next();
                    };

                    this.MouseLeftButtonUp +=
                        delegate
                        {
                            if (TriggerOnClick)
                                Trigger();
                        };

                    return Trigger;
                };


            var once = false;
            this.MouseLeftButtonUp +=
                 delegate
                 {
                     if (once)
                         return;
                     once = true;
                     PrepareAnimation();
                     Trigger();
                 };



            AnimationCompleted += () => PrepareAnimation();
        }
        public ApplicationCanvas()
        {
            Width = DefaultWidth;
            Height = DefaultHeight;


            this.Background = Brushes.Blue;

            var left = new PartialView(Colors.Blue, true).AttachTo(this);
            var right = new PartialView(Colors.Green).AttachTo(this).MoveTo(DefaultWidth / 2, 0);

            this.InfoOverlay = new Canvas().AttachTo(this);

            this.About = new TextBox
            {
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                Foreground = Brushes.Black,
                AcceptsReturn = true,

                Text =

@"Windows Presentation Foundation Touch demo
- Debuggable under .NET (fullscreen when maximized and touch events)
- Adobe Flash 10.1 version via jsc
     No touch events in fullscreen
     Browser fullscreen feature shall be used instead
     Tested for IE, Firefox, Chrome

- Javascript version for Firefox4 via jsc
- Tested with 4 touch points on Dell Latitude XT
- Galaxy S/ Galaxy Tab within browser have only 1 touchpoint
- 2012.09 flash no longer available on android
- multitouch seems to work in firefox/ flash (11.0)
- multitouch seems to work in ie/flash (10.0)
- p2p LAN no longer works?
- Works on AIR for Android! :) 2013-03-05
- Works on AIR for iPad! :) 2014-03-01
"


            }.AttachTo(InfoOverlay).MoveTo(128, 32);

            var c1 = new cloud_mid().AttachTo(InfoOverlay);
            var c2 = new cloud_mid().AttachTo(InfoOverlay);


            this.TouchOverlay = new Canvas
            {



            }.AttachTo(this); //.SizeTo(DefaultWidth, DefaultHeight);

            var TouchArea = new Rectangle
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
                Fill = Brushes.White,
                Opacity = 0
            }.AttachTo(TouchOverlay);


            var t = TouchOverlay.ToTouchEvents(
                m =>
                {
                    // a new reusable finger introduced by the system!
                    var Content = new Canvas();

                    new Avalon.Images.white_jsc().AttachTo(Content).MoveTo(
                        Avalon.Images.white_jsc.ImageDefaultWidth / -2,
                        Avalon.Images.white_jsc.ImageDefaultHeight / -2
                    );

                    var CurrentTouchPoint = default(Tuple<double, double>);

                    Func<Tuple<double, double>> GetTouchPoint = () => CurrentTouchPoint;

                    m.TouchDown += e =>
                    {
                        var p = e.GetTouchPoint(TouchOverlay).Position;

                        CurrentTouchPoint = Tuple.Create(p.X, p.Y);
                        Content.AttachTo(InfoOverlay);
                    };

                    m.TouchUp += e =>
                    {
                        CurrentTouchPoint = null;
                        Content.Orphanize();
                    };

                    m.TouchMove += e =>
                    {
                        var p = e.GetTouchPoint(TouchOverlay).Position;

                        CurrentTouchPoint = Tuple.Create(p.X, p.Y);



                        Content.MoveTo(e, TouchOverlay);
                    };


                    // this is what will be visible for the rest.
                    return new
                    {
                        Content,
                        GetTouchPoint,
                    };
                }
            );




            var touches = from k in t.Touches
                          let p = k.GetTouchPoint()
                          where p != null
                          let x = p.Item1
                          let y = p.Item2
                          select new { x, y, touch = k };

            var left_touch = from k in touches
                             let x = k.x
                             let y = k.y
                             where x < 64
                             where y > Height - 64
                             select k;




            var RocketsPending = new Dictionary<object, Canvas>();

            var buildmode_touches =
                from c in left_touch.Take(1)
                from k in touches
                where k.y < Height - 64

                // um, this finger already seems to have a pending rocket...
                where !RocketsPending.ContainsKey(k.touch)

                select k;

            var RocketsPendingToUpdate =
                from c in touches
                where RocketsPending.ContainsKey(c.touch)
                let rocket = RocketsPending[c.touch]
                let Update = new Action(() => rocket.MoveTo(c.x, c.y))
                select new { rocket, c, Update };

            var __left_buildmode = false;


            #region NotifyBuildRocket
            this.NotifyBuildRocket =
                (x, y) =>
                {
                    #region create a pending rocket
                    var n = new { Content = new Canvas().AttachTo(InfoOverlay) };

                    //Tuple

                    var i = new Avalon.Images.rocket
                    {

                    }.AttachTo(n.Content).MoveTo(
                       Avalon.Images.rocket.ImageDefaultWidth / -4,
                       Avalon.Images.rocket.ImageDefaultHeight / -4
                   ).SizeTo(
                       Avalon.Images.rocket.ImageDefaultWidth / 2,
                       Avalon.Images.rocket.ImageDefaultHeight / 2
                   );


                    // hold/build!
                    //i.Opacity = 0.5;
                    n.Content.MoveTo(x, y);
                    MultitouchExample.Sounds.launch.Source.PlaySound();
                    n.Content.AccelerateAndFade();
                    #endregion
                };
            #endregion




            #region VisualizeTouch
            Action<double, double> VisualizeTouch = (x, y) =>
            {
                var n = new { Content = new Canvas().AttachTo(InfoOverlay) };

                //Tuple

                new Avalon.Images.white_jsc
                {

                }.AttachTo(n.Content).MoveTo(
                   Avalon.Images.white_jsc.ImageDefaultWidth / -4,
                   Avalon.Images.white_jsc.ImageDefaultHeight / -4
               ).SizeTo(
                   Avalon.Images.white_jsc.ImageDefaultWidth / 2,
                   Avalon.Images.white_jsc.ImageDefaultHeight / 2
               );

                n.Content.FadeOut();

                n.Content.MoveTo(x, y);
            };

            this.NotifyVisualizeTouch = VisualizeTouch;
            #endregion



            #region MouseLeftButtonUp
            TouchOverlay.MouseLeftButtonUp +=
                (sender, args) =>
                {
                    // um this device has no finger support?
                    // 2014! less tech?

                    var p = args.GetPosition(TouchOverlay);
                    var item = new { x = p.X, y = p.Y };


                    #region create a pending rocket
                    var n = new { Content = new Canvas().AttachTo(InfoOverlay) };

                    //Tuple

                    var rocket = new Avalon.Images.rocket
                    {

                    }.AttachTo(n.Content).MoveTo(
                       Avalon.Images.rocket.ImageDefaultWidth / -4,
                       Avalon.Images.rocket.ImageDefaultHeight / -4
                   ).SizeTo(
                       Avalon.Images.rocket.ImageDefaultWidth / 2,
                       Avalon.Images.rocket.ImageDefaultHeight / 2
                   );


                    // hold/build!
                    //i.Opacity = 0.5;
                    n.Content.Opacity = 0.5;
                    n.Content.MoveTo(item.x, item.y);
                    #endregion

                    MultitouchExample.Sounds.launch.Source.PlaySound();

                    rocket.Opacity = 1;
                    rocket.AccelerateAndFade();

                    //RocketsPending.Remove(item.touch);

                    if (AtNotifyBuildRocket != null)
                        AtNotifyBuildRocket(item.x, item.y);

                };
            #endregion

            (1000 / 15).AtInterval(
                delegate
                {


                    #region visualize all touches
                    foreach (var item in touches)
                    {
                        VisualizeTouch(item.x, item.y);

                        if (this.AtNotifyVisualizeTouch != null)
                            this.AtNotifyVisualizeTouch(item.x, item.y);
                    }
                    #endregion

                    foreach (var item in
                        from k in t.Touches
                        where k.GetTouchPoint() == null
                        where RocketsPending.ContainsKey(k)
                        select new { rocket = RocketsPending[k], touch = k }
                        )
                    {
                        // finger was lifted and rocked should be launched
                        // no sound in .net
                        // and NO fingers in this laptop!!!! 2014 
                        MultitouchExample.Sounds.launch.Source.PlaySound();

                        item.rocket.Opacity = 1;
                        item.rocket.AccelerateAndFade();

                        RocketsPending.Remove(item.touch);

                        if (AtNotifyBuildRocket != null)
                            AtNotifyBuildRocket(Canvas.GetLeft(item.rocket), Canvas.GetTop(item.rocket));
                    }

                    var left_buildmode = left_touch.Any();
                    if (left_buildmode)
                    {
                        if (!__left_buildmode)
                        {
                            MultitouchExample.Sounds.building.Source.PlaySound();
                            // sound: build mode engaged!
                        }

                        // all other touches in range are now build orders!

                        foreach (var item in RocketsPendingToUpdate)
                        {
                            item.Update();
                        }

                        #region build
                        foreach (var item in buildmode_touches)
                        {
                            #region create a pending rocket
                            var n = new { Content = new Canvas().AttachTo(InfoOverlay) };

                            //Tuple

                            var i = new Avalon.Images.rocket
                            {

                            }.AttachTo(n.Content).MoveTo(
                               Avalon.Images.rocket.ImageDefaultWidth / -4,
                               Avalon.Images.rocket.ImageDefaultHeight / -4
                           ).SizeTo(
                               Avalon.Images.rocket.ImageDefaultWidth / 2,
                               Avalon.Images.rocket.ImageDefaultHeight / 2
                           );


                            // hold/build!
                            //i.Opacity = 0.5;
                            n.Content.Opacity = 0.5;
                            n.Content.MoveTo(item.x, item.y);
                            #endregion

                            RocketsPending[item.touch] = n.Content;


                            //n.Content.AccelerateAndFade();
                        }
                        #endregion


                        left.buildmode_off.Hide();
                        left.buildmode_on.Show();
                    }
                    else
                    {
                        left.buildmode_on.Hide();
                        left.buildmode_off.Show();
                    }
                    __left_buildmode = left_buildmode;
                }
            );

            #region SizeChanged


            Action SizeChanged =
                delegate
                {
                    c1.MoveTo(
    (Width - c1.Width) / 2, 0);

                    c2.MoveTo(
                        (Width - c1.Width) / 2, Height / 2);

                    left.SizeTo(

                        Width / 2,
                        Height
                    );


                    right.MoveTo(
                        Width / 2, 0).SizeTo(

                        Width / 2,
                        Height
                    );

                    TouchArea.SizeTo(Width, Height);
                };

            this.SizeChanged +=
                (s, e) =>
                {
                    SizeChanged();
                };

            SizeChanged();
            #endregion


        }
        public ImageCarouselCanvas(Arguments args)
        {
            this.CloseOnClick = true;

            this.Container = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight
            };

            //var r = new Rectangle
            //{
            //    Fill = Brushes.Red,
            //    Opacity = 0.05
            //};

            //r.SizeTo(DefaultWidth, DefaultHeight);
            //r.AttachTo(this);

            //this.Container.Background = Brushes.Transparent;
            //this.Container.Background = Brushes.Red;

            var y = 0;

            var s = new Stopwatch();

            s.Start();

            var images = new List<XImage>();

            #region AddImages
            Func<Image, XImage> Add =
                i =>
                {
                    y += 32;

                    var n = new XImage
                    {
                        Image = i,
                        Opacity = 0,
                        Radius = 72
                    };


                    RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.Fant);
                    images.Add(n);
                    i.Opacity = 0;
                    i.AttachTo(this);

                    return n;
                };

            args.AddImages(Add);
            #endregion



            var size = 64;



            Action<DispatcherTimer> AtAnimation = delegate { };

            // .net is fast, but js will be slow :)

            var randomphase = Math.PI * 2 * new Random().NextDouble();

            #region AtIntervalWithTimer
            var AnimationTimer = (1000 / 50).AtIntervalWithTimer(
                t =>
                {
                    var ms = s.ElapsedMilliseconds;


                    var i = 0;

                    AtAnimation(t);

                    // using for each must be the last thing in a method 
                    // because .leave operator currently cannot be understood by jsc

                    foreach (var item_ in images)
                    {
                        var item = item_.Image;
                        var phase = Math.PI * 2 * i / images.Count + randomphase;


                        var cos = Math.Cos(step * ms + phase);
                        var sin = Math.Sin(step * ms + phase);

                        var z1margin = 0.7;
                        var z1 = (cos + (1 + z1margin)) / (2 + z1margin);

                        var z2margin = 1.0;
                        var z2 = (cos + (1 + z2margin)) / (2 + z2margin);


                        item.Opacity = z1 * item_.Opacity;
                        item.SizeTo(size * z2, size * z2);
                        item.MoveTo(
                            -sin * item_.Radius

                            + (DefaultWidth + args.ImageDefaultWidth * 0.3) / 2
                            //+ jsc.ImageDefaultWidth 

                            + cos * item_.Radius
                            - size * z2 * 0.5

                            ,
                            sin * item_.Radius

                            + (DefaultHeight + args.ImageDefaultHeight * 0.3) / 2
                            //+ jsc.ImageDefaultHeight

                            - size * z2 * 0.5

                        );

                        Canvas.SetZIndex(item, Convert.ToInt32(z1 * 1000));
                        i++;
                    }

                }
            );
            #endregion

            var logo = args.CreateCenterImage();


            #region WaitAndAppear
            Action<int, double, Action<double>> WaitAndAppear =
                (delay, step__, set_Opacity) =>
                {
                    var a = 0.0;

                    set_Opacity(a);

                    delay.AtDelay(
                        delegate
                        {


                            (1000 / 30).AtIntervalWithTimer(
                                t =>
                                {
                                    a += step__;

                                    if (a > 1)
                                    {
                                        set_Opacity(1);

                                        t.Stop();
                                        return;
                                    }
                                    set_Opacity(a);
                                }
                            );
                        }
                    );
                };
            #endregion


            WaitAndAppear(200, 0.07, n => logo.Opacity = n);
            SattelitesHidden = false;

            ShowSattelites(images, WaitAndAppear);

            ShowSattelitesAgain =
                delegate
                {
                    if (!SattelitesHidden)
                        return;

                    SattelitesHidden = false;

                    if (AtSattelitesShownAgain != null)
                        AtSattelitesShownAgain();

                    foreach (var item__ in images.ToArray())
                    {
                        var item = item__;

                        WaitAndAppear(0, StepSpeedToToggleSattelites, n => item.Opacity = n);
                    }

                };

            Canvas.SetZIndex(logo, 500);

            logo.AttachTo(this).MoveTo(
                (DefaultWidth - args.ImageDefaultWidth) / 2,
                (DefaultHeight - args.ImageDefaultHeight) / 2
            );

            var logo_hit = new Rectangle
            {
                Fill = Brushes.Red,
                Opacity = 0
            };

            Canvas.SetZIndex(logo_hit, 501);

            logo_hit.Cursor = Cursors.Hand;
            logo_hit.AttachTo(this).MoveTo(
                (DefaultWidth - args.ImageDefaultWidth) / 2,
                (DefaultHeight - args.ImageDefaultHeight) / 2
            );
            logo_hit.SizeTo(args.ImageDefaultWidth, args.ImageDefaultHeight);

            #region MouseLeftButtonUp
            logo_hit.MouseLeftButtonUp +=
                delegate
                {
                    if (AtLogoClick != null)
                        AtLogoClick();
                    //new Uri("http://www.jsc-solutions.net").NavigateTo(this.Container);

                    if (CloseOnClick)
                    {
                        Close();
                        logo_hit.Orphanize();
                    }
                };
            #endregion

            #region HideSattelites
            this.HideSattelites =
                delegate
                {
                    SattelitesHidden = true;

                    Action TriggerClose = () => AtAnimation =
                        t =>
                        {
                            if (images.Any(k => k.Opacity > 0))
                                return;

                            t.Stop();

                            if (AtClose != null)
                                AtClose();
                        };


                    AtAnimation =
                        t =>
                        {
                            if (images.Any(k => k.Opacity < 1))
                                return;

                            foreach (var item__ in images.ToArray())
                            {
                                var item = item__;

                                var NextDelay = 1;

                                if (!DisableTimerShutdown)
                                    NextDelay = 1 + 1000.Random();

                                WaitAndAppear(NextDelay, StepSpeedToToggleSattelites, n => item.Opacity = 1 - n);
                            }

                            if (DisableTimerShutdown)
                            {
                                AtAnimation = delegate { };
                            }
                            else
                                TriggerClose();
                        };


                };
            #endregion


            this.Close =
                delegate
                {
                    WaitAndAppear(1, 0.12, n => logo.Opacity = 1 - n);

                    HideSattelites();
                };
        }
        public ApplicationCanvas()
        {
            var c = new CheckBox
            {
                Content = new TextBlock { Text = "Print to Console  " }
            }.MoveTo(8, 96);


            var t = new TextBlock { Text = "?" }.AttachTo(this);

            var redblockcontainer = new Canvas();

            redblockcontainer.Opacity = 0.8;
            redblockcontainer.Background = Brushes.Red;
            redblockcontainer.AttachTo(this);
            redblockcontainer.MoveTo(8, 8);
            this.SizeChanged += (s, e) => redblockcontainer.MoveTo(64 - 16, this.Height / 3 - 16).SizeTo(this.Width - 96, this.Height / 3 - 8);

            var redblockoverlay = new Canvas();

            redblockoverlay.Opacity = 0.1;
            redblockoverlay.Background = Brushes.Red;
            redblockoverlay.AttachTo(this);
            redblockoverlay.MoveTo(8, 8);
            this.SizeChanged += (s, e) => redblockoverlay.MoveTo(64 + 64, this.Height / 3).SizeTo(this.Width - 96 - 64, this.Height / 3 - 8);

            var yellowblock = new Canvas();
            yellowblock.Opacity = 0.7;
            yellowblock.Background = Brushes.Yellow;
            yellowblock.AttachTo(this);
            yellowblock.SizeTo(400, 200);


            var a_case1 = Enumerable.Range(0, 64).Select(
              i =>
              {
                  var rr = new Rectangle();
                  rr.Fill = Brushes.Blue;
                  rr.AttachTo(yellowblock);
                  rr.SizeTo(32, 32);
                  rr.MoveTo(-32, -32);
                  return rr;
              }
          ).ToArray();


            var a_case2 = Enumerable.Range(0, 64).Select(
              i =>
              {
                  var rr = new Rectangle();
                  rr.Fill = Brushes.Green;
                  rr.AttachTo(this);
                  rr.SizeTo(32, 32);
                  rr.MoveTo(-32, -32);
                  return rr;
              }
          ).ToArray();


            var greenblock = new Canvas();
            greenblock.Opacity = 0.5;
            greenblock.Background = Brushes.Green;
            greenblock.AttachTo(this);
            greenblock.SizeTo(400, 200);
            greenblock.MoveTo(200 - 12, 12);

            var a_case3 = Enumerable.Range(0, 64).Select(
              i =>
              {
                  var rr = new Rectangle();
                  rr.Fill = Brushes.Black;
                  rr.AttachTo(redblockcontainer);
                  rr.SizeTo(32, 32);
                  rr.MoveTo(-32, -32);
                  return rr;
              }
          ).ToArray();



            var case1 = yellowblock;
            var case2 = greenblock;
            var case3 = redblockoverlay;

            #region case1
            case1.TouchDown +=
                (s, e) =>
                {
                    e.Handled = true;
                    // is called implicitly on Android Chrome
                    e.TouchDevice.Capture(case1);

                    Console.WriteLine("TouchDown");
                };

            case1.MouseMove +=
            (s, e) =>
            {
                // case 1
                var p = e.GetPosition(case1);

                a_case1.Last().MoveTo(p);

                if ((bool)c.IsChecked)
                    Console.WriteLine("MouseMove " + p);
            };

            case1.TouchUp +=
          (s, e) =>
          {
              Console.WriteLine("TouchUp");
          };
            #endregion

            case1.TouchMove +=
                (s, e) =>
                {
                    // case 1
                    var p = e.GetTouchPoint(case1).Position;

                    a_case1[e.TouchDevice.Id].MoveTo(p);

                    if ((bool)c.IsChecked)
                        Console.WriteLine("TouchMove " + e.TouchDevice.Id + " " + p);
                };




            #region case2
            case2.TouchDown +=
            (s, e) =>
            {
                e.Handled = true;
                // is called implicitly on Android Chrome
                e.TouchDevice.Capture(case2);

                Console.WriteLine("TouchDown");
            };

            case2.MouseMove +=
             (s, e) =>
             {
                 // case 1
                 var p = e.GetPosition(this);

                 a_case2.Last().MoveTo(p);

                 if ((bool)c.IsChecked)
                     Console.WriteLine("MouseMove " + p);
             };
            case2.TouchUp +=
          (s, e) =>
          {
              Console.WriteLine("TouchUp");
          };
            #endregion
            case2.TouchMove +=
                (s, e) =>
                {
                    var p = e.GetTouchPoint(this).Position;

                    t.Text = new { case2 = p }.ToString();

                    //// case 2
                    //var a = ((object)e as __TouchEventArgs);
                    //if (a != null)
                    //{



                    //    var pp = new Point(
                    //       a.InternalValue.pageX,
                    //       a.InternalValue.pageY
                    //    );


                    //    var b = GetPositionData.Of(a.InternalElement);

                    //    var item = b.Last();

                    //    pp.X -= item.X;
                    //    pp.Y -= item.Y;


                    //    t.Text = new
                    //    {
                    //        case2 = new
                    //        {
                    //            pp,

                    //            a.InternalValue.screenX,
                    //            a.InternalValue.screenY,
                    //            a.InternalValue.clientX,
                    //            a.InternalValue.clientY,
                    //            a.InternalValue.pageX,
                    //            a.InternalValue.pageY
                    //        }
                    //    }.ToString();
                    //}



                    a_case2[e.TouchDevice.Id].MoveTo(p);


                    if ((bool)c.IsChecked)
                        Console.WriteLine("TouchMove " + e.TouchDevice.Id + " " + p);
                };



            #region case3
            case3.TouchDown +=
            (s, e) =>
            {
                e.Handled = true;
                // is called implicitly on Android Chrome
                e.TouchDevice.Capture(case3);

                Console.WriteLine("TouchDown");
            };

            case3.MouseMove +=
                (s, e) =>
                {
                    // case 1
                    var p = e.GetPosition(redblockcontainer);

                    a_case3.Last().MoveTo(p);

                    if ((bool)c.IsChecked)
                        Console.WriteLine("MouseMove " + p);
                };
            case3.TouchUp +=
                  (s, e) =>
                  {
                      Console.WriteLine("TouchUp");
                  };
            #endregion
            case3.TouchMove +=
                (s, e) =>
                {
                    // case 3
                    var p = e.GetTouchPoint(redblockcontainer).Position;

                    t.Text = new { case3 = p }.ToString();

                    //var args = ((object)e as __TouchEventArgs);
                    //if (args != null)
                    //{

                    //    var pp = new Point(
                    //       args.InternalValue.pageX,
                    //       args.InternalValue.pageY
                    //    );


                    //    var a = GetPositionData.Of(((object)redblockcontainer as __Canvas).InternalContent);
                    //    var b = GetPositionData.Of(args.InternalElement);


                    //    if (b.Count > 0)
                    //    {
                    //        var item = b.Last();

                    //        pp.X -= item.X;
                    //        pp.Y -= item.Y;
                    //    }


                    //    // top elements might be the same so we remove them
                    //    var loop = true;

                    //    while (loop)
                    //    {
                    //        loop = false;

                    //        if (a.Count > 0)
                    //            if (b.Count > 0)
                    //                if (a[a.Count - 1].Element == b[b.Count - 1].Element)
                    //                {
                    //                    a.RemoveAt(a.Count - 1);
                    //                    b.RemoveAt(b.Count - 1);

                    //                    loop = true;
                    //                }
                    //    }

                    //    if (a.Count > 0)
                    //    {
                    //        var itembb = a.Last();

                    //        pp.X -= itembb.X;
                    //        pp.Y -= itembb.Y;
                    //    }

                    //    if (b.Count > 0)
                    //    {
                    //        var item = b.Last();

                    //        pp.X += item.X;
                    //        pp.Y += item.Y;
                    //    }

                    //    t.Text = new
                    //    {
                    //        case2 = new
                    //        {
                    //            p,
                    //            pp,

                    //            //a.InternalValue.screenX,
                    //            //a.InternalValue.screenY,
                    //            //a.InternalValue.clientX,
                    //            //a.InternalValue.clientY,
                    //            args.InternalValue.pageX,
                    //            args.InternalValue.pageY
                    //        }
                    //    }.ToString();

                    //    a_case3[e.TouchDevice.Id].MoveTo(pp);

                    //    Console.WriteLine("TouchMove " + e.TouchDevice.Id + " " + pp);
                    //    return;
                    //}


                    a_case3[e.TouchDevice.Id].MoveTo(p);

                    if ((bool)c.IsChecked)
                        Console.WriteLine("TouchMove " + e.TouchDevice.Id + " " + p);
                };

            c.AttachTo(this);

        }
        public ApplicationCanvas()
        {
            var r0shadow = new Rectangle();
            r0shadow.Opacity = 0.5;
            r0shadow.Fill = Brushes.Black;
            r0shadow.AttachTo(this);

            var r1shadow = new Rectangle();
            r1shadow.Opacity = 0.5;
            r1shadow.Fill = Brushes.Black;
            r1shadow.AttachTo(this);


            var rcontent = new Canvas().AttachTo(this);


            var r0 = new Rectangle();


            r0.Opacity = 0.8;
            r0.Fill = Brushes.DarkGreen;
            r0.AttachTo(rcontent);

            var r1 = new Rectangle();


            r1.Opacity = 0.8;
            r1.Fill = Brushes.DarkGreen;
            r1.AttachTo(rcontent);
            r1.MoveTo(0, 96);



            r0.SizeTo(500, 96 - 16);
            r1.SizeTo(500, 230);
            r0shadow.SizeTo(500, 96 - 16);
            r1shadow.SizeTo(500, 230);

            var t = new TextBox();

            t.IsReadOnly = true;

            //            type: System.Windows.Controls.TextBlock
            //method: Void set_Foreground(System.Windows.Media.Brush)

            t.Foreground = Brushes.Yellow;
            t.AttachTo(rcontent);
            t.AcceptsReturn = true;
            t.Text = "You have found a shop.\nSee something you like?";
            t.Background = Brushes.Transparent;
            t.BorderThickness = new Thickness(0);
            t.FontSize = 24;
            t.MoveTo(8, 8);


            t2 = new TextBox();

            t2.IsReadOnly = true;

            //            type: System.Windows.Controls.TextBlock
            //method: Void set_Foreground(System.Windows.Media.Brush)

            t2.Foreground = Brushes.Yellow;
            t2.AttachTo(rcontent);
            t2.AcceptsReturn = true;
            t2.Text = "! First we will log you in to FACEBOOK and then to PAYPAL, takes a few...";
            t2.Background = Brushes.Transparent;
            t2.BorderThickness = new Thickness(0);
            t2.MoveTo(8, 290);

            t2o = t2.ToAnimatedOpacity();

            t2o.Opacity = 0.2;

            bg_ammo = new Rectangle();


            bg_ammo.Fill = Brushes.Yellow;
            bg_ammo.AttachTo(rcontent);
            bg_ammo.SizeTo(400, 48);
            bg_ammo.MoveTo(50, 128 + 32 - 8);

            var ammo = new Avalon.Images.avatars_ammo();

            ammo.AttachTo(rcontent);
            ammo.MoveTo(32 + 32, 128 + 32);



            overlay_ammo.Fill = Brushes.Yellow;
            overlay_ammo.Opacity = 0;
            overlay_ammo.AttachTo(rcontent);
            overlay_ammo.SizeTo(400, 48);
            overlay_ammo.MoveTo(50, 128 + 32 - 8);

            var bg_ammo_opacity = bg_ammo.ToAnimatedOpacity();

            overlay_ammo.Cursor = Cursors.Hand;
            bg_ammo_opacity.Opacity = 0.5;
            overlay_ammo.MouseEnter +=
                delegate
                {
                    bg_ammo_opacity.Opacity = 1.0;
                };

            overlay_ammo.MouseLeave +=
                delegate
                {
                    bg_ammo_opacity.Opacity = 0.5;
                };
            overlay_ammo.MouseLeftButtonUp +=
             delegate
             {
                 t2o.Opacity = 1;

                 if (BuyAmmo != null)
                     BuyAmmo();
             };














            bg_shotgun.Opacity = 0.5;
            bg_shotgun.Fill = Brushes.Yellow;
            bg_shotgun.AttachTo(rcontent);
            bg_shotgun.SizeTo(400, 48);
            bg_shotgun.MoveTo(50, 128 + 64 + 32);

            var shotgun = new Avalon.Images.avatars_shotgun();

            shotgun.AttachTo(rcontent);
            shotgun.MoveTo(32 + 32, 128 + 64 + 32 + 8);



            var overlay_shotgun = new Rectangle();


            overlay_shotgun.Opacity = 0;
            overlay_shotgun.Fill = Brushes.Yellow;
            overlay_shotgun.AttachTo(rcontent);
            overlay_shotgun.SizeTo(400, 48);
            overlay_shotgun.MoveTo(50, 128 + 64 + 32);

            var bg_shotgun_opacity = bg_shotgun.ToAnimatedOpacity();

            overlay_shotgun.Cursor = Cursors.Hand;
            bg_shotgun_opacity.Opacity = 0.5;
            overlay_shotgun.MouseEnter +=
                delegate
                {
                    bg_shotgun_opacity.Opacity = 1.0;
                };

            overlay_shotgun.MouseLeave +=
                delegate
                {
                    bg_shotgun_opacity.Opacity = 0.5;
                };

            overlay_shotgun.MouseLeftButtonUp +=
                delegate
                {
                    t2o.Opacity = 1;

                    if (BuyShotgun != null)
                        BuyShotgun();
                };

            this.SizeChanged += (s, e) =>
            {



                r0shadow.MoveTo((this.Width - 500) / 2 + 4, (this.Height - 400) / 2 + 4);
                r1shadow.MoveTo((this.Width - 500) / 2 + 4, (this.Height - 400) / 2 + 4 + 96);
                rcontent.MoveTo((this.Width - 500) / 2, (this.Height - 400) / 2);

            };

            var rclose_shadow = new Rectangle();


            //rclose.Opacity = 0.8;
            rclose_shadow.Fill = Brushes.Black;
            rclose_shadow.AttachTo(rcontent);
            rclose_shadow.Opacity = 0.3;

            rclose_shadow.SizeTo(32, 32);

            var rclose = new Rectangle();


            rclose.Fill = Brushes.Red;
            rclose.AttachTo(rcontent);
            rclose.Cursor = Cursors.Hand;

            rclose.SizeTo(32, 32);

            var rclosemagin = -8;
            rclose.MoveTo(500 - 32 - rclosemagin, rclosemagin);
            rclose_shadow.MoveTo(500 - 32 - rclosemagin + 4, rclosemagin + 4);


            rclose.MouseLeftButtonUp +=
                delegate
                {
                    if (Close != null)
                        Close();
                };
        }