Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix imgui #50

Merged
merged 6 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/nuint-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ name: NUnit integration test

on:
push:
branches: [ master ]
pull_request:
types: [ opened, reopened, synchronize, ready_for_review ]
branches: [ master ]
workflow_dispatch:

env:
DOTNET_NOLOGO: true
DOTNET_CLI_TELEMETRY_OPTOUT: true

jobs:
build-windows:
build:
name: Windows Build
runs-on: windows-latest

Expand All @@ -20,22 +23,19 @@ jobs:
with:
fetch-depth: 0

- name: 🛠️ Setup Submodules
- name: ️ Setup Submodules
run: git submodule update --init --recursive

- name: 🛠️ Setup .NET
- name: ️ Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: 🛠️ Install dotnet dependencies
- name: 📦 Install dotnet dependencies
run: dotnet restore

- name: 🛠️ Install dotnet tools
- name: 📦 Install dotnet tools
run: dotnet tool restore

- name: 🔨 Build and Test (Release)
run: dotnet test --configuration=Release Hypercube.UnitTests/Hypercube.UnitTests.csproj

- name: 🔨 Build and Test (Debug)
- name: 🛠️ Build and Test (Debug)
run: dotnet test --configuration=Debug Hypercube.UnitTests/Hypercube.UnitTests.csproj
1 change: 1 addition & 0 deletions Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Hypercube.Client.Runtimes;
using Hypercube.Client.Runtimes.Loop;
using Hypercube.Dependencies;
using Hypercube.ImGui;
using Hypercube.Shared;
using Hypercube.Shared.Runtimes;
using Hypercube.Shared.Runtimes.Loop;
Expand Down
1 change: 1 addition & 0 deletions Hypercube.Client/Graphics/ImGui/Events/ImGuiRenderEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hypercube.EventBus.Events;
using Hypercube.ImGui;

namespace Hypercube.Client.Graphics.ImGui.Events;

Expand Down
9 changes: 0 additions & 9 deletions Hypercube.Client/Graphics/ImGui/IImGui.cs

This file was deleted.

39 changes: 24 additions & 15 deletions Hypercube.Client/Graphics/ImGui/ImGui.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Hypercube.Client.Graphics.Events;
using Hypercube.Client.Graphics.ImGui.Events;
using Hypercube.Client.Graphics.Rendering;
using Hypercube.Client.Input.Events;
using Hypercube.Dependencies;
using Hypercube.EventBus;
using Hypercube.ImGui;
using Hypercube.Input;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Runtimes.Loop.Event;
using JetBrains.Annotations;
Expand All @@ -23,48 +25,55 @@ public sealed class ImGui : IImGui, IEventSubscriber, IPostInject
public void PostInject()
{
_eventBus.Subscribe<GraphicsLibraryInitializedEvent>(this, OnGraphicsInitialized);

_eventBus.Subscribe<InputFrameEvent>(this, OnInputFrame);
_eventBus.Subscribe<UpdateFrameEvent>(this, OnUpdateFrame);
_eventBus.Subscribe<RenderDrawingEvent>(this, OnRenderDrawing);
_eventBus.Subscribe<RenderFrameEvent>(this, OnRenderFrame);

_eventBus.Subscribe<MouseButtonHandledEvent>(this, OnMouseButton);
_eventBus.Subscribe<MousePositionHandledEvent>(this, OnMousePosition);
_eventBus.Subscribe<ScrollHandledEvent>(this, OnInputScroll);
}


private void OnGraphicsInitialized(ref GraphicsLibraryInitializedEvent args)
{
_controller = ImGuiFactory.Create(_renderer.MainWindow);
_controller.OnErrorHandled += message => _logger.Error(message);

_controller.Initialize();
}

private void OnInputFrame(ref InputFrameEvent args)
{
_controller.InputFrame();
}

private void OnUpdateFrame(ref UpdateFrameEvent args)
{
_controller.Update(args.DeltaSeconds);
}

private void OnRenderDrawing(ref RenderDrawingEvent args)
private void OnRenderFrame(ref RenderFrameEvent args)
{
var ev = new ImGuiRenderEvent(this);
_eventBus.Raise(ev);

_controller.Render();
}

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

public void Text(string label)

private void OnMouseButton(ref MouseButtonHandledEvent args)
{
_controller.Text(label);
_controller.UpdateMouseButtons(args.Button, args.State is KeyState.Pressed or KeyState.Held);
}

public bool Button(string label)
private void OnMousePosition(ref MousePositionHandledEvent args)
{
return _controller.Button(label);
_controller.UpdateMousePosition(args.Position);
}

