MuscleInspectorEditor.cs
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
namespace UniHumanoid
{
class BoneNode : IEnumerable<BoneNode>
{
public HumanBodyBones Bone { get; private set; }
public List<BoneNode> Children = new List<BoneNode>();
public int[] Muscles;
public BoneNode(HumanBodyBones bone, params int[] muscles)
{
Bone = bone;
Muscles = muscles;
}
public IEnumerator<BoneNode> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public void Add(BoneNode child)
{
Children.Add(child);
}
}
class BoneTreeViewItem : TreeViewItem
{
//HumanBodyBones m_bone;
public BoneTreeViewItem(int id, int depth, HumanBodyBones bone) : base(id, depth, bone.ToString())
{
//m_bone = bone;
}
}
class MuscleTreeViewItem : TreeViewItem
{
public int Muscle
{
get;
private set;
}
public MuscleTreeViewItem(int id, int depth, int muscle) : base(id, depth, HumanTrait.MuscleName[muscle])
{
Muscle = muscle;
}
}
class BoneTreeView : TreeView
{
static BoneNode Skeleton = new BoneNode(HumanBodyBones.Hips)
{
new BoneNode(HumanBodyBones.Spine, 0, 1, 2){
new BoneNode(HumanBodyBones.Chest, 3, 4, 5){
new BoneNode(HumanBodyBones.UpperChest, 6, 7, 8){
new BoneNode(HumanBodyBones.Neck, 9, 10, 11){
new BoneNode(HumanBodyBones.Head, 12, 13, 14){
new BoneNode(HumanBodyBones.LeftEye, 15, 16),
new BoneNode(HumanBodyBones.RightEye, 17, 18)
}
},
new BoneNode(HumanBodyBones.LeftShoulder, 37, 38){
new BoneNode(HumanBodyBones.LeftUpperArm, 39, 40, 41){
new BoneNode(HumanBodyBones.LeftLowerArm, 42, 43){
new BoneNode(HumanBodyBones.LeftHand, 44, 45)
}
}
},
new BoneNode(HumanBodyBones.RightShoulder, 46, 47){
new BoneNode(HumanBodyBones.RightUpperArm, 48, 49, 50){
new BoneNode(HumanBodyBones.RightLowerArm, 51, 52){
new BoneNode(HumanBodyBones.RightHand, 53, 54)
}
}
}
}
}
},
new BoneNode(HumanBodyBones.LeftUpperLeg, 21, 22, 23){
new BoneNode(HumanBodyBones.LeftLowerLeg, 24, 25){
new BoneNode(HumanBodyBones.LeftFoot, 26, 27){
new BoneNode(HumanBodyBones.LeftToes, 28)
}
}
},
new BoneNode(HumanBodyBones.RightUpperLeg, 29, 30, 31){
new BoneNode(HumanBodyBones.RightLowerLeg, 32, 33){
new BoneNode(HumanBodyBones.RightFoot, 34, 35){
new BoneNode(HumanBodyBones.RightToes, 36)
}
}
}
};
//Animator m_animator;
HumanPoseHandler m_handler;
HumanPose m_pose;
bool m_updated;
public void Begin()
{
m_handler.GetHumanPose(ref m_pose);
}
public void End()
{
if (m_updated)
{
m_handler.SetHumanPose(ref m_pose);
}
m_updated = false;
}
public BoneTreeView(TreeViewState treeViewState, MultiColumnHeader header, HumanPoseHandler handler)
: base(treeViewState, header)
{
m_handler = handler;
Reload();
}
protected override TreeViewItem BuildRoot()
{
return new TreeViewItem { id = 0, depth = -1 };
}
protected override IList<TreeViewItem> BuildRows(TreeViewItem root)
{
var rows = GetRows() ?? new List<TreeViewItem>(200);
// We use the GameObject instanceIDs as ids for items as we want to
// select the game objects and not the transform components.
rows.Clear();
var item = CreateTreeViewItemForBone(HumanBodyBones.Hips);
root.AddChild(item);
rows.Add(item);
if (IsExpanded(item.id))
{
AddChildrenRecursive(Skeleton, item, rows);
}
else
{
item.children = CreateChildListForCollapsedParent();
}
SetupDepthsFromParentsAndChildren(root);
return rows;
}
void AddChildrenRecursive(BoneNode bone, TreeViewItem item, IList<TreeViewItem> rows)
{
int childCount = bone.Children.Count;
item.children = new List<TreeViewItem>(childCount);
if (bone.Muscles != null)
{
foreach (var muscle in bone.Muscles)
{
var childItem = new MuscleTreeViewItem(muscle + 20000, -1, muscle);
item.AddChild(childItem);
rows.Add(childItem);
}
}
foreach (var child in bone.Children)
{
var childItem = CreateTreeViewItemForBone(child.Bone);
item.AddChild(childItem);
rows.Add(childItem);
//if (child.Children.Count > 0)
{
if (IsExpanded(childItem.id))
{
AddChildrenRecursive(child, childItem, rows);
}
else
{
childItem.children = CreateChildListForCollapsedParent();
}
}
}
}
static TreeViewItem CreateTreeViewItemForBone(HumanBodyBones bone)
{
return new TreeViewItem((int)bone, -1, Enum.GetName(typeof(HumanBodyBones), bone));
}
protected override void RowGUI(RowGUIArgs args)
{
for (int i = 0; i < args.GetNumVisibleColumns(); ++i)
{
CellGUI(args.GetCellRect(i), args.GetColumn(i), ref args);
}
}
void CellGUI(Rect cellRect, int index, ref RowGUIArgs args)
{
// Center cell rect vertically (makes it easier to place controls, icons etc in the cells)
CenterRectUsingSingleLineHeight(ref cellRect);
switch (index)
{
case 0:
{
// Default icon and label
args.rowRect = cellRect;
base.RowGUI(args);
}
break;
case 1:
{
var muscleItem = args.item as MuscleTreeViewItem;
if (muscleItem != null)
{
var muscleIndex = muscleItem.Muscle;
var muscles = m_pose.muscles;
var value = EditorGUI.Slider(cellRect, GUIContent.none, muscles[muscleIndex], -1f, 1f);
if (value != muscles[muscleIndex])
{
muscles[muscleIndex] = value;
m_updated = true;
}
}
else
{
}
}
break;
}
}
public static MultiColumnHeaderState CreateDefaultMultiColumnHeaderState()
{
var columns = new[]
{
new MultiColumnHeaderState.Column
{
headerContent = new GUIContent("Name"),
headerTextAlignment = TextAlignment.Left,
width = 250,
minWidth = 60,
autoResize = false,
allowToggleVisibility = false
},
new MultiColumnHeaderState.Column
{
headerContent = new GUIContent("Muscle value"),
headerTextAlignment = TextAlignment.Left,
width = 110,
minWidth = 60,
autoResize = true
},
};
return new MultiColumnHeaderState(columns);
}
}
[CustomEditor(typeof(MuscleInspector))]
public class MuscleInspectorEditor : Editor
{
[NonSerialized] bool m_Initialized;
[SerializeField] TreeViewState m_TreeViewState; // Serialized in the window layout file so it survives assembly reloading
//[SerializeField] MultiColumnHeaderState m_MultiColumnHeaderState;
SearchField m_SearchField;
BoneTreeView m_TreeView;
MuscleInspector m_target;
HumanPoseHandler m_handler;
MultiColumnHeader GetHeaderState()
{
//bool firstInit = m_MultiColumnHeaderState == null;
var headerState = BoneTreeView.CreateDefaultMultiColumnHeaderState();
/*
if (MultiColumnHeaderState.CanOverwriteSerializedFields(m_MultiColumnHeaderState, headerState))
{
MultiColumnHeaderState.OverwriteSerializedFields(m_MultiColumnHeaderState, headerState);
}
m_MultiColumnHeaderState = headerState;
*/
var multiColumnHeader = new MultiColumnHeader(headerState);
multiColumnHeader.ResizeToFit();
return multiColumnHeader;
}
void OnEnable()
{
var mi = this.target as MuscleInspector;
var animator = mi.GetComponent<Animator>();
if (animator != null
&& animator.avatar != null
&& animator.avatar.isValid
&& animator.avatar.isHuman
)
{
Debug.LogFormat("MuscleInspectorEditor.OnEnable");
m_handler = new HumanPoseHandler(animator.avatar, animator.transform);
m_TreeView = new BoneTreeView(new TreeViewState(), GetHeaderState(), m_handler);
}
}
void OnDisable()
{
if (m_handler != null)
{
m_handler.Dispose();
m_handler = null;
}
}
public override void OnInspectorGUI()
{
if (m_TreeView == null)
{
EditorGUILayout.HelpBox("Animator required", MessageType.Error);
return;
}
var rect = GUILayoutUtility.GetRect(0, 10000, 0, m_TreeView.totalHeight);
m_TreeView.Begin();
m_TreeView.OnGUI(rect);
m_TreeView.End();
}
}
}