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

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.DS_Store
FNA/
ImGui.NET/
fnalibs/
project_name/bin
project_name/obj

View File

@ -62,6 +62,35 @@ function updateFNA()
fi
}
# Clones FNA from the git master branch
function downloadImGui()
{
checkGit
echo "Downloading ImGui..."
git -C $MY_DIR clone https://github.com/mellinoe/ImGui.NET.git --recursive
if [ $? -eq 0 ]; then
echo "Finished downloading!\n"
else
echo >&2 "ERROR: Unable to download successfully. Maybe try again later?"
fi
}
# Pulls FNA from the git master branch
function updateImGui()
{
checkGit
echo "Updating to the latest git version of ImGui.NET..."
git -C "$MY_DIR/ImGui.NET" pull --recurse-submodules
if [ $? -eq 0 ]; then
echo "Finished updating!\n"
else
echo >&2 "ERROR: Unable to update."
exit 1
fi
}
# Downloads and extracts prepackaged archive of native libraries ("fnalibs")
function getLibs()
{
@ -114,17 +143,36 @@ if [[ $shouldDownloadLibs =~ ^[Yy]$ ]]; then
getLibs
fi
# Dear ImGui
if [ ! -d "$MY_DIR/ImGui.NET" ]; then
read -p "Download ImGui.NET (y/n)? " shouldDownload
if [[ $shouldDownload =~ ^[Yy]$ ]]; then
downloadImGui
fi
else
read -p "Update ImGui.NET (y/n)? " shouldUpdate
if [[ $shouldUpdate =~ ^[Yy]$ ]]; then
updateImGui
fi
fi
# install t4 engine
installT4
# Rename project
# Only proceed from here if we have not yet renamed the project
if [ ! -d "$MY_DIR/project_name" ]; then
# old project_name folder already renamed so we are all done here
exit 1
fi
# copy over ImGui files before renaming the project
echo "Copying ImGui renderer to project..."
cp "$MY_DIR/ImGui.NET/src/ImGui.NET.SampleProgram.XNA/DrawVertDeclaration.cs" "$MY_DIR/project_name/ImGui"
cp "$MY_DIR/ImGui.NET/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs" "$MY_DIR/project_name/ImGui"
sed -i '' "s/cimgui/cimgui.dylib/g" ImGui.NET/src/ImGui.NET/Generated/ImGuiNative.gen.cs
read -p "Enter the project name to use for your folder and csproj file or 'exit' to quit: " newProjectName
if [[ $newProjectName = 'exit' || -z "$newProjectName" ]]; then

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>