Skip to content

Commit

Permalink
Removed second code path for getting properties.
Browse files Browse the repository at this point in the history
Also added accessors for BindableMember.ViewModelTypeName and MemberName.
  • Loading branch information
RoryDungan committed Jun 20, 2017
1 parent d6f0cff commit a3e2798
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 36 deletions.
24 changes: 23 additions & 1 deletion UnityWeld/Binding/Internal/BindableMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ public class BindableMember<MemberType> where MemberType : MemberInfo
/// </summary>
public readonly Type ViewModelType;

/// <summary>
/// Name of the view model type.
/// </summary>
public string ViewModelTypeName
{
get
{
return ViewModelType.Name;
}
}

/// <summary>
/// Name of the member.
/// </summary>
public string MemberName
{
get
{
return Member.Name;
}
}

public BindableMember(MemberType member, Type viewModelType)
{
Member = member;
Expand All @@ -29,7 +51,7 @@ public BindableMember(MemberType member, Type viewModelType)

public override string ToString()
{
return string.Concat(ViewModelType.ToString(), ".", Member.Name);
return string.Concat(ViewModelType.ToString(), ".", MemberName);
}
}
}
15 changes: 10 additions & 5 deletions UnityWeld/Binding/Internal/PropertyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ public static class PropertyFinder
/// <summary>
/// Use reflection to find all components with properties we can bind to.
/// </summary>
public static IEnumerable<PropertyInfo> GetBindableProperties(GameObject gameObject) //todo: Maybe move this to the TypeResolver.
public static IEnumerable<BindableMember<PropertyInfo>> GetBindableProperties(GameObject gameObject) //todo: Maybe move this to the TypeResolver.
{
return gameObject.GetComponents<Component>()
.SelectMany(component => component.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public))
.Where(prop => !hiddenTypes.Contains(prop.ReflectedType))
.Where(prop => !prop.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any());
.SelectMany(component =>
{
var type = component.GetType();
return type
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Select(p => new BindableMember<PropertyInfo>(p, type));
})
.Where(prop => !hiddenTypes.Contains(prop.ViewModelType))
.Where(prop => !prop.Member.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any());
}
}
}
2 changes: 1 addition & 1 deletion UnityWeld/Binding/Internal/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static BindableMember<MethodInfo>[] FindBindableMethods(EventBinding targ
)
.Where(m => m.Member.GetParameters().Length == 0)
.Where(m => m.Member.GetCustomAttributes(typeof(BindingAttribute), false).Any()
&& !m.Member.Name.StartsWith("get_")) // Exclude property getters, since we aren't doing anything with the return value of the bound method anyway.
&& !m.MemberName.StartsWith("get_")) // Exclude property getters, since we aren't doing anything with the return value of the bound method anyway.
.ToArray();
}

Expand Down
36 changes: 14 additions & 22 deletions UnityWeld_Editor/BaseBindingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Func<PropertyInfo, bool> menuEnabled
InspectorUtils.DoPopup(
new GUIContent(curPropertyValue),
label,
prop => string.Concat(prop.ViewModelType, "/", prop.Member.Name, " : ", prop.Member.PropertyType.Name),
prop => string.Concat(prop.ViewModelType, "/", prop.MemberName, " : ", prop.Member.PropertyType.Name),
prop => menuEnabled(prop.Member),
prop => prop.ToString() == curPropertyValue,
prop =>
Expand All @@ -95,8 +95,8 @@ Func<PropertyInfo, bool> menuEnabled
);
},
bindableProperties
.OrderBy(property => property.ViewModelType.Name)
.ThenBy(property => property.Member.Name)
.OrderBy(property => property.ViewModelTypeName)
.ThenBy(property => property.MemberName)
.ToArray()
);
}
Expand Down Expand Up @@ -135,11 +135,11 @@ Func<PropertyInfo, bool> menuEnabled
{
var options = bindableProperties
.Select(prop => new OptionInfo(
string.Concat(prop.ViewModelType, "/", prop.Member.Name, " : ", prop.Member.PropertyType.Name),
string.Concat(prop.ViewModelType, "/", prop.MemberName, " : ", prop.Member.PropertyType.Name),
prop
))
.OrderBy(option => option.Property.ViewModelType.Name)
.ThenBy(option => option.Property.Member.Name);
.OrderBy(option => option.Property.ViewModelTypeName)
.ThenBy(option => option.Property.MemberName);

var noneOption = new OptionInfo(NoneOptionString, null);

Expand Down Expand Up @@ -174,22 +174,22 @@ Func<PropertyInfo, bool> menuEnabled
/// </summary>
protected void ShowViewPropertyMenu(
GUIContent label,
PropertyInfo[] properties,
BindableMember<PropertyInfo>[] properties,
Action<string> propertyValueSetter,
string curPropertyValue,
out Type selectedPropertyType
)
{
var propertyNames = properties
.Select(MemberInfoToString)
.Select(m => m.ToString())
.ToArray();
var selectedIndex = Array.IndexOf(propertyNames, curPropertyValue);
var content = properties.Select(prop => new GUIContent(string.Concat(
prop.ReflectedType.Name,
prop.ViewModelTypeName,
"/",
prop.Name,
prop.MemberName,
" : ",
prop.PropertyType.Name
prop.Member.PropertyType.Name
)))
.ToArray();

Expand All @@ -201,11 +201,11 @@ out Type selectedPropertyType
UpdateProperty(
propertyValueSetter,
curPropertyValue,
MemberInfoToString(newSelectedProperty),
newSelectedProperty.ToString(),
"Set view property"
);

selectedPropertyType = newSelectedProperty.PropertyType;
selectedPropertyType = newSelectedProperty.Member.PropertyType;
}
else
{
Expand All @@ -215,7 +215,7 @@ out Type selectedPropertyType
return;
}

selectedPropertyType = properties[selectedIndex].PropertyType;
selectedPropertyType = properties[selectedIndex].Member.PropertyType;
}
}

