Skip to content

Commit

Permalink
DialogueAdvanceInput now supports Input Buttons.
Browse files Browse the repository at this point in the history
Likewise can now be configured to respond to Button/Keycode Up or Down.
Fixes #241
  • Loading branch information
McJones committed Jan 23, 2024
1 parent 010611a commit 68b30a5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `ActionsGenerator` will now generate C# warnings for non-private methods that are attributed as `YarnFunction` or `YarnCommand`.
- `ActionsGenerator` still logs to a temporary location, but now into a `dev.yarnspinner.logs` folder inside the temporary location.
- Auto-advancing `LineView`s will no longer attempt to advance dialogue that has been stopped.
- `DialogueAdvanceInput` now supports Virtual Button names in addition to KeyCodes and Input Actions
- this can be configured to work on button or keycode release or press.
- defaults to on release.

### Removed

Expand Down
20 changes: 20 additions & 0 deletions Editor/Editors/DialogueAdvanceInputEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class DialogueAdvanceInputEditor : UnityEditor.Editor
private SerializedProperty dialogueViewProperty;
private SerializedProperty continueActionTypeProperty;
private SerializedProperty continueActionKeyCodeProperty;
private SerializedProperty continueActionButtonNameProperty;
private SerializedProperty continueActionOnButtonReleaseProperty;
private SerializedProperty continueActionReferenceProperty;
private SerializedProperty continueActionProperty;
private SerializedProperty enableActionOnStartProperty;
Expand All @@ -33,6 +35,8 @@ public void OnEnable()
dialogueViewProperty = serializedObject.FindProperty(nameof(DialogueAdvanceInput.dialogueView));
continueActionTypeProperty = serializedObject.FindProperty(nameof(DialogueAdvanceInput.continueActionType));
continueActionKeyCodeProperty = serializedObject.FindProperty(nameof(DialogueAdvanceInput.continueActionKeyCode));
continueActionButtonNameProperty = serializedObject.FindProperty(nameof(DialogueAdvanceInput.continueActionButtonName));
continueActionOnButtonReleaseProperty = serializedObject.FindProperty(nameof(DialogueAdvanceInput.continueActionOnButtonRelease));

#if USE_INPUTSYSTEM && ENABLE_INPUT_SYSTEM
continueActionReferenceProperty = serializedObject.FindProperty(nameof(DialogueAdvanceInput.continueActionReference));
Expand All @@ -56,6 +60,10 @@ public override void OnInspectorGUI()
case (int)DialogueAdvanceInput.ContinueActionType.KeyCode:
DrawInputActionTypeKeycode();
break;

case (int)DialogueAdvanceInput.ContinueActionType.VirtualButton:
DrawInputActionTypeButton();
break;

case (int)DialogueAdvanceInput.ContinueActionType.InputSystemAction:
DrawInputActionTypeAction();
Expand Down Expand Up @@ -100,11 +108,23 @@ private void DrawInputActionTypeKeycode()
EditorGUI.indentLevel += 1;
#if ENABLE_LEGACY_INPUT_MANAGER
EditorGUILayout.PropertyField(continueActionKeyCodeProperty);
EditorGUILayout.PropertyField(continueActionOnButtonReleaseProperty);
#else
EditorGUILayout.HelpBox(LegacyInputSystemNotAvailableWarning, MessageType.Warning);
#endif
EditorGUI.indentLevel -= 1;
}

private void DrawInputActionTypeButton()
{
EditorGUI.indentLevel += 1;
#if ENABLE_LEGACY_INPUT_MANAGER
EditorGUILayout.PropertyField(continueActionButtonNameProperty);
EditorGUILayout.PropertyField(continueActionOnButtonReleaseProperty);
#else
EditorGUILayout.HelpBox(LegacyInputSystemNotAvailableWarning, MessageType.Warning);
#endif
EditorGUI.indentLevel -= 1;
}

private void DrawInputActionTypeNone()
Expand Down
81 changes: 74 additions & 7 deletions Runtime/Views/DialogueAdvanceInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ namespace Yarn.Unity
/// </remarks>
public class DialogueAdvanceInput : MonoBehaviour
{
/// <summary>
/// The button/keycode action type, should the button be fired on release or press?
/// </summary>
public enum ContinueButtonActionType
{
/// <summary>
/// Action will fire on key release
/// </summary>
Up,
/// <summary>
/// Action will fire on key pressed
/// </summary>
Down,
}

/// <summary>
/// The type of input that this component is listening for in order to signal that its dialogue view should advance.
/// </summary>
Expand All @@ -45,6 +60,15 @@ public enum ContinueActionType
/// </remarks>
KeyCode,

/// <summary>
/// The component is listening for a virtual button to be pressed.
/// </summary>
/// <remarks>
/// <para style="info">This input will only be used if the legacy
/// Input Manager is enabled.</para>
/// </remarks>
VirtualButton,

/// <summary>
/// The component is listening for the action configured in <see
/// cref="continueAction"/> to be performed.
Expand Down Expand Up @@ -97,6 +121,33 @@ public enum ContinueActionType
[SerializeField]
public KeyCode continueActionKeyCode = KeyCode.Space;

/// <summary>
/// The virtual button that this component is listening for.
/// </summary>
/// <remarks>
/// <para style="info">
/// This value is only used when <see cref="continueActionType"/> is
/// <see cref="ContinueActionType.VirtualButton"/>.
/// </para>
/// </remarks>
[SerializeField]
public string continueActionButtonName = "Jump";

/// <summary>
/// Should the continue action respond to key being pressed down or released.
/// </summary>
/// <remarks>
/// <para style="info">
/// Defaults to firing the advancement on key released.
/// </para>
/// <para style="info">
/// This value is only used when <see cref="continueActionType"/> is
/// <see cref="ContinueActionType.KeyCode"/> or <see cref="ContinueActionType.VirtualButton"/>.
/// </para>
/// </remarks>
[SerializeField]
public ContinueButtonActionType continueActionOnButtonRelease = ContinueButtonActionType.Up; // defaults to on release

#if USE_INPUTSYSTEM && ENABLE_INPUT_SYSTEM
/// <summary>
/// An <see cref="InputActionReference"/> that refers to the action that
Expand Down Expand Up @@ -234,17 +285,33 @@ private void UserPerformedAdvanceAction(InputAction.CallbackContext obj)
#if ENABLE_LEGACY_INPUT_MANAGER
internal void Update()
{
// We need to be configured to use a keycode to interrupt/continue
// lines.
if (continueActionType != ContinueActionType.KeyCode)
var advance = false;

if (continueActionType == ContinueActionType.KeyCode)
{
return;
if (this.continueActionOnButtonRelease == ContinueButtonActionType.Up)
{
advance = Input.GetKeyUp(continueActionKeyCode);
}
else
{
advance = Input.GetKeyDown(continueActionKeyCode);
}
}
else if (continueActionType == ContinueActionType.VirtualButton)
{
if (this.continueActionOnButtonRelease == ContinueButtonActionType.Up)
{
advance = Input.GetButtonUp(continueActionButtonName);
}
else
{
advance = Input.GetButtonDown(continueActionButtonName);
}
}

// Has the keycode been pressed this frame?
if (Input.GetKeyUp(continueActionKeyCode))
if (advance)
{
// Indicate that we want to skip/continue.
dialogueView.UserRequestedViewAdvancement();
}
}
Expand Down

0 comments on commit 68b30a5

Please sign in to comment.