|
| 1 | +// // ---------------------------------------------------------------------------- |
| 2 | +// // <copyright file="KinetixMaskEditor.cs" company="Kinetix SAS"> |
| 3 | +// // Kinetix Unity SDK - Copyright (C) 2022 Kinetix SAS |
| 4 | +// // </copyright> |
| 5 | +// // ---------------------------------------------------------------------------- |
| 6 | + |
| 7 | +using UnityEditor; |
| 8 | +using UnityEngine; |
| 9 | +using UEditor = UnityEditor.Editor; |
| 10 | + |
| 11 | +namespace Kinetix.Editor |
| 12 | +{ |
| 13 | + |
| 14 | + [CustomEditor(typeof(KinetixMask), true)] |
| 15 | + public class KinetixMaskEditor : UEditor |
| 16 | + { |
| 17 | + private SerializedProperty disabledBones; |
| 18 | + private bool hasChanged; |
| 19 | + private int shownBodyPart; |
| 20 | + |
| 21 | + private void OnEnable() |
| 22 | + { |
| 23 | + disabledBones = serializedObject.FindProperty( nameof(disabledBones) ); |
| 24 | + } |
| 25 | + |
| 26 | + public override void OnInspectorGUI() |
| 27 | + { |
| 28 | + serializedObject.Update(); |
| 29 | + hasChanged = false; |
| 30 | + |
| 31 | + if (disabledBones.arraySize != (int)HumanBodyBones.LastBone) |
| 32 | + { |
| 33 | + disabledBones.arraySize = (int)HumanBodyBones.LastBone; |
| 34 | + serializedObject.ApplyModifiedProperties(); |
| 35 | + serializedObject.Update(); |
| 36 | + } |
| 37 | + |
| 38 | + DrawBoneMapping(); |
| 39 | + |
| 40 | + GUILayout.Space(8); |
| 41 | + GUILayout.BeginHorizontal(); |
| 42 | + if (GUILayout.Button("Enable all" , EditorStyles.miniButtonLeft)) |
| 43 | + { |
| 44 | + HumanBodyBones[] bones = KinetixMaskView.VIEW_BONES[shownBodyPart]; |
| 45 | + for (int i = bones.Length - 1; i >= 0; i--) |
| 46 | + { |
| 47 | + disabledBones.GetArrayElementAtIndex((int)bones[i]).boolValue = false; |
| 48 | + } |
| 49 | + |
| 50 | + hasChanged = true; |
| 51 | + } |
| 52 | + |
| 53 | + if (GUILayout.Button("Toggle all" , EditorStyles.miniButtonMid)) |
| 54 | + { |
| 55 | + HumanBodyBones[] bones = KinetixMaskView.VIEW_BONES[shownBodyPart]; |
| 56 | + for (int i = bones.Length - 1; i >= 0; i--) |
| 57 | + { |
| 58 | + SerializedProperty serializedProperty = disabledBones.GetArrayElementAtIndex((int)bones[i]); |
| 59 | + serializedProperty.boolValue = !serializedProperty.boolValue; |
| 60 | + } |
| 61 | + |
| 62 | + hasChanged = true; |
| 63 | + } |
| 64 | + |
| 65 | + if (GUILayout.Button("Disable all", EditorStyles.miniButtonRight)) |
| 66 | + { |
| 67 | + HumanBodyBones[] bones = KinetixMaskView.VIEW_BONES[shownBodyPart]; |
| 68 | + for (int i = bones.Length - 1; i >= 0; i--) |
| 69 | + { |
| 70 | + disabledBones.GetArrayElementAtIndex((int)bones[i]).boolValue = true; |
| 71 | + } |
| 72 | + |
| 73 | + hasChanged = true; |
| 74 | + } |
| 75 | + GUILayout.EndHorizontal(); |
| 76 | + |
| 77 | + |
| 78 | + if (hasChanged) |
| 79 | + { |
| 80 | + serializedObject.ApplyModifiedProperties(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + private void DrawBoneMapping() |
| 86 | + { |
| 87 | + GUILayout.BeginVertical("", "TE NodeBackground"); |
| 88 | + { |
| 89 | + shownBodyPart = KinetixMaskView.ShowBoneMapping(shownBodyPart, PartColor, BoneColor, Click); |
| 90 | + } |
| 91 | + GUILayout.EndVertical(); |
| 92 | + |
| 93 | + Color PartColor(BodyPart part) |
| 94 | + { |
| 95 | + HumanBodyBones[] bones = KinetixMaskView.VIEW_PART_BONES[shownBodyPart, (int)part]; |
| 96 | + int enabledCount = 0; |
| 97 | + int count = bones.Length; |
| 98 | + bool enabledBone; |
| 99 | + for (int i = 0; i < count; i++) |
| 100 | + { |
| 101 | + enabledBone = !disabledBones.GetArrayElementAtIndex((int)bones[i]).boolValue; |
| 102 | + if (enabledBone) |
| 103 | + ++enabledCount; |
| 104 | + } |
| 105 | + |
| 106 | + if (count == enabledCount) |
| 107 | + return Color.green; |
| 108 | + if (enabledCount > 0) |
| 109 | + return Color.Lerp(Color.red, Color.green, (float)enabledCount / count); |
| 110 | + |
| 111 | + return Color.red; |
| 112 | + } |
| 113 | + |
| 114 | + Color BoneColor(HumanBodyBones bone) |
| 115 | + { |
| 116 | + bool enabledBone = !disabledBones.GetArrayElementAtIndex((int)bone).boolValue; |
| 117 | + return enabledBone ? Color.green : Color.red; |
| 118 | + } |
| 119 | + |
| 120 | + void Click(HumanBodyBones bone) |
| 121 | + { |
| 122 | + SerializedProperty serializedProperty = disabledBones.GetArrayElementAtIndex((int)bone); |
| 123 | + serializedProperty.boolValue = !serializedProperty.boolValue; |
| 124 | + |
| 125 | + hasChanged = true; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments