Skip to content

Commit

Permalink
Totaly fix ImGui
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 17, 2024
1 parent c4046d9 commit 9f973f9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ private WindowRegistration WindowSetup(Window* window, WindowCreateSettings sett
WindowSetIcons(handle, settings.WindowImages.ToList());

// Setting callbacks
GLFW.SetKeyCallback(window, _keyCallback);
GLFW.SetWindowCloseCallback(window, _windowCloseCallback);
GLFW.SetWindowSizeCallback(window, _windowSizeCallback);
GLFW.SetWindowFocusCallback(window, _windowFocusCallback);

GLFW.SetCharCallback(window, _charCallback);
GLFW.SetScrollCallback(window, _scrollCallback);
GLFW.SetKeyCallback(window, _keyCallback);
GLFW.SetMouseButtonCallback(window, _mouseButtonCallback);
GLFW.SetCursorPosCallback(window, _cursorPosCallback);

return registration;
}

Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Client/Input/Handler/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void OnKeyHandled(ref WindowingKeyHandledEvent args)

case KeyState.Released:
_keys[KeyState.Held].Remove(args.Key);
_keys[KeyState.Pressed].Add(args.Key);
_keys[KeyState.Released].Add(args.Key);
break;

default:
Expand Down Expand Up @@ -146,7 +146,7 @@ private void OnMouseButtonHandled(ref WindowingMouseButtonHandledEvent args)

case KeyState.Released:
_mouseButtons[KeyState.Held].Remove(args.Button);
_mouseButtons[KeyState.Pressed].Add(args.Button);
_mouseButtons[KeyState.Released].Add(args.Button);
break;

default:
Expand Down
5 changes: 0 additions & 5 deletions Hypercube.ImGui/Implementations/GlfwImGuiController.Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public partial class GlfwImGuiController

public void InputFrame()
{
// Clear mouse input
for (var i = 0; i < MouseButtons; i++)
{
_io.MouseDown[i] = false;
}
}

public void UpdateMousePosition(Vector2Int position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public partial class GlfwImGuiController
{
public void Render()
{
if (!_frameBegun)
return;

_frameBegun = false;

ImGuiNET.ImGui.Render();
Render(ImGuiNET.ImGui.GetDrawData());
}
Expand Down Expand Up @@ -61,10 +66,11 @@ private void Render(ImDrawDataPtr data)
CheckErrors("Setup data");
}

var project = Matrix4X4.CreateScaleY(-1) * Matrix4X4.CreateOrthographic(_io.DisplaySize, -1f, 1f);
var project = Matrix4X4.CreateOrthographicOffCenter(0f, _io.DisplaySize.X, _io.DisplaySize.Y, 0f, -1f, 1f);

_shader.Use();
_shader.SetUniform("projection", project);
_shader.SetUniform("fontTexture", 0);

CheckErrors("Projection");

Expand Down
55 changes: 14 additions & 41 deletions Hypercube.ImGui/Implementations/GlfwImGuiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public partial class GlfwImGuiController : IImGuiController, IDisposable

private int _vertexBufferSize;
private int _indexBufferSize;

private MouseButtonCallback? _mouseButtonCallback;
private ScrollCallback? _scrollCallback;
private KeyCallback? _keyCallback;
private CharCallback? _charCallback;
private bool _frameBegun;

public GlfwImGuiController(WindowHandle window)
{
Expand All @@ -54,7 +50,6 @@ public void Initialize()
ImGuiNET.ImGui.StyleColorsDark();

InitializeIO();
InitializeGlfw();
InitializeShaders();
}

Expand All @@ -70,17 +65,7 @@ public void InitializeIO()

_io.ClipboardUserData = _window;
}

public unsafe void InitializeGlfw()
{
_mouseButtonCallback = OnMouseButton;
_scrollCallback = OnScroll;
_keyCallback = OnKey;
_charCallback = OnChar;

CheckErrors("Glfw initialized");
}


