Page 1 of 1

Delay before response input, Button transition with UI Button Key Trigger

Posted: Tue Mar 31, 2020 10:01 am
by ProvencalG
Hi,

I have two issues at the moment.

First I want to set a delay before being able to respond, but still after the responses shows up.

I know that this request was already brought up here. For the same reason, I don't want players inadvertently spam the buttons too much and skip dialogs, send unwanted responses... Which is I think an issue most people should care about.
But the script "ResponseMenuInputDelay.cs" does not work in my case, it disable events forever, even when I set a very little time. I don't know why the re-enabling does not happen. And because I use UI Button Key Triggers, those are not disabled anyway, like you precised in the other topic.
How can I set up a delay that works and that also disable UI Button Key Triggers on all my buttons?
________________

Secondly, I had an issue with the new input system which I posted about there and your help was really great!

You told me to use UI Button Key Triggers to link my registered inputs to the UI buttons. It works.
But the button transitions does not happen, which is an issue because they lack a needed visual feedback. They are still happening when the buttons are clicked, but not when the key is pressed.
How do I trigger button transitions with the mouse and the keys?

Thanks.

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Tue Mar 31, 2020 10:43 am
by Tony Li
Hi,
ProvencalG wrote: Tue Mar 31, 2020 10:01 amFirst I want to set a delay before being able to respond, but still after the responses shows up.
Customization like that will require a little scripting. Since you're using the new input system, you could always disable the controls for a duration. Also disable the canvas's graphic raycaster to prevent mouse input. Example:

Code: Select all

void OnConversationResponseMenu(Response[] responses)
 {
     SetInput(false); // Disable input as soon as response menu starts.
     Invoke("EnableInput", 1); // Re-enable it after 1 second.
 }
 
 void EnableInput()
 {
     SetInput(true);
 }
 
 void SetInput(bool value)
 {
     FindObjectOfType<StandardInputModule>().enabled = value;
     DialogueManager.instance.GetComponentInChildren<GraphicRaycaster>().enabled = value;
 }
ProvencalG wrote: Tue Mar 31, 2020 10:01 amSecondly, I had an issue with the new input system which I posted about there and your help was really great!

You told me to use UI Button Key Triggers to link my registered inputs to the UI buttons. It works.
But the button transitions does not happen, which is an issue because they lack a needed visual feedback. They are still happening when the buttons are clicked, but not when the key is pressed.
How do I trigger button transitions with the mouse and the keys?
You'll need to make a custom version of UIButtonKeyTrigger. Copy Assets/Plugins/Pixel Crushers/Common/Scripts/UI/UIButtonKeyTrigger.cs. Change the Update method to something like:

Code: Select all

private void Update()
{
    if (InputDeviceManager.IsKeyDown(key) || 
        (!string.IsNullOrEmpty(buttonName) && InputDeviceManager.IsButtonDown(buttonName)) ||
        (anyKeyOrButton && InputDeviceManager.IsAnyKeyDown()))
    {
        if (skipIfBeingClickedBySubmit && IsBeingClickedBySubmit()) return;
        StartCoroutine(SimulateButtonClick());
    }
}

IEnumerator SimulateButtonClick()
{
    m_selectable.Select();
    yield return new WaitForSeconds(0.1f); // Show the button in the pressed position for 0.1 seconds.
    m_selectable.OnDeselect(null);
    ExecuteEvents.Execute(m_selectable.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler);
}
Note: To keep the code shorter, I omitted 'using' statements.

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Wed Apr 01, 2020 10:40 am
by ProvencalG
Thank you for your help.

For the first issue, the script does not work. It does not disable inputs, for mouse or keys.
And is

Code: Select all

FindObjectOfType<StandardInputModule>().enabled = value;
supposed to be

Code: Select all

FindObjectOfType<StandaloneInputModule>().enabled = value;
?
I wrote it StandaloneInputModule so it could be found. But still it does not do anything.
What is weird is that it disable the inputs once (it does not work but the method "SetInput(false)" is executed), but it enables them twice (EnableInput(), and therefore SetInput(true) is called two times).

_________________

For the second issue. The custom UI Button Key Trigger does work. But it shows the button in the selected state and not the pressed state.

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Wed Apr 01, 2020 12:26 pm
by Tony Li
For the second issue, try m_selectable.OnPointerDown() instead of m_selectable.Select().

For the first issue, yes, sorry, that was a typo. It's StandaloneInputModule. Make sure you add it to the Dialogue Manager. But I forgot that you also asked about UI Button Key Trigger components. For those, you could try add this to the SetInput() method:

Code: Select all

