feat: add reusable virtual hud input
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using NUnit.Framework;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
using XGame;
|
||||
using XGame.VirtualInput;
|
||||
|
||||
public sealed class LobbyWorldControllerVirtualJoystickTests
|
||||
{
|
||||
[Test]
|
||||
public void MapMoveInputForTest_UsesJoystickVectorWithCameraAxes()
|
||||
{
|
||||
Vector3 dir = LobbyWorldController.MapMoveInputForTest(
|
||||
new Vector2(1f, 0f),
|
||||
Vector3.forward,
|
||||
Vector3.right,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false);
|
||||
|
||||
Assert.That(dir, Is.EqualTo(Vector3.right));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MapMoveInputForTest_CombinesKeyboardAndJoystick()
|
||||
{
|
||||
Vector3 dir = LobbyWorldController.MapMoveInputForTest(
|
||||
new Vector2(0f, 1f),
|
||||
Vector3.forward,
|
||||
Vector3.right,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false);
|
||||
|
||||
Assert.That(dir, Is.EqualTo(Vector3.forward * 2f));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReleaseJoystickUIForTest_ClearsVirtualJoystickReferences()
|
||||
{
|
||||
GameObject controllerObj = new GameObject("LobbyWorldControllerTest");
|
||||
GameObject joystickObj = new GameObject("UI_VirtualJoystick", typeof(RectTransform));
|
||||
try
|
||||
{
|
||||
LobbyWorldController controller = controllerObj.AddComponent<LobbyWorldController>();
|
||||
VirtualJoystick joystick = joystickObj.AddComponent<VirtualJoystick>();
|
||||
SetPrivateField(controller, "joystickRoot", joystickObj.GetComponent<RectTransform>());
|
||||
SetPrivateField(controller, "virtualJoystick", joystick);
|
||||
|
||||
InvokePrivate(controller, "ReleaseJoystickUI", false);
|
||||
|
||||
Assert.That(GetPrivateField(controller, "joystickRoot"), Is.Null);
|
||||
Assert.That(GetPrivateField(controller, "virtualJoystick"), Is.Null);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Object.DestroyImmediate(joystickObj);
|
||||
Object.DestroyImmediate(controllerObj);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetPrivateField(object target, string name, object value)
|
||||
{
|
||||
FieldInfo field = target.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
Assert.That(field, Is.Not.Null);
|
||||
field.SetValue(target, value);
|
||||
}
|
||||
|
||||
private static object GetPrivateField(object target, string name)
|
||||
{
|
||||
FieldInfo field = target.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
Assert.That(field, Is.Not.Null);
|
||||
return field.GetValue(target);
|
||||
}
|
||||
|
||||
private static void InvokePrivate(object target, string name, params object[] args)
|
||||
{
|
||||
MethodInfo method = target.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
Assert.That(method, Is.Not.Null);
|
||||
method.Invoke(target, args);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user