public void InitializeShaders()
{
_shader = new ShaderProgram(ShaderSource.VertexShader, ShaderSource.FragmentShader);
Expand Down Expand Up @@ -119,6 +104,11 @@ public void Update(float deltaTime)
{
if (!_io.Fonts.IsBuilt())
throw new Exception("Unable to update state, without font atlas built");

if (_frameBegun)
{
ImGuiNET.ImGui.Render();
}

GLFWHelper.GetWindowSize(_window, out var size);
GLFWHelper.GetFramebufferSize(_window, out var framebufferSize);
Expand All @@ -128,7 +118,8 @@ public void Update(float deltaTime)
_io.DisplayFramebufferScale = framebufferSize / size;

_io.DeltaTime = deltaTime;


_frameBegun = true;
ImGuiNET.ImGui.NewFrame();
}

Expand Down Expand Up @@ -165,9 +156,11 @@ public void ShowDemoWindow()
public void ShowDebugInput()
{
Begin("ImGui input");

Button("a");
Text($"Mouse position");

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();
}
Expand All @@ -177,26 +170,6 @@ public void Dispose()

}

private unsafe void OnMouseButton(Window* window, MouseButton button, InputAction action, KeyModifiers mods)
{

}

private unsafe void OnScroll(Window* window, double offsetx, double offsety)
{

}

private unsafe void OnKey(Window* window, Keys key, int scancode, InputAction action, KeyModifiers mods)
{

}

private unsafe void OnChar(Window* window, uint codepoint)
{

}

private void CreateFontsTexture()
{
_io.Fonts.GetTexDataAsRGBA32(out nint pixels, out var width, out var height, out var bytesPerPixel);
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.ImGui/Implementations/ShaderSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ void main()
in vec4 Color;
in vec2 TexCoord;
uniform sampler2D texture0;
uniform sampler2D fontTexture;
void main()
{
oColor = Color * texture(texture0, TexCoord.st);
oColor = Color * texture(fontTexture, TexCoord.st);
}";
}
30 changes: 21 additions & 9 deletions Hypercube.Mathematics/Matrices/Matrix4X4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using Hypercube.Mathematics.Shapes;
using Hypercube.Mathematics.Transforms;
using Hypercube.Mathematics.Vectors;
using JetBrains.Annotations;

namespace Hypercube.Mathematics.Matrices;

[StructLayout(LayoutKind.Sequential)]
[PublicAPI, StructLayout(LayoutKind.Sequential)]
public partial struct Matrix4X4 : IEquatable<Matrix4X4>
{
/// <summary>
Expand Down Expand Up @@ -734,15 +735,26 @@ public static Matrix4X4 CreateOrthographic(Vector2 size, float zNear, float zFar

public static Matrix4X4 CreateOrthographic(float width, float height, float zNear, float zFar)
{
var result = Identity;
var range = 1.0f / (zNear - zFar);

result.Row0 = new Vector4(2.0f / width, 0, 0, 0);
result.Row1 = new Vector4(0, 2.0f / height, 0, 0);
result.Row2 = new Vector4(0, 0, range, 0);
result.Row3 = new Vector4(0, 0, range * zNear, 1);
return CreateOrthographicOffCenter(-width / 2d, width / 2f, -height / 2d, height / 2f, zNear, zFar);
}

public static Matrix4X4 CreateOrthographicOffCenter(double left, float right, double bottom, float top, float zNear, float zFar)
{
return CreateOrthographicOffCenter((float) left, right, (float) bottom, top, zNear, zFar);
}

public static Matrix4X4 CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar)
{
var v1 = (float) (1d / ((double) right - left));
var v2 = (float) (1d / ((double) top - bottom));
var zD = (float) (1d / ((double) zFar - zNear));

return result;
return new Matrix4X4(
2f * v1, 0, 0, 0,
0, 2f * v2, 0, 0,
0, 0, -2f * zD, 0,
(float) -((double) right + left) * v1, (float) -((double) top + bottom) * v2, (float) -((double) zFar + zNear) * zD, 1
);
}

/// <summary>
Expand Down

0 comments on commit 9f973f9

Please sign in to comment.