Tanoda
PropertyRecorderEditor.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Linq;
11using System.Collections.Generic;
12using UnityEngine;
13using UnityEditor;
14using Leap.Unity;
15using Leap.Unity.Query;
16
17namespace Leap.Unity.Recording {
18
19 [CanEditMultipleObjects]
20 [CustomEditor(typeof(PropertyRecorder))]
21 public class PropertyRecorderEditor : CustomEditorBase<PropertyRecorder> {
22
23 protected override void OnEnable() {
24 base.OnEnable();
25
26 specifyCustomDrawer("_bindings", drawProperties);
27 hideField("_expandedTypes");
28 }
29
30 private void drawProperties(SerializedProperty list) {
31 List<EditorCurveBinding> bindings = null;
32
33 foreach (var target in targets) {
34 var parent = target.transform.root.gameObject;
35 var singleBinding = new List<EditorCurveBinding>(AnimationUtility.GetAnimatableBindings(target.gameObject, parent));
36 if (bindings == null) {
37 bindings = new List<EditorCurveBinding>(singleBinding);
38 } else {
39 for (int i = bindings.Count; i-- != 0;) {
40 if (!singleBinding.Query().Any(t => t.propertyName == bindings[i].propertyName && t.type == bindings[i].type)) {
41 bindings.RemoveAt(i);
42 }
43 }
44 }
45 }
46
47 bindings = bindings.Query().
48 Where(t => t.type != typeof(PropertyRecorder) &&
49 t.type != typeof(GameObject) &&
50 t.type != typeof(Transform) &&
51 t.propertyName != "m_Enabled").
52 ToList();
53
54 bool shouldOverride = false;
55 bool overrideValue = false;
56 Type currType = null;
57 EditorGUI.indentLevel++;
58
59 foreach (var binding in bindings) {
60 bool isTypeExpanded = targets.All(t => t.IsBindingExpanded(binding));
61
62 if (binding.type != currType) {
63 currType = binding.type;
64 shouldOverride = false;
65
66 EditorGUI.indentLevel--;
67 EditorGUILayout.BeginHorizontal();
68
69 isTypeExpanded = EditorGUILayout.Foldout(isTypeExpanded, binding.type.Name);
70 foreach (var target in targets) {
71 target.SetBindingExpanded(binding, isTypeExpanded);
72 }
73
74 if (GUILayout.Button("Record All")) {
75 shouldOverride = true;
76 overrideValue = true;
77 }
78
79 if (GUILayout.Button("Clear All")) {
80 shouldOverride = true;
81 overrideValue = false;
82 }
83
84 EditorGUILayout.EndHorizontal();
85 EditorGUI.indentLevel++;
86 }
87
88 if (isTypeExpanded) {
89 EditorGUI.showMixedValue = !targets.Query().Select(t => t.IsBindingEnabled(binding)).AllEqual();
90 bool isEnabled = target.IsBindingEnabled(binding);
91
92 EditorGUI.BeginChangeCheck();
93 isEnabled = EditorGUILayout.ToggleLeft(binding.propertyName, isEnabled);
94
95 if (EditorGUI.EndChangeCheck()) {
96 foreach (var target in targets) {
97 target.SetBindingEnabled(binding, isEnabled);
98 }
99 }
100 }
101
102 if (shouldOverride) {
103 foreach (var target in targets) {
104 target.SetBindingEnabled(binding, overrideValue);
105 }
106 }
107 }
108
109 EditorGUI.indentLevel--;
110
111 serializedObject.Update();
112 }
113 }
114
115}
void specifyCustomDrawer(string propertyName, Action< SerializedProperty > propertyDrawer)
Specify a callback to be used to draw a specific named property. Should be called in OnEnable.