From 45a7ca0fc626abd9416a181824d9f94a80849257 Mon Sep 17 00:00:00 2001 From: onepiecefreak3 Date: Sun, 18 Feb 2024 18:27:28 +0100 Subject: [PATCH] Update content of loaded image, if same image is wrapped into a new ImageResource; --- ImGui.Forms/Factories/ImageFactory.cs | 36 +++++++++++++++++++-------- ImGui.Forms/ImGui.Forms.nuspec | 2 +- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/ImGui.Forms/Factories/ImageFactory.cs b/ImGui.Forms/Factories/ImageFactory.cs index e241e2a..4b7c97b 100644 --- a/ImGui.Forms/Factories/ImageFactory.cs +++ b/ImGui.Forms/Factories/ImageFactory.cs @@ -12,8 +12,8 @@ class ImageFactory private readonly GraphicsDevice _gd; private readonly ImGuiRenderer _controller; - private readonly IDictionary _inputPointers; - private readonly IDictionary _inputPointersReverse; + private readonly IDictionary _inputPointers; + private readonly IDictionary _inputPointersReverse; private readonly IDictionary _ptrTextures; private readonly IDictionary _ptrTexturesRefCount; @@ -23,8 +23,8 @@ public ImageFactory(GraphicsDevice gd, ImGuiRenderer controller) { _gd = gd; _controller = controller; - _inputPointers = new Dictionary(); - _inputPointersReverse = new Dictionary(); + _inputPointers = new Dictionary(); + _inputPointersReverse = new Dictionary(); _ptrTextures = new Dictionary(); _ptrTexturesRefCount = new Dictionary(); _unloadQueue = new List(); @@ -37,6 +37,7 @@ public IntPtr LoadImage(Bitmap img) if (_inputPointers.ContainsKey(img)) { ptr = _inputPointers[img]; + UpdateImage(ptr); _ptrTexturesRefCount[ptr]++; @@ -52,6 +53,14 @@ public IntPtr LoadImage(Bitmap img) return ptr; } + public void UpdateImage(IntPtr ptr) + { + if (!_ptrTextures.ContainsKey(ptr) || !_inputPointersReverse.ContainsKey(ptr)) + return; + + CopyImageData(_ptrTextures[ptr], _inputPointersReverse[ptr]); + } + public void UnloadImage(IntPtr ptr) { if (!_ptrTextures.ContainsKey(ptr)) @@ -66,13 +75,7 @@ private IntPtr LoadImageInternal(Bitmap image) var texture = _gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D( (uint)image.Width, (uint)image.Height, 1, 1, Veldrid.PixelFormat.B8_G8_R8_A8_UNorm, TextureUsage.Sampled)); - var data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), - ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - - _gd.UpdateTexture(texture, data.Scan0, (uint)(4 * image.Width * image.Height), - 0, 0, 0, (uint)image.Width, (uint)image.Height, 1, 0, 0); - - image.UnlockBits(data); + CopyImageData(texture, image); // Add image pointer to cache var imgPtr = _controller.GetOrCreateImGuiBinding(_gd.ResourceFactory, texture); @@ -81,6 +84,17 @@ private IntPtr LoadImageInternal(Bitmap image) return imgPtr; } + private void CopyImageData(Texture texture, Bitmap image) + { + var data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), + ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + + _gd.UpdateTexture(texture, data.Scan0, (uint)(4 * image.Width * image.Height), + 0, 0, 0, (uint)image.Width, (uint)image.Height, 1, 0, 0); + + image.UnlockBits(data); + } + internal void FreeTextures() { foreach (var toFree in _unloadQueue) diff --git a/ImGui.Forms/ImGui.Forms.nuspec b/ImGui.Forms/ImGui.Forms.nuspec index 50b84ca..290a3b7 100644 --- a/ImGui.Forms/ImGui.Forms.nuspec +++ b/ImGui.Forms/ImGui.Forms.nuspec @@ -2,7 +2,7 @@ Imgui.Forms - 1.0.51 + 1.0.52 A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui) onepiecefreak