Skip to content

Commit

Permalink
Move large conditional blocks to partial classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Oct 28, 2024
1 parent c217ed5 commit 5bed573
Show file tree
Hide file tree
Showing 7 changed files with 451 additions and 439 deletions.
99 changes: 99 additions & 0 deletions Source/CustomAvatar-Editor/Scripts/AvatarDescriptor.Editor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.IO;
using System.Reflection;
using UnityEngine;

namespace CustomAvatar
{
public partial class AvatarDescriptor
{
private Mesh _saberMesh;

protected void OnDrawGizmos()
{
if (!isActiveAndEnabled) return;
if (!_saberMesh) _saberMesh = LoadMesh(Assembly.GetExecutingAssembly().GetManifestResourceStream("CustomAvatar.Resources.saber.dat"));

DrawSaber(transform.Find("LeftHand"), _saberMesh, new Color(0.78f, 0.08f, 0.08f));
DrawSaber(transform.Find("RightHand"), _saberMesh, new Color(0, 0.46f, 0.82f));
}

private Mesh LoadMesh(Stream stream)
{
var mesh = new Mesh();

using (var reader = new BinaryReader(stream))
{
int length = reader.ReadInt32();
var vertices = new Vector3[length];

for (int i = 0; i < length; i++)
{
vertices[i] = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}

length = reader.ReadInt32();
var normals = new Vector3[length];

for (int i = 0; i < length; i++)
{
normals[i] = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}

length = reader.ReadInt32();
int[] triangles = new int[length];

for (int i = 0; i < length; i++)
{
triangles[i] = reader.ReadInt32();
}

mesh.SetVertices(vertices);
mesh.SetNormals(normals);
mesh.SetTriangles(triangles, 0);
}

return mesh;
}

internal void SaveMesh(Mesh mesh)
{
using (var writer = new BinaryWriter(File.OpenWrite("mesh.dat")))
{
writer.Write(mesh.vertices.Length);

foreach (Vector3 vertex in mesh.vertices)
{
writer.Write(vertex.x);
writer.Write(vertex.y);
writer.Write(vertex.z);
}

writer.Write(mesh.normals.Length);

foreach (Vector3 normal in mesh.normals)
{
writer.Write(normal.x);
writer.Write(normal.y);
writer.Write(normal.z);
}

writer.Write(mesh.triangles.Length);

foreach (int triangle in mesh.triangles)
{
writer.Write(triangle);
}
}
}

private void DrawSaber(Transform transform, Mesh mesh, Color color)
{
if (!transform) return;

Color prev = Gizmos.color;
Gizmos.color = color;
Gizmos.DrawMesh(mesh, transform.position, transform.rotation, Vector3.one);
Gizmos.color = prev;
}
}
}
79 changes: 79 additions & 0 deletions Source/CustomAvatar-Editor/Scripts/EventManager.Editor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace CustomAvatar
{
public partial class EventManager
{
[SerializeField]
[FormerlySerializedAs("OnSlice")]
private UnityEvent _leftGoodCut;

[SerializeField]
[FormerlySerializedAs("OnSlice")]
private UnityEvent _rightGoodCut;

[SerializeField]
private UnityEvent _leftBadCut;

[SerializeField]
private UnityEvent _rightBadCut;

[SerializeField]
private UnityEvent _leftNoteMissed;

[SerializeField]
private UnityEvent _rightNoteMissed;

[SerializeField]
[FormerlySerializedAs("SaberStartColliding")]
private UnityEvent _leftSaberStartedColliding;

[SerializeField]
[FormerlySerializedAs("SaberStartColliding")]
private UnityEvent _rightSaberStartedColliding;

[SerializeField]
[FormerlySerializedAs("SaberStopColliding")]
private UnityEvent _leftSaberStoppedColliding;

[SerializeField]
[FormerlySerializedAs("SaberStopColliding")]
private UnityEvent _rightSaberStoppedColliding;

[SerializeField]
[FormerlySerializedAs("OnComboChanged")]
private IntUnityEvent _comboIncreased;

[SerializeField]
[FormerlySerializedAs("OnComboBreak")]
private UnityEvent _comboBroken;

[SerializeField]
[FormerlySerializedAs("MultiplierUp")]
private IntUnityEvent _multiplierIncreased;

[SerializeField]
private IntUnityEvent _multiplierDecreased;

[SerializeField]
[FormerlySerializedAs("OnLevelStart")]
private UnityEvent _levelStarted;

[SerializeField]
[FormerlySerializedAs("OnLevelFinish")]
private UnityEvent _levelFinished;

[SerializeField]
[FormerlySerializedAs("OnLevelFail")]
private UnityEvent _levelFailed;

[SerializeField]
[FormerlySerializedAs("OnMenuEnter")]
private UnityEvent _menuEntered;

[SerializeField]
private UnityEvent _multiplayerLobbyEntered;
}
}
79 changes: 79 additions & 0 deletions Source/CustomAvatar-Editor/Scripts/VRIKManager.Editor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using UnityEditor;
using UnityEngine;

