ImGui up and running

This commit is contained in:
Mike
2019-02-12 20:02:04 -08:00
parent f4f9f314a0
commit ed4a035574
4 changed files with 151 additions and 2 deletions

View File

@@ -0,0 +1,78 @@
using System;
using System.Reflection;
using ImGuiNET;
using ImGuiNET.SampleProgram.XNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Nez
{
public class ImGuiFinalRenderDelegate : IFinalRenderDelegate
{
public Scene scene { get; set; }
ImGuiRenderer _imGuiRenderer;
RenderTarget2D _lastRenderTarget;
IntPtr _renderTargetId;
public ImGuiFinalRenderDelegate()
{
var core = typeof(Core).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null) as Core;
_imGuiRenderer = new ImGuiRenderer(core);
_imGuiRenderer.RebuildFontAtlas();
}
public void handleFinalRender( Color letterboxColor, RenderTarget2D source, Rectangle finalRenderDestinationRect, SamplerState samplerState )
{
if(_lastRenderTarget != source)
{
// unbind the old texture if we had one
if(_lastRenderTarget != null)
_imGuiRenderer.UnbindTexture(_renderTargetId);
// bind the new texture
_lastRenderTarget = source;
_renderTargetId = _imGuiRenderer.BindTexture(source);
}
Core.graphicsDevice.setRenderTarget( null );
Core.graphicsDevice.Clear( letterboxColor );
_imGuiRenderer.BeforeLayout(new GameTime(TimeSpan.FromDays(0), TimeSpan.FromMilliseconds(Time.deltaTime * 1000)));
layoutGui();
_imGuiRenderer.AfterLayout();
}
void layoutGui()
{
ImGui.ShowDemoWindow();
var maxSize = new System.Numerics.Vector2(_lastRenderTarget.Width, _lastRenderTarget.Height);
var minSize = maxSize / 4;
unsafe
{
ImGui.SetNextWindowSizeConstraints(minSize, maxSize, data =>
{
var size = (*data).CurrentSize;
var ratio = size.X / _lastRenderTarget.Width;
(*data).DesiredSize.Y = ratio * _lastRenderTarget.Height;
});
}
ImGui.SetNextWindowPos(new System.Numerics.Vector2(0, 0), ImGuiCond.FirstUseEver);
ImGui.Begin("Game Window");
ImGui.Image(_renderTargetId, ImGui.GetContentRegionAvail());
ImGui.End();
}
public void onAddedToScene()
{}
public void onSceneBackBufferSizeChanged( int newWidth, int newHeight )
{}
public void unload()
{}
}
}

View File

@@ -2,17 +2,19 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net46</TargetFramework>
<TargetFramework>net462</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<PlatformTarget>AnyCPU</PlatformTarget>
<AssemblyName>project_name</AssemblyName>
<MonoGamePlatform>DesktopGL</MonoGamePlatform>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<!-- Reference FNA project -->
<ItemGroup>
<ProjectReference Include="../FNA/FNA.csproj" />
<ProjectReference Include="../Nez.FNA/Nez.FNA/Nez.FNA.csproj" />
<ProjectReference Include="../ImGui.NET/src/ImGui.NET/ImGui.NET.csproj"/>
</ItemGroup>
<!-- Include the Content directory (except for .fx files, since we use .fxb at runtime) -->
@@ -66,5 +68,25 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<!-- Copy ImGui native code to output -->
<ItemGroup>
<Content Include="..\ImGui.NET\deps\cimgui\win-x86\*.*" Condition="'$(OS)' == 'Windows_NT' AND '$(Platform)' != 'x64'">
<Link>osx\%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\ImGui.NET\deps\cimgui\win-x64\*.*" Condition="'$(OS)' == 'Windows_NT' AND '$(Platform)' != 'x86'">
<Link>osx\%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\ImGui.NET\deps\cimgui\osx-x64\*.*" Condition="'$(OS)' != 'Windows_NT' AND $(IsOSX) == 'true'">
<Link>osx\%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\ImGui.NET\deps\cimgui\linux-x64\*.*" Condition="'$(OS)' != 'Windows_NT' AND $(IsLinux) == 'true'">
<Link>osx\%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>