Skip to content

Commit

Permalink
Renamings, disabled irrelevant warnings, removed dead code (#4)
Browse files Browse the repository at this point in the history
- Renamed variables for a simpler naming convention
- Disabled irrelevant warnings
- Removed dead code
  • Loading branch information
viktor-ferenczi authored Sep 26, 2021
1 parent 486bd42 commit 4fd291f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 100 deletions.
51 changes: 28 additions & 23 deletions EnhancedUI/BatchDataPlayer.cs → EnhancedUI/Gui/BatchDataPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using HarmonyLib;
using SharpDX;
using SharpDX.Direct3D;
Expand All @@ -12,7 +9,7 @@
using Device = SharpDX.Direct3D11.Device;
using MapFlags = SharpDX.Direct3D11.MapFlags;

namespace EnhancedUI
namespace EnhancedUI.Gui
{
public class BatchDataPlayer : IVideoPlayer
{
Expand All @@ -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<byte[]> _dataGetter;
private Texture2D _texture;
private ShaderResourceView _srv;
#pragma warning disable 8618
private readonly Vector2I videoSize;
private readonly Func<byte[]> dataGetter;
private Texture2D? texture;
private ShaderResourceView? shaderResourceView;

public BatchDataPlayer(Vector2I size, Func<byte[]> dataGetter)
#pragma warning restore 8618
{
_size = size;
_dataGetter = dataGetter;
videoSize = size;
this.dataGetter = dataGetter;
}

public void Init(string filename)
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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;
}
}
27 changes: 13 additions & 14 deletions EnhancedUI/BrowserHost.cs → EnhancedUI/Gui/Chromium.cs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
}

Expand Down
90 changes: 45 additions & 45 deletions EnhancedUI/Gui/ChromiumGuiControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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();

Expand All @@ -169,28 +169,28 @@ public override MyGuiControlBase HandleInput()

if (pressedKeys.Count == 0)
{
_lastKey = MyKeys.None;
lastKey = MyKeys.None;
}

foreach (var key in pressedKeys)
{
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;
Expand Down Expand Up @@ -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) |
Expand Down
Loading

0 comments on commit 4fd291f

Please sign in to comment.