Exemplo n.º 1
0
        public Program(IHTMLElement placeholder)
        {
            // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
            // http://www.quirksmode.org/js/doctypes.html
            // http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/index.html

            // we do not want to see those scrollbars
            Native.Document.body.style.overflow = IStyle.OverflowEnum.hidden;

            // Preload Images
            foreach (var v in Frames.Harvester1)
            {
                var dummy = v.Image;
            }

            //Console.EnableActiveXConsole();

            var MyDudeHotRange = 24;
            var MyDudes = new List<Dude2>();
            var MySelectedDudes = new Dude2[] { };

            Session = new MySession();

            this.Session.IServer_EnterLobby(
                delegate(string e)
                {
                    var TheWorld = new MyGameWorld();

                    #region supporting user chat

                    var ChatBox = new LayeredTextBox();

                    Native.Document.onkeypress +=
                        delegate(IEvent ev)
                        {
                            if (ev.KeyCode == 'q') Console.WriteLine("Q");
                            if (ev.KeyCode == 'w') Console.WriteLine("W");
                            if (ev.KeyCode == 'e') Console.WriteLine("E");
                            if (ev.KeyCode == 'r')
                            {
                                var random_spawn_position = new __Type1
                                                            {
                                                                x = new System.Random().NextDouble() * 600,
                                                                y = new System.Random().NextDouble() * 400,
                                                            };

                                Console.WriteLine("random_spawn_position: " + random_spawn_position);

                                // Lets Spawn Something into the world
                            }

                            //Console.Log("onkeypress: " + new { KeyCode = ev.KeyCode });

                            if (!ChatBox.IsVisible)
                            {
                                if (ev.KeyCode == 't')
                                {
                                    ev.PreventDefault();

                                    Timer.DoAsync(
                                        delegate
                                        {
                                            ChatBox.ShowAndFocus();
                                        }
                                    );
                                }
                            }
                        };

                    #endregion

                    this.Session.ClientName = e;


                    var a = new ArenaControl();
                    var m = new ArenaMinimapControl();

                    a.Control.AttachToDocument();

                    a.Layers.Canvas.style.backgroundColor = Color.FromRGB(0, 0x80, 0);

                    // set the map to be somewhere left
                    a.SetLocation(Rectangle.Of(32, 32, 640, 480));


                    // set tha map canvas size to be something big
                    a.SetCanvasSize(new Point(8000, 8000));

                    #region DrawTextWithTimeout
                    System.Action<string, Color> DrawTextWithTimeout =
                                   delegate(string text, Color color)
                                   {
                                       var z = new IHTMLDiv(new ITextNode(text));

                                       z.style.color = color;
                                       z.style.backgroundColor = Color.Black;

                                       a.Layers.Info.appendChild(z);

                                       this.SessionTimer[TimerEvent.DelayOnce(9000)] =
                                           delegate
                                           {
                                               z.Dispose();
                                           };
                                   };
                    #endregion

                    this.SessionTimer[TimerEvent.DelayOnce(1000)] =
                       delegate
                       {
                           a.DrawTextToInfo("just some data", new Point(46, 246), Color.Black);
                           a.DrawTextToInfo("just some data", new Point(45, 245), Color.Yellow);

                           DrawTextWithTimeout("hello world", Color.Red);
                       };



                    var data = new List<Pair<Rectangle, Color>>();

                    #region minimap


                    m.Zoom.Validate += delegate
                    {
                        if (a.CurrentCanvasSize.X > a.CurrentCanvasSize.Y)
                        {
                            var w = m.CurrentLocation.Width / a.CurrentCanvasSize.X;

                            if (m.Zoom.Value < w)
                                m.Zoom.Value = w;
                        }
                        else
                        {
                            var h = m.CurrentLocation.Height / a.CurrentCanvasSize.Y;

                            if (m.Zoom.Value < h)
                                m.Zoom.Value = h;
                        }
                    };


                    m.Zoom.Changed += delegate
                    {
                        m.Layers.Canvas.removeChildren();

                        m.SetCanvasSize(a.CurrentCanvasSize * m.Zoom.Value);
                        m.SetSelectionLocation(a.CanvasView * m.Zoom.Value);
                        m.MakeSelectionVisible();


                        var data_array = data.ToArray();

                        foreach (var v in data_array)
                        {
                            var r = v.A;
                            var c = v.B;

                            m.DrawRectangleToCanvas(r * m.Zoom.Value, c);
                        }
                    };

                    m.Control.AttachToDocument();

                    m.SetLocation(Rectangle.Of(690, 50, 200, 200));


                    #endregion

                    #region DrawRectangle
                    Action<Rectangle, Color> DrawRectangleLocal =
                        delegate(Rectangle r, Color c)
                        {
                            var p = new Pair<Rectangle, Color>(r, c);

                            data.Add(p);

                            a.DrawRectangleToCanvas(r, c);
                            m.DrawRectangleToCanvas(r * m.Zoom.Value, c);
                        };



                    this.Session.OnIClient_DrawRectangle += delegate(Message._IClient_DrawRectangle p)
                    {
                        var r = new Rectangle
                                {
                                    Left = p.rect.Left,
                                    Top = p.rect.Top,
                                    Width = p.rect.Width,
                                    Height = p.rect.Height,
                                };

                        DrawRectangleLocal(r, p.color);
                    };


                    Action<Rectangle, Color> DrawRectangle =
                        delegate(Rectangle r, Color c)
                        {
                            DrawRectangleLocal(r, c);

                            this.Session.IServer_DrawRectangle(r, c);
                        };

                    #endregion

                    #region SpawnHarvester
                    Action<Point, double> SpawnHarvester_Local =
                        delegate(Point Location, double Direction)
                        {
                            // add new dude
                            var dude = CreateHarvester();

                            dude.Direction = Direction;
                            dude.Zoom.StaticZoom = 1;
                            dude.TeleportTo(Location.X, Location.Y);

                            dude.Control.AttachTo(a.Layers.Canvas);

                            MyDudes.Add(dude);
                        };

                    this.Session.OnIClient_SpawnHarvester += 
                        ev => SpawnHarvester_Local(ev.Location, (ev.Direction / 32d));

                    Action<Point, double> SpawnHarvester =
                        (Location, Direction) =>
                        {
                            SpawnHarvester_Local(Location, Direction);
                            this.Session.IServer_SpawnHarvester(Location, (Direction * 32).ToInt32());
                        };

                    //(Location, Direction) =>
                    //{
                    //    SpawnHarvester_Local(Location, Direction);
                    //};
                    #endregion

                    a.SelectionClick += delegate(Point p, IEvent ev)
                    {
                        Console.WriteLine("SelectionClick_1");

                        if (ev.ctrlKey)
                        {
                            SpawnHarvester(p, (Math.PI * 2).Random());

                            // DrawRectangle(p.WithMargin(a.SelectionMinimumSize * 2), Color.Green);
                        }
                        else
                        {
                            var Selection = MyDudes.Where(v => v.CurrentLocation.GetRange(p) < MyDudeHotRange).ToArray();

                            if (Selection.Length == 0)
                            {
                                MySelectedDudes.ForEach(v => v.WalkTo(p));
                            }
                            else
                            {
                                MySelectedDudes.ForEach(v => v.IsSelected = false);
                                MySelectedDudes = Selection;
                                MySelectedDudes.ForEach(v => v.IsSelected = true);
                            }

                        }
                    };

                    a.MouseMove +=
                        point =>
                        {
                            foreach (var v in MyDudes)
                            {
                                v.IsHot = v.CurrentLocation.GetRange(point) < MyDudeHotRange;
                            }
                        };

                    a.ApplySelection += delegate(Rectangle r, IEvent ev)
                    {
                        if (ev.ctrlKey)
                        {
                            DrawRectangle(r, RandomColor);
                        }
                        else
                        {
                            MySelectedDudes.ForEach(v => v.IsSelected = false);
                            MySelectedDudes = MyDudes.Where(v => r.Contains(v.CurrentLocation)).ToArray();
                            MySelectedDudes.ForEach(v => v.IsSelected = true);
                        }
                    };

                    a.CanvasViewChanged += delegate(Rectangle p)
                    {
                        m.SetSelectionLocation(p * m.Zoom.Value);
                        m.MakeSelectionVisible();
                    };

                    a.SetCanvasPosition(Point.Zero);

                    m.SelectionCenterChanged += delegate(Point p)
                    {
                        a.SetCanvasViewCenter(p / m.Zoom.Value);
                    };

                    m.Zoom.SetValue(0);

                    //chatbox.style.SetLocation(32, 480 - 32, 650, 22);
                    //chatbox.style.height = "1em";
                    //chatbox.attachToDocument();



                    ChatBox.SetLocation(Rectangle.Of(0, 440, 640, 22));
                    a.Layers.Info.appendChild(ChatBox.Control);

                    ChatBox.Layers.Canvas.style.backgroundColor = Color.Yellow;
                    ChatBox.Layers.Canvas.style.Opacity = 0.8;

                    ChatBox.Layers.Text.style.color = Color.White;

                    ChatBox.Hide();



                    ChatBox.Send += delegate(string text)
                    {
                        DrawTextWithTimeout(text, Color.White);

                        this.Session.IServer_TalkToOthers(text);
                    };



                    this.Session.OnIClient_DisplayNotification += x => DrawTextWithTimeout(x.text, x.color);


                    // put some elements on the canvas
                    //DrawRectangleLocal(Rectangle.Of(48, 48, 128, 64), Color.Green);
                    //DrawRectangleLocal(Rectangle.Of(48, 128, 128, 64), Color.Gray);
                    //DrawRectangleLocal(Rectangle.Of(400, 300, 128, 64), Color.Black);
                    //DrawRectangleLocal(Rectangle.Of(400, 500, 128, 64), 0xff5566);
                    //DrawRectangleLocal(Rectangle.Of(700, 600, 128, 64), 0x3f5466);


                    #region game units

                    var ai = new ArenaInfo();

                    ai.Arena = a;
                    ai.Minimap = m;

                    #region hidden
                    /*
                    {
                        var mcy = new ArenaUnit();

                        mcy.Color = Color.Red;
                        mcy.Owner = ai;
                        mcy.Title = "My Construction Yard Unit";


                        mcy.SetLocation(new Point(64, 64));
                        mcy.SetSize(new Point(72, 72));

                        // put the unit on the canvas
                        ai.Arena.Layers.Canvas.appendChild(mcy.Control);

                        // update its location
                        mcy.UpdateControlLocation();

                        // remeber that we have such a unit
                        ai.Units.Add(mcy);
                    }
                    */

                    /*

                    {

                        var mcy = new ArenaUnit();

                        mcy.Health.Max = 2000;
                        mcy.Health.Current = 1500;


                        mcy.Color = Color.Red;
                        mcy.Owner = ai;
                        mcy.Title = "My Construction Yard Unit 3";

                        mcy.IsSelectable = true;


                        mcy.SetLocation(new Point(250, 200));
                        var mfx = fx.Settings.ConstructionYard;

                        mcy.SetSize(mfx.Size);

                        mfx.ShowFrame(mcy.Control, 12);


                        // put the unit on the canvas
                        ai.Arena.Layers.Canvas.appendChild(mcy.Control);

                        // update its location
                        mcy.UpdateControlLocation();

                        // remeber that we have such a unit
                        ai.Units.Add(mcy);
                    }


                    {

                        var mcy = new ArenaUnit();

                        mcy.Health.Max = 2000;
                        mcy.Health.Current = 1500;


                        mcy.Color = Color.Red;
                        mcy.Owner = ai;
                        mcy.Title = "My Construction Yard Unit 3";

                        mcy.IsSelectable = true;


                        mcy.SetLocation(new Point(250, 300));
                        var mfx = fx.Settings.ConstructionYard;

                        mcy.SetSize(mfx.Size);

                        mfx.ShowFrame(mcy.Control, 12);


                        // put the unit on the canvas
                        ai.Arena.Layers.Canvas.appendChild(mcy.Control);

                        // update its location
                        mcy.UpdateControlLocation();

                        // remeber that we have such a unit
                        ai.Units.Add(mcy);
                    }*/
                    #endregion

                    m.Zoom.Changed +=
                        delegate
                        {
                            // redraw all units to the minimap
                            ai.DrawToMinimap();
                        };

                    #endregion

                    a.SelectionClick +=
                        delegate(Point p, IEvent ev)
                        {
                            ai.SelectUnits(p);
                        };

                    a.ApplySelection +=
                        delegate(Rectangle r, IEvent ev)
                        {
                            ai.SelectUnits(r);
                        };

                    ai.DrawToMinimap();
                }
            );
        }
