Skip to content

Commit 96b7c1e

Browse files
committed
Fixed an issue where drag gesture wouldn't trigger directly when threshold is zero
1 parent 1bc2c53 commit 96b7c1e

File tree

5 files changed

+423
-80
lines changed

5 files changed

+423
-80
lines changed

Assets/Samples/RebindingUI/OnScreen/CustomOnScreenControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected override void OnEnable()
168168
if (control is ButtonControl)
169169
m_Detector = new Detector<ActiveDetector>(1, new ActiveDetector());
170170
else if (control is StickControl)
171-
m_Detector = new Detector<DragDetector>(1, new DragDetector(threshold));
171+
m_Detector = new Detector<DragDetector>(1, new DragDetector(0.0f));
172172
else
173173
throw new Exception($"Unsupported control type: {control.GetType()}");
174174

Assets/Samples/RebindingUI/OnScreen/DragDetector.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public void OnTouchBegin(Detector context, in TouchState[] touches, int count, i
2323
{
2424
m_InitialPosition = touches[index].position;
2525
m_InitialTime = Time.realtimeSinceStartupAsDouble;
26+
27+
// Require that we move outside "dead zone" before we consider the drag to start.
28+
if (IsWithinMargin(touches[index].position))
29+
return;
30+
31+
m_Valid = true;
32+
Fire(context, touches, count, index, GestureEvent.Flags.PhaseStart);
2633
}
2734
else if (m_Valid)
2835
{
@@ -78,7 +85,7 @@ private void Fire(Detector context, in TouchState[] touches, int count, int inde
7885
private bool IsWithinMargin(Vector2 point)
7986
{
8087
var sqrMagnitude = (point - m_InitialPosition).sqrMagnitude;
81-
return sqrMagnitude <= m_ThresholdSqr;
88+
return sqrMagnitude < m_ThresholdSqr;
8289
}
8390
}
8491
}

Assets/Samples/RebindingUI/OnScreen/GestureEvent.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,24 @@ public enum Flags
3939
/// </summary>
4040
Active = 1 << 3,
4141

42+
/// <summary>
43+
/// Indicates that the interaction of the associated continuous gesture started.
44+
/// </summary>
4245
PhaseStart = 1 << 28,
46+
47+
/// <summary>
48+
/// Indicates that the interaction of the associated continuous gesture was updated and changed.
49+
/// </summary>
4350
PhaseChange = 1 << 29,
51+
52+
/// <summary>
53+
/// Indicates that the interaction of the associated continuous gesture ended.
54+
/// </summary>
4455
PhaseEnd = 1 << 30,
56+
57+
/// <summary>
58+
/// Indicates that the interaction of the associated continuous gesture was cancelled.
59+
/// </summary>
4560
PhaseCancel = 1 << 31,
4661
}
4762

@@ -50,6 +65,13 @@ public enum Flags
5065
public readonly Vector2 delta;
5166
public readonly double duration;
5267

68+
/// <summary>
69+
/// Constructs a new gesture event.
70+
/// </summary>
71+
/// <param name="delta">The delta of the gesture event.</param>
72+
/// <param name="duration">The duration of the gesture event.</param>
73+
/// <param name="flags">Flags indicating what type of event it is.</param>
74+
/// <param name="start">The initial absolute position of the gesture event.</param>
5375
public GestureEvent(Vector2 delta, double duration, Flags flags, Vector2 start)
5476
{
5577
this.delta = delta;
@@ -58,6 +80,9 @@ public GestureEvent(Vector2 delta, double duration, Flags flags, Vector2 start)
5880
this.start = start;
5981
}
6082

83+
/// <summary>
84+
/// Converts the event to string representation.
85+
/// </summary>
6186
public override string ToString()
6287
{
6388
var sb = new StringBuilder();

Assets/Samples/RebindingUI/OnScreen/OnScreenControlUI.cs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
// Should warn about not having a canvas or parent canvas assigned.
77
// Should warn about ray-casted UI consuming events.
88

9-
9+
/// <summary>
10+
/// A passive UI visualization of an on-screen control in Unity UI (UGUI).
11+
/// </summary>
1012
[ExecuteInEditMode]
1113
public class OnScreenControlUI : MonoBehaviour
1214
{
@@ -17,41 +19,6 @@ public class OnScreenControlUI : MonoBehaviour
1719
public RectTransform bounds;
1820
public RectTransform knob;
1921

20-
private void OnDrawGizmosSelected()
21-
{
22-
// This will not produce meaningful results unless we have a rect transform (ISXB-915, ISXB-916).
23-
var parentRectTransform = transform.parent as RectTransform;
24-
if (parentRectTransform == null)
25-
return;
26-
27-
Gizmos.matrix = parentRectTransform.localToWorldMatrix;
28-
29-
var startPos = parentRectTransform.anchoredPosition;
30-
31-
/*var startPos = parentRectTransform.anchoredPosition;
32-
if (Application.isPlaying)
33-
startPos = m_StartPos;
34-
*/
35-
Gizmos.color = new Color32(84, 173, 219, 255);
36-
37-
var center = startPos;
38-
/*if (Application.isPlaying && m_Behaviour == Behaviour.ExactPositionWithDynamicOrigin)
39-
center = m_PointerDownPos;*/
40-
41-
var radius = UnitConverter.MillimetersToPixels(control.stickRadiusMillimeters);
42-
ScreenGizmos.DrawGizmoCircle(center, radius);
43-
44-
//if (m_Behaviour != Behaviour.ExactPositionWithDynamicOrigin) return;
45-
46-
//Gizmos.color = new Color32(158, 84, 219, 255);
47-
//DrawGizmoCircle(startPos, m_DynamicOriginRange);
48-
}
49-
50-
// Start is called once before the first execution of Update after the MonoBehaviour is created
51-
void Start()
52-
{
53-
}
54-
5522
void OnEnable()
5623
{
5724
if (canvas == null)
@@ -118,8 +85,11 @@ void Update()
11885
var stickRadiusPixels = UnitConverter.MillimetersToPixels(control.stickRadiusMillimeters);
11986
var stickCenter = control.stickCenter;
12087
var stickViewport = camera.ScreenToViewportPoint(new Vector2(stickRadiusPixels, stickRadiusPixels));
121-
var stickRect = new Rect(stickCenter.x - stickViewport.x,
122-
stickCenter.y - stickViewport.y, stickViewport.x * 2, stickViewport.y * 2);
88+
var stickRect = new Rect(
89+
x: stickCenter.x - stickViewport.x,
90+
y: stickCenter.y - stickViewport.y,
91+
width: stickViewport.x * 2,
92+
height: stickViewport.y * 2);
12393

12494
// Optionally transform a UI object to represent the interactable area in viewport space.
12595
if (area != null)

0 commit comments

Comments
 (0)