forked from Real-Serious-Games/Unity-Weld
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSubViewModelBindingEditor.cs
More file actions
95 lines (82 loc) · 2.96 KB
/
Copy pathSubViewModelBindingEditor.cs
File metadata and controls
95 lines (82 loc) · 2.96 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
using UnityEngine;
using UnityEditor;
using UnityWeld.Binding;
using UnityWeld.Binding.Internal;
using System.Linq;
using System.Reflection;
namespace UnityWeld_Editor
{
/// <summary>
/// Inspector window for SubViewModelBinding
/// </summary>
[CustomEditor(typeof(SubViewModelBinding))]
public class SubViewModelBindingEditor : BaseBindingEditor
{
private SubViewModelBinding targetScript;
/// <summary>
/// Whether or not the value on our target matches its prefab.
/// </summary>
private bool propertyPrefabModified;
protected override void OnEnabled()
{
targetScript = (SubViewModelBinding)target;
}
protected override void OnInspector()
{
UpdatePrefabModifiedProperties();
BeginArea(new GUIContent("Sub View-Model"));
var bindableProperties = FindBindableProperties();
EditorStyles.label.fontStyle = propertyPrefabModified
? FontStyle.Bold
: DefaultFontStyle;
ShowViewModelPropertyMenu(
new GUIContent(
"Property",
"The Property on the top level View-Model containing the Sub View-Model"
),
bindableProperties,
updatedValue =>
{
targetScript.ViewModelPropertyName = updatedValue;
targetScript.ViewModelTypeName = bindableProperties
.Single(prop => prop.ToString() == updatedValue)
.Member.PropertyType.ToString();
},
targetScript.ViewModelPropertyName,
p => true
);
EndArea();
}
private BindableMember<PropertyInfo>[] FindBindableProperties()
{
return TypeResolver.FindBindableProperties(targetScript)
.Where(prop => prop.Member.PropertyType.HasBindingAttribute()
)
.ToArray();
}
/// <summary>
/// Check whether each of the properties on the object have been changed
/// from the value in the prefab.
/// </summary>
private void UpdatePrefabModifiedProperties()
{
var property = serializedObject.GetIterator();
// Need to call Next(true) to get the first child. Once we have it, Next(false)
// will iterate through the properties.
propertyPrefabModified = false;
property.Next(true);
do
{
switch (property.name)
{
case "viewModelPropertyName":
case "viewModelTypeName":
propertyPrefabModified = property.prefabOverride
|| propertyPrefabModified;
break;
}
}
while (property.Next(false));
}
}
}