Exemplo n.º 1
0
        protected override void LoadContent()
        {
            // We have to inject our teardown code here because if we let XNA's
            //  normal disposal handlers run, Chrome's message pump gets confused
            //  and hangs our process
            var form = (System.Windows.Forms.Control.FromHandle(Window.Handle) as System.Windows.Forms.Form);
            form.FormClosing += (s, e) => Teardown();

            spriteBatch = new SpriteBatch(GraphicsDevice);

            background = Content.Load<Texture2D>("background");

            var context = Context.Create();

            var assetRoot = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            assetProtocol = new FileProtocolHandler(
                context, "asset", (filename) => {
                    try {
                        return File.OpenRead(Path.Combine(assetRoot, filename));
                    } catch {
                        return null;
                    }
                });

            browser = new TextureBackedWindow(context, GraphicsDevice);
            browser.Transparent = true;
            browser.LoadingStateChanged += WebKit_LoadingStateChanged;
            browser.Load += WebKit_Load;
            browser.AddressBarChanged += WebKit_AddressBarChanged;
            browser.Resize(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height - 27);

            int rtWidth = browser.Texture.Width;
            int rtHeight = browser.Texture.Height;

            try {
                oldPageRt = new RenderTarget2D(
                    GraphicsDevice, rtWidth, rtHeight, 2,
                    SurfaceFormat.Color, RenderTargetUsage.DiscardContents
                );
            } catch {
                rtWidth = NextPowerOfTwo(rtWidth);
                rtHeight = NextPowerOfTwo(rtHeight);
                oldPageRt = new RenderTarget2D(
                    GraphicsDevice, rtWidth, rtHeight, 2,
                    SurfaceFormat.Color, RenderTargetUsage.DiscardContents
                );
            }

            focusedFrame = navBar = new TextureBackedWindow(context, GraphicsDevice);
            navBar.Focus();
            navBar.Resize(GraphicsDevice.Viewport.Width, 27);

            navBar.ChromeSend.Register("back", browser.GoBack);
            navBar.ChromeSend.Register("forward", browser.GoForward);
            navBar.ChromeSend.Register("go", (args) =>
                browser.NavigateTo(args[0])
            );

            browser.NavigateTo("asset://./xnatest.html");
            navBar.NavigateTo("asset://./navbar.html");
        }