namespace CustomAvatar
{
public partial class VRIKManager
{
private GUIStyle _redLabelStyle;
private GUIStyle _greenLabelStyle;
private GUIStyle _blueLabelStyle;

protected void OnDrawGizmosSelected()
{
if (_redLabelStyle == null)
{
_redLabelStyle = new GUIStyle(EditorStyles.label);
_redLabelStyle.normal.textColor = Color.red;
}

if (_greenLabelStyle == null)
{
_greenLabelStyle = new GUIStyle(EditorStyles.label);
_greenLabelStyle.normal.textColor = Color.green;
}

if (_blueLabelStyle == null)
{
_blueLabelStyle = new GUIStyle(EditorStyles.label);
_blueLabelStyle.normal.textColor = Color.blue;
}

DrawHandAxes(references_leftHand, solver_leftArm_wristToPalmAxis, solver_leftArm_palmToThumbAxis, true);
DrawHandAxes(references_rightHand, solver_rightArm_wristToPalmAxis, solver_rightArm_palmToThumbAxis, false);
}

private void DrawHandAxes(Transform reference, Vector3 wristToPalmAxis, Vector3 palmToThumbAxis, bool invertNormal)
{
if (!reference)
{
return;
}

Vector3 wristToPalmVector = default;
Vector3 palmToThumbVector = default;

if (wristToPalmAxis.sqrMagnitude > 0)
{
wristToPalmVector = reference.rotation * wristToPalmAxis.normalized;

Handles.color = Color.green;
Handles.ArrowHandleCap(0, reference.position, Quaternion.LookRotation(wristToPalmVector), 0.1f, EventType.Repaint);
Handles.Label(reference.position + wristToPalmVector * 0.12f, "Wrist to Palm Axis", _greenLabelStyle);
}

if (palmToThumbAxis.sqrMagnitude > 0)
{
palmToThumbVector = reference.rotation * palmToThumbAxis.normalized;

Handles.color = Color.red;
Handles.ArrowHandleCap(0, reference.position, Quaternion.LookRotation(palmToThumbVector), 0.1f, EventType.Repaint);
Handles.Label(reference.position + palmToThumbVector * 0.12f, "Palm to Thumb Axis", _redLabelStyle);
}

if (wristToPalmAxis.sqrMagnitude > 0 && palmToThumbAxis.sqrMagnitude > 0)
{
Vector3 planeNormal = new Plane(reference.position, reference.position + wristToPalmVector, reference.position + palmToThumbVector).normal;

if (invertNormal)
{
planeNormal = -planeNormal;
}

Handles.color = Color.blue;
Handles.ArrowHandleCap(0, reference.position, Quaternion.LookRotation(planeNormal), 0.1f, EventType.Repaint);
Handles.Label(reference.position + planeNormal * 0.12f, "Palm Inside Axis", _blueLabelStyle);
}
}
}
}
99 changes: 1 addition & 98 deletions Source/CustomAvatar/Scripts/AvatarDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
using UnityEngine;
using System.Linq;

#if UNITY_EDITOR
using System.IO;
using System.Reflection;
#endif

// keeping root namespace for compatibility
namespace CustomAvatar
{
/// <summary>
/// Container for an avatar's name and other information configured before exportation.
/// </summary>
[DisallowMultipleComponent]
public class AvatarDescriptor : MonoBehaviour, ISerializationCallbackReceiver
public partial class AvatarDescriptor : MonoBehaviour, ISerializationCallbackReceiver
{
/// <summary>
/// Avatar's name.
Expand Down Expand Up @@ -74,97 +69,5 @@ public void OnAfterDeserialize()

// Editor calls DoesObjectWithInstanceIDExist which doesn't exist at run time and blows up if not on the main thread, so just check the cached pointer.
private T FirstNonNullUnityObject<T>(params T[] objects) where T : Object => objects.FirstOrDefault(o => o is not null && o.GetCachedPtr() != System.IntPtr.Zero);

#if UNITY_EDITOR
private Mesh _saberMesh;

protected void OnDrawGizmos()
{
if (!isActiveAndEnabled) return;
if (!_saberMesh) _saberMesh = LoadMesh(Assembly.GetExecutingAssembly().GetManifestResourceStream("CustomAvatar.Resources.saber.dat"));

DrawSaber(transform.Find("LeftHand"), _saberMesh, new Color(0.78f, 0.08f, 0.08f));
DrawSaber(transform.Find("RightHand"), _saberMesh, new Color(0, 0.46f, 0.82f));
}

private Mesh LoadMesh(Stream stream)
{
var mesh = new Mesh();

using (var reader = new BinaryReader(stream))
{
int length = reader.ReadInt32();
var vertices = new Vector3[length];

for (int i = 0; i < length; i++)
{
vertices[i] = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}

length = reader.ReadInt32();
var normals = new Vector3[length];

for (int i = 0; i < length; i++)
{
normals[i] = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}

length = reader.ReadInt32();
int[] triangles = new int[length];

for (int i = 0; i < length; i++)
{
triangles[i] = reader.ReadInt32();
}

mesh.SetVertices(vertices);
mesh.SetNormals(normals);
mesh.SetTriangles(triangles, 0);
}

return mesh;
}

internal void SaveMesh(Mesh mesh)
{
using (var writer = new BinaryWriter(File.OpenWrite("mesh.dat")))
{
writer.Write(mesh.vertices.Length);

foreach (Vector3 vertex in mesh.vertices)
{
writer.Write(vertex.x);
writer.Write(vertex.y);
writer.Write(vertex.z);
}

writer.Write(mesh.normals.Length);

foreach (Vector3 normal in mesh.normals)
{
writer.Write(normal.x);
writer.Write(normal.y);
writer.Write(normal.z);
}

writer.Write(mesh.triangles.Length);

foreach (int triangle in mesh.triangles)
{
writer.Write(triangle);
}
}
}

private void DrawSaber(Transform transform, Mesh mesh, Color color)
{
if (!transform) return;

Color prev = Gizmos.color;
Gizmos.color = color;
Gizmos.DrawMesh(mesh, transform.position, transform.rotation, Vector3.one);
Gizmos.color = prev;
}
#endif
}
}
Loading

0 comments on commit 5bed573

Please sign in to comment.