diff --git a/EnhancedUI/BatchDataPlayer.cs b/EnhancedUI/Gui/BatchDataPlayer.cs similarity index 68% rename from EnhancedUI/BatchDataPlayer.cs rename to EnhancedUI/Gui/BatchDataPlayer.cs index 43168c5..350e526 100644 --- a/EnhancedUI/BatchDataPlayer.cs +++ b/EnhancedUI/Gui/BatchDataPlayer.cs @@ -1,7 +1,4 @@ using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.IO; using HarmonyLib; using SharpDX; using SharpDX.Direct3D; @@ -12,7 +9,7 @@ using Device = SharpDX.Direct3D11.Device; using MapFlags = SharpDX.Direct3D11.MapFlags; -namespace EnhancedUI +namespace EnhancedUI.Gui { public class BatchDataPlayer : IVideoPlayer { @@ -21,16 +18,15 @@ public class BatchDataPlayer : IVideoPlayer Type.GetType("VRage.Platform.Windows.Render.MyPlatformRender, VRage.Platform.Windows", true), "DeviceInstance")); - private readonly Vector2I _size; - private readonly Func _dataGetter; - private Texture2D _texture; - private ShaderResourceView _srv; -#pragma warning disable 8618 + private readonly Vector2I videoSize; + private readonly Func dataGetter; + private Texture2D? texture; + private ShaderResourceView? shaderResourceView; + public BatchDataPlayer(Vector2I size, Func dataGetter) -#pragma warning restore 8618 { - _size = size; - _dataGetter = dataGetter; + videoSize = size; + this.dataGetter = dataGetter; } public void Init(string filename) @@ -52,7 +48,12 @@ public void Init(string filename) }, OptionFlags = ResourceOptionFlags.None, }; - _texture = new(_deviceInstance(), texture2DDescription); + + texture = new Texture2D(_deviceInstance(), texture2DDescription) + { + DebugName = "BatchDataPlayer.Texture" + }; + var shaderResourceViewDescription = new ShaderResourceViewDescription { Format = Format.B8G8R8A8_UNorm_SRgb, @@ -63,15 +64,19 @@ public void Init(string filename) MostDetailedMip = 0 } }; - _srv = new(_deviceInstance(), _texture, shaderResourceViewDescription); - _texture.DebugName = _srv.DebugName = "BatchDataPlayer.Texture"; + + shaderResourceView = new ShaderResourceView(_deviceInstance(), texture, shaderResourceViewDescription) + { + DebugName = texture.DebugName + }; } public void Dispose() { Stop(); - _srv.Dispose(); - _texture.Dispose(); + + shaderResourceView?.Dispose(); + texture?.Dispose(); } public void Play() @@ -86,30 +91,30 @@ public void Stop() public void Update(object context) { - if (CurrentState == VideoState.Playing && _dataGetter() is { } data) + if (CurrentState == VideoState.Playing && dataGetter() is { } data) OnFrame((DeviceContext)context, data); } private void OnFrame(DeviceContext context, byte[] data) { - var dataBox = context.MapSubresource(_texture, 0, MapMode.WriteDiscard, MapFlags.None); + var dataBox = context.MapSubresource(texture, 0, MapMode.WriteDiscard, MapFlags.None); if (dataBox.IsEmpty) return; Utilities.Write(dataBox.DataPointer, data, 0, data.Length); - context.UnmapSubresource(_texture, 0); + context.UnmapSubresource(texture, 0); } - public int VideoWidth => _size.X; + public int VideoWidth => videoSize.X; - public int VideoHeight => _size.Y; + public int VideoHeight => videoSize.Y; public float Volume { get; set; } public VideoState CurrentState { get; private set; } - public IntPtr TextureSrv => _srv.NativePointer; + public IntPtr TextureSrv => shaderResourceView?.NativePointer ?? IntPtr.Zero; } } \ No newline at end of file diff --git a/EnhancedUI/BrowserHost.cs b/EnhancedUI/Gui/Chromium.cs similarity index 78% rename from EnhancedUI/BrowserHost.cs rename to EnhancedUI/Gui/Chromium.cs index 6be05d6..c0acbbd 100644 --- a/EnhancedUI/BrowserHost.cs +++ b/EnhancedUI/Gui/Chromium.cs @@ -1,34 +1,36 @@ using System; +using System.Drawing; using System.Runtime.InteropServices; using CefSharp; using CefSharp.OffScreen; using VRageMath; -using VRageRender; -namespace EnhancedUI +namespace EnhancedUI.Gui { - public class BrowserHost : IDisposable + public class Chromium : IDisposable { - public byte[] VideoData { get; private set; } + private byte[] videoData; public event Action? Ready; public readonly ChromiumWebBrowser Browser; - public BrowserHost(Vector2I size) + public Chromium(Vector2I size) { - VideoData = new byte[size.X * size.Y * 4]; - Browser = new () + videoData = new byte[size.X * size.Y * 4]; + + Browser = new ChromiumWebBrowser { - Size = new (size.X, size.Y) + Size = new Size(size.X, size.Y), + LifeSpanHandler = new LifespanHandler() }; + Browser.Paint += BrowserOnPaint; Browser.BrowserInitialized += BrowserOnBrowserInitialized; - Browser.LifeSpanHandler = new LifespanHandler(); } public byte[] GetVideoData() { - return VideoData; + return videoData; } private void BrowserOnBrowserInitialized(object sender, EventArgs e) @@ -49,10 +51,7 @@ public void Draw() private void BrowserOnPaint(object sender, OnPaintEventArgs e) { - var videoData = VideoData; - Marshal.Copy(e.BufferHandle, videoData, 0, e.Width * e.Height * 4); - VideoData = videoData; - + Marshal.Copy(e.BufferHandle, this.videoData, 0, e.Width * e.Height * 4); e.Handled = true; } diff --git a/EnhancedUI/Gui/ChromiumGuiControl.cs b/EnhancedUI/Gui/ChromiumGuiControl.cs index c678290..4b989c5 100644 --- a/EnhancedUI/Gui/ChromiumGuiControl.cs +++ b/EnhancedUI/Gui/ChromiumGuiControl.cs @@ -13,30 +13,30 @@ namespace EnhancedUI.Gui { public class ChromiumGuiControl : MyGuiControlBase { - private BrowserHost? _browserHost; + private Chromium? chromium; public static BatchDataPlayer? Player; - private uint _videoId; + private uint videoId; //Returns false if the browser is not initialized else it returns true. - private bool IsBrowserInitialized => _browserHost?.Browser.IsBrowserInitialized ?? false; + private bool IsBrowserInitialized => chromium?.Browser.IsBrowserInitialized ?? false; public readonly MyGuiControlRotatingWheel Wheel = new(Vector2.Zero) { Visible = false }; - private readonly WebContent _content; - private readonly string _name; + private readonly WebContent content; + private readonly string name; - private bool _capsLock; - private MyKeys _lastKey; - private int _delay; + private bool capsLock; + private MyKeys lastKey; + private int delay; public ChromiumGuiControl(WebContent content, string name) { - _content = content; - _name = name; + this.content = content; + this.name = name; } protected override void OnSizeChanged() @@ -50,48 +50,48 @@ protected override void OnSizeChanged() private void CreatePlayerIfNeeded() { - if (_browserHost != null) + if (chromium != null) return; var rect = GetVideoScreenRectangle(); - _browserHost = new(new(rect.Width, rect.Height)); + chromium = new Chromium(new Vector2I(rect.Width, rect.Height)); - _browserHost.Ready += BrowserHostOnReady; - _browserHost.Browser.LoadingStateChanged += BrowserOnLoadingStateChanged; + chromium.Ready += OnChromiumReady; + chromium.Browser.LoadingStateChanged += OnBrowserLoadingStateChanged; - Player = new BatchDataPlayer(new Vector2I(rect.Width, rect.Height), _browserHost.GetVideoData); + Player = new BatchDataPlayer(new Vector2I(rect.Width, rect.Height), chromium.GetVideoData); } public override void OnRemoving() { base.OnRemoving(); - if (_browserHost == null) + if (chromium == null) return; - _browserHost.Ready -= BrowserHostOnReady; - _browserHost.Browser.LoadingStateChanged -= BrowserOnLoadingStateChanged; + chromium.Ready -= OnChromiumReady; + chromium.Browser.LoadingStateChanged -= OnBrowserLoadingStateChanged; - _browserHost.Dispose(); - MyRenderProxy.CloseVideo(_videoId); + chromium.Dispose(); + MyRenderProxy.CloseVideo(videoId); Player = null; - _browserHost = null; + chromium = null; } - private void BrowserOnLoadingStateChanged(object sender, LoadingStateChangedEventArgs e) + private void OnBrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e) { Wheel.Visible = e.IsLoading; } - private void BrowserHostOnReady() + private void OnChromiumReady() { - if (_browserHost == null) + if (chromium == null) throw new Exception("This should not happen"); - var url = _content.FormatIndexUrl(_name); - _browserHost.Navigate(url); - _videoId = MyRenderProxy.PlayVideo(VideoPlayPatch.VIDEO_NAME, 0); + var url = content.FormatIndexUrl(name); + chromium.Navigate(url); + videoId = MyRenderProxy.PlayVideo(VideoPlayPatch.VIDEO_NAME, 0); } // Removes the browser instance when ChromiumGuiControl is no longer needed. @@ -110,25 +110,25 @@ private Rectangle GetVideoScreenRectangle() // Renders the HTML document on the screen using the video player public override void Draw(float transitionAlpha, float backgroundTransitionAlpha) { - if (!MyRenderProxy.IsVideoValid(_videoId)) + if (!MyRenderProxy.IsVideoValid(videoId)) return; - if (_browserHost == null) + if (chromium == null) throw new Exception("This should not happen"); - _browserHost.Draw(); - MyRenderProxy.UpdateVideo(_videoId); - MyRenderProxy.DrawVideo(_videoId, GetVideoScreenRectangle(), new(Vector4.One), + chromium.Draw(); + MyRenderProxy.UpdateVideo(videoId); + MyRenderProxy.DrawVideo(videoId, GetVideoScreenRectangle(), new(Vector4.One), MyVideoRectangleFitMode.AutoFit, false); } // Reloads the HTML document private void ReloadPage() { - if (_browserHost == null) + if (chromium == null) throw new Exception("This should not happen"); - _browserHost.Browser.Reload(); + chromium.Browser.Reload(); } // Clears the cookies from the CEF browser @@ -153,14 +153,14 @@ public override MyGuiControlBase HandleInput() return base.HandleInput(); } - if (_browserHost == null) + if (this.chromium == null) throw new Exception("This should not happen"); - var browser = _browserHost.Browser; + var browser = this.chromium.Browser; var browserHost = browser.GetBrowser().GetHost(); if (input.IsKeyPress(MyKeys.CapsLock)) - _capsLock = !_capsLock; + capsLock = !capsLock; var modifiers = GetModifiers(); @@ -169,7 +169,7 @@ public override MyGuiControlBase HandleInput() if (pressedKeys.Count == 0) { - _lastKey = MyKeys.None; + lastKey = MyKeys.None; } foreach (var key in pressedKeys) @@ -177,20 +177,20 @@ public override MyGuiControlBase HandleInput() if (key == MyKeys.Escape) continue; - if (key == _lastKey) + if (key == lastKey) { - if (_delay > 0) + if (delay > 0) { - _delay--; + delay--; continue; } - _delay = 5; + delay = 5; } else { - _lastKey = key; - _delay = 20; + lastKey = key; + delay = 20; } var keyChar = (char)key; @@ -276,7 +276,7 @@ private CefEventFlags GetModifiers() { var input = MyInput.Static; return ( - (_capsLock ? CefEventFlags.CapsLockOn : 0) | + (capsLock ? CefEventFlags.CapsLockOn : 0) | (input.IsAnyShiftKeyPressed() ? CefEventFlags.ShiftDown : 0) | (input.IsAnyCtrlKeyPressed() ? CefEventFlags.ControlDown : 0) | (input.IsAnyAltKeyPressed() ? CefEventFlags.AltDown : 0) | diff --git a/EnhancedUI/Gui/CreateControlPanelPatch.cs b/EnhancedUI/Gui/CreateControlPanelPatch.cs index 7ed61ef..08bd451 100644 --- a/EnhancedUI/Gui/CreateControlPanelPatch.cs +++ b/EnhancedUI/Gui/CreateControlPanelPatch.cs @@ -9,24 +9,23 @@ namespace EnhancedUI.Gui { //Replaces the controls on the Control Panel section of the terminal. [HarmonyPatch(typeof(MyGuiScreenTerminal), "CreateControlPanelPageControls")] + // ReSharper disable once UnusedType.Global internal static class CreateControlPanelPatch { private const string NAME = "Terminal"; - private static WebContent Content = new WebContent(); + private static readonly WebContent _content = new(); + // ReSharper disable once UnusedMember.Local private static bool Prefix( MyGuiControlTabPage page, + // ReSharper disable once InconsistentNaming Dictionary ___m_defaultFocusedControlKeyboard) { - // Code for a reload button - //MyGuiControlButton refreshButton = new MyGuiControlButton(new Vector2(0, 0.0f), VRage.Game.MyGuiControlButtonStyleEnum.Default, null, null, MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER, "Reload HTML page.", new System.Text.StringBuilder("Reload Page"), onButtonClick: new Action(ReloadAction)); - //page.Controls.Add(refreshButton); - page.Name = "PageControlPanel"; page.TextEnum = MySpaceTexts.ControlPanel; page.TextScale = 0.7005405f; - var control = new ChromiumGuiControl(Content, NAME) + var control = new ChromiumGuiControl(_content, NAME) { Position = new(0f, 0.005f), Size = new(0.9f, 0.7f) @@ -39,12 +38,5 @@ private static bool Prefix( ___m_defaultFocusedControlKeyboard[MyTerminalPageEnum.ControlPanel] = control; return false; } - - /* Code for reloading the page with the reload button - private static void ReloadAction(MyGuiControlButton myGuiControlButton) - { - control.ReloadPage(); - } - */ } } \ No newline at end of file diff --git a/EnhancedUI/Gui/InitControlPanelPatch.cs b/EnhancedUI/Gui/InitControlPanelPatch.cs index 92be9c0..1de4a8e 100644 --- a/EnhancedUI/Gui/InitControlPanelPatch.cs +++ b/EnhancedUI/Gui/InitControlPanelPatch.cs @@ -5,13 +5,16 @@ namespace EnhancedUI.Gui { [HarmonyPatch] + // ReSharper disable once UnusedType.Global internal static class InitControlPanelPatch { + // ReSharper disable once UnusedMember.Local private static MethodBase TargetMethod() { return AccessTools.Method(Type.GetType("Sandbox.Game.Gui.MyTerminalControlPanel, Sandbox.Game", true), "Init"); } + // ReSharper disable once UnusedMember.Local private static bool Prefix() => false; } } \ No newline at end of file diff --git a/EnhancedUI/VideoPlayPatch.cs b/EnhancedUI/Gui/VideoPlayPatch.cs similarity index 92% rename from EnhancedUI/VideoPlayPatch.cs rename to EnhancedUI/Gui/VideoPlayPatch.cs index 2f88302..0d8555f 100644 --- a/EnhancedUI/VideoPlayPatch.cs +++ b/EnhancedUI/Gui/VideoPlayPatch.cs @@ -2,11 +2,10 @@ using System.Reflection; using System.Runtime.ExceptionServices; using System.Security; -using EnhancedUI.Gui; using HarmonyLib; using VRageRender; -namespace EnhancedUI +namespace EnhancedUI.Gui { // Patch to allow loading HTML files using the video player [HarmonyPatch] @@ -20,6 +19,7 @@ internal static class VideoPlayPatch public const string VIDEO_NAME = "CefFrame"; + // ReSharper disable once UnusedMember.Local private static MethodBase TargetMethod() { return AccessTools.Method(_factoryType, "Play"); @@ -27,6 +27,7 @@ private static MethodBase TargetMethod() [HandleProcessCorruptedStateExceptions] [SecurityCritical] + // ReSharper disable once UnusedMember.Local private static bool Prefix(uint id, string videoFile) { if (videoFile != VIDEO_NAME) diff --git a/EnhancedUI/Gui/WebContent.cs b/EnhancedUI/Gui/WebContent.cs index a6efdcf..84433ce 100644 --- a/EnhancedUI/Gui/WebContent.cs +++ b/EnhancedUI/Gui/WebContent.cs @@ -1,3 +1,4 @@ +using System.Drawing.Design; using System.IO; using System.Web; using EnhancedUI.Utils; diff --git a/EnhancedUI/Main.cs b/EnhancedUI/Main.cs index 83e1517..f0a69bd 100644 --- a/EnhancedUI/Main.cs +++ b/EnhancedUI/Main.cs @@ -2,15 +2,13 @@ using System.Reflection; using CefSharp; using CefSharp.OffScreen; -using EnhancedUI.Gui; using HarmonyLib; -using Sandbox.Graphics.GUI; using VRage.FileSystem; -using VRage.Input; using VRage.Plugins; namespace EnhancedUI { + // ReSharper disable once UnusedType.Global public class Main : IPlugin { public void Dispose()