Skip to content

Commit

Permalink
Update ImGui implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 17, 2024
1 parent 9f973f9 commit 358cf91
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 113 deletions.
35 changes: 0 additions & 35 deletions Hypercube.Client/Graphics/ImGui/ImGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,4 @@ private void OnInputScroll(ref ScrollHandledEvent args)
{
_controller.UpdateMouseScroll(args.Offset);
}

public void Begin(string name)
{
_controller.Begin(name);
}

public void Text(string label)
{
_controller.Text(label);
}

public bool Button(string label)
{
return _controller.Button(label);
}

public void End()
{
_controller.End();
}

public void DockSpaceOverViewport()
{
_controller.DockSpaceOverViewport();
}

public void ShowDemoWindow()
{
_controller.ShowDemoWindow();
}

public void ShowDebugInput()
{
_controller.ShowDebugInput();
}
}
23 changes: 12 additions & 11 deletions Hypercube.Example.Client/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ public void PostInject()

private void Startup(ref RuntimeStartupEvent args)
{
for (var i = 0; i < 10; i++)
{
var x = _random.NextSingle() * 10 - 5;
var y = _random.NextSingle() * 10 - 5;

var coord = new SceneCoordinates(SceneId.Nullspace, new Vector2(x, y));
CreateEntity(coord, new RectangleShape(Vector2.One * 2f));
CreateEntity(coord, new CircleShape(1f));
}


CreateEntity(new SceneCoordinates(SceneId.Nullspace, new Vector2(0, 11)),
new RectangleShape(new Vector2(40, 1)), BodyType.Static);
CreateEntity(new SceneCoordinates(SceneId.Nullspace, new Vector2(0, -11)),
Expand All @@ -80,7 +71,17 @@ private void Startup(ref RuntimeStartupEvent args)
private void ImGuiRender(ref ImGuiRenderEvent args)
{
var imGui = args.Instance;
imGui.ShowDebugInput();

imGui.Begin("Test");
if (imGui.Button("Spawn"))
{
var x = _random.NextSingle() * 10 - 5;
var y = _random.NextSingle() * 10 - 5;
var coord = new SceneCoordinates(SceneId.Nullspace, new Vector2(x, y));
CreateEntity(coord, new RectangleShape(Vector2.One * 2f));
CreateEntity(coord, new CircleShape(1f));
}
imGui.End();
}

private void CreateEntity(SceneCoordinates coordinates, IShape shape, BodyType type = BodyType.Dynamic)
Expand Down
126 changes: 116 additions & 10 deletions Hypercube.ImGui/IImGui.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,119 @@
namespace Hypercube.ImGui;
using Hypercube.Mathematics.Vectors;
using ImGuiNET;
using JetBrains.Annotations;

namespace Hypercube.ImGui;

[PublicAPI]
public interface IImGui
{
void Begin(string name);
void Text(string label);
bool Button(string label);
void End();

void DockSpaceOverViewport();
void ShowDemoWindow();
void ShowDebugInput();
{
public void Dummy(Vector2 size)
{
ImGuiNET.ImGui.Dummy(size);
}

public bool ArrowButton(string label, ImGuiDirection direction)
{
return ImGuiNET.ImGui.ArrowButton(label, (ImGuiDir) direction);
}

public bool CheckboxFlags(string label, ref int flags, int value)
{
return ImGuiNET.ImGui.CheckboxFlags(label, ref flags, value);
}

public void Text(string label)
{
ImGuiNET.ImGui.Text(label);
}

public bool Button(string label)
{
return ImGuiNET.ImGui.Button(label);
}

public void Begin(string name)
{
ImGuiNET.ImGui.Begin(name);
}

public bool Checkbox(string label, ref bool value)
{
return ImGuiNET.ImGui.Checkbox(label, ref value);
}

public void Bullet()
{
ImGuiNET.ImGui.Bullet();
}

public void Columns()
{
ImGuiNET.ImGui.Columns();
}

public void Columns(int count)
{
ImGuiNET.ImGui.Columns(count);
}

public void Columns(int count, string id)
{
ImGuiNET.ImGui.Columns(count, id);
}

public void Columns(int count, string id, bool border)
{
ImGuiNET.ImGui.Columns(count, id, border);
}

public void Combo(string label, ref int item, string separatedItems)
{
ImGuiNET.ImGui.Combo(label, ref item, separatedItems);
}

public void Image(nint texture, Vector2 size)
{
ImGuiNET.ImGui.Image(texture, size);
}

public void Image(nint texture, Vector2 size, Vector2 uv)
{
ImGuiNET.ImGui.Image(texture, size, uv);
}

public void Separator()
{
ImGuiNET.ImGui.Separator();
}

public void Spacing()
{
ImGuiNET.ImGui.Spacing();
}

public void BeginGroup()
{
ImGuiNET.ImGui.BeginGroup();
}

public void BeginMenu(string label)
{
ImGuiNET.ImGui.BeginMenu(label);
}

public void End()
{
ImGuiNET.ImGui.End();
}

public void DockSpaceOverViewport()
{
ImGuiNET.ImGui.DockSpaceOverViewport();
}

public void ShowDemoWindow()
{
ImGuiNET.ImGui.ShowDemoWindow();
}
}
3 changes: 2 additions & 1 deletion Hypercube.ImGui/IImGuiController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Hypercube.Input;
using Hypercube.Mathematics.Vectors;
using JetBrains.Annotations;

namespace Hypercube.ImGui;

[PublicAPI]
public interface IImGuiController : IImGui
{
event Action<string>? OnErrorHandled;
Expand All @@ -15,5 +17,4 @@ public interface IImGuiController : IImGui
void UpdateMousePosition(Vector2Int position);
void UpdateMouseButtons(MouseButton button, bool state);
void UpdateMouseScroll(Vector2 offset);
void UpdateMouseCursor();
}
16 changes: 16 additions & 0 deletions Hypercube.ImGui/ImGuiDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JetBrains.Annotations;

namespace Hypercube.ImGui;

[PublicAPI]
public enum ImGuiDirection
{
None = -1,

Left = 0,
Right = 1,
Up = 2,
Down = 3,

Count = 4
}
4 changes: 3 additions & 1 deletion Hypercube.ImGui/ImGuiFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Hypercube.Graphics.Windowing;
using Hypercube.ImGui.Implementations;
using JetBrains.Annotations;

namespace Hypercube.ImGui;

[PublicAPI]
public static class ImGuiFactory
{
public static IImGuiController Create(WindowHandle window)
{
return new GlfwImGuiController(window);
return new OpenGLImGuiController(window);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Hypercube.ImGui.Implementations;

public partial class GlfwImGuiController
public partial class OpenGLImGuiController
{
private const int MouseButtons = 5;

Expand All @@ -30,9 +30,4 @@ public void UpdateMouseScroll(Vector2 offset)
_io.MouseWheelH = offset.X;
_io.MouseWheel = offset.Y;
}

public void UpdateMouseCursor()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hypercube.ImGui.Implementations;

public partial class GlfwImGuiController
public partial class OpenGLImGuiController
{
public void Render()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
using Hypercube.OpenGL.Utilities.Helpers;
using ImGuiNET;
using JetBrains.Annotations;
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenToolkit.Graphics.OpenGL4;
using static OpenTK.Windowing.GraphicsLibraryFramework.GLFWCallbacks;

namespace Hypercube.ImGui.Implementations;

[PublicAPI]
public partial class GlfwImGuiController : IImGuiController, IDisposable
public partial class OpenGLImGuiController : IImGuiController, IDisposable
{
private const int Framerate = 60;

Expand All @@ -35,7 +33,7 @@ public partial class GlfwImGuiController : IImGuiController, IDisposable
private int _indexBufferSize;
private bool _frameBegun;

public GlfwImGuiController(WindowHandle window)
public OpenGLImGuiController(WindowHandle window)
{
_window = window;
_mousePressed = new bool[(int) ImGuiMouseButton.COUNT];
Expand All @@ -46,7 +44,6 @@ public void Initialize()
{
var context = ImGuiNET.ImGui.CreateContext();
ImGuiNET.ImGui.SetCurrentContext(context);

ImGuiNET.ImGui.StyleColorsDark();

InitializeIO();
Expand Down Expand Up @@ -123,51 +120,13 @@ public void Update(float deltaTime)
ImGuiNET.ImGui.NewFrame();
}

public void Begin(string name)
{
ImGuiNET.ImGui.Begin(name);
}

public void Text(string label)
{
ImGuiNET.ImGui.Text(label);
}

public bool Button(string label)
{
return ImGuiNET.ImGui.Button(label);
}

public void End()
{
ImGuiNET.ImGui.End();
}

public void DockSpaceOverViewport()
{
ImGuiNET.ImGui.DockSpaceOverViewport();
}

public void ShowDemoWindow()
{
ImGuiNET.ImGui.ShowDemoWindow();
}

public void ShowDebugInput()
{
Begin("ImGui input");

Text($"Mouse LBM: {_io.MouseDown[0]}");
Text($"Mouse RBM: {_io.MouseDown[1]}");
Text($"Mouse position: {_io.MousePos}");
Text($"Mouse wheel: <{_io.MouseWheel}, {_io.MouseWheelH}>");

End();
}

public void Dispose()
{
_shader.Dispose();

_vao.Dispose();
_vbo.Dispose();
_ebo.Dispose();
}

private void CreateFontsTexture()
Expand Down Expand Up @@ -201,7 +160,7 @@ private void CreateFontsTexture()

private void CheckErrors(string title)
{
var errorString = GLHelper.CheckErrors($"{nameof(GlfwImGuiController)} {title}");
var errorString = GLHelper.CheckErrors($"{nameof(OpenGLImGuiController)} {title}");
if (errorString == string.Empty)
return;

Expand Down

0 comments on commit 358cf91

Please sign in to comment.