Page 1 of 1

bug fix in mouse position on Mac OSX

Posted: Thu Apr 20, 2017 2:49 pm
by glut32
found and I fixed a bug about mouse position on Mac OSX version of Irrlicht 1.8.4.

This is the steps to reproduce :

1. Compile this code : I modify the "06.2DGraphics\main.cpp" example like : https://pastebin.com/CnupCugU
You can see the mouse cursor position

2. Launch the exe

3. Go to in fullscreen mode

4. Check the position on Left Top of the view of the window, see the mouse position you have (0,18) instead of (0,0)

5. Go to windowed mode

6. Check the position on Left Top of the view of the window, see the mouse position you have again (0,18) instead of (0,0)

remark: I think that PositionInWindow count the title bar of the window in the position


This is the bug fix : There are only two modified functions in "Irrlicht/MacOSX/CIrrDeviceMacOSX.mm"

Code: Select all

void CIrrDeviceMacOSX::postMouseEvent(void *event,irr::SEvent &ievent)
{
    bool post = true;
 
    if (Window != NULL)
    {     
        NSView* mainView = [Window contentView];
        NSPoint locationInMainView = [mainView convertPoint:[(NSEvent *)event locationInWindow] fromView:mainView];
 
       
        ievent.MouseInput.X = (int)(locationInMainView.x - mainView.bounds.origin.x);
        ievent.MouseInput.Y = (int)(mainView.bounds.size.height - locationInMainView.y  - mainView.bounds.origin.y);
 
       
        if (ievent.MouseInput.Y < 0)
            post = false;
    }
    else
    {
        CGEventRef ourEvent = CGEventCreate(NULL);
        CGPoint point = CGEventGetLocation(ourEvent);
        CFRelease(ourEvent);
 
        ievent.MouseInput.X = (int)point.x;
        ievent.MouseInput.Y = (int)point.y;
 
        if (ievent.MouseInput.Y < 0)
            post = false;
    }
 
    if (post)
        postEventFromUser(ievent);
 
    [NSApp sendEvent:(NSEvent *)event];
}
 

Code: Select all

void CIrrDeviceMacOSX::storeMouseLocation()
{
    int x,y;
 
    if (Window != NULL)
    {
        NSPoint    p;
        p = [NSEvent mouseLocation];
        p = [Window convertScreenToBase:p];
        NSView* mainView = [Window contentView];
        NSPoint locationInMainView = [mainView convertPoint:p fromView:mainView];
       
        x = (int)(locationInMainView.x - mainView.bounds.origin.x);
        y = (int)(mainView.bounds.size.height - locationInMainView.y  - mainView.bounds.origin.y);
    }
    else
    {
        CGEventRef ourEvent = CGEventCreate(NULL);
        CGPoint point = CGEventGetLocation(ourEvent);
        CFRelease(ourEvent);
 
        x = (int)point.x;
        y = (int)point.y;
 
        const core::position2di& curr = ((CCursorControl *)CursorControl)->getPosition();
        if (curr.X != x || curr.Y != y)
        {
            // In fullscreen mode, events are not sent regularly so rely on polling
            irr::SEvent ievent;
            ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
            ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
            ievent.MouseInput.X = x;
            ievent.MouseInput.Y = y;
            postEventFromUser(ievent);
        }
    }
 
    ((CCursorControl *)CursorControl)->updateInternalCursorPosition(x,y);
}
Does someone can put this fix on the svn of irrlicht? Please contact cutealien, if you want to know my real name.