Page 1 of 1

Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 10 Oct 2022, 22:09
by echapin
Hello!
I'm currently attempting to extend the default GamepadTrigger behavior so that OnButtonPress will handle the associated event:
if (GetButton() == (GamepadButton)(e.originalKey - Noesis::Key_GamepadLeft))
    {
        InvokeActions(0);

        // need logic here to only change e.handled if actions were actually invoked in InvokeActions
        e.handled = true;
    }
However, as noted in the code comment above, I'd like to only handle the event if the associated action was actually invoked in InvokeActions. InvokeActions performs a check to only actually call invoke on the associated actions if mPreviewInvoke does not set the PreviewInvokeEventArgs to 'cancelling = true'. For example, in the case where the GamepadTrigger has a ConditionalBehavior associated with it that is not satisfied in the given context, the call to InvokeActions would not actually perform the actions associated with the trigger.

However, InvokeActions does not return anything to indicate whether the associated actions were actually performed or not, so I'm not sure the best way to know in OnButtonPress whether the actions were performed - essentially, do you have any recommendations on replicating this code from InvokeActions within OnButtonPress/my GamepadTrigger class:
PreviewInvokeEventArgs args; args.cancelling = false;
mPreviewInvoke(this, args);

    if (!args.cancelling)
    {}
Any help would be appreciated!

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 11 Oct 2022, 11:21
by sfernandez
Hello,

You can hook to the PreviewInvoke event too, so you can check if it was cancelled or not:
void GamepadTrigger::OnButtonPress(Noesis::BaseComponent*, const Noesis::KeyEventArgs& e)
{
    if (GetButton() == (GamepadButton)(e.originalKey - Noesis::Key_GamepadLeft))
    {
        bool invoked = false;
        auto OnPreviewInvoke = [&invoked](Noesis::BaseComponent*, const PreviewInvokeEventArgs& e)
        {
            invoked = !e.cancelling;
        };

        PreviewInvoke() += OnPreviewInvoke;
        InvokeActions(0);
        PreviewInvoke() -= OnPreviewInvoke;

        e.handled = invoked;
    }
}
Hope this helps.

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 11 Oct 2022, 18:32
by echapin
This worked perfectly! Thanks for the help!

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 14 Oct 2022, 17:07
by sfernandez
You're welcome, marking this as solved.

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 18 Apr 2023, 01:52
by christyjquinn
Sorry to comment on a solved thread but I'm wanting to do a similar thing and just wondered what the background was for GamepadTrigger and KeyTrigger not setting handled to true by default?

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 19 Apr 2023, 16:58
by sfernandez
We were just following the official implementation by Microsoft of KeyTrigger: https://github.com/microsoft/XamlBehavi ... Trigger.cs
And there might be situations where you want to listen to some key to fire an interactivity action (play an animation or sound for example), but still want to route the key event.

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 19 Apr 2023, 19:41
by christyjquinn
Thanks that makes sense. The actual use case we have is the same as what is in the documentation being calling a close command in response to "Cancel". In this case handled should be set to true to avoid any further bubbling, is your suggestion to do something like above by extending GamepadTrigger or is there another approach maybe using code behind?

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 20 Apr 2023, 10:51
by sfernandez
All the interactivity package comes with source code because we expect customers to fine-tune it to their needs or just use it as base to create their own required triggers, actions and behaviors.

In your case I would probably extend the GamepadTrigger with a new property to indicate if the trigger should handle the key event when it is executed (with a similar code as I proposed before). That way the trigger can be used in scenarios requiring the event to bubble up and others where you want to stop the routing.

Anyway, I'm thinking that as GamepadTrigger is a Noesis extension, we can expose that property in our implementation. Could you please create a feature request for this in our bugtracker?

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

Posted: 21 Apr 2023, 19:10
by christyjquinn