Skip to content

Commit

Permalink
[ImGui.Stride] Gets rid of earlier introduced hack by using same code…
Browse files Browse the repository at this point in the history
… path for notifications as normal Skia rendering uses (issue #672)
  • Loading branch information
azeno committed May 23, 2024
1 parent 8e87a5a commit 2b92219
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion VL.ImGui.Skia/src/ToSkiaLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public bool Notify(INotification notification, CallerInfo caller)
{
using (_context.MakeCurrent())
{
_io.HandleNotification(notification, useWorldSpace: true);
_io.HandleNotification(notification);

foreach (var layer in _context.Layers)
{
Expand Down
31 changes: 25 additions & 6 deletions VL.ImGui.Stride/src/ImGuiRenderer.InputHandling.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Stride.Input;
using SkiaSharp;
using Stride.Input;
using Stride.Rendering;
using System.Reactive.Disposables;
using VL.Skia;
using VL.Stride;
using CommonSpace = VL.Skia.CommonSpace;

namespace VL.ImGui
{
Expand All @@ -16,13 +19,29 @@ IDisposable SubscribeToInputSource(IInputSource inputSource, RenderDrawContext c
if (inputManager is null)
return Disposable.Empty;

// Mimic the behavior of the Skia render path
var viewport = context.RenderContext.ViewportState.Viewport0;
var viewportLayer = new InViewportUpstream();
var withinCommonSpaceLayer = new SetSpaceUpstream2();

var self = new SetNotify();
self.Update(null, (n, c) =>
{
using (_context.MakeCurrent())
{
_io.HandleNotification(n);
}
return n.Handled;
}, out _);

withinCommonSpaceLayer.Update(self, out var spaceLayer, CommonSpace.DIPTopLeft);
viewportLayer.Update(spaceLayer, SKRect.Create(viewport.X, viewport.Y, viewport.Width, viewport.Height), CommonSpace.PixelTopLeft, out _);

var callerInfo = CallerInfo.InRenderer(viewport.Width, viewport.Height, canvas: null, context: null);
return inputManager.GetNotifications(inputSource, sender: this)
.Subscribe(notification =>
.Subscribe(n =>
{
using (_context.MakeCurrent())
{
_io.HandleNotification(notification, useWorldSpace: false);
}
viewportLayer.Notify(n, callerInfo);
});
}
}
Expand Down
4 changes: 3 additions & 1 deletion VL.ImGui.Stride/src/ImGuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public partial class ImGuiRenderer : RendererBase, IDisposable
private Texture? fontTexture;

private IInputSource? lastInputSource;
private System.Numerics.Vector2 lastDisplaySize;
private readonly SerialDisposable inputSubscription = new SerialDisposable();

//VL
Expand Down Expand Up @@ -191,9 +192,10 @@ protected override void DrawCore(RenderDrawContext context)
_io.DeltaTime = (float)context.RenderContext.Time.TimePerFrame.TotalSeconds;

var inputSource = context.RenderContext.GetWindowInputSource();
if (inputSource != lastInputSource)
if (inputSource != lastInputSource || _io.DisplaySize != lastDisplaySize)
{
lastInputSource = inputSource;
lastDisplaySize = _io.DisplaySize;
inputSubscription.Disposable = SubscribeToInputSource(inputSource, context);
}

Expand Down
4 changes: 0 additions & 4 deletions VL.ImGui.Stride/src/StrideContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public IntPtr AddLayer(SkiaWidget layer, System.Numerics.Vector2 pos, System.Num
// We take ownership
managedSkiaRenderers.Add(skiaRenderer);

InViewportUpstream viewportLayer = new InViewportUpstream();
SetSpaceUpstream2 withinCommonSpaceLayer = new SetSpaceUpstream2();


skiaRenderer.Layer = layer;

var viewPort = new Viewport(pos.X, pos.Y, size.X, size.Y);
Expand Down
4 changes: 2 additions & 2 deletions VL.ImGui/src/Helper/RenderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static unsafe void UpdateUIScaling(float scaling = 1f)
//style.ScaleAllSizes(scale);
}

public static void HandleNotification(this ImGuiIOPtr _io, INotification notification, bool useWorldSpace /* HACK - breaks LayerWidget*/)
public static void HandleNotification(this ImGuiIOPtr _io, INotification notification)
{
if (notification is KeyNotification keyNotification)
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public static void HandleNotification(this ImGuiIOPtr _io, INotification notific
}

// The up & down event methods don't take the position as an argument. Therefor make sure it's present, or we end up with wrong clicks when using touch devices.
var pos = useWorldSpace ? mouseNotification.PositionInWorldSpace.FromHectoToImGui() : mouseNotification.Position.ToImGui();
var pos = mouseNotification.PositionInWorldSpace.FromHectoToImGui();
_io.AddMousePosEvent(pos.X, pos.Y);

switch (mouseNotification.Kind)
Expand Down

0 comments on commit 2b92219

Please sign in to comment.