// "Wave SDK
// © 2020 HTC Corporation. All Rights Reserved.
//
// Unless otherwise required by copyright law and practice,
// upon the execution of HTC SDK license agreement,
// HTC grants you access to and use of the WaveVR SDK(s).
// You shall fully comply with all of HTC’s SDK license agreement terms and
// conditions signed by you and all SDK and API requirements,
// specifications, and documentation provided by HTC to You."
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
{
#if UNITY_EDITOR
///
/// The class is designed to record all grab poses for all hand Grabbables.
///
public class GrabPoseBinder : ScriptableObject
{
///
/// This struct records the grab pose for grabbable object.
///
[Serializable]
private struct GrabPoseBindFormat
{
[SerializeField]
public string grabbableName;
[SerializeField]
public List grabPoses;
public GrabPoseBindFormat(string in_GrabbableName, List in_GrabPoses)
{
grabbableName = in_GrabbableName;
grabPoses = in_GrabPoses;
}
public GrabPoseBindFormat Identity => new GrabPoseBindFormat(string.Empty, new List());
public void Update(List grabPoses)
{
this.grabPoses.Clear();
this.grabPoses.AddRange(grabPoses);
}
public void Reset()
{
grabbableName = string.Empty;
grabPoses.Clear();
}
public override bool Equals(object obj)
{
return obj is GrabPoseBindFormat grabPoseBindFormat &&
grabbableName == grabPoseBindFormat.grabbableName &&
grabPoses == grabPoseBindFormat.grabPoses;
}
public override int GetHashCode()
{
return grabbableName.GetHashCode() ^ grabPoses.GetHashCode();
}
public static bool operator ==(GrabPoseBindFormat source, GrabPoseBindFormat target) => source.Equals(target);
public static bool operator !=(GrabPoseBindFormat source, GrabPoseBindFormat target) => !(source == target);
}
[SerializeField]
private List m_BindingInfos = new List();
///
/// Update the binding information for each hand grabbable object.
///
public void UpdateBindingInfos()
{
m_BindingInfos.Clear();
foreach (HandGrabInteractable grabbable in GrabManager.handGrabbables)
{
m_BindingInfos.Add(new GrabPoseBindFormat(grabbable.name, grabbable.grabPoses));
}
}
///
/// Stores the binding information.
///
/// True if storage is successful; otherwise, false.
public bool StorageData()
{
if (m_BindingInfos.Count == 0) { return false; }
EditorApplication.delayCall += () =>
{
AssetDatabase.Refresh();
EditorUtility.SetDirty(this);
AssetDatabase.SaveAssets();
};
return true;
}
///
/// Finds grab poses associated with the specified hand grabbable object.
///
/// The hand grabbable object to search for.
/// The output parameter to store the found grab poses.
/// True if grab poses are found for the grabbable object; otherwise, false.
public bool FindGrabPosesWithGrabbable(HandGrabInteractable grabbable, out List grabPoses)
{
grabPoses = new List();
GrabPoseBindFormat bindingInfo = m_BindingInfos.Find(x => x.grabbableName == grabbable.name);
if (bindingInfo != null)
{
grabPoses = bindingInfo.grabPoses;
return true;
}
return false;
}
}
#endif
}