-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b909537
commit 976afce
Showing
10 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { } | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.