void SetInput(bool value)
{
    FindObjectOfType<StandaloneInputModule>().enabled = value;
    DialogueManager.instance.GetComponentInChildren<GraphicRaycaster>().enabled = value;
    foreach (var trigger in FindObjectsOfType<UIButtonKeyTrigger>())
    {
        trigger.enabled = value;
    }
}
BTW, SetInput(true) should only be called once. I don't see anything in the code that would call it twice.

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Thu Apr 02, 2020 11:41 am
by ProvencalG
Yes I put the script on the Dialogue Manager. When I change it to m_selectable.OnPointerDown() it's telling me this.
Image
I guess it's much more complicated than I thought.

For the first issue I wonder if it's not better to deactivate the buttons directly. I want the player to not be able to spam or misclick and miss the text. But I don't want to deactivate unrelated inputs and interactions.

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Thu Apr 02, 2020 1:44 pm
by Tony Li
Use:

Code: Select all

m_selectable.OnPointerDown(new PointerEventData(EventSystem.current)); 
Here's a full example:

DS_ProvencalG_Example_2020-04-02.unitypackage

I updated the scripts. Here are the scripts used in the example:

BrieflyDisableResponseMenu.cs

Code: Select all

using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using PixelCrushers.DialogueSystem;
using UnityEngine.UI;

// This script disables input to the response menu for 1 second.
public class BrieflyDisableResponseMenu : MonoBehaviour
{
    void OnConversationResponseMenu(Response[] responses)
    {
        StartCoroutine(DisableThenEnable());
    }

    IEnumerator DisableThenEnable()
    {
        yield return new WaitForEndOfFrame();
        SetInput(false);
        yield return new WaitForSeconds(1); // Disable input for 1 second.
        SetInput(true);
    }

    void SetInput(bool value)
    {
        var ui = DialogueManager.dialogueUI as StandardDialogueUI;
        ui.conversationUIElements.mainPanel.GetComponent<CanvasGroup>().interactable = value;
        EventSystem.current.GetComponent<StandaloneInputModule>().enabled = value;
        foreach (var trigger in FindObjectsOfType<CustomUIButtonKeyTrigger>())
        {
            trigger.enabled = value;
        }
    }
}

CustomUIButtonKeyTrigger.cs

Code: Select all

using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using PixelCrushers;

// This is a copy of UIButtonKeyTrigger that plays the OnPointerDown() transition.
[RequireComponent(typeof(UnityEngine.UI.Selectable))]
public class CustomUIButtonKeyTrigger : MonoBehaviour
{

    [Tooltip("Trigger the selectable when this key is pressed.")]
    public KeyCode key = KeyCode.None;

    [Tooltip("Trigger the selectable when this input button is pressed.")]
    public string buttonName = string.Empty;

    [Tooltip("Trigger if any key, input button, or mouse button is pressed.")]
    public bool anyKeyOrButton = false;

    [Tooltip("Ignore trigger key/button if UI button is being clicked Event System's Submit input. Prevents unintentional double clicks.")]
    public bool skipIfBeingClickedBySubmit = true;

    private UnityEngine.UI.Selectable m_selectable;

    private void Awake()
    {
        m_selectable = GetComponent<UnityEngine.UI.Selectable>();
        if (m_selectable == null) enabled = false;
    }

    private void Update()
    {
        if (InputDeviceManager.IsKeyDown(key) ||
            (!string.IsNullOrEmpty(buttonName) && InputDeviceManager.IsButtonDown(buttonName)) ||
            (anyKeyOrButton && InputDeviceManager.IsAnyKeyDown()))
        {
            if (skipIfBeingClickedBySubmit && IsBeingClickedBySubmit()) return;
            StartCoroutine(SimulateButtonClick());
        }
    }

    IEnumerator SimulateButtonClick()
    {
        m_selectable.OnPointerDown(new PointerEventData(EventSystem.current));
        yield return new WaitForSeconds(0.1f); // Show the button in the pressed position for 0.1 seconds.
        m_selectable.OnDeselect(null);
        ExecuteEvents.Execute(m_selectable.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler);
    }

    private bool IsBeingClickedBySubmit()
    {
        return EventSystem.current != null &&
            EventSystem.current.currentSelectedGameObject != m_selectable.gameObject &&
            InputDeviceManager.instance != null &&
            InputDeviceManager.IsButtonDown(InputDeviceManager.instance.submitButton);
    }

}

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Thu Apr 02, 2020 2:17 pm
by ProvencalG
Damn this works so well! Thanks a lot!

And yeah of course I shouldn't deactivate the button because it would disappear...

Do you consider updating the Dialogue System in the future, for the UI Button Key Trigger to include pressed animations?
And the input delay feature on the response buttons?

Re: Delay before response input, Button transition with UI Button Key Trigger

Posted: Thu Apr 02, 2020 2:32 pm
by Tony Li
Yes, I'll add those as options in the next version.