Skip to content

Instantly share code, notes, and snippets.

@JSandusky
Created April 28, 2018 19:52
Show Gist options
  • Save JSandusky/11b6a6ea85d42c9ab8606378a78c50cf to your computer and use it in GitHub Desktop.
Save JSandusky/11b6a6ea85d42c9ab8606378a78c50cf to your computer and use it in GitHub Desktop.
ImGuiCLI Example
using ImGuiCLI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ImGuiCLITest
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class ImGuiViewport : Game
{
ImGuiContext imguiContext_;
GraphicsDeviceManager graphics;
bool IsFocused
{
get
{
return ((MonoGame.Framework.WinFormsGameWindow)Window).Form.Focused;
}
}
public ImGuiViewport()
{
graphics = new GraphicsDeviceManager(this);
graphics.GraphicsProfile = GraphicsProfile.HiDef;
Content.RootDirectory = "Content";
Window.Title = "MonoGame ImGui-Viewport";
Window.AllowUserResizing = true;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
public const int WM_MOUSEHWHEEL = 0x020E;
public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
public const int WM_CHAR = 0x0102;
protected override void Initialize()
{
// TODO: Add your initialization logic here
IsMouseVisible = true;
base.Initialize();
System.IntPtr device = ((SharpDX.Direct3D11.Device)GraphicsDevice.Handle).NativePointer;
System.IntPtr deviceContext = ((SharpDX.Direct3D11.DeviceContext)GraphicsDevice.ContextHandle).NativePointer;
System.IntPtr backBuffer = ((SharpDX.Direct3D11.RenderTargetView)GraphicsDevice.BackBuffer).NativePointer;
var form = ((MonoGame.Framework.WinFormsGameWindow)Window).Form;
form.SignalNativeMessages.Add(WM_MOUSEHWHEEL);
form.SignalNativeMessages.Add(WM_KEYDOWN);
form.SignalNativeMessages.Add(WM_KEYUP);
form.SignalNativeMessages.Add(WM_CHAR);
form.NotifyMessage += (o, e) =>
{
if (e.Msg == WM_KEYDOWN)
ImGuiIO.SetKeyState(e.WParam.ToInt32(), true);
else if (e.Msg == WM_KEYUP)
ImGuiIO.SetKeyState(e.WParam.ToInt32(), false);
else if (e.Msg == WM_CHAR && e.WParam.ToInt64() > 0 && e.WParam.ToInt64() < 0x10000)
ImGuiIO.AddText((ushort)e.WParam.ToInt64());
};
imguiContext_ = new ImGuiContext(this.Window.Handle, device, deviceContext, backBuffer);
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
imguiContext_.Shutdown();
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
///
static int scrollValue = 0;
static bool someValue = false;
static MouseCursor lastCursor = MouseCursor.Arrow;
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.Viewport = new Viewport(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height, 0, 1000);
float td = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
ImGuiIO.DeltaTime = td;
var mousePos = Mouse.GetState().Position;
int newScroll = Mouse.GetState().ScrollWheelValue;
int scrollDelta = newScroll - scrollValue;
scrollValue = newScroll;
if (IsFocused)
ImGuiIO.SetMouseWheel(scrollDelta / 120.0f);
ImGuiIO.SetMouseButton(0, Mouse.GetState().LeftButton == ButtonState.Pressed);
ImGuiIO.SetMouseButton(1, Mouse.GetState().RightButton == ButtonState.Pressed);
ImGuiIO.SetMouseButton(2, Mouse.GetState().MiddleButton == ButtonState.Pressed);
switch (ImGuiIO.MouseCursor)
{
case ImGuiMouseCursor_.None:
break;
case ImGuiMouseCursor_.Arrow:
if (lastCursor != MouseCursor.Arrow)
Mouse.SetCursor(MouseCursor.Arrow);
lastCursor = MouseCursor.Arrow;
break;
case ImGuiMouseCursor_.ResizeAll:
Mouse.SetCursor(MouseCursor.SizeAll);
lastCursor = MouseCursor.SizeAll;
break;
case ImGuiMouseCursor_.ResizeEW:
Mouse.SetCursor(MouseCursor.SizeWE);
lastCursor = MouseCursor.SizeWE;
break;
case ImGuiMouseCursor_.ResizeNS:
Mouse.SetCursor(MouseCursor.SizeNS);
lastCursor = MouseCursor.SizeNS;
break;
case ImGuiMouseCursor_.ResizeNESW:
Mouse.SetCursor(MouseCursor.SizeNESW);
lastCursor = MouseCursor.SizeNESW;
break;
case ImGuiMouseCursor_.ResizeNWSE:
Mouse.SetCursor(MouseCursor.SizeNWSE);
lastCursor = MouseCursor.SizeNWSE;
break;
case ImGuiMouseCursor_.TextInput:
Mouse.SetCursor(MouseCursor.IBeam);
lastCursor = MouseCursor.IBeam;
break;
}
imguiContext_.NewFrame(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ImGuiDock.RootDock(new Vector2(Window.ClientBounds.Left, Window.ClientBounds.Top), new Vector2(Window.ClientBounds.Width, Window.ClientBounds.Height));
if (ImGuiDock.BeginDock("Trial window #1"))
{
ImGuiCli.Text("Drawing some text");
ImGuiCli.Checkbox("Checkbox", ref someValue);
}
ImGuiDock.EndDock();
if (ImGuiDock.BeginDock("Trial window #2")) { ImGuiCli.Text("Drawing some text"); ImGuiCli.Checkbox("Checkbox", ref someValue); }
ImGuiDock.EndDock();
if (ImGuiDock.BeginDock("Trial window #3")) { ImGuiCli.Text("Drawing some text"); ImGuiCli.Checkbox("Checkbox", ref someValue); }
ImGuiDock.EndDock();
if (ImGuiDock.BeginDock("Trial window #4")) { ImGuiCli.Text("Drawing some text"); ImGuiCli.Checkbox("Checkbox", ref someValue); }
ImGuiDock.EndDock();
imguiContext_.RenderAndDraw(((SharpDX.Direct3D11.RenderTargetView)GraphicsDevice.BackBuffer).NativePointer);
GraphicsDevice.Textures[0] = null;
GraphicsDevice.SetRenderTarget(null);
// TODO: Add your drawing code here
Color[] Colors = new Color[]
{
new Color(30, 200, 30, 255),
new Color(100, 200, 50, 255),
new Color(80, 200, 30, 255),
new Color(30, 200, 30, 255),
new Color(80, 200, 80, 255),
};
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment