imgui icon indicating copy to clipboard operation
imgui copied to clipboard

How can I keep hover/focus status after Popup menu selection using gamepad?

Open Fredrum opened this issue 2 years ago • 7 comments

Version/Branch of Dear ImGui: https://github.com/ocornut/imgui/commit/fa2b318dd6190852a6fe7ebc952b6551e93899e0

Version: fa2b318 Branch: master

Back-end/Renderer/Compiler/OS Raspberry Pi 4b Bullseye 32bit Linux raspberrypi 5.10.92-v7l+ https://github.com/ocornut/imgui/issues/1514 SMP Mon Jan 17 17:38:03 GMT 2022 armv7l GNU/Linux

Back-ends: imgui_impl_sdl.cpp + imgui_impl_opengl3.cpp Compiler: gcc (Raspbian 10.2.1-6+rpi1) 10.2.1 20210110

My Issue/Question:

Hi!

I'm not able to figure out how I can keep a Popup menu widget in Focus/Hover state after I used gamepad to switch that widget. If I use a mouse the widget just takes the Hover state from the mouse pointer which is over the widget after the popup change. But that doesn't happen when I use a gamepad to switch widget content.

This is the code I'm using, https://github.com/Fredrum/chiaki/blob/357bfb4644418856f95a801aa5de4871b1a3a1f4/rpi/src/gui.cpp#L352

I tied using the no mouse input window flag but that didn't seem to change the behaviour.

Any Ideas of how I can keep the Popup button/widget hovered/focused after selection change?

Cheers!

Fredrum avatar May 16 '22 17:05 Fredrum

I just figured out a super hacky way that I could do something which seems to work. Problem is that I have no idea what's really happening behind the curtains so not sure if or when it might break. I guess my repo is locked to a particular commit/version of Imgui so if it works it works?? Still dodgy as heck though. :D

It seems like if i 'go right' with gamepad DPadRight or keyboard RightArrowKey the correct/current popup-button gets focused/highlighted again. If I select the same item that was already selected it tries to go right if there's an opportunity so the hack breaks, but I can probably test for that case before executing the dark magic.

/// hack to highlight the popup after selection ImGuiIO &imio = ImGui::GetIO(); imio.AddKeyEvent(ImGuiKey_RightArrow, true); imio.AddKeyEvent(ImGuiKey_RightArrow, false);

EDIT: My other idea was to follow with the mouse pointer and have the mouse pointer hidden somehow but I didn't figure out how to do that.

Fredrum avatar Jul 04 '22 16:07 Fredrum

You never explained why you want to do things you want to do. Chances are there is a better solution, but we can not point you the right way if you do not explain what you are trying to achieve in the first place. https://xyproblem.info/

rokups avatar Jul 07 '22 08:07 rokups

Basically it should be pretty clear from the OP post title but here I made a quick clip to showcase the issue more clearly,

https://www.youtube.com/shorts/DeE0T-aVLNk

This is the code linked higher up in action. Remember this is using (Imgui built-in) gamepad navigation.

What I don't want: When selecting a submenu item the focus/highlight state (blue button) is not restored when I return back.

What I do want: When I return from selecting a submenu item the top-level widget should return to being highlighted/focused.

Fun fact: When the submenu item selected matches the original state of the top level widget, the highlight state stays 'correctly' focused/highlighted.

Hope that makes it more clear what I'm after!

Cheers

Fredrum avatar Jul 10 '22 02:07 Fredrum

I was trying to create a asset browser where I can click on assets and right click and delete or rename them. But it loses focus so it doesn't look too good.

Shadowblitz16 avatar Jul 31 '22 23:07 Shadowblitz16

Im sorry for a late reply. @Fredrum your issue is using selected option as a button string in if(ImGui::Button(select.c_str(), button_sz)). String passed to Button() is used to calculate button widget ID. When you change a setting, this ID changes and focus can not be restored to that button as from the point of view of nav system that button is gone. You need to keep button ID static in order to have focus restored properly. You could use ## separator to give a static ID in the button label. For example ImGui::Button(Format("%s###button", select.c_str()).c_str(), button_sz), which will use ##button to derive button ID while only displaying select.c_str() string in UI.

P.S. Format() is a pseudo-code for your own formatting function.

rokups avatar Aug 01 '22 09:08 rokups

Ah ok that sounds promising! Thank you very much! I will try it out when I next take a pass on that project! Cheers!

Fredrum avatar Aug 02 '22 01:08 Fredrum

I have incorrectly stated need to use ## separator. I constantly confuse them.. Library supports ## and ### separators that both serve different purposes.

  • ## is to be used in cases where multiple widgets have same text, but need to have different IDs. Everything starting with ## will not be rendered so we can have "Foo##foo" and "Foo##bar" as button labels, these buttons both will display "Foo", but have different IDs.
  • ### is used to give widget a consistent ID even when widget label changes. Only portion of the label starting with ### will be used to generate an ID. So we can have two buttons with "Foo###foo" and "Bar###foo" they will have same ID. In both cases widget ID is derived from "###foo". (Not a great example, we do not need two buttons with same ID, it just illustrates idea).

So you need to use ### (and i corrected my previous message).

Now that i have written an essay i should finally remember which should be used when. 🥳

rokups avatar Aug 02 '22 10:08 rokups