Howto: Button with multiple hit targets


#1

I want to create a custom button with multiple click actions. For e.g., the button can have 4 actions depending on whether the user clicked on the top, bottom, left or right.

I’m looking for pointers on how I should go about this. Should I group 4 buttons and a Text widget into a group or if there is a way for the button to get the coordinate of where the click occurred then translate it to the region within the button and take the correct action. The same hit-test can also be done for hover to tell the user that there are multiple actions possible.

Any help / guidance is appreciated.
P.S. New to C++, have spent the last couple of weeks working with Dear ImGui for my tool / app while getting up to speed on Desktop GUI, C++, IMGUI …


#2

if there is a way for the button to get the coordinate of where the click occurred then translate it to the region within the button and take the correct action.

When the button returns true you can use GetItemRectMin(), GetItemRectMax() GetItemRectSize() and GetMousePos() to do that.

However if you do it your support for keyboard/gamepad control will be incorrect.

You just as well use 4 buttons stuck to each others as you mentioned, or go low level and create your own widget.


#3

Thanks for the suggestion. I had not considered the impact on Keyboard usage. For now I may be ok without having keyboard support but I would like to have everything as accessible with keyboard as possible. I’ll explore the option of sticking them together for now.