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

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

            this.SizeChanged += (s, e) => Scene.MoveTo(this.Width / 2, this.Height / 2);

            var EgoSpeed = 0.5;
            var EgoPosition = 0.0;

            {
                var ego = new ski1();

                ego.AttachTo(Scene);

                (1000 / 60).AtIntervalWithCounter(
                    c =>
                    {
                        EgoPosition += EgoSpeed;

                        ego.MoveTo(0, (EgoPosition) % this.Height - this.Height / 2);
                    }
                );
            }

            Action<double, double> tree = (x, y) => new ski51().AttachTo(Scene).MoveTo(x, y);
            Action<double, double> deadtree = (x, y) => new ski50().AttachTo(Scene).MoveTo(x, y);
            Action<double, double> stone = (x, y) => new ski45().AttachTo(Scene).MoveTo(x, y);
            Action<double, double> stonefield = (x, y) => new ski27().AttachTo(Scene).MoveTo(x, y);

            tree(-64, 32);
            tree(64, 32);

            tree(-64, -32);
            tree(64, -32);

            var logo = new Avalon.Images.jsc().AttachTo(this);

            this.RotateScene =
                a =>
                {
                    Scene.RenderTransform = new RotateTransform(a);
                };

            RotateScene(12);

            this.Accelerate =
                (ax, ay, az) =>
                {
                    Scene.RenderTransform = new RotateTransform(ax * 90);

                    EgoSpeed = (1 - az) * 2;
                };
        }
        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 OrcasAvalonApplicationCanvas()
        {
            Width = DefaultWidth;
            Height = DefaultHeight;

            this.ClipToBounds = true;

            Colors.Blue.ToGradient(Colors.Black, DefaultHeight / 4).Select(
                (c, i) =>
                    new Rectangle
                    {
                        Fill = new SolidColorBrush(c),
                        Width = DefaultWidth,
                        Height = 5,
                    }.MoveTo(0, i * 4).AttachTo(this)
            ).ToArray();

            var mouse = new Image
            {
                Source = (KnownAssets.Path.Assets + "/mouse.png").ToSource(),
                Width = 32,
                Height = 32
            }.MoveTo(0, 0).AttachTo(this);

            var img = new Image
            {
                Source = (KnownAssets.Path.Assets + "/jsc.png").ToSource()
            }.MoveTo(DefaultWidth - 96, 0).AttachTo(this);

            var Content = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,

            }.AttachTo(this);

            var ContentY = new AnimatedDouble(0);

            ContentY.ValueChanged += y => Content.MoveTo(0, y);

            {
                var maze = new MazeGenerator(12, 8, null);
                var blocks = new BlockMaze(maze);
                var w = new BlockMaze(maze);
                Colors.Black.ToGradient(Colors.Yellow, 30).ForEach(
                    (c, i) =>
                        RenderMaze(60 + i * 0.1, w, new SolidColorBrush(c), Content)
                );
            }

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

            TouchOverlay.MouseEnter +=
                delegate
                {
                    mouse.Show();
                };
            TouchOverlay.MouseLeave +=
                delegate
                {
                    mouse.Hide();
                };
            TouchOverlay.Cursor = Cursors.None;
            TouchOverlay.MouseMove +=
                (s, args) =>
                {
                    var p = args.GetPosition(this);

                    mouse.MoveTo(p.X - 4, p.Y - 4);
                };

            TouchOverlay.MouseLeftButtonUp +=
                (s, args) =>
                {
                    var p = args.GetPosition(this);

                    ShowExplosion(Convert.ToInt32(p.X), Convert.ToInt32(p.Y), Content);
                    ("assets/AvalonMouseMaze/explosion.mp3").PlaySound();
                    150.AtDelay(
                        delegate
                        {
                            ShowExplosion(Convert.ToInt32(p.X + 6), Convert.ToInt32(p.Y - 6), Content);
                        }
                    );

                    300.AtDelay(
                        delegate
                        {
                            ShowExplosion(Convert.ToInt32(p.X + 4), Convert.ToInt32(p.Y + 6), Content);

                            ContentY.SetTarget(DefaultHeight);
                        }
                    );

                    1500.AtDelay(
                        delegate
                        {

                            ContentY.SetTarget(0);
                        }
                    );

                };

            new GameMenuWithGames(DefaultWidth, DefaultHeight, 32).AttachContainerTo(this).Hide();
        }
        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()
        {
            {
                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 void CreateWindow(Position WindowLocation, Action<WindowInfo> Yield = null)
        {
            var GlassArea = new Canvas();

            GlassArea.AttachTo(this);
            GlassArea.MoveTo(WindowLocation.Left, WindowLocation.Top);
            GlassArea.SizeTo(WindowLocation.Width, WindowLocation.Height);
            GlassArea.ClipToBounds = true;

            for (int i = 0; i < WindowLocation.Width; i += 456)
                for (int j = 0; j < WindowLocation.Height; j += 696)
                {
                    var i2 = new Avalon.Images.s_bg().SizeTo(456, 696);

                    i2.MoveTo(i, j);
                    i2.Opacity = 0.8;

                    i2.AttachTo(GlassArea);
                }

            var GlassOpacity = GlassArea.ToAnimatedOpacity();

            GlassOpacity.Opacity = 1;
            var w = new WindowInfo { GlassArea = GlassArea, GlassOpacity = GlassOpacity, WindowLocation = WindowLocation };


            var Left = new Avalon.Images.s_l
            {
                Stretch = Stretch.Fill
            }
            .AttachTo(this);


            var Top = new Avalon.Images.s_t
            {
                Stretch = Stretch.Fill
            }
            .AttachTo(this);




            var Right = new Avalon.Images.s_r
            {
                Stretch = Stretch.Fill
            }.AttachTo(this);




            var Bottom = new Avalon.Images.s_b
            {
                Stretch = Stretch.Fill
            }
             .AttachTo(this);


            var TopLeft = new Avalon.Images.s_tl
            {
                Stretch = Stretch.Fill
            }
           .AttachTo(this);


            var BottomRight = new Avalon.Images.s_br
            {
                Stretch = Stretch.Fill
            }
             .AttachTo(this);



            var TopRight = new Avalon.Images.s_tr
            {
                Stretch = Stretch.Fill
            }
           .AttachTo(this);


            var BottomLeft = new Avalon.Images.s_bl
            {
                Stretch = Stretch.Fill
            }
           .AttachTo(this);


            w.WindowLocationChanged +=
               delegate
               {
                   Left.MoveTo(WindowLocation.Left - 16, WindowLocation.Top + 6)
                   .SizeTo(16, WindowLocation.Height - 6 - 4);

                   Top.MoveTo(WindowLocation.Left + 6, WindowLocation.Top - 16)
          .SizeTo(WindowLocation.Width - 6 - 4, 22);

                   Right.MoveTo(WindowLocation.Left + WindowLocation.Width, WindowLocation.Top + 6)
               .SizeTo(20, WindowLocation.Height - 4 - 6);


                   Bottom.MoveTo(WindowLocation.Left + 5, WindowLocation.Top + WindowLocation.Height)
                    .SizeTo(WindowLocation.Width - 4 - 5, 20);


                   TopLeft.MoveTo(WindowLocation.Left - 22 + 6, WindowLocation.Top - 22 + 6)
                   .SizeTo(22, 22);
                   BottomRight.MoveTo(WindowLocation.Left + WindowLocation.Width - 4, WindowLocation.Top + WindowLocation.Height - 4)
                .SizeTo(22, 22);
                   TopRight.MoveTo(WindowLocation.Left + WindowLocation.Width - 4, WindowLocation.Top - 22 + 6)
                  .SizeTo(22, 22);
                   BottomLeft.MoveTo(WindowLocation.Left - 22 + 5, WindowLocation.Top + WindowLocation.Height - 4)
                  .SizeTo(22, 22);

               };

            w.WindowLocationChanged();

            w.Orphanize +=
                delegate
                {
                    new FrameworkElement[]
                    {
                        Left,
                        Top,
                        Right,
                        Bottom,
                        TopLeft,
                        BottomRight,
                        TopRight,
                        BottomLeft
                    }.WithEach(k => k.Orphanize());
                };

            w.Attach +=
               delegate
               {
                   new FrameworkElement[]
                    {
                        Left,
                        Top,
                        Right,
                        Bottom,
                        TopLeft,
                        BottomRight,
                        TopRight,
                        BottomLeft
                    }.WithEach(k => k.AttachTo(this));
               };

            if (Yield != null)
            {

                Yield(
                   w
                );
            }
        }
        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();
        }
Esempio n. 8
0
        public ExtendedField(int SizeX, int SizeY, int Width, int Height)
        {
            this.Container = new Canvas
            {
                Width = Width,
                Height = Height
            };

            this.Container.ClipTo(0, 0, Width, Height);

            this.Field = new Field(SizeX, SizeY);

            this.Field.Container.AttachTo(this.Container);

            #region interactive layers
            var CurrentTileCanvas = new Canvas
            {
                Width = this.Field.Tiles.Width,
                Height = this.Field.Tiles.Height
            }.AttachTo(this.Container);

            var ExplosionCanvas = new Canvas
            {
                Width = Width,
                Height = Height
            }.AttachTo(this.Container);

            var InfoOverlay = new Canvas
            {
                Width = Width,
                Height = Height
            }.AttachTo(this.Container);

            this.Field.InfoOverlay.AttachTo(InfoOverlay);

            double CurrentTileX = 0;
            double CurrentTileY = 0;

            var CurrentTile = default(SimplePipe);

            // SimplePipe.BuildablePipes.Random()();

            this.GetPipeToBeBuilt = () => CurrentTile;

            this.SetPipeToBeBuilt =
                value =>
                {
                    if (CurrentTile != null)
                    {
                        CurrentTile.OverlayBlackAnimationStop();
                        CurrentTile.Container.Orphanize();
                        CurrentTile.Container.Opacity = 1;
                    }

                    CurrentTile = value;

                    if (CurrentTile != null)
                    {
                        CurrentTile.Container.Opacity = 0.7;
                        CurrentTile.Container.MoveTo(CurrentTileX, CurrentTileY);
                        CurrentTile.Container.AttachTo(CurrentTileCanvas);
                        CurrentTile.OverlayBlackAnimationStart();
                    }
                };

            #endregion

            #region overlay
            this.Overlay = new Canvas
            {
                Width = Width,
                Height = Height
            };

            var OverlayRectangle = new Rectangle
            {
                Fill = Brushes.Black,
                Width = Width,
                Height = Height,
                Opacity = 0
            }.AttachTo(Overlay);

            this.Field.Tiles.Overlay.AttachTo(Overlay);
            #endregion

            #region move the map with the mouse yet not too often anf smooth enough

            #region MoveTo
            Action<int, int> MoveTo = NumericEmitter.Of(
                (x, y) =>
                {
                    this.Field.Tiles.Overlay.MoveTo(x, y);
                    this.Field.Container.MoveTo(x, y);

                    CurrentTileCanvas.MoveTo(x, y);
                    ExplosionCanvas.MoveTo(x, y);
                    InfoOverlay.MoveTo(x, y);
                }
            );
            #endregion

            #region CalculateMoveTo
            Action<int, int> CalculateMoveTo =
                (int_x, int_y) =>
                {
                    double x = int_x;
                    double y = int_y;

                    //Console.WriteLine(new { x, y }.ToString());

                    const int PaddingX = Tile.Size / 2;
                    const int MarginLeft = Tile.ShadowBorder + Tile.Size + PaddingX;
                    const int MarginWidth = MarginLeft + Tile.ShadowBorder + Tile.Size + PaddingX;
                    var _x = PaddingX + (Width - (this.Field.Tiles.Width + PaddingX * 2)) * ((x - MarginLeft).Max(0) / (Width - MarginWidth)).Min(1);

                    const int PaddingY = Tile.SurfaceHeight;
                    const int MarginTop = Tile.ShadowBorder + Tile.SurfaceHeight + PaddingY;
                    const int MarginHeight = MarginTop + Tile.ShadowBorder + Tile.Size + PaddingY;
                    var _y = PaddingY + (Height - (this.Field.Tiles.Height + PaddingY * 2)) * ((y - MarginTop).Max(0) / (Height - MarginHeight)).Min(1);

                    if (this.Field.Tiles.Width < Width)
                        _x = (Width - this.Field.Tiles.Width) / 2;

                    if (this.Field.Tiles.Height < Height)
                        _y = (Height - this.Field.Tiles.Height) / 2;

                    MoveTo(Convert.ToInt32(_x), Convert.ToInt32(_y));
                }
            ;
            #endregion

            CalculateMoveTo(0, 0);

            Overlay.MouseMove +=
                (Sender, Arguments) =>
                {
                    var p = Arguments.GetPosition(Overlay);

                    CalculateMoveTo(Convert.ToInt32(p.X), Convert.ToInt32(p.Y));
                };
            #endregion

            #region IsBlockingPipe
            Func<bool> IsBlockingPipe =
                delegate
                {
                    var u = this.Field.Tiles.FocusTile;

                    if (u != null)
                    {
                        var q = this.Field[u];
                        if (q != null)
                        {
                            var qt = q.GetType();
                            if (!SimplePipe.BuildablePipeTypes.Any(t => t.Equals(qt)))
                            {
                                // we got a pipe on which we should not build upon
                                return true;
                            }

                        }

                        if (this.Field.ByIndex(u.IndexX, u.IndexY).Any(k => k.Value.HasWater))
                            return true;
                    }

                    return false;
                };
            #endregion

            #region MouseMove
            Overlay.MouseMove +=
                (Sender, Arguments) =>
                {
                    if (CurrentTile == null)
                        return;

                    var p = Arguments.GetPosition(this.Field.Tiles.Overlay);

                    var u = this.Field.Tiles.FocusTile;

                    if (IsBlockingPipe())
                    {
                        // we got a pipe on which we should not build upon
                        u = null;
                    }

                    if (u != null)
                    {
                        p.X = 0 + u.IndexX * Tile.Size + Tile.ShadowBorder;
                        p.Y = 0 + (u.IndexY + 1) * Tile.SurfaceHeight + Tile.ShadowBorder - Tile.Size;

                        CurrentTile.Container.Opacity = 1;
                    }
                    else
                    {
                        p.X -= Tile.Size / 2;
                        p.Y -= Tile.SurfaceHeight / 2;

                        CurrentTile.Container.Opacity = 0.7;
                    }

                    CurrentTileX = p.X;
                    CurrentTileY = p.Y;
                    CurrentTile.Container.MoveTo(p.X, p.Y);
                };
            #endregion

            this.Field.Tiles.Click +=
                Target =>
                {
                    if (CurrentTile == null)
                        return;

                    var u = this.Field.Tiles.FocusTile;

                    if (u != null)
                    {
                        var q = this.Field[u];

                        if (q != null)
                            if (q.Input.Pump != null)
                            {
                                // we are clicking on a pump
                                if (q.AddTimerAbort != null)
                                    q.AddTimerAbort();

                                q.Input.Pump();
                                return;

                            }
                    }

                    if (IsBlockingPipe())
                    {
                        // we got a pipe on which we should not build upon
                        u = null;
                    }

                    if (u != null)
                    {
                        var PipeToBeBuilt = this.PipeToBeBuilt;
                        this.PipeToBeBuilt = null;

                        if (this.Field.ByIndex(Target.IndexX, Target.IndexY).Any())
                        {

                            var px = u.IndexX * Tile.Size + Tile.ShadowBorder;
                            var py = (u.IndexY + 1) * Tile.SurfaceHeight + Tile.ShadowBorder - Tile.Size;

                            new Explosion().PlayAndOrphanize().Container.MoveTo(px, py).AttachTo(ExplosionCanvas);
                        }

                        this.Field[u] = PipeToBeBuilt;
                        this.Field.RefreshPipes();

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

                };
        }
        public AvalonMinesweeperCanvas()
        {
            this.Width = DefaultWidth;
            this.Height = DefaultHeight;

            InitializeBackground();

            #region layers

            this.Content = new Canvas
            {
                Width = ContentWidth,
                Height = ContentHeight,
            }.AttachTo(this);

            var Shadows = new Canvas
            {
                Width = ContentWidth,
                Height = ContentHeight,
            }.AttachTo(this.Content);

            var Visuals = new Canvas
            {
                Width = ContentWidth,
                Height = ContentHeight,
            }.AttachTo(this.Content);

            var InfoOverlay = new Canvas
            {
                Width = ContentWidth,
                Height = ContentHeight,
            }.AttachTo(this.Content);

            #region lets insert a console
            var GameConsole = new GameConsole
            {

            };

            GameConsole.SizeTo(DefaultWidth, DefaultHeight);
            GameConsole.AttachContainerTo(this);
            var GameConsoleOpacity = GameConsole.Shade.ToAnimatedOpacity();

            Action GameConsoleShow = delegate
            {
                GameConsole.WriteLine(Info.Title + " console");

                GameConsole.Show();
                GameConsoleOpacity.Opacity = 0.4;
                GameConsole.TextBox.Show();
            };

            Action GameConsoleHide = delegate
            {
                GameConsole.TextBox.Hide();
                GameConsoleOpacity.SetOpacity(0, GameConsole.Hide);
            };
            GameConsoleHide();

            Action GameConsoleToggle = GameConsoleShow.WhereCounter(k => k % 2 == 0);

            GameConsoleToggle += GameConsoleHide.WhereCounter(k => k % 2 == 1);

            this.KeyUp +=
                (sender, args) =>
                {
                    if (args.Key == Key.Oem7)
                    {
                        GameConsoleToggle();
                    }
                };

            // we are going for the keyboard input
            // we want to enable the tilde console feature
            this.FocusVisualStyle = null;
            this.Focusable = true;
            this.Focus();

            this.MouseLeftButtonDown +=
                (sender, key_args) =>
                {
                    this.Focus();
                };

            #endregion

            var TouchOverlay = new Canvas
            {
                Width = ContentWidth,
                Height = ContentHeight,
            }.AttachTo(this);

            var TouchOverlayTrap = new Rectangle
            {
                Fill = Brushes.Red,
                Width = ContentWidth,
                Height = ContentHeight,
                Opacity = 0
            }.AttachTo(TouchOverlay);
            #endregion

            var clone = new NamedArrowCursorControl
            {
                Text = "player revived!"
            }.AttachContainerTo(InfoOverlay);

            clone.Hide();

            var shadowoffset = 4;

            this.Content.MoveTo(-DefaultWidth / 2, -DefaultHeight / 2);
            TouchOverlay.MoveTo(-DefaultWidth / 2, -DefaultHeight / 2);

            // the button shall be atteched to the playfield with the following rules
            var MineFieldButtons = new BindingList<MineFieldButton>().WithEvents(
                k =>
                {
                    var x = (ContentWidth / 2) + (k.IndexX * MineFieldButton.Size);
                    var y = (ContentHeight / 2) + (k.IndexY * MineFieldButton.Size);

                    k.Shadow.MoveTo(x + shadowoffset, y + shadowoffset).AttachTo(Shadows);
                    k.MoveContainerTo(x, y).AttachContainerTo(Visuals);
                    k.TouchRectangle.MoveTo(x, y).AttachTo(TouchOverlay);

                    return delegate
                    {
                        k.Shadow.Orphanize();
                        k.OrphanizeContainer();
                        k.TouchRectangle.Orphanize();
                    };
                }
            );

            Action HideCursor =
                delegate
                {
                    TouchOverlay.Cursor = Cursors.None;
                    MineFieldButtons.Source.ForEach(k => k.TouchRectangle.Cursor = Cursors.None);
                };

            Action ShowCursor =
                delegate
                {
                    TouchOverlay.Cursor = Cursors.Arrow;
                    MineFieldButtons.Source.ForEach(k => k.TouchRectangle.Cursor = Cursors.Hand);
                };

            var SoundFlag = ("assets/AvalonMinesweeper.Core/flag.mp3").ToSound();
            var SoundReveal = ("assets/AvalonMinesweeper.Core/reveal.mp3").ToSound();
            var SoundClick = ("assets/AvalonMinesweeper.Core/click.mp3").ToSound();
            var SoundTick = ("assets/AvalonMinesweeper.Core/tick.mp3").ToSound();
            var SoundBuzzer = ("assets/AvalonMinesweeper.Core/buzzer.mp3").ToSound();

            #region the tule
            MineFieldButtons.Added +=
                (k, i) =>
                {
                    k.Click +=
                        (sender, args) =>
                        {
                            if (Keyboard.Modifiers != ModifierKeys.None)
                            {
                                SoundFlag.Start();
                                if (k.Flag.Visibility == Visibility.Visible)
                                {
                                    k.Flag.Hide();
                                    k.Question.Show();
                                }
                                else if (k.Question.Visibility == Visibility.Visible)
                                    k.Question.Hide();
                                else
                                    k.Flag.Show();

                                return;
                            }

                            if (k.Flag.Visibility == Visibility.Visible)
                            {
                                SoundBuzzer.Start();
                                return;
                            }

                            if (k.Question.Visibility == Visibility.Visible)
                            {
                                SoundBuzzer.Start();
                                return;
                            }

                            if (!k.IsMined)
                            {
                                var Region = k.Region.ToArray();

                                if (Region.Length == 1)
                                    SoundClick.Start();
                                else
                                    SoundReveal.Start();

                                foreach (var r in Region)
                                {

                                    r.VisibleNumber.Value = r.Neighbours.Where(kk => kk.IsMined).Count();
                                    r.IsPressed.Value = true;
                                    r.IsEnabled = false;
                                    r.BackgroundColor = 0xFFc0c0c0.ToColor();
                                }

                                return;
                            }

                            k.Flag.Hide();
                            k.Mine.Show();
                            k.IsPressed.Value = true;
                            k.Background.Fill = Brushes.Red;

                            var p = args.GetPosition(TouchOverlay);

                            Explosion.Create(Convert.ToInt32(p.X), Convert.ToInt32(p.Y), InfoOverlay);

                            HideCursor();

                            //150.AtDelay(
                            //    delegate
                            //    {
                            //        Explosion.Create(Convert.ToInt32(p.X + 6), Convert.ToInt32(p.Y - 12), InfoOverlay, 48);
                            //    }
                            //);

                            //300.AtDelay(
                            //    delegate
                            //    {
                            //        Explosion.Create(Convert.ToInt32(p.X + 16), Convert.ToInt32(p.Y + 6), InfoOverlay);
                            //    }
                            //);

                            #region revive
                            2000.AtDelay(
                                delegate
                                {
                                    ("assets/AvalonMinesweeper.Core/reveal.mp3").PlaySound();
                                    clone.Color = Colors.White;
                                    clone.Cursor.Show();
                                    clone.Show();

                                    // could we rewrite this as LINQ ?

                                    Action FlashRed = () => clone.Color = Colors.Red;
                                    Action FlashWhite = () => clone.Color = Colors.White;
                                    Action FlashHold = () => { clone.Cursor.Hide(); ShowCursor(); };
                                    Action<DispatcherTimer> FlashClose = t => { clone.Hide(); t.Stop(); };

                                    Func<int, bool> FlashRedFilter = c => { if (c > 8) return false; return c % 2 == 1; };
                                    Func<int, bool> FlashWhiteFilter = c => { if (c > 8) return false; return c % 2 == 0; };

                                    120.ToTimerBuilder()
                                        .With(FlashRed.WhereCounter(FlashRedFilter))
                                        .With(FlashWhite.WhereCounter(FlashWhiteFilter))
                                        .With(FlashHold.WhereCounter(8))
                                        .With(FlashClose.WhereCounter(8 + 10))
                                        .Start();

                                }
                            );
                            #endregion

                        };
                };
            #endregion

            var MapCollection = default(ZIPFile);
            var MapLoader = new MineMapLoader(MineFieldButtons.Source);

            2000.AtDelay(
                delegate
                {
                    "assets/AvalonMinesweeper.Core/Levels.zip".ToMemoryStreamAsset(
                        levels =>
                        {
                            MapCollection = levels;

                            foreach (var k in MapCollection.Entries)
                            {
                                GameConsole.WriteLine("level: " + k.FileName);
                            }
                        }
                    );
                }
            );

            3000.AtIntervalWithTimer(
                t =>
                {
                    if (MapCollection == null)
                        return;

                    t.Stop();

                    var map = new ASCIIImage(
                        new ASCIIImage.ConstructorArguments
                        {
                            // this map will be built in 2 seconds
                            value = MapCollection.Entries.Random().Text
                        }
                    );

                    GameConsole.WriteLine("ready to load map");

                    //var Gradient = Colors.LightGreen.ToGradient(Colors.Yellow, map.Height).ToArray();
                    var Gradient = 0xFFd0d0d0.ToColor().ToGradient(0xff404040.ToColor(), map.Height).ToArray();

                    var Entries = Enumerable.ToArray(
                        from k in map
                        where k.Value != "M"
                        select new MineMapLoader.Entry
                        {
                            IndexX = k.X - map.Width / 2,
                            IndexY = k.Y - map.Height / 2,
                            BackgroundColor = Gradient[k.Y],
                        }
                    );

                    Entries.Randomize().Take(Convert.ToInt32(Entries.Length * 0.2)).ForEach(k => k.IsMined = true);

                    MapLoader.Prepare(Entries,
                        delegate
                        {
                            GameConsole.WriteLine("map loaded!");
                        }
                    );
                }
            );

            var yellow = new NamedArrowCursorControl
            {
                Text = "Minesweeper 1",
                Color = Colors.Yellow
            }.AttachContainerTo(InfoOverlay).MoveContainerTo(64 + DefaultWidth, 64 + DefaultHeight);

            var white = new NamedArrowCursorControl
            {
                Text = "More is Less, a really long name in here to test automation"
            }.AttachContainerTo(InfoOverlay).MoveContainerTo(64 + 100 + DefaultWidth, 64 + 16 + DefaultHeight);

            var red = new NamedArrowCursorControl
            {
                Color = Colors.Red,
                Text = "John Doe"
            }.AttachContainerTo(InfoOverlay).MoveContainerTo(64 + 16 + DefaultWidth, 64 + 100 + DefaultHeight);

            (1000 / 24).AtIntervalWithCounter(
                c =>
                {
                    yellow.MoveContainerTo(64 + 100 + Math.Cos(c * 0.1) * 12 + DefaultWidth, DefaultHeight + 64 + 64 + Math.Sin(c * 0.1) * 12);
                }
            );

            TouchOverlay.MouseMove +=
                (sender, args) =>
                {
                    var p = args.GetPosition(TouchOverlay);
                    //var r = args.GetPosition(ScrollTrap);

                    //var rx = r.X.Min(DefaultWidth - ScrollMargin * 2).Max(0);
                    //var ry = r.Y.Min(DefaultHeight - ScrollMargin * 2).Max(0);

                    //this.Status.Value = "x: " + rx + ", y: " + ry;

                    //this.Parallax1.MoveTo(
                    //    (DefaultWidth - Parallax1Width) * rx / (DefaultWidth - ScrollMargin * 2),
                    //    (DefaultHeight - Parallax1Height) * ry / (DefaultHeight - ScrollMargin * 2)
                    //);

                    clone.MoveContainerTo(p.X, p.Y);
                };

            //("assets/AvalonMinesweeper.Core/applause.mp3").PlaySound();

            #region SocialLinks
            var SocialLinks = new GameSocialLinks(this)
            {
                new GameSocialLinks.AddToGoogleButton {
                    Hyperlink = new Uri(Info.AddLink)
                },
                new GameSocialLinks.TikiButton {
                    Hyperlink = new Uri(Info.Web)
                },
                new GameSocialLinks.StumbleUponButton {
                    Hyperlink = new Uri( "http://www.stumbleupon.com/submit?url=" + Info.Web)
                },
                new GameSocialLinks.BlogFeedButton {

                    Hyperlink = new Uri( "http://zproxy.wordpress.com")
                }
            };
            #endregion

            #region GameMenuWithGames
            var ShadowSize = 24;
            var SocialLinksMenu = new GameMenuWithGames(DefaultWidth, DefaultHeight, ShadowSize);

            SocialLinksMenu.Carousel.Caption.FontFamily = new FontFamily("Verdana");
            SocialLinksMenu.IdleText = "-= zproxy games =-";

            SocialLinksMenu.DownloadStarting +=
                delegate
                {
                    GameConsole.WriteLine("downloading game information...");
                };
            SocialLinksMenu.DownloadComplete +=
                delegate
                {
                    GameConsole.WriteLine("downloading game information done...");
                };

            1000.AtDelay(SocialLinksMenu.Download);

            SocialLinksMenu.AttachContainerTo(this);

            SocialLinksMenu.AfterMove +=
                (x, y) =>
                {
                    SocialLinks.OffsetY = y + SocialLinksMenu.ContentHeight + ShadowSize;
                };

            SocialLinksMenu.Hide();
            #endregion
        }
        public SimpleCarouselControl(int DefaultWidth, int DefaultHeight)
        {
            this.Container = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight
            };

            var ContentContainer = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight
            }.AttachTo(this.Container);

            this.Caption = new TextBox
            {
                Width = DefaultWidth,
                Height = 32,
                TextAlignment = TextAlignment.Center,
                Foreground = Brushes.White,
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                IsReadOnly = true
            }.MoveTo(0, (DefaultHeight - 32) / 2).AttachTo(this.Container);

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

            var OverlayFill = new Rectangle
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
                Fill = Brushes.Red,
                Opacity = 0
            }.AttachTo(this.Overlay);

            var a = new List<Entry>();

            var OverlayReorderingEnabled = true;

            #region Timer
            this.Timer = (1000 / 30).AtInterval(
                delegate
                {
                    a.ForEach(k => k.Tick());

                    a.OrderBy(k => k.cy).ForEach(
                        k =>
                        {

                            if (OverlayReorderingEnabled)
                            {
                                // in javascript reordering an element under mouse
                                // will leave you without the leave event
                                // we could sort other nodes around it tho

                                k.Overlay.Orphanize();
                                k.Overlay.AttachTo(this.Overlay);
                            }

                            k.pc.Orphanize();
                            k.pc.AttachTo(this.Container);

                        }
                    );
                }
            );
            #endregion


            var s = 0.01;

            this.AddEntry =
                e =>
                {
                    var pc_Width = 166 + 9;
                    var pc_Height = 90 + 9 * 2;

                    var pc = new Canvas
                    {
                        //Background = Brushes.Green,
                        Width = pc_Width,
                        Height = pc_Height
                    }.AttachTo(ContentContainer);

                    const string Assets = "assets/ScriptCoreLib.Avalon.Carousel";

                    var p = new Image
                    {
                        Width = 166,
                        Height = 90,
                        Stretch = Stretch.Fill,
                        Source = (Assets + "/PreviewShadow.png").ToSource()
                    }.AttachTo(pc);


                    var ps = new Image
                    {
                        Width = 138,
                        Height = 108,
                        Stretch = Stretch.Fill,
                        Source = (Assets + "/PreviewSelection.png").ToSource()
                    }.AttachTo(pc);

                    var pi = new Image
                    {
                        Width = 120,
                        Height = 90,
                        Stretch = Stretch.Fill,
                        Source = e.Source,
                        Cursor = Cursors.Hand
                    }.AttachTo(pc);

                    var Overlay_Width = 120;
                    var Overlay_Height = 90;

                    var Overlay = new Rectangle
                    {
                        Fill = Brushes.Black,
                        Width = Overlay_Width,
                        Height = Overlay_Height,
                        Cursor = Cursors.Hand,
                        Opacity = 0
                    }.AttachTo(this.Overlay);

                    ps.Hide();

                    Overlay.TouchDown +=
                        (sender, ee) =>
                        {
                            ee.Handled = true;

                            if (e.Click != null)
                                e.Click();
                        };

                    Overlay.TouchMove +=
                      (sender, ee) =>
                      {
                          ee.Handled = true;
                      };

                    Overlay.TouchUp +=
                       (sender, ee) =>
                       {
                           ee.Handled = true;
                       };


                    Overlay.MouseLeftButtonUp +=
                         (sender, ee) =>
                         {
                             ee.Handled = true;


                             if (e.Click != null)
                                 e.Click();

                         };

                    var IsHot = false;

                    Overlay.MouseLeave +=
                        delegate
                        {
                            OverlayReorderingEnabled = true;

                            if (e.Text != null)
                                this.Caption.Text = "";

                            if (Idle != null)
                                Idle();

                        };

                    Overlay.MouseEnter +=
                        delegate
                        {
                            OverlayReorderingEnabled = false;

                            if (e.MouseEnter != null)
                                e.MouseEnter();

                            if (Hover != null)
                                Hover(e);

                            if (e.Text != null)
                                this.Caption.Text = e.Text;

                            IsHot = true;
                            s = 0.004;

                            p.Opacity = 1;
                            pi.Opacity = 1;

                            a.ForEach(
                                k =>
                                {
                                    if (k.ps == ps)
                                    {
                                        ps.Show();
                                    }
                                    else
                                    {
                                        k.ps.Hide();
                                    }



                                }
                            );

                        };

                    var MyEntry = new Entry
                        {
                            o = 1,
                            cy = 0,
                            pc = (Canvas)pc,
                            ps = (Image)ps,
                            Overlay = (Rectangle)Overlay,
                            Info = e
                        };

                    //var o = new Boxed<double>();
                    //var _cy = new Boxed<double>();

                    Overlay.MouseLeave +=
                        delegate
                        {
                            if (e.MouseLeave != null)
                                e.MouseLeave();

                            IsHot = false;
                            p.Opacity = MyEntry.o;
                            pi.Opacity = MyEntry.o;


                            s = 0.01;

                            ps.Hide();
                        };





                    #region Tick
                    MyEntry.Tick =
                        delegate
                        {

                            var x = Math.Cos(e.Position);
                            var y = Math.Sin(e.Position);

                            var z = (y + 3) / 4;

                            MyEntry.o = z;

                            if (!IsHot)
                            {
                                pi.Opacity = MyEntry.o;
                                //p.Opacity = MyEntry.o;
                            }

                            e.Position += s * z;

                            pc.Width = pc_Width * z;
                            pc.Height = pc_Height * z;

                            Overlay.Width = Overlay_Width * z;
                            Overlay.Height = Overlay_Height * z;

                            p.Width = 166 * z;
                            p.Height = 90 * z;


                            pi.Width = 120 * z;
                            pi.Height = 90 * z;

                            ps.Width = 138 * z;
                            ps.Height = 108 * z;

                            var cx = x * (DefaultWidth - 160) / 2 + DefaultWidth / 2;
                            var cy = y * (DefaultHeight / 2) / 2 + DefaultHeight / 2;

                            MyEntry.cy = cy;

                            pc.MoveTo(
                                cx - pc.Width / 2,
                                cy - pc.Height / 2
                            );

                            p.MoveTo(
                                z * 9,
                                z * 9
                            );

                            pi.MoveTo(
                                z * 9,
                                z * 9
                            );


                            ps.MoveTo(
                                0,
                                0
                            );

                            Overlay.MoveTo(
                                cx - pc.Width / 2 + 9 * z,
                                cy - pc.Height / 2 + 9 * z
                            );
                        };
                    #endregion


                    a.Add(
                        MyEntry
                    );

                    MyEntry.Tick();
                };

        }
        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 BrowserAvalonExampleCanvas()
		{
			// jsc:javascript does not work well with structs
			this.Cursor = Cursors.None;

			this.Width = DefaultWidth;
			this.Height = DefaultHeight;

			new Rectangle
			{
				Fill = 0xff3D87FF.ToSolidColorBrush(),
				Width = DefaultWidth,
				Height = DefaultHeight / 2
			}.AttachTo(this).MoveTo(0, 0);

			new Rectangle
			{
				Fill = 0xff72BC3E.ToSolidColorBrush(),
				Width = DefaultWidth,
				Height = DefaultHeight / 2
			}.AttachTo(this).MoveTo(0, DefaultHeight / 2);


			new Rectangle
			{
				Fill = Brushes.GreenYellow,
				Width = 62,
				Height = 62
			}.AttachTo(this).MoveTo(32, 8);

			new Image
			{
				Source = "assets/BrowserAvalonExample.Assets/ground.png".ToSource()
			}.AttachTo(this).MoveTo(0, 160);

			new Image
			{
				Source = "assets/BrowserAvalonExample.Assets/shadowtop.png".ToSource(),
				Stretch = Stretch.Fill,
				Width = DefaultWidth,
				Height = 64
			}.AttachTo(this).MoveTo(0, 160);

			new Image
			{
				Source = "assets/BrowserAvalonExample.Assets/gradientwhite.png".ToSource(),
				Stretch = Stretch.Fill,
				Width = DefaultWidth,
				Height = 64
			}.AttachTo(this).MoveTo(0, 160 - 64);


			var info = new TextBox
			{
				Text = "hello world",
				Background = Brushes.Transparent,
				BorderThickness = new Thickness(0),
				IsReadOnly = true
			}.AttachTo(this).MoveTo(32, 32);

			var e = new Image 
			{
				Source = "assets/BrowserAvalonExample.Assets/tipsi2.png".ToSource()
			}.AttachTo(this).MoveTo(4, 5);

			var underlay = new Canvas
			{
				Width = DefaultWidth,
				Height = DefaultHeight,

			}.AttachTo(this);

			 new Image
			{
				Source = "assets/BrowserAvalonExample.Assets/shadowtop.png".ToSource(),
				Stretch = Stretch.Fill,
				Width = DefaultWidth,
				Height = 64
			}.AttachTo(this).MoveTo(0, 0);

			 var cursor = new Canvas
			 {
			 }.AttachTo(this).MoveTo(4, 5);

			var arrow = new Image
			{
				Source = "assets/BrowserAvalonExample.Assets/arrow.png".ToSource()
			}.AttachTo(cursor).MoveTo(-16, -16);

			var bluearrow = new Image
			{
				Source = "assets/BrowserAvalonExample.Assets/bluearrow.png".ToSource()
			}.AttachTo(cursor).MoveTo(-16, -16);


			bluearrow.Visibility = Visibility.Hidden;



			Action<double, double> DrawBrush =
				(x, y) =>
				{
					var img = new Image
					{
						Source = "assets/BrowserAvalonExample.Assets/bluebrush.png".ToSource()
					}.AttachTo(underlay).MoveTo(x, y);


					2000.AtDelay(
						delegate
						{
							img.FadeOut();
						}
					);

				};

			var overlay = new Rectangle
			{
				Fill = Brushes.Red,
				Width = DefaultWidth,
				Height = DefaultHeight,
				Opacity = 0
			}.AttachTo(this).MoveTo(0, 0);

			var Cursor = new Point();
			var Counter = 0;
			var MyBrushPainter = 50.AtInterval(
				delegate
				{
					Counter++;

					info.Text = "counter: " + Counter;


					DrawBrush(Cursor.X - 32, Cursor.Y -32);

				}
			);

			MyBrushPainter.Stop();

			overlay.MouseLeftButtonDown +=
				delegate
				{
					bluearrow.Visibility = Visibility.Visible;
					MyBrushPainter.Start();
				};

			overlay.MouseLeftButtonUp +=
				delegate
				{
					bluearrow.Visibility = Visibility.Hidden;
					MyBrushPainter.Stop();
				};


			overlay.MouseMove +=
				(s, a)=>
				{
					Cursor = a.GetPosition(this);

					cursor.MoveTo(Cursor.X, Cursor.Y);
				};

			new TextBox
			{
				Text = "enter text here",
				Background = Brushes.GreenYellow,
				Foreground = Brushes.Red,
				BorderThickness = new Thickness(0),
				IsReadOnly = false
			}.AttachTo(this).MoveTo(32, 64);

			
		}
        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();
                };
        }