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

Capture mouse wheel event #4303

Closed
raunakshrivastava7 opened this issue Jul 9, 2021 · 4 comments
Closed

Capture mouse wheel event #4303

raunakshrivastava7 opened this issue Jul 9, 2021 · 4 comments

Comments

@raunakshrivastava7
Copy link

Hi,

I want to capture the event when mouse is hovered and mouse wheel is scrolled on a rectangle in a window. Based on the mouse wheel event, I have to increment or decrement text value.

The red circled portion in the below image is the rectangle. When the user moves mouse wheel over that rectangle, I want to increment or decrement the value '1150' depending upon direction of wheel movement.

I am developing this on Windows 10 and using v1.82 of Dear ImGUI.
P.S. : I am new to c++ and ImGUI.

image

// Here's the code snippet

 draw_list->AddRectFilled(ImVec2(p.x + 6, p.y + 161), ImVec2(p.x + 64, p.y + 479), col_black);

if(ImGui::IsItemClicked() || ImGui::IsItemHovered()) 
{

    mouseFloat += ImGui::GetIO().MouseWheel;

}


std::stringstream ss;
ss << mouseFloat;
mouseString = ss.str();
const char* floatval = mouseString.c_str();

draw_list->AddText(ImVec2(p.x+44, p.y + 45), IM_COL32_WHITE, floatval); 
@PathogenDavid
Copy link
Contributor

AddRectFilled only draws a rectangle on screen, it doesn't create an item. So IsItemClicked/IsItemHovered won't work on it.

The easiest solution would probably be to add an InvisibleButton item that covers your rectangle.

There are some examples for using it this way in the examples. In particular the clipping sample and the canvas sample.

@ocornut
Copy link
Owner

ocornut commented Jul 9, 2021

In addition, note that by default mouse wheel will trigger scrolling.
To claim the mouse wheel you'll need to use SetItemUsingMouseWheel() currently declared in imgui_internal.h, you can call that function after your item. As suggested if you are drawing all elements yourself you'll probably want to use InvisibleButton(), so you would do:

InvisibleButton(...)
SetItemUsingMouseWheel(....)
if (ImGui::IsItemHovered())
{
    // use io.MouseWheel
}

(linking to #4207, #3281, #2891)

@ocornut ocornut closed this as completed Jul 9, 2021
@raunakshrivastava7
Copy link
Author

Thank you so much. It worked.

@moneyharassment
Copy link

Wouldn't it be better to use ImGui::IsMouseHoveringRect()?

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

No branches or pull requests

4 participants