Skip to content

Commit

Permalink
Merge pull request #159 from amazingalek/tai/InputCommandHandlers
Browse files Browse the repository at this point in the history
input command handlers
  • Loading branch information
TAImatem authored Jul 10, 2020
2 parents 3ed7362 + 0af28b3 commit 94c6dcb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
62 changes: 62 additions & 0 deletions OWML.ModHelper.Input/ModCommandListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
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;

private float _minPressDuration, _maxTapDuration;
private SingleAxisCommand _command;

public void Initialize(SingleAxisCommand command, float minPressDuration = 0.1f, float maxTapDuration = 0.1f)
{
_command = command;
_minPressDuration = minPressDuration;
_maxTapDuration = maxTapDuration;
}

private void Start()
{
DontDestroyOnLoad(gameObject);
}

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))
{
OnTapped?.Invoke();
}
}
}
}
24 changes: 24 additions & 0 deletions OWML.ModHelper.Input/ModCommandUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEngine;

namespace OWML.ModHelper.Input
{
public class ModCommandUpdater : MonoBehaviour
{
private InputCommand _command;

public void Initialize(InputCommand command)
{
_command = command;
}

private void Start()
{
DontDestroyOnLoad(gameObject);
}

private void Update()
{
_command?.UpdateInputCommand();
}
}
}
2 changes: 2 additions & 0 deletions OWML.ModHelper.Input/OWML.ModHelper.Input.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<ItemGroup>
<Compile Include="InputInterceptor.cs" />
<Compile Include="BindingChangeListener.cs" />
<Compile Include="ModCommandListener.cs" />
<Compile Include="ModCommandUpdater.cs" />
<Compile Include="ModInputCombination.cs" />
<Compile Include="ModInputHandler.cs" />
<Compile Include="ModInputTextures.cs" />
Expand Down

0 comments on commit 94c6dcb

Please sign in to comment.