// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using UltimateXR.Avatar;
using UltimateXR.Manipulation.HandPoses;
using UnityEditor;
using UnityEngine;
namespace UltimateXR.Editor
{
public static partial class UxrEditorUtils
{
#region Public Methods
///
/// Shows a dropdown hand pose list for an avatar using .
///
/// GUI content
/// Avatar with the poses that will be listed
///
/// The serialized property with the hand pose. Should be SerializedProperty for a UxrHandPoseAsset field.
///
/// The selected hand pose
/// Whether the current selection changed
public static bool HandPoseDropdown(GUIContent guiContent, UxrAvatar avatar, SerializedProperty handPosesProperty, out UxrHandPoseAsset selectedHandPose)
{
bool hasChanged = false;
GetPoseDropdownInfo(avatar, handPosesProperty, out List handPoses, out selectedHandPose, out int selectedPoseIndex);
EditorGUI.BeginChangeCheck();
int popup = EditorGUILayout.Popup(guiContent.text, selectedPoseIndex, handPoses.Select(p => p.name).ToArray());
if (EditorGUI.EndChangeCheck())
{
hasChanged = true;
selectedHandPose = handPoses[popup];
handPosesProperty.objectReferenceValue = selectedHandPose;
}
return hasChanged;
}
///
/// Shows a dropdown hand pose list for an avatar using .
///
/// GUI rect
/// GUI content
/// Avatar with the poses that will be listed
///
/// The serialized property with the hand pose. Should be SerializedProperty for a UxrHandPoseAsset field.
///
/// The selected hand pose
/// Whether the current selection changed
public static bool HandPoseDropdown(Rect rect, GUIContent guiContent, UxrAvatar avatar, SerializedProperty handPosesProperty, out UxrHandPoseAsset selectedHandPose)
{
bool hasChanged = false;
GetPoseDropdownInfo(avatar, handPosesProperty, out List handPoses, out selectedHandPose, out int selectedPoseIndex);
EditorGUI.BeginChangeCheck();
int popup = EditorGUI.Popup(rect, guiContent.text, selectedPoseIndex, handPoses.Select(p => p.name).ToArray());
if (EditorGUI.EndChangeCheck())
{
hasChanged = true;
selectedHandPose = handPoses[popup];
handPosesProperty.objectReferenceValue = selectedHandPose;
}
return hasChanged;
}
#endregion
#region Private Methods
///
/// Gets information used by the HandPoseDropdown methods.
///
/// Avatar to get the poses of
/// SerializedProperty of the field that stores the
/// Returns the avatar hand poses
/// Returns the selected avatar hand pose
///
/// Returns the list position of in
///
///
private static void GetPoseDropdownInfo(UxrAvatar avatar, SerializedProperty handPosesProperty, out List handPoses, out UxrHandPoseAsset selectedHandPose, out int selectedPoseIndex)
{
selectedHandPose = null;
handPoses = avatar != null ? avatar.GetAllHandPoses().ToList() : new List();
selectedPoseIndex = -1;
if (handPoses.Count > 0 && handPosesProperty.objectReferenceValue != null)
{
UxrHandPoseAsset handPoseAsset = handPosesProperty.objectReferenceValue as UxrHandPoseAsset;
// Instead of using the pose directly we make sure it comes from the avatar list, in case a pose is overriden
if (handPoseAsset)
{
selectedHandPose = handPoses.FirstOrDefault(p => p.name == handPoseAsset.name);
if (selectedHandPose != null)
{
selectedPoseIndex = handPoses.IndexOf(selectedHandPose);
}
}
}
}
#endregion
}
}