lachfoy
Topic Author
Posts: 12
Joined: 20 Feb 2024, 06:25

Sound triggers for custom controls

17 May 2024, 04:15

Hi, I'm trying to hook up UI sounds to my custom controls, but I'm having a few issues.
Firstly, the screen I'm implementing looks like this: A custom tab control contains two tab items, and each tab item has a list box inside it.

The main purpose of the tab control is to implement code behind for switching tabs based on gamepad bumper input which looks like this:
void CustomTabControl::OnKeyUp(const KeyEventArgs& e)
{
	// Uses SetFocus from base TabItem. This also attempts to traverse focus to the content of the tab item.
	switch (e.key)
	{
	case Key_PageLeft: {
		CustomTabItem* nextTabItem = FindNextTabItem(GetSelectedIndex(), -1);
		nextTabItem->Focus();                // Set focus to the tab item
		e.handled = nextTabItem->SetFocus(); // Traverse focus to the tab item's selected content
		break;
	}
	case Key_PageRight: {
		CustomTabItem* nextTabItem = FindNextTabItem(GetSelectedIndex(), 1);
		nextTabItem->Focus();                // Set focus to the tab item
		e.handled = nextTabItem->SetFocus(); // Traverse focus to the tab item's selected content
		break;
	}
	default:
		break;
	}
}

In my style for the custom tab control, I've got the following code for triggering UI sounds.
<Style TargetType="{x:Type local:CustomTabControl}">
    ...
    <Setter Property="noesis:StyleInteraction.Triggers">
        <Setter.Value>
            <noesis:StyleTriggerCollection>
                <noesis:GamepadTrigger Button="PageLeft">
                    <b:PlaySoundAction Source="menutab"/>
                </noesis:GamepadTrigger>
                <noesis:GamepadTrigger Button="PageRight">
                    <b:PlaySoundAction Source="menutab"/>
                </noesis:GamepadTrigger>
            </noesis:StyleTriggerCollection>
        </Setter.Value>
    </Setter>
</Style>
 
However, I can't get it to work. I can't get KeyTriggers or GamepadTriggers to fire my PlaySoundAction. Using EventTriggers does in fact work, but I wanted to play sounds on key presses in this situation. I'm not quite sure what the expected approach is for this issue.

Another issue I'm having is that if I do try to use an EventTrigger instead, like "SelectionChanged" in the tab control, it fires when the list box's selection changes as well!! Since the list box is in the content of the tab item. So that approach doesn't really work either.
 
User avatar
sfernandez
Site Admin
Posts: 3027
Joined: 22 Dec 2011, 19:20

Re: Sound triggers for custom controls

28 May 2024, 19:09

Hello,

I don't think you need to create a custom TabControl to handle tab switching with the gamepad. Our default implementation already supports it, you can use the gamepad bumpers to switch between tabs if the focus is inside the TabControl or in the tab headers.

Regarding the playing of sounds the problem is that the TabControl is handling the KeyDown event, so it won't be received by the GamepadTrigger. You should listen to the button up, something like this:
<noesis:GamepadTrigger Button="PageLeft" FiredOn="ButtonUp" ActiveOnFocus="True">
  <b:PlaySoundAction Source="menutab"/>
</noesis:GamepadTrigger>
<noesis:GamepadTrigger Button="PageRight" FiredOn="ButtonUp" ActiveOnFocus="True">
  <b:PlaySoundAction Source="menutab"/>
</noesis:GamepadTrigger>
Could you try that? It is working fine in my tests.
 
lachfoy
Topic Author
Posts: 12
Joined: 20 Feb 2024, 06:25

Re: Sound triggers for custom controls

29 May 2024, 02:14

Hey thanks for the reply.

On the custom Tab Control, we were using the default implementation originally but I believe we wanted tabs changing on key up, as well as the option in the future to remap tabbing to triggers instead of bumpers in some situations.

For the sound triggers, your solution worked perfectly! Thanks for the simple fix.
 
User avatar
jsantos
Site Admin
Posts: 3959
Joined: 20 Jan 2012, 17:18
Contact:

Re: Sound triggers for custom controls

30 May 2024, 01:31

Thanks for the feedback. Marking this as solved.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 7 guests