Skip to content

Commit

Permalink
FIX: OnScreenStick expecting Vector3 control (#157).
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Damm committed Jul 2, 2018
1 parent b5a18f6 commit 15cc5b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Expand Up @@ -532,7 +532,7 @@ private unsafe IntPtr GetStatePtrFromStateEvent(InputEventPtr eventPtr)
return new IntPtr(stateEvent->state.ToInt64() - (int)deviceStateOffset);
}

protected TValue Process(TValue value)
public TValue Process(TValue value)
{
if (m_ProcessorStack.length > 0)
{
Expand Down
Expand Up @@ -146,13 +146,16 @@ private void SetupInputControl()

protected void SendValueToControl<TValue>(TValue value)
{
if (m_Control == null)
return;

////TODO: only cast once
var control = m_Control as InputControl<TValue>;
if (control == null)
{
throw new Exception(string.Format(
"The control path {0} yields a control of type {1} which is not an InputControl",
controlPath, m_Control.GetType().Name));
"The control path {0} yields a control of type {1} which is not an InputControl with value type {2}",
controlPath, m_Control.GetType().Name, typeof(TValue).Name));
}

m_InputEventPtr.time = InputRuntime.s_Instance.currentTime;
Expand Down
Expand Up @@ -19,23 +19,28 @@ public void OnPointerDown(PointerEventData data)

public void OnDrag(PointerEventData data)
{
var newPos = Vector3.zero;
var delta = 0;
var newPos = Vector2.zero;

delta = (int)(data.position.x - m_StartPos.x);
delta = Mathf.Clamp(delta, -movementRange, movementRange);
newPos.x = delta;
////REVIEW: is this Y up?

delta = (int)(data.position.y - m_StartPos.y);
delta = Mathf.Clamp(delta, -movementRange, movementRange);
newPos.y = delta;
////REVIEW: this doesn't make sense; data.position and transform.position are not in the same coordinate space

var deltaX = (int)(data.position.x - m_StartPos.x);
deltaX = Mathf.Clamp(deltaX, -movementRange, movementRange);
newPos.x = deltaX;

var deltaY = (int)(data.position.y - m_StartPos.y);
deltaY = Mathf.Clamp(deltaY, -movementRange, movementRange);
newPos.y = deltaY;

////FIXME: this is setting up a square movement space, not a radial one; relies on normalization on the control to work

newPos.x /= movementRange;
newPos.y /= movementRange;

SendValueToControl(newPos);

transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z);
}

public void OnPointerUp(PointerEventData data)
Expand Down
@@ -1,7 +1,9 @@
using NUnit.Framework;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Experimental.Input;
using UnityEngine.Experimental.Input.Controls;
using UnityEngine.Experimental.Input.Plugins.OnScreen;

public class OnScreenTests : InputTestFixture
Expand All @@ -11,12 +13,30 @@ public class OnScreenTests : InputTestFixture
public void Devices_CanCreateOnScreenStick()
{
var gameObject = new GameObject();
var eventSystem = gameObject.AddComponent<EventSystem>();

var stick = gameObject.AddComponent<OnScreenStick>();
stick.controlPath = "/<Gamepad>/leftStick";

Assert.That(InputSystem.devices, Has.Exactly(1).TypeOf<Gamepad>());
Assert.That(stick.control.device, Is.TypeOf<Gamepad>());
Assert.That(stick.control, Is.SameAs(stick.control.device["leftStick"]));
Assert.That(stick.control, Is.TypeOf<StickControl>());
var stickControl = (StickControl)stick.control;

////FIXME: OnScreenStick mixes up transform.position and 2D position space

stick.OnDrag(new PointerEventData(eventSystem)
{
position = new Vector2(stick.movementRange, stick.movementRange)
});

InputSystem.Update();

Assert.That(stick.control.ReadValueAsObject(),
Is.EqualTo(stickControl.Process(new Vector2(stick.movementRange / 2f, stick.movementRange / 2)))
.Using(vector2Comparer));

////TODO: test stick movement
////REVIEW: check transform?
}

[Test]
Expand Down

0 comments on commit 15cc5b4

Please sign in to comment.