Skip to content

Instantly share code, notes, and snippets.

@agaertner
Last active September 29, 2023 11:10
Show Gist options
  • Save agaertner/1ee6b75d7817327a4051b02c5487f697 to your computer and use it in GitHub Desktop.
Save agaertner/1ee6b75d7817327a4051b02c5487f697 to your computer and use it in GitHub Desktop.
BlishHUD Notes
using Blish_HUD;
using Microsoft.Xna.Framework.Graphics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Color = Microsoft.Xna.Framework.Color;
using Control = Blish_HUD.Controls.Control;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace GitHub.Gist.BlishHud.Nekres {
    internal class TargetCanvas : Control {
        private Bitmap _canvas;
        private Texture2D _browserTexture;
        private WebBrowser _webBrowser;
        public TargetCanvas() {

            // Create the WebBrowser. Note: Does NOT support JavaScript
            _webBrowser                        =  new WebBrowser();
            _webBrowser.DocumentCompleted      += WebBrowser_DocumentCompleted;
            _webBrowser.ScriptErrorsSuppressed =  true;
            _webBrowser.Width                  =  520;
            _webBrowser.Height                 =  440;
            _canvas                            =  new Bitmap(_webBrowser.Width, _webBrowser.Height);

            // You can either set the document text or navigate to a resource
            _webBrowser.DocumentText    =  "<html><head><script type='text/javascript'>alert('Hello World');</script></head><body></body></html>";
            _webBrowser.Navigate("https://d3b4yo2b5lbfy.cloudfront.net/rampage/index.html");

            using var gdc = GameService.Graphics.LendGraphicsDeviceContext();
            _browserTexture = new Texture2D(gdc.GraphicsDevice, _canvas.Width, _canvas.Height);
        }

        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
            this.Invalidate();
        }

        protected override void DisposeControl() {
            base.DisposeControl();
        }

        protected override void Paint(SpriteBatch spriteBatch, Rectangle bounds) {
            if (_webBrowser.Document == null || _webBrowser.Document.Body == null) {
                return;
            }

            _webBrowser.DrawToBitmap(_canvas, _webBrowser.Bounds);
            ConvertBitmapToTexture2D(_canvas, _browserTexture);
            spriteBatch.DrawOnCtrl(this, _browserTexture, bounds, Color.White);
        }

        private void ConvertBitmapToTexture2D(Bitmap bitmap, Texture2D texture) {
            BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
            int dataSize = bitmapData.Stride * bitmapData.Height;
            byte[] data = new byte[dataSize];
            Marshal.Copy(bitmapData.Scan0, data, 0, dataSize);
            bitmap.UnlockBits(bitmapData);
            texture.SetData(data);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment