Skip to content

Commit 4dfe01d

Browse files
committed
[ScreenAreas] Fixed bug when touch ou TOP_LEFT or DOWN_LEFT side
Sometimes, this direction is not triggered correctly. Needs more tests
1 parent 145c80a commit 4dfe01d

File tree

3 files changed

+75
-34
lines changed

3 files changed

+75
-34
lines changed

Runtime/LeanTouchScreen.cs

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ public Vector2 IndicatorSidePos
4747
public static Action<Vector3, SideDirection, LeanFinger> OnTapScreenSide;
4848
public static Action<Vector3, SideDirection, LeanFinger> OnTapScreenDiagonal;
4949

50+
[Header("Touch references")]
51+
[Tooltip("The player reference to check the screen sides")]
52+
[SerializeField]
53+
protected GameObject player;
54+
55+
[Tooltip("If player is in Isometric plan, the reference will be change Y => Z")]
56+
[SerializeField]
57+
protected bool isIsometric = false;
58+
5059
[Tooltip("The camera to calculate where show gizmos, touch indicators and sides")]
5160
[SerializeField]
5261
protected Camera cam;
53-
62+
5463
[Header("Touch indicator")]
5564
[Tooltip("Duration in seconds of touch indicator circle")]
5665
[SerializeField]
@@ -71,7 +80,7 @@ public Vector2 IndicatorSidePos
7180
[SerializeField]
7281
protected bool cancelMiddle = true;
7382

74-
[Tooltip("How is the DISTANCE between the touch and the middle will be ignored?")]
83+
[Tooltip("The X and Y values of the DISTANCE between the touch and the middle will be ignored")]
7584
[SerializeField]
7685
protected Vector2 middleDistance = Vector2.right;
7786

@@ -106,6 +115,29 @@ public Vector3 ScreenMiddlePos
106115
}
107116
}
108117

118+
public Vector3 ReferenceTouch
119+
{
120+
get
121+
{
122+
Vector3 reference = screenMiddleRaw;
123+
124+
if (player != null)
125+
{
126+
reference = player.transform.position;
127+
128+
// TODO: Create a Editor script to show this field, only if player != null
129+
if (isIsometric)
130+
{
131+
reference.y = reference.z;
132+
}
133+
}
134+
135+
return reference;
136+
}
137+
138+
protected set { }
139+
}
140+
109141
void Awake()
110142
{
111143
fingerTap = GetComponentInChildren<LeanFingerTap>();
@@ -210,7 +242,7 @@ void DrawTouchDiagonal()
210242
IEnumerator DrawTouchIndicator(float seconds = 2)
211243
{
212244
float touchRadius = 0;
213-
Vector2 indicatorPosition = touchedFinger != null ? touchedFinger.ScreenPosition : Vector2.zero;
245+
214246
if (touchedFinger != null && touchedFinger.IsActive)
215247
{
216248
if (touchUI != null && touchUI.IgnoreUI)
@@ -227,7 +259,8 @@ IEnumerator DrawTouchIndicator(float seconds = 2)
227259
touchRadius = GetTouchRadius();
228260
}
229261

230-
Handles.DrawSolidDisc(/*indicatorPosition*/ touchWorldPos, Vector3.forward, touchRadius);
262+
//Draw a solid disc in the coordinates in a space in the game world
263+
Handles.DrawSolidDisc(touchWorldPos, Vector3.forward, touchRadius);
231264
yield return new WaitForSeconds(seconds);
232265
}
233266
#endif
@@ -236,60 +269,67 @@ void OnEnable()
236269
{
237270
if (fingerTap != null && fingerTap.isActiveAndEnabled)
238271
{
239-
fingerTap.OnPosition.AddListener(TapScreenPos);
272+
fingerTap.OnFinger.AddListener(TapScreen);
240273
}
241274
else
242275
{
243276
LeanTouch.OnFingerTap += TapScreen;
244277
}
245278

279+
#if UNITY_EDITOR
280+
// Subscribe this event, only in editor (Gizmos debug)
246281
LeanTouch.OnFingerSet += TapIndicator;
282+
#endif
247283
}
248284

249285
void OnDisable()
250286
{
251287
if (fingerTap != null && fingerTap.isActiveAndEnabled)
252288
{
253-
fingerTap.OnPosition.RemoveListener(TapScreenPos);
289+
fingerTap.OnFinger.RemoveListener(TapScreen);
254290
}
255291
else
256292
{
257293
LeanTouch.OnFingerTap -= TapScreen;
258294
}
259295

296+
#if UNITY_EDITOR
297+
// Unsubscribe this event, only in editor (Gizmos debug)
260298
LeanTouch.OnFingerSet -= TapIndicator;
299+
#endif
261300
}
262301

263302
public virtual void TapScreen(LeanFinger finger)
264303
{
265304
TapScreenVerify(finger.LastScreenPosition, finger);
266305
}
267306

268-
public virtual void TapScreenPos(Vector3 position)
269-
{
270-
TapScreenVerify(position);
271-
}
272-
273-
public virtual void TapScreenVerify(Vector3 position, LeanFinger finger = null)
307+
public virtual void TapScreenVerify(Vector3 touchPosition, LeanFinger finger = null)
274308
{
275309
GameObject uiElement;
276-
bool touchedInUI = finger != null ? touchUI.IsTouched(finger, out uiElement) : touchUI.IsTouched(position, out uiElement);
310+
bool touchedInUI = finger != null ? touchUI.IsTouched(finger, out uiElement) : touchUI.IsTouched(touchPosition, out uiElement);
311+
277312

278313
if (touchedInUI)
279314
{
280-
var direction = GetDirection(position);
315+
var direction = GetDirection(touchPosition);
281316

282-
LeanTouchUI.OnTapUI?.Invoke(position, direction, uiElement, finger);
317+
LeanTouchUI.OnTapUI?.Invoke(touchPosition, direction, uiElement, finger);
283318
} else
284319
{
285-
var callbackEvent = GetDirectionAction(position);
286-
callbackEvent?.Invoke(position, finger);
320+
var callbackEvent = GetDirectionAction(touchPosition);
321+
callbackEvent?.Invoke(touchPosition, finger);
287322
}
288323
}
289324

325+
/// <summary>
326+
/// Get touch data (touch world position, direction...)
327+
///<b>PS:</b> This is only to debug
328+
/// </summary>
329+
/// <param name="finger">The last touch touch information object</param>
290330
public virtual void TapIndicator(LeanFinger finger)
291331
{
292-
touchWorldPos = cam.ScreenToWorldPoint(finger.StartScreenPosition);
332+
touchWorldPos = cam.ScreenToWorldPoint(finger.LastScreenPosition);
293333

294334
touchedFinger = finger;
295335
lastTouchedDirection = GetDirection(touchWorldPos);
@@ -342,28 +382,29 @@ public virtual float GetTouchRadius()
342382
return radius;
343383
}
344384

345-
public SideDirection IsScreenLeft(Vector2 touchPosition)
385+
public SideDirection IsScreenLeft(Vector3 touchPosition)
346386
{
387+
347388
if (cancelMiddle && IsScreenMiddle(touchPosition).Equals(SideDirection.MIDDLE))
348389
{
349390
return SideDirection.NONE;
350391
}
351392

352-
if (touchPosition != Vector2.zero)
393+
if (touchPosition != Vector3.zero)
353394
{
354395
/**
355396
* Left: Only change "lastTouchedDirection" property when the
356397
* direction is not equal to SideDirection.NONE
357398
*
358399
* This to not impact the Right directions
359400
*/
360-
if (touchPosition.x < ScreenMiddlePos.x)
401+
if (touchPosition.x < ReferenceTouch.x)
361402
{
362-
if (diagonals && touchPosition.y > ScreenMiddlePos.y)
403+
if (diagonals && touchPosition.y > ReferenceTouch.y)
363404
{
364405
lastTouchedDirection = SideDirection.TOP_LEFT;
365406
}
366-
else if (diagonals && touchPosition.y < ScreenMiddlePos.y)
407+
else if (diagonals && touchPosition.y < ReferenceTouch.y)
367408
{
368409
lastTouchedDirection = SideDirection.DOWN_LEFT;
369410
}
@@ -379,25 +420,25 @@ public SideDirection IsScreenLeft(Vector2 touchPosition)
379420
return SideDirection.NONE;
380421
}
381422

382-
public SideDirection IsScreenRight(Vector2 touchPosition)
423+
public SideDirection IsScreenRight(Vector3 touchPosition)
383424
{
384425

385426
if (cancelMiddle && IsScreenMiddle(touchPosition).Equals(SideDirection.MIDDLE))
386427
{
387428
return SideDirection.NONE;
388429
}
389430

390-
if (touchPosition != Vector2.zero)
431+
if (touchPosition != Vector3.zero)
391432
{
392433

393434
// Right
394-
if (touchPosition.x > ScreenMiddlePos.x)
435+
if (touchPosition.x > ReferenceTouch.x)
395436
{
396-
if (diagonals && touchPosition.y > ScreenMiddlePos.y)
437+
if (diagonals && touchPosition.y > ReferenceTouch.y)
397438
{
398439
lastTouchedDirection = SideDirection.TOP_RIGHT;
399440
}
400-
else if (diagonals && touchPosition.y < ScreenMiddlePos.y)
441+
else if (diagonals && touchPosition.y < ReferenceTouch.y)
401442
{
402443
lastTouchedDirection = SideDirection.DOWN_RIGHT;
403444
}
@@ -435,13 +476,13 @@ public SideDirection IsScreenMiddle(Vector2 touchPosition, (bool? x, bool? y) ch
435476
if (x)
436477
{
437478
posForDistance.touch.x = touchPosition.x;
438-
posForDistance.screen.x = ScreenMiddlePos.x;
479+
posForDistance.screen.x = ReferenceTouch.x;
439480
}
440481

441482
if (y)
442483
{
443484
posForDistance.touch.y = touchPosition.y;
444-
posForDistance.screen.y = ScreenMiddlePos.y;
485+
posForDistance.screen.y = ReferenceTouch.y;
445486
}
446487

447488
/**

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "com.lean.touch.extensions.screenareas",
3-
"displayName": "Lean Touch Screen sides",
4-
"version": "1.0.1",
3+
"displayName": "Lean Touch Screen Sides",
4+
"version": "1.0.2",
55
"unity": "2017.4",
66
"author": {
77
"name": "Felipe Michel",
88
"email": "mfelipeof@gmail.com",
99
"url": "https://github.com/mfdeveloper"
1010
},
11-
"description": "Example for playing with new package manager feature in git",
11+
"description": "Subscribe actions when touch on screen sides (left, right, top, down...) or in an UI element",
1212
"dependencies": {},
1313
"repository": {}
1414
}

0 commit comments

Comments
 (0)