Skip to content

Commit

Permalink
Command listener event commands (#160)
Browse files Browse the repository at this point in the history
* added command to event signatures
* made it possible to assign multiple commands

Co-authored-by: Aleksandr <[email protected]>
  • Loading branch information
amazingalek and TAImatem authored Jul 10, 2020
1 parent 94c6dcb commit 82a8ebd
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions OWML.ModHelper.Input/ModCommandListener.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace OWML.ModHelper.Input
{
public class ModCommandListener : MonoBehaviour
{
public event Action OnNewlyPressed;
public event Action OnNewlyReleased;
public event Action OnNewlyHeld;
public event Action OnPressed;
public event Action OnTapped;
public event Action OnHeld;
public event Action<SingleAxisCommand> OnNewlyPressed;
public event Action<SingleAxisCommand> OnNewlyReleased;
public event Action<SingleAxisCommand> OnNewlyHeld;
public event Action<SingleAxisCommand> OnPressed;
public event Action<SingleAxisCommand> OnTapped;
public event Action<SingleAxisCommand> OnHeld;

private float _minPressDuration, _maxTapDuration;
private SingleAxisCommand _command;
public float MinimalPressDuration { get; set; } = 0.1f;
public float MaximalTapDuration { get; set; } = 0.1f;

public void Initialize(SingleAxisCommand command, float minPressDuration = 0.1f, float maxTapDuration = 0.1f)
private readonly HashSet<SingleAxisCommand> _commands = new HashSet<SingleAxisCommand>();

public void AddToListener(SingleAxisCommand command)
{
if (!_commands.Contains(command))
{
_commands.Add(command);
}
}

public void RemoveFromListener(SingleAxisCommand command)
{
_command = command;
_minPressDuration = minPressDuration;
_maxTapDuration = maxTapDuration;
if (_commands.Contains(command))
{
_commands.Remove(command);
}
}

private void Start()
Expand All @@ -29,34 +41,33 @@ private void Start()

private void Update()
{
if (_command == null)
{
return;
}
if (_command.IsNewlyPressed())
{
OnNewlyPressed?.Invoke();
}
if (_command.IsNewlyHeld(_minPressDuration))
{
OnNewlyHeld?.Invoke();
}
if (_command.IsNewlyReleased())
{
OnNewlyReleased?.Invoke();
}
if (_command.IsPressed())
{
OnPressed?.Invoke();
}
if (_command.IsHeld(_minPressDuration))
{
OnHeld?.Invoke();
}
if (_command.IsTapped(_maxTapDuration))
foreach (var command in _commands)
{
OnTapped?.Invoke();
if (command.IsNewlyPressed())
{
OnNewlyPressed?.Invoke(command);
}
if (command.IsNewlyHeld(MinimalPressDuration))
{
OnNewlyHeld?.Invoke(command);
}
if (command.IsNewlyReleased())
{
OnNewlyReleased?.Invoke(command);
}
if (command.IsPressed())
{
OnPressed?.Invoke(command);
}
if (command.IsHeld(MinimalPressDuration))
{
OnHeld?.Invoke(command);
}
if (command.IsTapped(MaximalTapDuration))
{
OnTapped?.Invoke(command);
}
}
}
}
}
}

0 comments on commit 82a8ebd

Please sign in to comment.