Skip to content

Commit

Permalink
Improve button and toggle behaviour (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
FejZa authored Jul 16, 2024
1 parent faa5d1f commit 6b02c3c
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 188 deletions.
27 changes: 27 additions & 0 deletions Editor/Inspectors/ButtonBehaviourInspector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Reality Collective. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using RealityToolkit.Input.InteractionBehaviours;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace RealityToolkit.Editor.Inspectors
{
[CustomEditor(typeof(ButtonBehaviour), true)]
public class ButtonBehaviourInspector : BaseInteractionBehaviourInspector
{
private const string raiseOnInputDown = "raiseOnInputDown";
private const string click = "click";

public override VisualElement CreateInspectorGUI()
{
var inspector = base.CreateInspectorGUI();

inspector.Add(new PropertyField(serializedObject.FindProperty(raiseOnInputDown)));
inspector.Add(new PropertyField(serializedObject.FindProperty(click)));

return inspector;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Editor/Inspectors/ToggleBehaviourInspector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Reality Collective. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using RealityToolkit.Input.InteractionBehaviours;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace RealityToolkit.Editor.Inspectors
{
[CustomEditor(typeof(ToggleBehaviour), true)]
public class ToggleBehaviourInspector : BaseInteractionBehaviourInspector
{
private const string isOn = "isOn";
private const string raiseOnInputDown = "raiseOnInputDown";
private const string valueChanged = "valueChanged";

public override VisualElement CreateInspectorGUI()
{
var inspector = base.CreateInspectorGUI();

inspector.Add(new PropertyField(serializedObject.FindProperty(isOn)));
inspector.Add(new PropertyField(serializedObject.FindProperty(raiseOnInputDown)));
inspector.Add(new PropertyField(serializedObject.FindProperty(valueChanged)));

return inspector;
}
}
}
11 changes: 11 additions & 0 deletions Editor/Inspectors/ToggleBehaviourInspector.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions Runtime/Input/Interactables/IInteractable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public interface IInteractable
/// </summary>
bool IsValid { get; }

/// <summary>
/// Is the <see cref="IInteractable"/> currently considered activated?
/// </summary>
bool IsActivated { get; }

/// <summary>
/// Is the <see cref="IInteractable"/> currently focused by an <see cref="IInteractor"/>?
/// </summary>
Expand All @@ -52,12 +47,6 @@ public interface IInteractable
/// </summary>
InteractableFocusMode FocusMode { get; }

/// <summary>
/// The <see cref="IInteractable"/>'s activation mode determines how <see cref="IInteractionBehaviour.OnActivated(Events.InteractionEventArgs)"/>
/// and <see cref="IInteractionBehaviour.OnDeactivated(Events.InteractionExitEventArgs)"/> are raised.
/// </summary>
InteractableActivationMode ActivationMode { get; }

/// <summary>
/// Does the <see cref="IInteractable"/> allow direct interaction?
/// </summary>
Expand Down
75 changes: 1 addition & 74 deletions Runtime/Input/Interactables/Interactable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public class Interactable : MonoBehaviour,
[SerializeField, Tooltip("The focus mode for this interactable.")]
private InteractableFocusMode focusMode = InteractableFocusMode.Single;

[SerializeField, Tooltip("The activation mode for this interactable.")]
private InteractableActivationMode activationMode = InteractableActivationMode.Button;

private readonly HashSet<IInteractor> focusingInteractors = new HashSet<IInteractor>();
private readonly HashSet<IInteractor> selectingInteractors = new HashSet<IInteractor>();
private readonly HashSet<IInteractor> grabbingInteractors = new HashSet<IInteractor>();
Expand All @@ -81,9 +78,6 @@ public string Label
/// <inheritdoc/>
public bool IsValid => isActiveAndEnabled && (DirectInteractionEnabled || FarInteractionEnabled);

/// <inheritdoc/>
public bool IsActivated { get; private set; }

/// <inheritdoc/>
public bool IsFocused => focusingInteractors.Count > 0;

Expand All @@ -96,9 +90,6 @@ public string Label
/// <inheritdoc/>
public InteractableFocusMode FocusMode => focusMode;

/// <inheritdoc/>
public InteractableActivationMode ActivationMode => activationMode;

/// <inheritdoc/>
public bool DirectInteractionEnabled => InputService.DirectInteractionEnabled && directInteraction;

Expand Down Expand Up @@ -151,59 +142,6 @@ private void OnReset()
selectingInteractors.Clear();
}

/// <summary>
/// The <see cref="IInteractable"/> was activated by <paramref name="interactor"/>.
/// </summary>
/// <param name="interactor">The <see cref="IInteractor"/> causing the activation.</param>
protected virtual void OnActivated(IInteractor interactor)
{
if (IsActivated || ActivationMode == InteractableActivationMode.None)
{
return;
}

IsActivated = ActivationMode == InteractableActivationMode.Toggle;

for (var i = 0; i < behaviours.Count; i++)
{
var behaviour = behaviours[i];
var eventArgs = new Events.InteractionEventArgs
{
Interactable = this,
Interactor = interactor
};

behaviour.OnActivated(eventArgs);
}
}

/// <summary>
/// The <see cref="IInteractable"/> was deactivated by <paramref name="interactor"/>.
/// </summary>
/// <param name="interactor">The <see cref="IInteractor"/> causing the deactivation.</param>
protected virtual void OnDeactivated(IInteractor interactor)
{
if (!IsActivated || ActivationMode == InteractableActivationMode.None)
{
return;
}

IsActivated = false;

for (var i = 0; i < behaviours.Count; i++)
{
var behaviour = behaviours[i];
var eventArgs = new Events.InteractionExitEventArgs
{
Interactable = this,
Interactor = interactor,
IsCanceled = false
};

behaviour.OnDeactivated(eventArgs);
}
}

/// <summary>
/// The <see cref="IInteractable"/> is focused by <paramref name="interactor"/>.
/// </summary>
Expand Down Expand Up @@ -496,18 +434,7 @@ public void OnPointerUp(PointerEventData eventData)
}

/// <inheritdoc/>
public void OnPointerClicked(PointerEventData eventData)
{
eventData.Use();

if (IsActivated)
{
OnDeactivated(eventData.Pointer);
return;
}

OnActivated(eventData.Pointer);
}
public void OnPointerClicked(PointerEventData eventData) { }

#endregion IPointerHandler

Expand Down
30 changes: 0 additions & 30 deletions Runtime/Input/Interactables/InteractableActivationMode.cs

This file was deleted.

24 changes: 0 additions & 24 deletions Runtime/Input/InteractionBehaviours/BaseInteractionBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,6 @@ protected virtual void OnDestroy() { }
/// </summary>
protected virtual void OnValidate() { }

/// <inheritdoc/>
void IInteractionBehaviour.OnActivated(InteractionEventArgs eventArgs)
{
if (ShouldPerformBehaviour(eventArgs))
{
OnActivated(eventArgs);
}
}

/// <inheritdoc cref="IInteractionBehaviour.OnActivated"/>
protected virtual void OnActivated(InteractionEventArgs eventArgs) { }

/// <inheritdoc/>
void IInteractionBehaviour.OnDeactivated(InteractionExitEventArgs eventArgs)
{
if (ShouldPerformBehaviour(eventArgs))
{
OnDeactivated(eventArgs);
}
}

/// <inheritdoc cref="IInteractionBehaviour.OnDeactivated"/>
protected virtual void OnDeactivated(InteractionExitEventArgs eventArgs) { }

/// <inheritdoc/>
void IInteractionBehaviour.OnFirstFocusEntered(InteractionEventArgs eventArgs)
{
Expand Down
19 changes: 17 additions & 2 deletions Runtime/Input/InteractionBehaviours/ButtonBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ButtonBehaviour : BaseInteractionBehaviour
[Serializable]
public class ButtonClickEvent : UnityEvent { }

[SerializeField, Tooltip("If set, the button will raise click on input down instead of when input is released.")]
private bool raiseOnInputDown = false;

[Space]
[SerializeField, Tooltip("List of click delegates triggered on click.")]
private ButtonClickEvent click = null;
Expand All @@ -31,9 +34,21 @@ public class ButtonClickEvent : UnityEvent { }
public ButtonClickEvent Click => click;

/// <inheritdoc/>
protected override void OnActivated(InteractionEventArgs eventArgs)
protected override void OnSelectEntered(InteractionEventArgs eventArgs)
{
if (raiseOnInputDown)
{
Click?.Invoke();
}
}

/// <inheritdoc/>
protected override void OnSelectExited(InteractionExitEventArgs eventArgs)
{
Click?.Invoke();
if (!raiseOnInputDown)
{
Click?.Invoke();
}
}
}
}
18 changes: 1 addition & 17 deletions Runtime/Input/InteractionBehaviours/IInteractionBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Reality Collective. All rights reserved.
// Copyright (c) Reality Collective. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using RealityToolkit.Input.Events;
Expand All @@ -23,22 +23,6 @@ public interface IInteractionBehaviour
/// </summary>
IInteractable Interactable { get; }

#region Activate

/// <summary>
/// The <see cref="IInteractable"/> was activated.
/// </summary>
/// <param name="eventArgs"><see cref="InteractionEventArgs"/>.</param>
void OnActivated(InteractionEventArgs eventArgs);

/// <summary>
/// The <see cref="IInteractable"/> was deactivated.
/// </summary>
/// <param name="eventArgs"><see cref="InteractionExitEventArgs"/>.</param>
void OnDeactivated(InteractionExitEventArgs eventArgs);

#endregion Activate

#region Focus

/// <summary>
Expand Down
19 changes: 0 additions & 19 deletions Runtime/Input/InteractionBehaviours/InteractionEventsBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ namespace RealityToolkit.Input.InteractionBehaviours
[AddComponentMenu(RealityToolkitRuntimePreferences.Toolkit_InteractionsAddComponentMenu + "/" + nameof(InteractionEventsBehaviour))]
public class InteractionEventsBehaviour : BaseInteractionBehaviour
{
[Space]
[SerializeField]
private InteractionEvent activated = null;

[SerializeField]
private InteractionExitEvent deactivated = null;

[Space]
[SerializeField]
private InteractionEvent firstFocusEntered = null;
Expand Down Expand Up @@ -64,12 +57,6 @@ public class InteractionEventsBehaviour : BaseInteractionBehaviour
[SerializeField]
private InteractionExitEvent lastGrabExited = null;

/// <inheritdoc cref="OnActivated(InteractionEventArgs)"/>
public InteractionEvent Activated => activated;

/// <inheritdoc cref="OnDeactivated(InteractionExitEventArgs)"/>
public InteractionExitEvent Deactivated => deactivated;

/// <inheritdoc cref="OnFirstFocusEntered(InteractionEventArgs)"/>
public InteractionEvent FirstFocusEntered => firstFocusEntered;

Expand Down Expand Up @@ -106,12 +93,6 @@ public class InteractionEventsBehaviour : BaseInteractionBehaviour
/// <inheritdoc cref="OnLastGrabExited(InteractionExitEventArgs)"/>
public InteractionExitEvent LastGrabExited => lastGrabExited;

/// <inheritdoc/>
protected override void OnActivated(InteractionEventArgs eventArgs) => Activated?.Invoke(eventArgs);

/// <inheritdoc/>
protected override void OnDeactivated(InteractionExitEventArgs eventArgs) => Deactivated?.Invoke(eventArgs);

/// <inheritdoc/>
protected override void OnFirstFocusEntered(InteractionEventArgs eventArgs) => FirstFocusEntered?.Invoke(eventArgs);

Expand Down
Loading

0 comments on commit 6b02c3c

Please sign in to comment.