@ -1,151 +1,108 @@
// <auto-generated />
using System ;
namespace ClickableTransparentOverlay
{
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.IO ;
using System.Numerics ;
using System.Numerics ;
using System.Reflection ;
using System.Reflection ;
using System.IO ;
using Veldrid ;
using System.Runtime.CompilerServices ;
using System.Runtime.CompilerServices ;
using ImGuiNET ;
using ImGuiNET ;
using NativeLibraryLoader ;
using Veldrid ;
namespace ClickableTransparentOverlay
{
/// <summary>
/// <summary>
/// A modified version of ImGui.NET.SampleProgram's ImGuiController.
/// A modified version of Veldrid.ImGui's ImGuiRender er.
/// Manages input for ImGui and handles rendering ImGui's DrawLists with Veldrid.
/// Manages input for ImGui and handles rendering ImGui's DrawLists with Veldrid.
/// </summary>
/// </summary>
public sealed class ImGuiController : IDisposable
public class ImGuiController : IDisposable
{
{
#pragma warning disable CS0169 // Force Copy this DLL
private GraphicsDevice _ gd ;
private readonly DefaultPathResolver useless ;
private bool _f rameBegun ;
#pragma warning restore CS0169
private readonly IntPtr fontAtlasID = ( IntPtr ) 1 ;
// Veldrid objects
private DeviceBuffer _ vertexBuffer ;
private DeviceBuffer _ indexBuffer ;
private DeviceBuffer _ projMatrixBuffer ;
private Texture _f ontTexture ;
private TextureView _f ontTextureView ;
private Shader _ vertexShader ;
private Shader _f ragmentShader ;
private ResourceLayout _l ayout ;
private ResourceLayout _ textureLayout ;
private Pipeline _ pipeline ;
private ResourceSet _ mainResourceSet ;
private ResourceSet _f ontTextureResourceSet ;
private readonly IntPtr _f ontAtlasID = ( IntPtr ) 1 ;
private bool _ controlDown ;
private bool _ shiftDown ;
private bool _ altDown ;
private bool _ winKeyDown ;
private int _ windowWidth ;
private int _ windowHeight ;
private Vector2 _ scaleFactor = Vector2 . One ;
// Image trackers
// Image trackers
private readonly Dictionary < TextureView , ResourceSetInfo > setsByView
private readonly Dictionary < TextureView , ResourceSetInfo > _ setsByView
= new Dictionary < TextureView , ResourceSetInfo > ( ) ;
= new Dictionary < TextureView , ResourceSetInfo > ( ) ;
private readonly Dictionary < Texture , TextureView > _ autoViewsByTexture
private readonly Dictionary < Texture , TextureView > autoViewsByTexture
= new Dictionary < Texture , TextureView > ( ) ;
= new Dictionary < Texture , TextureView > ( ) ;
private readonly Dictionary < IntPtr , ResourceSetInfo > _ viewsById = new Dictionary < IntPtr , ResourceSetInfo > ( ) ;
private readonly Dictionary < IntPtr , ResourceSetInfo > viewsById
private readonly List < IDisposable > _ ownedResources = new List < IDisposable > ( ) ;
= new Dictionary < IntPtr , ResourceSetInfo > ( ) ;
private int _l astAssignedID = 1 0 0 ;
private readonly List < IDisposable > ownedResources
= new List < IDisposable > ( ) ;
private int lastAssignedID = 1 0 0 ;
private GraphicsDevice gd ;
private bool frameBegun ;
// Veldrid objects
private DeviceBuffer vertexBuffer ;
private DeviceBuffer indexBuffer ;
private DeviceBuffer projMatrixBuffer ;
private Texture fontTexture ;
private TextureView fontTextureView ;
private Shader vertexShader ;
private Shader fragmentShader ;
private ResourceLayout layout ;
private ResourceLayout textureLayout ;
private Pipeline pipeline ;
private ResourceSet mainResourceSet ;
private ResourceSet fontTextureResourceSet ;
private int windowWidth ;
private int windowHeight ;
private Vector2 scaleFactor = Vector2 . One ;
/// <summary>
/// <summary>
/// Initializes a new instance of the <see cref="ImGuiController"/> class .
/// Constructs a new ImGuiController.
/// </summary>
/// </summary>
/// <param name="gd">
public ImGuiController ( GraphicsDevice gd , OutputDescription outputDescription , int width , int height )
/// Graphic Device
{
/// </param>
_ gd = gd ;
/// <param name="outputDescription">
_ windowWidth = width ;
/// Output Description
_ windowHeight = height ;
/// </param>
/// <param name="width">
/// SDL2Window Window width
/// </param>
/// <param name="height">
/// SDL2Window Window height
/// </param>
/// <param name="fps">
/// desired FPS of the ImGui Overlay
/// </param>
public ImGuiController ( GraphicsDevice gd , OutputDescription outputDescription , int width , int height , int fps )
{
this . gd = gd ;
this . windowWidth = width ;
this . windowHeight = height ;
IntPtr context = ImGui . CreateContext ( ) ;
IntPtr context = ImGui . CreateContext ( ) ;
ImGui . SetCurrentContext ( context ) ;
ImGui . SetCurrentContext ( context ) ;
ImGui . GetIO ( ) . Fonts . AddFontDefault ( ) ;
ImGui . GetIO ( ) . Fonts . AddFontDefault ( ) ;
this . CreateDeviceResources ( gd , outputDescription ) ;
CreateDeviceResources ( gd , outputDescription ) ;
SetKeyMappings ( ) ;
SetKeyMappings ( ) ;
this . SetPerFrameImGuiData ( 1f / fps ) ;
SetPerFrameImGuiData ( 1f / 6 0f ) ;
ImGui . NewFrame ( ) ;
ImGui . NewFrame ( ) ;
this . f rameBegun = true ;
_f rameBegun = true ;
}
}
/// <summary>
/// Updates the ImGui about the SDL2Window Size
/// </summary>
/// <param name="width">
/// Width of the SDL2Window
/// </param>
/// <param name="height">
/// Height of the SDL2Window
/// </param>
public void WindowResized ( int width , int height )
public void WindowResized ( int width , int height )
{
{
this . windowWidth = width ;
_ windowWidth = width ;
this . windowHeight = height ;
_ windowHeight = height ;
}
}
/// <summary>
/// Disposes the resources acquired by the ImGuiController class.
/// </summary>
public void DestroyDeviceObjects ( )
public void DestroyDeviceObjects ( )
{
{
this . Dispose ( ) ;
Dispose ( ) ;
}
}
/// <summary>
/// Initializes different resources for ImGui Controller class.
/// </summary>
/// <param name="gd">
/// Graphic Device
/// </param>
/// <param name="outputDescription">
/// Output Description
/// </param>
public void CreateDeviceResources ( GraphicsDevice gd , OutputDescription outputDescription )
public void CreateDeviceResources ( GraphicsDevice gd , OutputDescription outputDescription )
{
{
this . gd = gd ;
_ gd = gd ;
ResourceFactory factory = gd . ResourceFactory ;
ResourceFactory factory = gd . ResourceFactory ;
this . vertexBuffer = factory . CreateBuffer ( new BufferDescription ( 1 0 0 0 0 , BufferUsage . VertexBuffer | BufferUsage . Dynamic ) ) ;
_ vertexBuffer = factory . CreateBuffer ( new BufferDescription ( 1 0 0 0 0 , BufferUsage . VertexBuffer | BufferUsage . Dynamic ) ) ;
this . vertexBuffer . Name = "ImGui.NET Vertex Buffer" ;
_ vertexBuffer . Name = "ImGui.NET Vertex Buffer" ;
this . indexBuffer = factory . CreateBuffer ( new BufferDescription ( 2 0 0 0 , BufferUsage . IndexBuffer | BufferUsage . Dynamic ) ) ;
_ indexBuffer = factory . CreateBuffer ( new BufferDescription ( 2 0 0 0 , BufferUsage . IndexBuffer | BufferUsage . Dynamic ) ) ;
this . indexBuffer . Name = "ImGui.NET Index Buffer" ;
_ indexBuffer . Name = "ImGui.NET Index Buffer" ;
this . RecreateFontDeviceTexture ( gd ) ;
RecreateFontDeviceTexture ( gd ) ;
this . projMatrixBuffer = factory . CreateBuffer ( new BufferDescription ( 6 4 , BufferUsage . UniformBuffer | BufferUsage . Dynamic ) ) ;
_ projMatrixBuffer = factory . CreateBuffer ( new BufferDescription ( 6 4 , BufferUsage . UniformBuffer | BufferUsage . Dynamic ) ) ;
this . projMatrixBuffer . Name = "ImGui.NET Projection Buffer" ;
_ projMatrixBuffer . Name = "ImGui.NET Projection Buffer" ;
byte [ ] vertexShaderBytes = this . LoadEmbeddedShaderCode ( gd . ResourceFactory , "imgui-vertex" , ShaderStages . Vertex ) ;
byte [ ] vertexShaderBytes = LoadEmbeddedShaderCode ( gd . ResourceFactory , "imgui-vertex" , ShaderStages . Vertex ) ;
byte [ ] fragmentShaderBytes = this . LoadEmbeddedShaderCode ( gd . ResourceFactory , "imgui-frag" , ShaderStages . Fragment ) ;
byte [ ] fragmentShaderBytes = LoadEmbeddedShaderCode ( gd . ResourceFactory , "imgui-frag" , ShaderStages . Fragment ) ;
this . vertexShader = factory . CreateShader ( new ShaderDescription ( ShaderStages . Vertex , vertexShaderBytes , "VS" ) ) ;
_ vertexShader = factory . CreateShader ( new ShaderDescription (
this . fragmentShader = factory . CreateShader ( new ShaderDescription ( ShaderStages . Fragment , fragmentShaderBytes , "FS" ) ) ;
ShaderStages . Vertex , vertexShaderBytes , _ gd . BackendType = = GraphicsBackend . Vulkan ? "main" : "VS" ) ) ;
_f ragmentShader = factory . CreateShader ( new ShaderDescription (
ShaderStages . Fragment , fragmentShaderBytes , _ gd . BackendType = = GraphicsBackend . Vulkan ? "main" : "FS" ) ) ;
VertexLayoutDescription [ ] vertexLayouts = new VertexLayoutDescription [ ]
VertexLayoutDescription [ ] vertexLayouts = new VertexLayoutDescription [ ]
{
{
@ -155,10 +112,10 @@ namespace ClickableTransparentOverlay
new VertexElementDescription ( "in_color" , VertexElementSemantic . Color , VertexElementFormat . Byte4_Norm ) )
new VertexElementDescription ( "in_color" , VertexElementSemantic . Color , VertexElementFormat . Byte4_Norm ) )
} ;
} ;
this . l ayout = factory . CreateResourceLayout ( new ResourceLayoutDescription (
_l ayout = factory . CreateResourceLayout ( new ResourceLayoutDescription (
new ResourceLayoutElementDescription ( "ProjectionMatrixBuffer" , ResourceKind . UniformBuffer , ShaderStages . Vertex ) ,
new ResourceLayoutElementDescription ( "ProjectionMatrixBuffer" , ResourceKind . UniformBuffer , ShaderStages . Vertex ) ,
new ResourceLayoutElementDescription ( "MainSampler" , ResourceKind . Sampler , ShaderStages . Fragment ) ) ) ;
new ResourceLayoutElementDescription ( "MainSampler" , ResourceKind . Sampler , ShaderStages . Fragment ) ) ) ;
this . textureLayout = factory . CreateResourceLayout ( new ResourceLayoutDescription (
_ textureLayout = factory . CreateResourceLayout ( new ResourceLayoutDescription (
new ResourceLayoutElementDescription ( "MainTexture" , ResourceKind . TextureReadOnly , ShaderStages . Fragment ) ) ) ;
new ResourceLayoutElementDescription ( "MainTexture" , ResourceKind . TextureReadOnly , ShaderStages . Fragment ) ) ) ;
GraphicsPipelineDescription pd = new GraphicsPipelineDescription (
GraphicsPipelineDescription pd = new GraphicsPipelineDescription (
@ -166,85 +123,96 @@ namespace ClickableTransparentOverlay
new DepthStencilStateDescription ( false , false , ComparisonKind . Always ) ,
new DepthStencilStateDescription ( false , false , ComparisonKind . Always ) ,
new RasterizerStateDescription ( FaceCullMode . None , PolygonFillMode . Solid , FrontFace . Clockwise , false , true ) ,
new RasterizerStateDescription ( FaceCullMode . None , PolygonFillMode . Solid , FrontFace . Clockwise , false , true ) ,
PrimitiveTopology . TriangleList ,
PrimitiveTopology . TriangleList ,
new ShaderSetDescription ( vertexLayouts , new [ ] { this . vertexShader , this . fragmentShader } ) ,
new ShaderSetDescription ( vertexLayouts , new [ ] { _ vertexShader , _f ragmentShader } ) ,
new ResourceLayout [ ] { this . layout , this . textureLayout } ,
new ResourceLayout [ ] { _l ayout , _ textureLayout } ,
outputDescription ) ;
outputDescription ,
this . pipeline = factory . CreateGraphicsPipeline ( ref pd ) ;
ResourceBindingModel . Default ) ;
_ pipeline = factory . CreateGraphicsPipeline ( ref pd ) ;
this . mainResourceSet = factory . CreateResourceSet ( new ResourceSetDescription (
_ mainResourceSet = factory . CreateResourceSet ( new ResourceSetDescription ( _l ayout ,
this . layout , this . projMatrixBuffer , gd . PointSampler ) ) ;
_ projMatrixBuffer ,
gd . PointSampler ) ) ;
this . fontTextureResourceSet = factory . CreateResourceSet (
_f ontTextureResourceSet = factory . CreateResourceSet ( new ResourceSetDescription ( _ textureLayout , _f ontTextureView ) ) ;
new ResourceSetDescription ( this . textureLayout , this . fontTextureView ) ) ;
}
}
/// <summary>
/// <summary>
/// Gets or creates a handle for a texture to be drawn with ImGui.
/// Gets or creates a handle for a texture to be drawn with ImGui.
/// Pass the returned handle to Image() or ImageButton().
/// E.G. Pass the returned handle to Image() or ImageButton().
/// </summary>
/// </summary>
/// <param name="factory">
/// Resource Factory
/// </param>
/// <param name="textureView">
/// Texture View
/// </param>
/// <returns>
/// Creates ImGui Binding
/// </returns>
public IntPtr GetOrCreateImGuiBinding ( ResourceFactory factory , TextureView textureView )
public IntPtr GetOrCreateImGuiBinding ( ResourceFactory factory , TextureView textureView )
{
{
if ( ! this . setsByView . TryGetValue ( textureView , out ResourceSetInfo rsi ) )
if ( ! _ setsByView . TryGetValue ( textureView , out ResourceSetInfo rsi ) )
{
{
ResourceSet resourceSet = factory . CreateResourceSet (
ResourceSet resourceSet = factory . CreateResourceSet ( new ResourceSetDescription ( _ textureLayout , textureView ) ) ;
new ResourceSetDescription ( this . textureLayout , textureView ) ) ;
rsi = new ResourceSetInfo ( GetNextImGuiBindingID ( ) , resourceSet ) ;
rsi = new ResourceSetInfo ( this . GetNextImGuiBindingID ( ) , resourceSet ) ;
this . setsByView . Add ( textureView , rsi ) ;
_ setsByView . Add ( textureView , rsi ) ;
this . viewsById . Add ( rsi . ImGuiBinding , rsi ) ;
_ viewsById . Add ( rsi . ImGuiBinding , rsi ) ;
this . ownedResources . Add ( resourceSet ) ;
_ ownedResources . Add ( resourceSet ) ;
}
}
return rsi . ImGuiBinding ;
return rsi . ImGuiBinding ;
}
}
/// <summary>
/// Removes the texture from the resources.
/// </summary>
/// <param name="textureView">texture to remove</param>
public void RemoveImGuiBinding ( TextureView textureView )
{
if ( _ setsByView . TryGetValue ( textureView , out ResourceSetInfo rsi ) )
{
_ setsByView . Remove ( textureView ) ;
_ viewsById . Remove ( rsi . ImGuiBinding ) ;
_ ownedResources . Remove ( rsi . ResourceSet ) ;
rsi . ResourceSet . Dispose ( ) ;
}
}
/// <summary>
/// Removes the texture from the resources.
/// </summary>
/// <param name="texture">texture to remove</param>
public void RemoveImGuiBinding ( Texture texture )
{
if ( _ autoViewsByTexture . TryGetValue ( texture , out TextureView textureView ) )
{
_ autoViewsByTexture . Remove ( texture ) ;
_ ownedResources . Remove ( textureView ) ;
textureView . Dispose ( ) ;
RemoveImGuiBinding ( textureView ) ;
}
}
private IntPtr GetNextImGuiBindingID ( )
{
int newID = _l astAssignedID + + ;
return ( IntPtr ) newID ;
}
/// <summary>
/// <summary>
/// Gets or creates a handle for a texture to be drawn with ImGui.
/// Gets or creates a handle for a texture to be drawn with ImGui.
/// Pass the returned handle to Image() or ImageButton().
/// Pass the returned handle to Image() or ImageButton().
/// </summary>
/// </summary>
/// <param name="factory">
/// Resource Factory
/// </param>
/// <param name="texture">
/// Texture information
/// </param>
/// <returns>
/// Pointer to the resource
/// </returns>
public IntPtr GetOrCreateImGuiBinding ( ResourceFactory factory , Texture texture )
public IntPtr GetOrCreateImGuiBinding ( ResourceFactory factory , Texture texture )
{
{
if ( ! this . autoViewsByTexture . TryGetValue ( texture , out TextureView textureView ) )
if ( ! _ autoViewsByTexture . TryGetValue ( texture , out TextureView textureView ) )
{
{
textureView = factory . CreateTextureView ( texture ) ;
textureView = factory . CreateTextureView ( texture ) ;
this . autoViewsByTexture . Add ( texture , textureView ) ;
_ autoViewsByTexture . Add ( texture , textureView ) ;
this . ownedResources . Add ( textureView ) ;
_ ownedResources . Add ( textureView ) ;
}
}
return this . GetOrCreateImGuiBinding ( factory , textureView ) ;
return GetOrCreateImGuiBinding ( factory , textureView ) ;
}
}
/// <summary>
/// <summary>
/// Retrieves the shader texture binding for the given helper handle.
/// Retrieves the shader texture binding for the given helper handle.
/// </summary>
/// </summary>
/// <param name="imGuiBinding">
/// ImGui Binding resource pointer.
/// </param>
/// <returns>
/// Resource
/// </returns>
public ResourceSet GetImageResourceSet ( IntPtr imGuiBinding )
public ResourceSet GetImageResourceSet ( IntPtr imGuiBinding )
{
{
if ( ! this . viewsById . TryGetValue ( imGuiBinding , out ResourceSetInfo tvi ) )
if ( ! _ viewsById . TryGetValue ( imGuiBinding , out ResourceSetInfo tvi ) )
{
{
throw new InvalidOperationException ( "No registered ImGui binding with id " + imGuiBinding . ToString ( ) ) ;
throw new InvalidOperationException ( "No registered ImGui binding with id " + imGuiBinding . ToString ( ) ) ;
}
}
@ -252,52 +220,71 @@ namespace ClickableTransparentOverlay
return tvi . ResourceSet ;
return tvi . ResourceSet ;
}
}
/// <summary>
/// Clears the cache images.
/// </summary>
public void ClearCachedImageResources ( )
public void ClearCachedImageResources ( )
{
{
foreach ( IDisposable resource in this . ownedResources )
foreach ( IDisposable resource in _ ownedResources )
{
{
resource . Dispose ( ) ;
resource . Dispose ( ) ;
}
}
this . ownedResources . Clear ( ) ;
_ ownedResources . Clear ( ) ;
this . setsByView . Clear ( ) ;
_ setsByView . Clear ( ) ;
this . viewsById . Clear ( ) ;
_ viewsById . Clear ( ) ;
this . autoViewsByTexture . Clear ( ) ;
_ autoViewsByTexture . Clear ( ) ;
this . lastAssignedID = 1 0 0 ;
_l astAssignedID = 1 0 0 ;
}
private byte [ ] LoadEmbeddedShaderCode ( ResourceFactory factory , string name , ShaderStages _ )
{
switch ( factory . BackendType )
{
case GraphicsBackend . Direct3D11 :
{
string resourceName = name + ".hlsl.bytes" ;
return GetEmbeddedResourceBytes ( resourceName ) ;
}
default :
throw new NotImplementedException ( ) ;
}
}
private byte [ ] GetEmbeddedResourceBytes ( string resourceName )
{
Assembly assembly = typeof ( ImGuiController ) . Assembly ;
using ( Stream s = assembly . GetManifestResourceStream ( resourceName ) )
{
byte [ ] ret = new byte [ s . Length ] ;
s . Read ( ret , 0 , ( int ) s . Length ) ;
return ret ;
}
}
}
/// <summary>
/// <summary>
/// Recreates the device texture used to render text.
/// Recreates the device texture used to render text.
/// </summary>
/// </summary>
/// <param name="gd">
public void RecreateFontDeviceTexture ( GraphicsDevice gd )
/// Graphic Device
/// </param>
public unsafe void RecreateFontDeviceTexture ( GraphicsDevice gd )
{
{
ImGuiIOPtr io = ImGui . GetIO ( ) ;
ImGuiIOPtr io = ImGui . GetIO ( ) ;
// Build
// Build
byte * pixels ;
io . Fonts . GetTexDataAsRGBA32 (
int width , height , bytesPerPixel ;
out IntPtr pixels ,
io . Fonts . GetTexDataAsRGBA32 ( out pixels , out width , out height , out bytesPerPixel ) ;
out int width ,
out int height ,
out int bytesPerPixel ) ;
// Store our identifier
// Store our identifier
io . Fonts . SetTexID ( this . fontAtlasID ) ;
io . Fonts . SetTexID ( _f ontAtlasID ) ;
this . fontTexture = gd . ResourceFactory . CreateTexture ( TextureDescription . Texture2D (
_f ontTexture = gd . ResourceFactory . CreateTexture ( TextureDescription . Texture2D (
( uint ) width ,
( uint ) width ,
( uint ) height ,
( uint ) height ,
1 ,
1 ,
1 ,
1 ,
PixelFormat . R8_G8_B8_A8_UNorm ,
PixelFormat . R8_G8_B8_A8_UNorm ,
TextureUsage . Sampled ) ) ;
TextureUsage . Sampled ) ) ;
this . f ontTexture. Name = "ImGui.NET Font Texture" ;
_f ontTexture . Name = "ImGui.NET Font Texture" ;
gd . UpdateTexture (
gd . UpdateTexture (
this . f ontTexture,
_f ontTexture ,
( IntPtr ) pixels ,
pixels ,
( uint ) ( bytesPerPixel * width * height ) ,
( uint ) ( bytesPerPixel * width * height ) ,
0 ,
0 ,
0 ,
0 ,
@ -307,7 +294,7 @@ namespace ClickableTransparentOverlay
1 ,
1 ,
0 ,
0 ,
0 ) ;
0 ) ;
this . f ontTextureView = gd . ResourceFactory . CreateTextureView ( this . f ontTexture) ;
_f ontTextureView = gd . ResourceFactory . CreateTextureView ( _f ontTexture ) ;
io . Fonts . ClearTexData ( ) ;
io . Fonts . ClearTexData ( ) ;
}
}
@ -318,180 +305,150 @@ namespace ClickableTransparentOverlay
/// or index data has increased beyond the capacity of the existing buffers.
/// or index data has increased beyond the capacity of the existing buffers.
/// A <see cref="CommandList"/> is needed to submit drawing and resource update commands.
/// A <see cref="CommandList"/> is needed to submit drawing and resource update commands.
/// </summary>
/// </summary>
/// <param name="gd">
/// Graphic Device
/// </param>
/// <param name="cl">
/// Command List
/// </param>
public void Render ( GraphicsDevice gd , CommandList cl )
public void Render ( GraphicsDevice gd , CommandList cl )
{
{
if ( this . f rameBegun)
if ( _f rameBegun )
{
{
this . f rameBegun = false ;
_f rameBegun = false ;
ImGui . Render ( ) ;
ImGui . Render ( ) ;
this . RenderImDrawData ( ImGui . GetDrawData ( ) , gd , cl ) ;
RenderImDrawData ( ImGui . GetDrawData ( ) , gd , cl ) ;
}
}
}
}
/// <summary>
/// <summary>
/// Initilizes a new frame
/// Updates ImGui input and IO configuration state.
/// </summary>
/// </summary>
/// <param name="deltaSeconds">
public void Update ( float deltaSeconds , InputSnapshot snapshot , IntPtr handle )
/// FPS delay
/// </param>
public void InitlizeFrame ( float deltaSeconds )
{
{
if ( this . f rameBegun)
if ( _f rameBegun )
{
{
ImGui . Render ( ) ;
ImGui . Render ( ) ;
}
}
this . SetPerFrameImGuiData ( deltaSeconds ) ;
SetPerFrameImGuiData ( deltaSeconds ) ;
UpdateImGuiInput ( snapshot , handle ) ;
this . f rameBegun = true ;
_f rameBegun = true ;
ImGui . NewFrame ( ) ;
ImGui . NewFrame ( ) ;
}
}
/// <summary>
/// <summary>
/// Frees all graphics resources used by the renderer.
/// Sets per-frame data based on the associated window.
/// This is called by Update(float).
/// </summary>
/// </summary>
public void Dispose ( )
private void SetPerFrameImGuiData ( float deltaSeconds )
{
{
this . vertexBuffer . Dispose ( ) ;
ImGuiIOPtr io = ImGui . GetIO ( ) ;
this . indexBuffer . Dispose ( ) ;
io . DisplaySize = new Vector2 (
this . projMatrixBuffer . Dispose ( ) ;
_ windowWidth / _ scaleFactor . X ,
this . fontTexture . Dispose ( ) ;
_ windowHeight / _ scaleFactor . Y ) ;
this . fontTextureView . Dispose ( ) ;
io . DisplayFramebufferScale = _ scaleFactor ;
this . vertexShader . Dispose ( ) ;
io . DeltaTime = deltaSeconds ; // DeltaTime is in seconds.
this . fragmentShader . Dispose ( ) ;
this . layout . Dispose ( ) ;
this . textureLayout . Dispose ( ) ;
this . pipeline . Dispose ( ) ;
this . mainResourceSet . Dispose ( ) ;
this . fontTextureResourceSet . Dispose ( ) ;
this . ClearCachedImageResources ( ) ;
}
}
/// <summary>
private void UpdateImGuiInput ( InputSnapshot snapshot , IntPtr handle )
/// Allows ImGui to identify the Keys
/// </summary>
private static void SetKeyMappings ( )
{
{
ImGuiIOPtr io = ImGui . GetIO ( ) ;
ImGuiIOPtr io = ImGui . GetIO ( ) ;
io . KeyMap [ ( int ) ImGuiKey . Tab ] = ( int ) System . Windows . Forms . Keys . Tab ;
io . KeyMap [ ( int ) ImGuiKey . LeftArrow ] = ( int ) System . Windows . Forms . Keys . Left ;
// Determine if any of the mouse buttons were pressed during this snapshot period, even if they are no longer held.
io . KeyMap [ ( int ) ImGuiKey . RightArrow ] = ( int ) System . Windows . Forms . Keys . Right ;
bool leftPressed = false ;
io . KeyMap [ ( int ) ImGuiKey . UpArrow ] = ( int ) System . Windows . Forms . Keys . Up ;
bool middlePressed = false ;
io . KeyMap [ ( int ) ImGuiKey . DownArrow ] = ( int ) System . Windows . Forms . Keys . Down ;
bool rightPressed = false ;
io . KeyMap [ ( int ) ImGuiKey . PageUp ] = ( int ) System . Windows . Forms . Keys . PageUp ;
for ( int i = 0 ; i < snapshot . MouseEvents . Count ; i + + )
io . KeyMap [ ( int ) ImGuiKey . PageDown ] = ( int ) System . Windows . Forms . Keys . PageDown ;
{
io . KeyMap [ ( int ) ImGuiKey . Home ] = ( int ) System . Windows . Forms . Keys . Home ;
MouseEvent me = snapshot . MouseEvents [ i ] ;
io . KeyMap [ ( int ) ImGuiKey . End ] = ( int ) System . Windows . Forms . Keys . End ;
if ( me . Down )
io . KeyMap [ ( int ) ImGuiKey . Delete ] = ( int ) System . Windows . Forms . Keys . Delete ;
{
io . KeyMap [ ( int ) ImGuiKey . Backspace ] = ( int ) System . Windows . Forms . Keys . Back ;
switch ( me . MouseButton )
io . KeyMap [ ( int ) ImGuiKey . Enter ] = ( int ) System . Windows . Forms . Keys . Enter ;
{
io . KeyMap [ ( int ) ImGuiKey . Escape ] = ( int ) System . Windows . Forms . Keys . Escape ;
case MouseButton . Left :
leftPressed = true ;
// io.KeyMap[(int)ImGuiKey.COUNT] = (int)System.Windows.Forms.Keys.un ;
break ;
io . KeyMap [ ( int ) ImGuiKey . Insert ] = ( int ) System . Windows . Forms . Keys . Insert ;
case MouseButton . Middle :
io . KeyMap [ ( int ) ImGuiKey . Space ] = ( int ) System . Windows . Forms . Keys . Spac e;
middlePressed = tru e ;
io . KeyMap [ ( int ) ImGuiKey . A ] = ( int ) System . Windows . Forms . Keys . A ;
break ;
io . KeyMap [ ( int ) ImGuiKey . C ] = ( int ) System . Windows . Forms . Keys . C ;
case MouseButton . Right :
io . KeyMap [ ( int ) ImGuiKey . V ] = ( int ) System . Windows . Forms . Keys . V ;
rightPressed = true ;
io . KeyMap [ ( int ) ImGuiKey . X ] = ( int ) System . Windows . Forms . Keys . X ;
break ;
io . KeyMap [ ( int ) ImGuiKey . Y ] = ( int ) System . Windows . Forms . Keys . Y ;
}
io . KeyMap [ ( int ) ImGuiKey . Z ] = ( int ) System . Windows . Forms . Keys . Z ;
}
}
}
/// <summary>
io . MouseDown [ 0 ] = leftPressed | | snapshot . IsMouseDown ( MouseButton . Left ) ;
/// Get the Next ImGui Binding ID.
io . MouseDown [ 1 ] = rightPressed | | snapshot . IsMouseDown ( MouseButton . Right ) ;
/// </summary>
io . MouseDown [ 2 ] = middlePressed | | snapshot . IsMouseDown ( MouseButton . Middle ) ;
/// <returns>
io . MousePos = NativeMethods . GetCursorPosition ( handle ) ;
/// ImGui next binding ID.
io . MouseWheel = snapshot . WheelDelta ;
/// </returns>
private IntPtr GetNextImGuiBindingID ( )
IReadOnlyList < char > keyCharPresses = snapshot . KeyCharPresses ;
for ( int i = 0 ; i < keyCharPresses . Count ; i + + )
{
{
int newID = this . lastAssignedID + + ;
char c = keyCharPresses [ i ] ;
return ( IntPtr ) newID ;
io . AddInputCharacter ( c ) ;
}
}
/// <summary>
IReadOnlyList < KeyEvent > keyEvents = snapshot . KeyEvents ;
/// Loading Shader Code
for ( int i = 0 ; i < keyEvents . Count ; i + + )
/// </summary>
/// <param name="factory">
/// Resource Factory
/// </param>
/// <param name="name">
/// Shader file name
/// </param>
/// <param name="stage">
/// Shader stage
/// </param>
/// <returns>
/// Returns shader byte code
/// </returns>
private byte [ ] LoadEmbeddedShaderCode ( ResourceFactory factory , string name , ShaderStages stage )
{
{
switch ( factory . BackendType )
KeyEvent keyEvent = keyEvents [ i ] ;
io . KeysDown [ ( int ) keyEvent . Key ] = keyEvent . Down ;
if ( keyEvent . Key = = Key . ControlLeft )
{
{
case GraphicsBackend . Direct3D11 :
_ controlDown = keyEvent . Down ;
string resourceName = name + ".hlsl.bytes" ;
}
return this . GetEmbeddedResourceBytes ( resourceName ) ;
if ( keyEvent . Key = = Key . ShiftLeft )
default :
{
throw new NotImplementedException ( ) ;
_ shiftDown = keyEvent . Down ;
}
if ( keyEvent . Key = = Key . AltLeft )
{
_ altDown = keyEvent . Down ;
}
if ( keyEvent . Key = = Key . WinLeft )
{
_ winKeyDown = keyEvent . Down ;
}
}
}
}
/// <summary>
io . KeyCtrl = _ controlDown ;
/// Get embedded resource file in bytes
io . KeyAlt = _ altDown ;
/// </summary>
io . KeyShift = _ shiftDown ;
/// <param name="resourceName">
io . KeySuper = _ winKeyDown ;
/// Name of the resource file
/// </param>
if ( io . WantCaptureKeyboard | | io . WantCaptureMouse )
/// <returns>
/// Byte code of the resource file
/// </returns>
private byte [ ] GetEmbeddedResourceBytes ( string resourceName )
{
{
Assembly assembly = typeof ( ImGuiController ) . Assembly ;
NativeMethods . SetOverlayClickable ( handle , true ) ;
using ( Stream s = assembly . GetManifestResourceStream ( resourceName ) )
}
else
{
{
byte [ ] ret = new byte [ s . Length ] ;
NativeMethods . SetOverlayClickable ( handle , false ) ;
s . Read ( ret , 0 , ( int ) s . Length ) ;
return ret ;
}
}
}
}
/// <summary>
private static void SetKeyMappings ( )
/// Sets per-frame data based on the associated window.
/// This is called by Update(float).
/// </summary>
/// <param name="deltaSeconds">
/// FPS delay
/// </param>
private void SetPerFrameImGuiData ( float deltaSeconds )
{
{
ImGuiIOPtr io = ImGui . GetIO ( ) ;
ImGuiIOPtr io = ImGui . GetIO ( ) ;
io . DisplaySize = new Vector2 (
io . KeyMap [ ( int ) ImGuiKey . Tab ] = ( int ) Key . Tab ;
this . windowWidth / this . scaleFactor . X ,
io . KeyMap [ ( int ) ImGuiKey . LeftArrow ] = ( int ) Key . Left ;
this . windowHeight / this . scaleFactor . Y ) ;
io . KeyMap [ ( int ) ImGuiKey . RightArrow ] = ( int ) Key . Right ;
io . DisplayFramebufferScale = this . scaleFactor ;
io . KeyMap [ ( int ) ImGuiKey . UpArrow ] = ( int ) Key . Up ;
io . DeltaTime = deltaSeconds ; // DeltaTime is in seconds.
io . KeyMap [ ( int ) ImGuiKey . DownArrow ] = ( int ) Key . Down ;
io . KeyMap [ ( int ) ImGuiKey . PageUp ] = ( int ) Key . PageUp ;
io . KeyMap [ ( int ) ImGuiKey . PageDown ] = ( int ) Key . PageDown ;
io . KeyMap [ ( int ) ImGuiKey . Home ] = ( int ) Key . Home ;
io . KeyMap [ ( int ) ImGuiKey . End ] = ( int ) Key . End ;
io . KeyMap [ ( int ) ImGuiKey . Delete ] = ( int ) Key . Delete ;
io . KeyMap [ ( int ) ImGuiKey . Backspace ] = ( int ) Key . BackSpace ;
io . KeyMap [ ( int ) ImGuiKey . Enter ] = ( int ) Key . Enter ;
io . KeyMap [ ( int ) ImGuiKey . Escape ] = ( int ) Key . Escape ;
io . KeyMap [ ( int ) ImGuiKey . A ] = ( int ) Key . A ;
io . KeyMap [ ( int ) ImGuiKey . C ] = ( int ) Key . C ;
io . KeyMap [ ( int ) ImGuiKey . V ] = ( int ) Key . V ;
io . KeyMap [ ( int ) ImGuiKey . X ] = ( int ) Key . X ;
io . KeyMap [ ( int ) ImGuiKey . Y ] = ( int ) Key . Y ;
io . KeyMap [ ( int ) ImGuiKey . Z ] = ( int ) Key . Z ;
}
}
/// <summary>
/// Draw the ImGui graphic data
/// </summary>
/// <param name="draw_data">
/// ImGui data to draw
/// </param>
/// <param name="gd">
/// Graphic Device
/// </param>
/// <param name="cl">
/// Command List
/// </param>
private void RenderImDrawData ( ImDrawDataPtr draw_data , GraphicsDevice gd , CommandList cl )
private void RenderImDrawData ( ImDrawDataPtr draw_data , GraphicsDevice gd , CommandList cl )
{
{
uint vertexOffsetInVertices = 0 ;
uint vertexOffsetInVertices = 0 ;
@ -503,19 +460,17 @@ namespace ClickableTransparentOverlay
}
}
uint totalVBSize = ( uint ) ( draw_data . TotalVtxCount * Unsafe . SizeOf < ImDrawVert > ( ) ) ;
uint totalVBSize = ( uint ) ( draw_data . TotalVtxCount * Unsafe . SizeOf < ImDrawVert > ( ) ) ;
if ( totalVBSize > this . vertexBuffer . SizeInBytes )
if ( totalVBSize > _ vertexBuffer . SizeInBytes )
{
{
gd . DisposeWhenIdle ( this . vertexBuffer ) ;
_ vertexBuffer . Dispose ( ) ;
this . vertexBuffer = gd . ResourceFactory . CreateBuffer ( new BufferDescription (
_ vertexBuffer = gd . ResourceFactory . CreateBuffer ( new BufferDescription ( ( uint ) ( totalVBSize * 1.5f ) , BufferUsage . VertexBuffer | BufferUsage . Dynamic ) ) ;
( uint ) ( totalVBSize * 1.5f ) , BufferUsage . VertexBuffer | BufferUsage . Dynamic ) ) ;
}
}
uint totalIBSize = ( uint ) ( draw_data . TotalIdxCount * sizeof ( ushort ) ) ;
uint totalIBSize = ( uint ) ( draw_data . TotalIdxCount * sizeof ( ushort ) ) ;
if ( totalIBSize > this . indexBuffer . SizeInBytes )
if ( totalIBSize > _ indexBuffer . SizeInBytes )
{
{
gd . DisposeWhenIdle ( this . indexBuffer ) ;
_ indexBuffer . Dispose ( ) ;
this . indexBuffer = gd . ResourceFactory . CreateBuffer ( new BufferDescription (
_ indexBuffer = gd . ResourceFactory . CreateBuffer ( new BufferDescription ( ( uint ) ( totalIBSize * 1.5f ) , BufferUsage . IndexBuffer | BufferUsage . Dynamic ) ) ;
( uint ) ( totalIBSize * 1.5f ) , BufferUsage . IndexBuffer | BufferUsage . Dynamic ) ) ;
}
}
for ( int i = 0 ; i < draw_data . CmdListsCount ; i + + )
for ( int i = 0 ; i < draw_data . CmdListsCount ; i + + )
@ -523,13 +478,13 @@ namespace ClickableTransparentOverlay
ImDrawListPtr cmd_list = draw_data . CmdListsRange [ i ] ;
ImDrawListPtr cmd_list = draw_data . CmdListsRange [ i ] ;
cl . UpdateBuffer (
cl . UpdateBuffer (
this . vertexBuffer ,
_ vertexBuffer ,
vertexOffsetInVertices * ( uint ) Unsafe . SizeOf < ImDrawVert > ( ) ,
vertexOffsetInVertices * ( uint ) Unsafe . SizeOf < ImDrawVert > ( ) ,
cmd_list . VtxBuffer . Data ,
cmd_list . VtxBuffer . Data ,
( uint ) ( cmd_list . VtxBuffer . Size * Unsafe . SizeOf < ImDrawVert > ( ) ) ) ;
( uint ) ( cmd_list . VtxBuffer . Size * Unsafe . SizeOf < ImDrawVert > ( ) ) ) ;
cl . UpdateBuffer (
cl . UpdateBuffer (
this . indexBuffer ,
_ indexBuffer ,
indexOffsetInElements * sizeof ( ushort ) ,
indexOffsetInElements * sizeof ( ushort ) ,
cmd_list . IdxBuffer . Data ,
cmd_list . IdxBuffer . Data ,
( uint ) ( cmd_list . IdxBuffer . Size * sizeof ( ushort ) ) ) ;
( uint ) ( cmd_list . IdxBuffer . Size * sizeof ( ushort ) ) ) ;
@ -548,12 +503,12 @@ namespace ClickableTransparentOverlay
- 1.0f ,
- 1.0f ,
1.0f ) ;
1.0f ) ;
this . gd . UpdateBuffer ( this . projMatrixBuffer , 0 , ref mvp ) ;
_ gd . UpdateBuffer ( _ projMatrixBuffer , 0 , ref mvp ) ;
cl . SetVertexBuffer ( 0 , this . vertexBuffer ) ;
cl . SetVertexBuffer ( 0 , _ vertexBuffer ) ;
cl . SetIndexBuffer ( this . indexBuffer , IndexFormat . UInt16 ) ;
cl . SetIndexBuffer ( _ indexBuffer , IndexFormat . UInt16 ) ;
cl . SetPipeline ( this . pipeline ) ;
cl . SetPipeline ( _ pipeline ) ;
cl . SetGraphicsResourceSet ( 0 , this . mainResourceSet ) ;
cl . SetGraphicsResourceSet ( 0 , _ mainResourceSet ) ;
draw_data . ScaleClipRects ( io . DisplayFramebufferScale ) ;
draw_data . ScaleClipRects ( io . DisplayFramebufferScale ) ;
@ -574,15 +529,13 @@ namespace ClickableTransparentOverlay
{
{
if ( pcmd . TextureId ! = IntPtr . Zero )
if ( pcmd . TextureId ! = IntPtr . Zero )
{
{
if ( pcmd . TextureId = = this . f ontAtlasID)
if ( pcmd . TextureId = = _f ontAtlasID )
{
{
cl . SetGraphicsResourceSet (
cl . SetGraphicsResourceSet ( 1 , _f ontTextureResourceSet ) ;
1 , this . fontTextureResourceSet ) ;
}
}
else
else
{
{
cl . SetGraphicsResourceSet (
cl . SetGraphicsResourceSet ( 1 , GetImageResourceSet ( pcmd . TextureId ) ) ;
1 , this . GetImageResourceSet ( pcmd . TextureId ) ) ;
}
}
}
}
@ -598,14 +551,30 @@ namespace ClickableTransparentOverlay
idx_offset + = ( int ) pcmd . ElemCount ;
idx_offset + = ( int ) pcmd . ElemCount ;
}
}
vtx_offset + = cmd_list . VtxBuffer . Size ;
vtx_offset + = cmd_list . VtxBuffer . Size ;
}
}
}
}
/// <summary>
/// <summary>
/// ResourceSetInfo
/// Frees all graphics resources used by the renderer.
/// </summary>
/// </summary>
public void Dispose ( )
{
_ vertexBuffer . Dispose ( ) ;
_ indexBuffer . Dispose ( ) ;
_ projMatrixBuffer . Dispose ( ) ;
_f ontTexture . Dispose ( ) ;
_f ontTextureView . Dispose ( ) ;
_ vertexShader . Dispose ( ) ;
_f ragmentShader . Dispose ( ) ;
_l ayout . Dispose ( ) ;
_ textureLayout . Dispose ( ) ;
_ pipeline . Dispose ( ) ;
_ mainResourceSet . Dispose ( ) ;
ClearCachedImageResources ( ) ;
_f ontTextureResourceSet . Dispose ( ) ;
}
private struct ResourceSetInfo
private struct ResourceSetInfo
{
{
public readonly IntPtr ImGuiBinding ;
public readonly IntPtr ImGuiBinding ;
@ -613,8 +582,8 @@ namespace ClickableTransparentOverlay
public ResourceSetInfo ( IntPtr imGuiBinding , ResourceSet resourceSet )
public ResourceSetInfo ( IntPtr imGuiBinding , ResourceSet resourceSet )
{
{
this . ImGuiBinding = imGuiBinding ;
ImGuiBinding = imGuiBinding ;
this . ResourceSet = resourceSet ;
ResourceSet = resourceSet ;
}
}
}
}
}
}