NogginBops / ImGui.NET_OpenTK_Sample

A sample project showing an ImGui (using ImGui.NET) renderer for OpenTK in C#
106 stars 27 forks source link

Incorrect position/scale on Mac OS #27

Open KyleKrein opened 1 year ago

KyleKrein commented 1 year ago

if I try to run your code on Windows, everything works, but on Mac OS there's something wrong with position or scale (or both of them). As you can see on the screenshot, I moved cursor out of the window and only after this one of the ImGui buttons became blue.

CleanShot 2022-12-24 at 20 18 53@2x

Also if I try to move the ImGui demo window, it starts to disappear if it gets out of the left bottom 1/4 part of the window

CleanShot 2022-12-24 at 20 24 32@2x
NogginBops commented 1 year ago

My initial guess here is that this is something related to DPI and that likely the mouse coordinates might not match up with window coordinates. I don't have a macos machine that I can test with, you could try looking at the mouse position sent to ImGui and compare it to the window size that is sent to ImGui and there will likely be some discrepancy.

KyleKrein commented 1 year ago

the mouse coordinates might not match up with window coordinates

I've checked this, and the coordinates are correct. Do you have any other ideas?

Also I forgot to mention, that on start and if I move window, ImGui disappears or becomes a black square

CleanShot 2022-12-25 at 03 52 57@2x

CleanShot 2022-12-25 at 03 53 03@2x
NogginBops commented 1 year ago

That sounds pretty weird too, my guess is that some event that is called when the window moves is doing something weird. Can you check what events get triggered when you move the window?

KyleKrein commented 1 year ago

Actually there're no events happening. Or at least on Move event in GameWindow. image

KyleKrein commented 1 year ago

So I managed to make ImGui use the whole window, not 1/4 part of it. But there's still an issue with window content disappearing when I move it out of the left up corner

https://user-images.githubusercontent.com/50716293/209475541-1358f4e2-38a5-4b48-a3b7-343db6719bf6.mp4

NogginBops commented 1 year ago

That looks like an issue with scissor testing... something where the scissor area gets put too far out

tingspain commented 1 year ago

It is happening the same to me, any update on this issue?

Thanks!

tingspain commented 1 year ago

In my case, I just fixed it by modifying the scalefactor, line 36 at ImGuiController.cs . I just multiply it by 2.

Previous

...
private System.Numerics.Vector2 _scaleFactor = System.Numerics.Vector2.One;
...

After

...
private System.Numerics.Vector2 _scaleFactor = System.Numerics.Vector2.One * 2;
...
NogginBops commented 1 year ago

This seems to me like a DPS/Retina thing as the factor is 2. I'm guessing something about how OpenTK/GLFW handles width and height on retina displays differs from how it is handled on other types of displays.

Maybe it's possible to get the scale factor from GLFW directly?

tingspain commented 1 year ago

@NogginBops, indeed. I just modified:

  1. The constructor of the ImGuiController to pass the Horizontal and Vertical scale factors
  2. The OnLoad() method of the Window class to get the right Scale Factor and pass it to the ImGuiController. I also passed to the constructor the 'ContextFlags.ForwardCompatible' to make it compatible with macOS

@ imGuiController.cs

.
.
. 

        /// <summary>
        /// Constructs a new ImGuiController.
        /// </summary>
        public ImGuiController(int width, int height, float scaleFactorX=1, float scaleFactorY=1 )
        {
            _windowWidth = width;
            _windowHeight = height;

            _scaleFactor.X *=  scaleFactorX;
            _scaleFactor.Y *=  scaleFactorY;

            .
            .
            .
         }

.
.
.

@ Window.cs

  .
  .
  .
    public class Window : GameWindow
    {
        ImGuiController _controller;

        public Window() : base(GameWindowSettings.Default, 
                               new NativeWindowSettings() { 
                                    Size = new Vector2i(1600, 900), 
                                    Flags = ContextFlags.ForwardCompatible 
                               })
        { }

        protected override void OnLoad()
        {
            base.OnLoad();

            Title += ": OpenGL Version: " + GL.GetString(StringName.Version);

            // Initialise the Scale Factor
            float scaleFactorX = 0;
            float scaleFactorY = 0;

            // Get the Scale Factor of the Monitor
            this.TryGetCurrentMonitorScale(out scaleFactorX, out scaleFactorY);

            // Instanciate the ImGuiController with the right Scale Factor
            _controller = new ImGuiController(ClientSize.X, ClientSize.Y, out_width, out_height);
        }
     .
     .
     .

    }
DigitalBox98 commented 6 months ago

Patch is working fine on MacOS. I have adjusted ImGUIController in order to have a sample working on MacOS/Win10/Linux : https://github.com/DigitalBox98/Dear-ImGui-OpenTK-Sample