jvcleave / ofxImGui

Use ImGui in openFrameworks
300 stars 123 forks source link

Not distinguishing between left and right mouse clicks #55

Closed rjx-ray closed 5 years ago

rjx-ray commented 7 years ago

I'm trying to implement a right click context menu but ImGui is not distinguishing between left and right mouse clicks.

It seems that any mouse click fills up all the IO.MouseDown array.

You can clearly see this on the ImGui demo if you press and hold any button in the Keyboard Mouse and Focus tree, the mouse down times for all 5 buttons increment together

I've been trying to investigate this but I'm not at all familiar with the code. Any help or suggestions would be greatly appreciated

jvcleave commented 7 years ago

Assuming you are building a Desktop app here is where we are passing the button info

https://github.com/jvcleave/ofxImGui/blob/master/src/EngineGLFW.cpp#L113

You should then check which button like this

if (ImGui::GetIO().MouseClicked[1])
{
    //do something
}
rjx-ray commented 7 years ago

I'm building and running on Windows 10. I can see onMousePressed gets the right click event with button number 2, then translates it to 1 and it sets mousepressed[1] true; So I need to check button 1 for the right click But then my code is like this

        bool gotInput = (ImGui::InputText("", urlText, 1024, EnterForTrue));
        if (ImGui::BeginPopupContextItem("item context menu", 1))   {
            (ImGui::Selectable("Copy")) ;
            (ImGui::Selectable("Paste")) ;
            ImGui::EndPopup();
        }

and I get the popup for any mouse button.

Following the thread through, as soon as the field is hovered BeginPopupContextItem calls isMouseClicked which looks at IO.MouseDownDuration. With no mouse clicks all 5 values are minus 1. isMouseClicked only returns true if the requested button duration has a value of zero.

I put a break on the true return and then I see

If the left mouse button is clicked, the value for button[0] is a small positive number and all the others are zero so I get a true return for button[1] == 0

If the right mouse button is clicked, the value for button[1] is zero and all the others remain at minus 1 and I also get a true return button[1] == 0

I can see the code setting the MouseDownDurations at lines 2086 to 2089 of ImGui.cpp but I can't see why button 0 should work differently from button 1- maybe timing on when the break happens.

Anyway, I think I will make my own version of BeginPopupContextItem checking just MouseClicked

rjx-ray commented 7 years ago

Now I have this code

        if (ImGui::IsItemHovered() && ImGui::GetIO().MouseClicked[1]) {
            ImGui::OpenPopupEx("Context menu", false);

Breaking on the second line when I click the left button the MouseClicked array is [false,true,true,true,true] when I click the right button the MouseClicked array is [false,true,false, false, false]

I believe the problem is here in line 147 of gui.cpp

            // Update for next frame; set to false only if the mouse has been released
            engine->mousePressed[i] = !engine->mouseReleased;

The state of buttons that were not pressed is inverted, so they go to true

changing line 147 to

if (engine->mousePressed[i]) engine->mousePressed[i] = !engine->mouseReleased;

fixes it and my original code now works.

I can make a pull request if you like.

jvcleave commented 7 years ago

@rjx-ray https://github.com/rjx-ray/ofxImGui/commit/81c54a1499defc0f91f2d77ec7acff74fe9f36ce seems reasonable. A PR would be appreciated

rjx-ray commented 7 years ago

Hi Jason,

I’m away for 2 weeks now so it will not happen till I get back.

My fork also has the changes from issue #56

Would you want those?

From: Jason Van Cleave [mailto:notifications@github.com] Sent: 30 June 2017 17:24 To: jvcleave/ofxImGui Cc: Richard Jackson; Mention Subject: Re: [jvcleave/ofxImGui] Not distinguishing between left and right mouse clicks (#55)

@rjx-ray https://github.com/rjx-ray rjx-ray/ofxImGui@81c54a1 https://github.com/rjx-ray/ofxImGui/commit/81c54a1499defc0f91f2d77ec7acff74fe9f36ce seems reasonable. A PR would be appreciated

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jvcleave/ofxImGui/issues/55#issuecomment-312312132, or mute the thread https://github.com/notifications/unsubscribe-auth/AXvRt9lQkdCCGrvKZyhgwqcQH-c4tfQFks5sJSEGgaJpZM4OGs7W .

jvcleave commented 7 years ago

no rush here. it seems like a simple tweak if someone needs it in the meantime

as for the clipboard changes that seems to be more complicated so it would probably be best at it's own PR

rjx-ray commented 5 years ago

Fixed in current master (1.65 imgui)