Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
burning-ape committed Sep 26, 2022
1 parent b909537 commit 976afce
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Runtime/Touch.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Controls",
"rootNamespace": "",
"references": [
"GUID:560b04d1a97f54a4e82edc0cbbb69285"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Runtime/Touch.asmdef.meta

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

20 changes: 20 additions & 0 deletions Runtime/TouchControlls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BurningApe.Touch
{
public class TouchControlls
{
private TouchEvents _touchEvents;

public TouchEvent OnTapEvent = new TouchEvent();
public TouchEvent OnSwipeEvent = new TouchEvent();
public TouchEvent OnTouchMadeEvent = new TouchEvent();
public TouchEvent OnTouchEndEvent = new TouchEvent();
public TouchEvent OnSwipeOnceEvent = new TouchEvent();

public TouchControlls(TouchData data) => _touchEvents = new TouchEvents(data);


public void Run() => _touchEvents
.OnTouchEvent(OnTapEvent, OnSwipeEvent, OnTouchMadeEvent, OnTouchEndEvent, OnSwipeOnceEvent);

}
}
11 changes: 11 additions & 0 deletions Runtime/TouchControlls.cs.meta

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

56 changes: 56 additions & 0 deletions Runtime/TouchData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using UnityEngine;

namespace BurningApe.Touch
{
[System.Serializable]
public struct TouchData
{
#region Swipe Range
/// <summary>
/// Range within which swipe is detected
/// </summary>
public float SwipeRange { get; private set; }
#endregion

#region Previous Touch Position
/// <summary>
/// The touch position that was made in previous frame
/// </summary>
[HideInInspector] public Vector2 PreviousPosition;
#endregion

#region Current Touch Position
/// <summary>
/// The touch position in this frame
/// </summary>
[HideInInspector] public Vector2 CurrentPosition;
#endregion

#region Distance
/// <summary>
/// Distance between previous and current touch position
/// </summary>
[HideInInspector] public Vector2 Distance;
#endregion

#region Swipe Directions
/// <summary>
/// Avaliable directions of the swipe.
/// </summary>
public enum SwipeDirections
{
Right = 0,
Left = 1,
Up = 2,
Down = 3
}
#endregion

#region Current Swipe Direction
/// <summary>
/// Current direction of the swipe.
/// </summary>
[HideInInspector] public SwipeDirections CurrentSwipeDirection;
#endregion
}
}
11 changes: 11 additions & 0 deletions Runtime/TouchData.cs.meta

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

7 changes: 7 additions & 0 deletions Runtime/TouchEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using UnityEngine.Events;

namespace BurningApe.Touch
{
public class TouchEvent : UnityEvent<TouchData> { }
}

11 changes: 11 additions & 0 deletions Runtime/TouchEvent.cs.meta

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

107 changes: 107 additions & 0 deletions Runtime/TouchEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using UnityEngine;

namespace BurningApe.Touch
{
public class TouchEvents
{
public TouchData _touchData;
private bool _swipe = false;


#region Constructor
public TouchEvents(TouchData newData) => _touchData = newData;
#endregion


#region Touch Event

/// <summary>
/// Called when player touches the screen.
/// </summary>
/// <param name="OnTapEvent"> Event to invoke when player taps the screen.</param>
/// <param name="OnSwipeEvent"> Event to invoke when player swipes the screen.</param>
/// <param name="OnTouchMade"> Event to invoke the first time player touches the screen.</param>
/// <param name="OnTouchEnd"> Event to invoke when player releases finger from the screen.</param>
/// <param name="OnSwipeOnce"> Evebt to invoke when player releases finger only after the swipe</param>
public void OnTouchEvent(TouchEvent OnTapEvent, TouchEvent OnSwipeEvent,
TouchEvent OnTouchMade, TouchEvent OnTouchEnd, TouchEvent OnSwipeOnce)
{
if (Input.touchCount > 0)
{
// Getting touch.
var touch = Input.GetTouch(0);

// Set current touch position.
_touchData.CurrentPosition = touch.position;

// Calculate distance between current touch position and previous.
_touchData.Distance = _touchData.CurrentPosition - _touchData.PreviousPosition;


switch (touch.phase)
{
case TouchPhase.Began:
// Invoke this event when the finger touches the screen
OnTouchMade.Invoke(_touchData);

break;


case TouchPhase.Moved:

// Unlock swipe if distance between prev and cur touch pos is bigger than swipe range.
if (_touchData.Distance.magnitude > _touchData.SwipeRange)
{
_swipe = true;

// Identify direction of swipe.
if (_touchData.PreviousPosition.x < _touchData.CurrentPosition.x)
{
// Right
_touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Right;
}
else if (_touchData.PreviousPosition.x > _touchData.CurrentPosition.x)
{
// Left
_touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Left;
}
else if (_touchData.PreviousPosition.y < _touchData.CurrentPosition.y)
{
// Up
_touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Up;
}
else if (_touchData.PreviousPosition.y > _touchData.CurrentPosition.y)
{
// Down
_touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Down;
}

// Invoke on swipe event
OnSwipeEvent.Invoke(_touchData);
}

break;


case TouchPhase.Ended:

// Invoke on touch event only if the tap on screen wasnt identified as swipe.
if (!_swipe) OnTapEvent.Invoke(_touchData);
else OnSwipeOnce.Invoke(_touchData);

// Lock swipe after calling touch event.
_swipe = false;

// Invoke this event when the finger is released from the screen
OnTouchEnd.Invoke(_touchData);

break;
}

// Set previous touch position to current.
_touchData.PreviousPosition = _touchData.CurrentPosition;
}
}
#endregion
}
}
11 changes: 11 additions & 0 deletions Runtime/TouchEvents.cs.meta

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

0 comments on commit 976afce

Please sign in to comment.