public void End()
private void OnInputScroll(ref ScrollHandledEvent args)
{
_controller.End();
_controller.UpdateMouseScroll(args.Offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ private unsafe void InitOpenGL()

GL.DebugMessageCallback(_debugProc, nint.Zero);

GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.DebugOutput);
GL.Enable(EnableCap.DebugOutputSynchronous);

GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.ClearColor(0, 0, 0, 0);

_loggerOpenGL.EngineInfo("Initialized");
_eventBus.Raise(new GraphicsLibraryInitializedEvent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public sealed partial class Renderer

private int _batchVertexIndex;
private int _batchIndexIndex; // Haha name it's fun

/// <summary>
/// Contains info about currently running batch.
/// </summary>
Expand Down Expand Up @@ -89,19 +89,36 @@ private void OnFrameRender(ref RenderFrameEvent args)
Render(MainWindow);
}

public void SetupRender()
{
GL.Enable(EnableCap.Blend);

GL.BlendEquation(BlendEquationMode.FuncAdd);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

GL.Disable(EnableCap.CullFace);
GL.Disable(EnableCap.ScissorTest);
GL.Disable(EnableCap.StencilTest);
GL.Disable(EnableCap.PrimitiveRestart);

GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

GL.ClearColor(0, 0, 0, 0);
}

public void Render(WindowHandle window)
{
Clear();

GL.Viewport(window.Size);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

var args = new RenderDrawingEvent();
_eventBus.Raise(ref args);

// break batch so we get all batches
BreakCurrentBatch();

SetupRender();

_vao.Bind();
_vbo.SetData(_batchVertices);
_ebo.SetData(_batchIndices);
Expand All @@ -116,6 +133,8 @@ public void Render(WindowHandle window)
_ebo.Unbind();

_windowManager.WindowSwapBuffers(window);

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
}

public void Clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed unsafe partial class GlfwWindowManager
private ScrollCallback? _scrollCallback;
private KeyCallback? _keyCallback;
private MouseButtonCallback? _mouseButtonCallback;
private CursorPosCallback? _cursorPosCallback;

private WindowCloseCallback? _windowCloseCallback;
private WindowSizeCallback? _windowSizeCallback;
Expand All @@ -41,7 +42,8 @@ private void HandleCallbacks()
_scrollCallback = OnWindowScrollHandled;
_keyCallback = OnWindowKeyHandled;
_mouseButtonCallback = OnMouseButtonHandled;

_cursorPosCallback = OnCursorPositionHandled;

_windowCloseCallback = OnWindowClosed;
_windowSizeCallback = OnWindowResized;
_windowFocusCallback = OnWindowFocusChanged;
Expand Down Expand Up @@ -83,6 +85,11 @@ private void OnMouseButtonHandled(Window* window, GlfwMouseButton button, InputA
));
}

private void OnCursorPositionHandled(Window* window, double x, double y)
{
RaiseInput(new WindowingCursorPositionHandledEvent(x, y));
}

private void RaiseInput<T>(T args) where T : IEventArgs
{
_eventBus.Raise(_inputHandler, ref args);
Expand Down
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
14 changes: 14 additions & 0 deletions Hypercube.Client/Input/Events/MousePositionHandledEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Hypercube.EventBus.Events;
using Hypercube.Mathematics.Vectors;

namespace Hypercube.Client.Input.Events;

public sealed class MousePositionHandledEvent : IEventArgs
{
public readonly Vector2 Position;

public MousePositionHandledEvent(Vector2 position)
{
Position = position;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Hypercube.EventBus.Events;
using Hypercube.Mathematics.Vectors;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Events.Windowing;

[PublicAPI]
public sealed class WindowingCursorPositionHandledEvent : IEventArgs
{
public readonly double X;
public readonly double Y;

public Vector2 Position => new(X, Y);

public WindowingCursorPositionHandledEvent(double x, double y)
{
X = x;
Y = y;
}
}
3 changes: 3 additions & 0 deletions Hypercube.Client/Input/Handler/IInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hypercube.Client.Input.Events;
using Hypercube.EventBus;
using Hypercube.Input;
using Hypercube.Mathematics.Vectors;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Handler;
Expand All @@ -13,6 +14,8 @@ namespace Hypercube.Client.Input.Handler;
[PublicAPI]
public interface IInputHandler : IEventSubscriber
{
Vector2 MousePosition { get; }

bool IsKeyState(Key key, KeyState state);

/// <summary>
Expand Down
20 changes: 16 additions & 4 deletions Hypercube.Client/Input/Handler/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Hypercube.Dependencies;
using Hypercube.EventBus;
using Hypercube.Input;
using Hypercube.Mathematics.Vectors;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Runtimes.Loop.Event;
using JetBrains.Annotations;
Expand Down Expand Up @@ -36,12 +37,15 @@ public sealed class InputHandler : IInputHandler, IPostInject
}.ToFrozenDictionary();

private readonly Logger _logger = LoggingManager.GetLogger("input_handler");


public Vector2 MousePosition { get; private set; }

public void PostInject()
{
_eventBus.Subscribe<InputFrameEvent>(this, OnInputFrameUpdate);

_eventBus.Subscribe<WindowingCharHandledEvent>(this, OnCharHandled);
_eventBus.Subscribe<WindowingCursorPositionHandledEvent>(this, OnCursorPositionHandled);
_eventBus.Subscribe<WindowingKeyHandledEvent>(this, OnKeyHandled);
_eventBus.Subscribe<WindowingMouseButtonHandledEvent>(this, OnMouseButtonHandled);
_eventBus.Subscribe<WindowingScrollHandledEvent>(this, OnScrollHandled);
Expand Down Expand Up @@ -74,6 +78,14 @@ private void OnCharHandled(ref WindowingCharHandledEvent args)
_eventBus.Raise(ev);
}

private void OnCursorPositionHandled(ref WindowingCursorPositionHandledEvent args)
{
MousePosition = args.Position;

var ev = new MousePositionHandledEvent(args.Position);
_eventBus.Raise(ev);
}

private void OnKeyHandled(ref WindowingKeyHandledEvent args)
{
#if DEBUG
Expand All @@ -100,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 @@ -134,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 All @@ -149,7 +161,7 @@ private void OnScrollHandled(ref WindowingScrollHandledEvent args)
var ev = new ScrollHandledEvent(args.Offset);
_eventBus.Raise(ev);
}

public bool IsKeyState(Key key, KeyState state)
{
return _keys[state].Contains(key);
Expand Down
Loading
Loading