Skip to content

Commit 1bc2c53

Browse files
committed
Minor changes to on screen controls.
1 parent b854395 commit 1bc2c53

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Assets/Samples/RebindingUI/OnScreen/CustomOnScreenControl.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public Curve curve
102102
/// <summary>
103103
/// Gets or sets the on-screen stick radius in millimeters.
104104
/// </summary>
105+
/// <remarks>Physical gamepads analog sticks have mechanical displacements of 7-8 millimeters.</remarks>
105106
/// <exception cref="ArgumentOutOfRangeException">If attempting to set the stick radius to a negative value.</exception>
106107
public float stickRadiusMillimeters
107108
{
@@ -115,6 +116,14 @@ public float stickRadiusMillimeters
115116
}
116117
}
117118

119+
public Vector2 stickCenter
120+
{
121+
get => m_Actuated ? m_StickCenter : m_NormalizedBounds.center;
122+
}
123+
124+
private Vector2 m_StickCenter;
125+
private bool m_Actuated;
126+
118127
[Header("Bounds")]
119128
[Tooltip("The geometric clipping area shape")]
120129
[SerializeField]
@@ -249,6 +258,9 @@ private void ChangeDevice(Touchscreen current)
249258

250259
private void OnGestureEvent(in GestureEvent gestureEvent)
251260
{
261+
// TODO We cannot only use drag for this, we need to also know when it gets "activated" so we
262+
// can set stick position at that point
263+
252264
// For button control, we consider the whole clip region as a button area.
253265
if (control is ButtonControl)
254266
{
@@ -266,12 +278,22 @@ private void OnGestureEvent(in GestureEvent gestureEvent)
266278
if (gestureEvent.flags.HasFlag(GestureEvent.Flags.PhaseStart) ||
267279
gestureEvent.flags.HasFlag(GestureEvent.Flags.PhaseChange))
268280
{
269-
// A DualSense has a mechanical displacement of ~7.4 mm.
270-
// A DualShock has a mechanical displacement of ~6.9 mm.
271-
272281
var deltaMillimeters = UnitConverter.PixelsToMillimeters(gestureEvent.delta);
273282
var stickRadius = Vector2.ClampMagnitude(deltaMillimeters, m_StickRadiusMillimeters);
274283
value = stickRadius / m_StickRadiusMillimeters;
284+
285+
if (!m_Actuated)
286+
{
287+
m_Actuated = true;
288+
m_StickCenter = new Vector2(gestureEvent.start.x / Display.displays[0].renderingWidth,
289+
gestureEvent.start.y / Display.displays[0].renderingHeight);
290+
//m_StickCenter = gestureEvent.start; // TODO Convert to normalized
291+
}
292+
}
293+
else
294+
{
295+
m_StickCenter = m_NormalizedBounds.center;
296+
m_Actuated = false;
275297
}
276298

277299
value = m_Curve.Transform(value);

Assets/Samples/RebindingUI/OnScreen/OnScreenControlUI.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void OnEnable()
6161

6262
if (bounds != null)
6363
{
64-
var rawImage = knob.GetComponent<RawImage>();
64+
var rawImage = bounds.GetComponent<RawImage>();
6565
if (rawImage != null /*&& rawImage.texture == null*/)
6666
{
6767
var g = new Gradient
@@ -116,9 +116,10 @@ void Update()
116116

117117
var normalizedBounds = control.bounds;
118118
var stickRadiusPixels = UnitConverter.MillimetersToPixels(control.stickRadiusMillimeters);
119+
var stickCenter = control.stickCenter;
119120
var stickViewport = camera.ScreenToViewportPoint(new Vector2(stickRadiusPixels, stickRadiusPixels));
120-
var stickRect = new Rect(normalizedBounds.center.x - stickViewport.x,
121-
normalizedBounds.center.y - stickViewport.y, stickViewport.x * 2, stickViewport.y * 2);
121+
var stickRect = new Rect(stickCenter.x - stickViewport.x,
122+
stickCenter.y - stickViewport.y, stickViewport.x * 2, stickViewport.y * 2);
122123

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

0 commit comments

Comments
 (0)