Expand Down Expand Up @@ -354,14 +354,6 @@ protected Type AdaptTypeForward(Type inputType, string adapterName)
return adapterAttribute != null ? adapterAttribute.OutputType : inputType;
}

/// <summary>
/// Convert a MemberInfo to a uniquely identifiable string.
/// </summary>
protected static string MemberInfoToString(MemberInfo member)
{
return string.Concat(member.ReflectedType.ToString(), ".", member.Name);
}

/// <summary>
/// Convert a BindableEvent to a uniquely identifiable string.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions UnityWeld_Editor/EventBindingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void ShowMethodMenu(EventBinding targetScript, BindableMember<MethodInfo
InspectorUtils.DoPopup(
new GUIContent(targetScript.viewModelMethodName),
new GUIContent("View-model method", tooltip),
m => m.ViewModelType + "/" + m.Member.Name,
m => m.ViewModelType + "/" + m.MemberName,
m => true,
m => m.ToString() == targetScript.viewModelMethodName,
m => UpdateProperty(
Expand All @@ -63,8 +63,8 @@ private void ShowMethodMenu(EventBinding targetScript, BindableMember<MethodInfo
"Set bound view-model method"
),
bindableMethods
.OrderBy(m => m.ViewModelType.Name)
.ThenBy(m => m.Member.Name)
.OrderBy(m => m.ViewModelTypeName)
.ThenBy(m => m.MemberName)
.ToArray()
);
}
Expand Down
4 changes: 2 additions & 2 deletions UnityWeld_Editor/OneWayPropertyBindingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public override void OnInspectorGUI()
ShowViewPropertyMenu(
new GUIContent("View property", "Property on the view to bind to"),
PropertyFinder.GetBindableProperties(targetScript.gameObject)
.OrderBy(property => property.ReflectedType.Name)
.ThenBy(property => property.Name)
.OrderBy(prop => prop.ViewModelTypeName)
.ThenBy(prop => prop.MemberName)
.ToArray(),
updatedValue => targetScript.uiPropertyName = updatedValue,
targetScript.uiPropertyName,
Expand Down
4 changes: 2 additions & 2 deletions UnityWeld_Editor/TwoWayPropertyBindingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public override void OnInspectorGUI()
ShowViewPropertyMenu(
new GUIContent("View property", "Property on the view to bind to"),
PropertyFinder.GetBindableProperties(targetScript.gameObject)
.OrderBy(prop => prop.ReflectedType.Name)
.ThenBy(prop => prop.Name)
.OrderBy(prop => prop.ViewModelTypeName)
.ThenBy(prop => prop.MemberName)
.ToArray(),
updatedValue => targetScript.uiPropertyName = updatedValue,
targetScript.uiPropertyName,
Expand Down

0 comments on commit a3e2798

Please sign in to comment.