diff --git a/GUIs/skyscraper8.UI.ImGui/Forms/SdlScottPlotWindow.cs b/GUIs/skyscraper8.UI.ImGui/Forms/SdlScottPlotWindow.cs new file mode 100644 index 0000000..58fb6e7 --- /dev/null +++ b/GUIs/skyscraper8.UI.ImGui/Forms/SdlScottPlotWindow.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Echo.UserInterface.Backend; +using ScottPlot; +using SDL2; +using SkiaSharp; +using skyscraper8.UI.ImGuiForms; +using testdrid.SdlWrapper; + +namespace skyscraper8.UI.ImGui.Forms +{ + internal class SdlScottPlotWindow : SdlWindow + { + private readonly Plot _plot; + private readonly SKBitmap _skBitmap; + private readonly SKCanvas _skCanvas; + + public SdlScottPlotWindow(ImGuiDevice imGuiDevice, Plot plot, int width, int height, string title) + : base(imGuiDevice, width, height, title) + { + this._plot = plot; + this._skBitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888)); + this._skCanvas = new SKCanvas(_skBitmap); + } + + private byte[] tempBuffer; + private int dataLength; + protected override void RenderInternal(Texture surface) + { + _plot.Render(_skCanvas, (int)this._size.X, (int)this._size.Y); + + nint data0 = surface.GetData0(); + nint pixels = _skBitmap.GetPixels(); + + if (dataLength == 0) + { + dataLength = _skBitmap.ByteCount; + tempBuffer = new byte[dataLength]; + } + + Marshal.Copy(pixels, tempBuffer, 0, dataLength); + Marshal.Copy(tempBuffer, 0, data0, dataLength); + + } + } +} diff --git a/GUIs/skyscraper8.UI.ImGui/Forms/SdlTestWindow.cs b/GUIs/skyscraper8.UI.ImGui/Forms/SdlTestWindow.cs new file mode 100644 index 0000000..a28b265 --- /dev/null +++ b/GUIs/skyscraper8.UI.ImGui/Forms/SdlTestWindow.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Echo.UserInterface.Backend; +using SDL2; +using skyscraper8.UI.ImGuiForms; +using testdrid; +using testdrid.SdlWrapper; + +namespace skyscraper8.UI.ImGui.Forms +{ + internal class SdlTestWindow : SdlWindow + { + private Random rng; + public SdlTestWindow(ImGuiDevice imGuiDevice) : base(imGuiDevice, 320, 240) + { + rng = new Random(); + } + + protected override void RenderInternal(Texture surface) + { + byte[] buffer = new byte[3]; + rng.NextBytes(buffer); + + surface.SetPixel(rng.Next(320), rng.Next(240), 255, buffer[0], buffer[1], buffer[2]); + } + } +} diff --git a/GUIs/skyscraper8.UI.ImGui/Forms/SdlWindow.cs b/GUIs/skyscraper8.UI.ImGui/Forms/SdlWindow.cs new file mode 100644 index 0000000..563c7d9 --- /dev/null +++ b/GUIs/skyscraper8.UI.ImGui/Forms/SdlWindow.cs @@ -0,0 +1,49 @@ +using Echo.Core.Common.Packed; +using Echo.UserInterface.Backend; +using ImGuiNET; +using SDL2; +using SDL2Demo; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using skyscraper8.Skyscraper.Drawing; +using testdrid.SdlWrapper; + +namespace skyscraper8.UI.ImGuiForms +{ + internal abstract class SdlWindow : IRenderable + { + protected readonly Vector2 _size; + protected readonly Texture _texture; + protected readonly string title; + + protected SdlWindow(ImGuiDevice imGuiDevice, int width, int height, string title = "SDL Window") + { + + IntPtr textureIntPtr = imGuiDevice.CreateTexture(new Int2(width, height), true, !BitConverter.IsLittleEndian); + _texture = Texture.FromIntPointer(textureIntPtr); + + _size = new Vector2(_texture.GetWidth(), _texture.GetHeight()); + this.title = title; + } + + public void Render() + { + _texture.Lock(); + RenderInternal(_texture); + _texture.Unlock(); + + + ImGuiNET.ImGui.PushStyleVar(ImGuiStyleVar.WindowMinSize, new Vector2(_texture.GetWidth() + 16, _texture.GetHeight() + 16)); + ImGuiNET.ImGui.Begin(title); + ImGuiNET.ImGui.Image(_texture.Pointer, _size); + ImGuiNET.ImGui.End(); + ImGuiNET.ImGui.PopStyleVar(); + } + + protected abstract void RenderInternal(Texture surface); + } +} diff --git a/GUIs/skyscraper8.UI.ImGui/Program.cs b/GUIs/skyscraper8.UI.ImGui/Program.cs index 6f7071d..2160649 100644 --- a/GUIs/skyscraper8.UI.ImGui/Program.cs +++ b/GUIs/skyscraper8.UI.ImGui/Program.cs @@ -4,6 +4,8 @@ using System.Net.NetworkInformation; using System.Reflection; using Echo.UserInterface.Backend; using ImGuiNET; +using ScottPlot; +using ScottPlot.Plottables; using SDL2; using SDL2Demo.SdlWrapper; using testdrid.SdlWrapper; @@ -23,7 +25,10 @@ using skyscraper5.Skyscraper.Scraper.Storage.InMemory; using skyscraper5.src.Skyscraper; using skyscraper8.Skyscraper.Plugins; using skyscraper8.Skyscraper.Scraper.Storage; +using skyscraper8.UI.ImGui.Forms; +using Color = ScottPlot.Color; using Exception = System.Exception; +using Version = System.Version; namespace SkyscraperUI @@ -767,7 +772,7 @@ namespace SkyscraperUI { uiBlockingWindow.Render(); } - + ImGui.EndFrame(); ImGui.Render(); diff --git a/GUIs/skyscraper8.UI.ImGui/RandomExtension.cs b/GUIs/skyscraper8.UI.ImGui/RandomExtension.cs index 4fb517e..29acc89 100644 --- a/GUIs/skyscraper8.UI.ImGui/RandomExtension.cs +++ b/GUIs/skyscraper8.UI.ImGui/RandomExtension.cs @@ -75,6 +75,7 @@ namespace testdrid } } + /// /// Returns n unique random numbers in the range [1, n], inclusive. /// This is equivalent to getting the first n numbers of some random permutation of the sequential numbers from 1 to max. diff --git a/GUIs/skyscraper8.UI.ImGui/SdlWrapper/Texture.cs b/GUIs/skyscraper8.UI.ImGui/SdlWrapper/Texture.cs index 60d2a6b..f9a58f4 100644 --- a/GUIs/skyscraper8.UI.ImGui/SdlWrapper/Texture.cs +++ b/GUIs/skyscraper8.UI.ImGui/SdlWrapper/Texture.cs @@ -137,6 +137,14 @@ namespace testdrid.SdlWrapper Marshal.WriteByte(locked, offset + 3, a); } + public IntPtr GetData0() + { + if (locked == IntPtr.Zero) + throw new InvalidOperationException("Texture not locked"); + + return locked; + } + public void Unlock() { if (locked == IntPtr.Zero) diff --git a/GUIs/skyscraper8.UI.ImGui/skyscraper8.UI.ImGui.csproj b/GUIs/skyscraper8.UI.ImGui/skyscraper8.UI.ImGui.csproj index 9ae04c4..4b62cb1 100644 --- a/GUIs/skyscraper8.UI.ImGui/skyscraper8.UI.ImGui.csproj +++ b/GUIs/skyscraper8.UI.ImGui/skyscraper8.UI.ImGui.csproj @@ -12,6 +12,7 @@ +