Page 1 of 1

Problem with gamepad and control focus

Posted: 20 Aug 2019, 12:49
by unvestigate

I have a very basic placeholder pause menu for the game I am developing. The layout code looks like this:
<Border Grid.Row="1" Grid.Column="1" Background="#FF474F54" BorderBrush="#FF2F3538" BorderThickness="2" CornerRadius="5">
    <StackPanel Margin="5">
        <TextBlock HorizontalAlignment="Center" Margin="0,0,0,8" FontSize="30" Text="{Binding UILanguage.Resources[TEXT_UI_PAUSED], FallbackValue='Paused'}"/>
        <Button x:Name="ResumeButton" Margin="0,0,0,4" Command="{Binding PauseResume}" Content="{Binding UILanguage.Resources[TEXT_UI_RESUME], FallbackValue='Resume'}"/>
        <Button x:Name="LoadButton" Margin="0,0,0,4" Command="{Binding PauseLoadGame}" Content="{Binding UILanguage.Resources[TEXT_UI_LOAD_GAME], FallbackValue='Load game'}"/>
        <Button x:Name="ExitButton" Command="{Binding PauseExit}" Content="{Binding UILanguage.Resources[TEXT_UI_EXIT], FallbackValue='Exit'}"/>
Previously, I had only the resume and exit buttons, ie. the load button did not exist. The gamepad input worked pretty well. I set the focus on the resume button when the pause menu appears and the user was able to move between the buttons with the gamepad D-pad. However, if the user pressed up when the focus was on the resume button the focus moved off the buttons completely. Perhaps it focused the textblock above them or something? This is my first issue, how to I restrict the focus to move only between the buttons when using gamepad input?

My second issue started occuring when I added the load button between the other two buttons. Now when I enter the pause menu the gamepad input will only move the focus between the resume and load buttons (+ the issue with moving up from the resume buttons is still there). However, the focus won't ever move down to the exit button. It can however be clicked with the mouse. How do I solve this?


Re: Problem with gamepad and control focus

Posted: 20 Aug 2019, 17:02
by sfernandez
You can restrict directional navigation in any panel by setting KeyboardNavigation.DirectionalNavigation property:
<StackPanel Margin="5" KeyboardNavigation.DirectionalNavigation="Cycle">
    <TextBlock .../>
    <Button x:Name="ResumeButton" .../>
    <Button x:Name="LoadButton" .../>
    <Button x:Name="ExitButton" .../>
About the focus not moving down to the exit button this is something that can happen if there is other control under your pause dialog that is placed by chance between load and exit buttons, so when pressing down in your gamepad (or down arrow) it will move the focus to that control instead. But this is something you will also avoid by setting the directional navigation behavior in the panel wrapping the buttons, because then the focus will be moved locally within that panel.

Re: Problem with gamepad and control focus

Posted: 20 Aug 2019, 19:59
by unvestigate
Awesome, that's exactly what I wanted. Thanks!

Re: Problem with gamepad and control focus

Posted: 20 Aug 2019, 20:30
by sfernandez
Great, marking this as solved.