echapin
Topic Author
Posts: 7
Joined: 14 Sep 2022, 21:28

Extending GamepadTrigger::OnButtonPress to conditionally handle events

10 Oct 2022, 22:09

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!
 
User avatar
sfernandez
Site Admin
Posts: 3059
Joined: 22 Dec 2011, 19:20

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

11 Oct 2022, 11:21

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.
 
echapin
Topic Author
Posts: 7
Joined: 14 Sep 2022, 21:28

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

11 Oct 2022, 18:32

This worked perfectly! Thanks for the help!
 
User avatar
sfernandez
Site Admin
Posts: 3059
Joined: 22 Dec 2011, 19:20

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

14 Oct 2022, 17:07

You're welcome, marking this as solved.
 
christyjquinn
Posts: 17
Joined: 17 Aug 2022, 17:03

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

18 Apr 2023, 01:52

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?
 
User avatar
sfernandez
Site Admin
Posts: 3059
Joined: 22 Dec 2011, 19:20

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

19 Apr 2023, 16:58

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.
 
christyjquinn
Posts: 17
Joined: 17 Aug 2022, 17:03

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

19 Apr 2023, 19:41

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?
 
User avatar
sfernandez
Site Admin
Posts: 3059
Joined: 22 Dec 2011, 19:20

Re: Extending GamepadTrigger::OnButtonPress to conditionally handle events

20 Apr 2023, 10:51

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?
 

Who is online

Users browsing this forum: Google [Bot], Sierra and 4 guests