Skip to content

Commit

Permalink
feat: decouple input logic from rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
rYuuk committed Nov 29, 2023
1 parent c986eac commit ede5544
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/AvatarMouseRotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using UnityEngine;
using UnityEngine.EventSystems;

namespace ReadyPlayerMe.AvatarCreator
{
public class AvatarMouseRotation: MonoBehaviour
{
private const int MOUSE_BUTTON_INDEX = 0;

[SerializeField] private float speed = 50;

private IMouseInput mouseInput;
private float lastPosX;
private bool rotate;

private void Awake()
{
mouseInput = GetComponent<IMouseInput>();
}

private void Update()
{
if (EventSystem.current.IsPointerOverGameObject() && !rotate)
{
return;
}

if (mouseInput.GetButtonDown(MOUSE_BUTTON_INDEX))
{
lastPosX = Input.mousePosition.x;
rotate = true;
}

if (mouseInput.GetButtonUp(MOUSE_BUTTON_INDEX))
{
rotate = false;
}

if (mouseInput.GetButtonPressed(MOUSE_BUTTON_INDEX))
{
transform.Rotate(Vector3.up, (lastPosX - Input.mousePosition.x) * (Time.deltaTime * speed));
lastPosX = Input.mousePosition.x;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/IMouseInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ReadyPlayerMe.AvatarCreator
{
public interface IMouseInput
{
bool GetButtonDown(int index);
bool GetButtonUp(int index);
bool GetButtonPressed(int index);
}
}
3 changes: 3 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/IMouseInput.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/MouseInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class MouseInput: MonoBehaviour, IMouseInput
{
public bool GetButtonDown(int index)
{
return Input.GetMouseButtonDown(index);
}

public bool GetButtonUp(int index)
{
return Input.GetMouseButtonUp(index);
}

public bool GetButtonPressed(int index)
{
return Input.GetMouseButton(index);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/MouseInput.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ private void ProcessAvatar(GameObject avatar)
avatar.GetComponent<Animator>().runtimeAnimatorController = animator;
}
avatar.transform.rotation = lastRotation;
avatar.AddComponent<RotateAvatar>();
avatar.AddComponent<MouseInput>();
avatar.AddComponent<AvatarMouseRotation>();
}

public void Dispose()
Expand Down

0 comments on commit ede5544

Please sign in to comment.