Exemplo n.º 2
0
        public DemoControl(IHTMLElement e)
        //: base(e)
        {
            //Native.DebugBreak();

            //Console.EnableActiveXConsole();
            // Console.WriteLine("!!!");

            ImportStyleSheet("fx/css/cnc.css");


            var loading = new IHTMLDiv("Connecting to server...");

            loading.style.fontSize = "36pt";
            loading.style.backgroundColor = Color.Black;
            loading.style.padding = "2em";

            Native.Document.body.appendChild(loading);

            Native.Document.body.style.color = Color.White;
            Native.Document.body.style.backgroundColor = Color.Black;


            this.CurrentSession = new ClientSession();
            this.CurrentSession.Control = this;

            Debugger.Break();

            this.CurrentSession.MethodA("a1", "a2",
                delegate(string text)
                {
                    loading.innerHTML = "Entering lobby...";

                    this.CurrentSession.IServer_EnterLobby(
                        delegate(string str)
                        {
                            this.CurrentSession.ClientName = str;

                            Native.Document.title = str;

                            Console.WriteLine("i am: " + str);

                            loading.innerHTML = "Loading gfx... " + text;

                            new IHTMLImage("fx/gfx/logo/636.jpg").InvokeOnComplete(
                                delegate(IHTMLImage bg)
                                {
                                    var bs = Native.Document.body.style;

                                    bs.backgroundImage = "url(" + bg.src + ")";
                                    bs.backgroundRepeat = "no-repeat";


                                    Setup(
                                        delegate
                                        {

                                            loading.innerHTML = "ready!";
                                            loading.Dispose();








                                        }

                                            );
                                }
                                    );
                        });

                });
        }
