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

[VL.ImGui.Stride.Viewports] #673

Open
wants to merge 41 commits into
base: main
Choose a base branch
from

Conversation

kopffarben
Copy link
Contributor

PR Details

Viewports support for VL.Imgui.Stride

Description

This is an attempt to support ImGui viewports in VL.

It already works quite well, there are a few small things that do not work satisfactorily yet.

IssueTracker: https://github.com/kopffarben/VL.StandardLibs/issues

The previous features should all still work. Not 100 percent sure.

The problem with the Skia input should also be fixed.
I think Kairos is still working, but I'm not quite sure as I don't know exactly how it should behave.

In general, it could be cleaned up again, I'm also not completely satisfied with the naming of some classes ... but in principle it's good for now.
I'm also not quite sure if this is the best solution for some things.

I would be very happy about a code review.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

compiler Error after Cloning in ImGuiEffect.sdfx.cs
VL.ImGui.Stride was not updated in VL when edit the source
problem was different TargetFramework in csproj (net8.0-windows) and vvvv\packs\VL.ImGui.Stride\lib (net8.0-windows7.0)
so vl use allways version from vvvv-folder
remove InputManager from RenderLayerWithViewPort and get resourceHandle in MappedInputSource
now inputSource works from beginning

See MappedInputSource.Dispose
// TODO ... this will lead into 'Access violation'
//inputManager.Sources.Remove(this);
CleanUp
more GC friendly (reuse PointerPoint if possible)
known issues:
InputHandling need to be checked
Didn't work after restart F9
some bugs need to be found
maybe fixed SkiaWidget
Remove unused RenderView from RenderWidget ... is now done in vl
ClenUp
Copy link
Member

@azeno azeno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this huge effort and sorry that it took a while to give it a spin. I left some comments in code.

Was testing a little bit with your demo patch and once you start moving windows out and back in again the input handling seems to fall apart at some point. From the help patch drag out (to the right) the StrideSceneWithInput, it still works. Now move it back in, it stops interacting.

@@ -60,13 +63,14 @@ protected override void Draw(Context context, in ImDrawListPtr drawList, in Syst
public RectangleF? Bounds => !_disposed ? Layer?.Bounds : default;

// What is this for?
//SKMatrix? trans;
// it is somehow necessary for the Stride/Skia inputHandleing to work ... is a strange workaround
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm ok, can you point to a patch where these lines of code do make a difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the CallerInfo in Notify has the wrong transformation ... The CallerInfo in Render has the correct ... just set a breakpoint in Notify ... then you can see it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - so this might be a bug in the Stride-Skia bridge then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have a look..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed a fix for the SkiaRenderer - like you say, it was indeed broken, as one could see with the Stride/Skia overlay help patch. Using a Skia Button there didn't work. Thanks for the pointer. You should be able to remove that above hack now.

// 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();
_io.AddMousePosEvent(pos.X, pos.Y);
var flag = ImGui.GetIO().ConfigFlags;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember why this change was necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If viewports are enabled, ImGui expects the mouse position in DesktopSpace/ScreenSpace and no longer in WindowSpace

I derived this from the viewport example from ImGui.net

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok but the code comment says it's about the position on touch down events - not about spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was an old code comment from you ... maybe I should have put it out there
I should clean it up again

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I wrote it. The comment says that the MouseMove is necessary to allow TouchDown events to be on the right spot, because the down event itself doesn't take a position. You surrounded the whole part in an if statement, which introduces different behavior. You say it was necessary because ImGui expects the positions to be in screen space, in which case I'd argue that we need to feed the right coordinates to the move event, but not skip it entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imgui, in viewport mode, expects a mouse position in the ScreenSpace for each frame. This is set in ImGuiWindows.cs SetPerFrameImGuiData().

I can therefore also put it in an If at this point, as it is set anyway

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, that solves the puzzle. Then either add that piece of info to the if statement or get rid of it entirely and set the mouse position in each frame in existing mode.

@@ -75,6 +75,11 @@ public override void Initialize()
base.Initialize();
}

public void Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling Dispose was not an option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no longer sure about this ... I think it had a reason ... my Close just calls Destroy ... just tested briefly ... Dispose also works at least ... I'll have to take a closer look again

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows7.0</TargetFramework>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this guy ;) Still not sure about it, we have many other projects using net8.0-windows which do not need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also works without ... is probably a leftover

@kopffarben
Copy link
Contributor Author

Because the window out and in again bug, i.e. losing the input handling, I will also take another close look at it. I'm just relatively busy at the moment. But hopefully I'll get back to it tonight or in the next few days. I think it also needs another cleanup

azeno added a commit that referenced this pull request May 23, 2024
… path for notifications as normal Skia rendering uses (issue vvvv#672)
@azeno
Copy link
Member

azeno commented May 23, 2024

@kopffarben My last commit (linked above) seems to have fixed #672 as well. However there was also that boolean flag I had to add as a workaround back in April which I now managed to get rid of by using the same code path for notifications as the normal Skia render path takes (see linked commit in #672, 2b92219). I didn't merge it back to main yet because of our discussion further up which is all about that line. You said in docking branch it expects the screen space coordinates in AddMousePosEvent - but then I wonder how this will work together with current approach / approach taken in my new branch where we feed the PositionInWorldSpace which to my understanding takes those Skia viewports into account.

remove transformation workaround ... not longer needed ... fixed with 8e87a5a
@kopffarben
Copy link
Contributor Author

the AddMousePosEvent is only set if ImGui is not in viewport mode. If ImGui is in viewport mode, the screen space mouse position is set every frame in ImGuiWindows.

private void SetPerFrameImGuiData()
{
    unsafe
    { 
        int x, y;
        uint buttons = global::Stride.Graphics.SDL.Window.SDL.GetGlobalMouseState(&x, &y);
        _strideDeviceContext.IO.MouseDown[0] = (buttons & 0b0001) != 0;
        _strideDeviceContext.IO.MouseDown[1] = (buttons & 0b0010) != 0;
        _strideDeviceContext.IO.MouseDown[2] = (buttons & 0b0100) != 0;
        _strideDeviceContext.IO.MousePos = new Vector2(x, y);
    }

    _strideDeviceContext.IO.DisplaySize = new Vector2(mainViewportWindow.Size.X, mainViewportWindow.Size.Y);
    _strideDeviceContext.IO.DisplayFramebufferScale = new Vector2(1.0f, 1.0f);
    
    
    ImGui.GetPlatformIO().Viewports[0].Pos = new Vector2(mainViewportWindow.Position.X, mainViewportWindow.Position.Y);    
    ImGui.GetPlatformIO().Viewports[0].Size = new Vector2(mainViewportWindow.Size.X, mainViewportWindow.Size.Y);
}

So it should not break the current approach

@kopffarben
Copy link
Contributor Author

SO I fixed most of it for now, merged main and the bugfix. The problem with pulling the window in and out still exists.

I lose the input source or the appropriate viewport after dragging in and out a few times. Sometimes it goes faster ... but I don't have enough time right now to investigate it more closely. But I will keep at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants