Skip to content

Commit

Permalink
ImGuiの初期設定をオーバーライドできるように
Browse files Browse the repository at this point in the history
  • Loading branch information
EbiseLutica committed Nov 24, 2024
1 parent 99ab22a commit 15078a7
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions Promete.ImGui/ImGuiPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
namespace Promete.ImGui;

/// <summary>
/// ImGUI との連携を提供する Promete プラグインです。このクラスは継承できません
/// 本プラグインは、Prometeが OpenGL デスクトップバックエンドである場合にのみ使用できます。
/// ImGUI との連携を提供する Promete プラグインです。起動時のカスタマイズが必要な場合は、継承し、OnConfigureメソッドをオーバーライドしてください
/// 本プラグインは、Prometeが OpenGL デスクトップバックエンドである場合にのみ使用できます。
/// </summary>
public sealed class ImGuiPlugin
public class ImGuiPlugin
{
private readonly PrometeApp app;
private readonly PrometeApp _app;

private readonly ImGuiController controller;
private readonly IWindow window;

private ImFontPtr font;
private nint fontData;
private readonly ImGuiController _controller;
private readonly IWindow _window;

/// <summary>
/// ImGuiPlugin の新しいインスタンスを初期化します。
Expand All @@ -28,42 +25,49 @@ public sealed class ImGuiPlugin
/// <exception cref="NotSupportedException">OpenGL デスクトップバックエンドでない場合スローされます。</exception>
public ImGuiPlugin(PrometeApp app, IWindow window)
{
this.window = window;
this.app = app;
_window = window;
_app = app;
// PrometeがOpenGLバックエンドでなければ例外をスローする
if (window is not OpenGLDesktopWindow glWindow)
throw new NotSupportedException("Promete.ImGui only supports OpenGL backend.");

controller = new ImGuiController(glWindow.GL, glWindow.NativeWindow, window._RawInputContext, ConfigureImGui);
_controller = new ImGuiController(glWindow.GL, glWindow.NativeWindow, window._RawInputContext, OnConfigure);

this.window.Destroy += OnWindowDestroy;
this.window.Render += OnWindowRender;
_window.Destroy += OnWindowDestroy;
_window.Render += OnWindowRender;
}

/// <summary>
/// ウィンドウのスケーリング値と同期するかどうかを取得または設定します。
/// ウィンドウのスケーリング値と同期するかどうかを取得または設定します。
/// </summary>
public bool IsSyncronizeWithWindowScaling { get; set; }

private unsafe void ConfigureImGui()
/// <summary>
/// ImGUIの初期設定を行います。
/// </summary>
/// <param name="io"></param>
protected virtual void OnConfigure(ImGuiIOPtr io)
{
}

private unsafe void OnConfigure()
{
var io = ImGuiNET.ImGui.GetIO();
io.NativePtr->IniFilename = null;
OnConfigure(io);
}

private void OnWindowRender()
{
controller.Update(window.DeltaTime);
if (IsSyncronizeWithWindowScaling) ImGuiNET.ImGui.GetIO().FontGlobalScale = window.Scale * window.PixelRatio;
_controller.Update(_window.DeltaTime);
if (IsSyncronizeWithWindowScaling) ImGuiNET.ImGui.GetIO().FontGlobalScale = _window.Scale * _window.PixelRatio;
Render?.Invoke();
controller.Render();
_controller.Render();
}

private void OnWindowDestroy()
{
controller.Dispose();
Marshal.FreeCoTaskMem(fontData);
font.Destroy();
_controller.Dispose();
}

public event Action? Render;
Expand Down

0 comments on commit 15078a7

Please sign in to comment.