Exemplo n.º 3
0
        // _1483b5833155c53585239c5e871e940c_600000c	2496	93.55%	871.254ms	1041.498ms	0.417ms

        public void DisplayNotification(string text, Color color)
        {
            var x = new IHTMLDiv(new ITextNode(text));

            x.style.color = color;

            x.style.backgroundColor = Color.Black;

            x.style.padding = "1em";
            x.style.zIndex = 1000;

            x.AttachToDocument();

            new Timer(
                delegate
                {
                    x.Dispose();
                }, 15000, 0);


        }
        public void Initialize(IHTMLElement Menu, Func<IHTMLImage, string, Type, Action<Type>, IHTMLElement> ConvertImageToControl)
        {
            var List = new IHTMLElement(IHTMLElement.HTMLElementEnum.ol).AttachTo(Menu);

            var ApplicationsWithLoadingImagesQuery =
                from t in Applications
                let assembly = t.Assembly.GetName().Name
                let preview = "assets/" + assembly + "/Preview.png"
                let image = new IHTMLImage(preview)
                orderby t.Name
                select new { t, assembly, preview, image };

            var ApplicationsWithLoadingImages = ApplicationsWithLoadingImagesQuery.ToArray();

            var LoadingMessage = new IHTMLDiv().AttachTo(Menu);

            var DoneLoading = 500.Until(
                t =>
                {
                    var count = ApplicationsWithLoadingImages.Count(i => !i.image.complete);

                    LoadingMessage.innerText = count + " images are still loading...";

                    return (count == 0 || t.Counter == 6);
                }
            );

            Func<Point> GetCenter =
                () => new Point(Native.Window.Width / 2, Native.Window.Height / 2);

            Action<Type> TypeClicked = t => { };

            DoneLoading +=
                delegate
                {
                    var query = from i in ApplicationsWithLoadingImages
                                let hasimage = i.image.complete && i.image.width > 0
                                select new { i.image, i.t, i.assembly, hasimage, i.preview }; ;

                    var WithImages =
                        from i in query
                        where i.hasimage
                        select i;


                    var WithoutImages =
                        from i in query
                        where !i.hasimage
                        select i;

                    #region WithImages
                    DoneLoading = WithImages.ForEachAtInterval(50,
                        v =>
                        {
                            LoadingMessage.innerText = v.t.Name;

                            var href = v.t.Name + ".htm";

                            var r = ConvertImageToControl(v.image, href, v.t, TypeClicked);

                            r.AttachTo(Menu);

                        }
                    );
                    #endregion

                    DoneLoading +=
                        delegate
                        {
                            LoadingMessage.Dispose();

                            var clr = new IHTMLBreak();

                            clr.style.clear = "both";
                            clr.AttachTo(Menu);

                            foreach (var v in WithoutImages)
                            {
                                new IHTMLDiv("image not found: " + v.preview).AttachTo(Menu);
                            }


                            "script".DisposeElementsByTagName();
                            "noscript".DisposeElementsByTagName();

                            
                        };

                };

            TypeClicked +=
                t =>
                {
                    Menu.Dispose();

                    try
                    {
                        Activator.CreateInstance(t);
                    }
                    catch (Exception exc)
                    {
                        Native.Window.alert("Error: " + exc.Message);

                        Menu.AttachToDocument();
                    }
                };

        }