Skip to content

Commit 4f06bca

Browse files
authored
Merge pull request #13 from RoryDungan/master
Added StringEmptyToBoolAdapter, minor fixes and cleanups
2 parents ce8730d + d4cf8a1 commit 4f06bca

25 files changed

+62
-79
lines changed

Unity-Weld.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>RSG.UnityWeld</id>
5-
<version>0.7.1</version>
5+
<version>0.7.2</version>
66
<title>RSG.UnityWeld</title>
77
<authors>Real Serious Games</authors>
88
<owners>Real Serious Games</owners>

UnityWeld/Binding/AbstractTemplateSelector.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,10 @@ private void CacheTemplates()
9090

9191
/// <summary>
9292
/// Create a clone of the template object and bind it to the specified view model.
93+
/// Place the new object under the parent at the specified index, or 0 if no index
94+
/// is specified.
9395
/// </summary>
94-
protected void InstantiateTemplate(object templateViewModel)
95-
{
96-
InstantiateTemplate(templateViewModel, 0);
97-
}
98-
99-
/// <summary>
100-
/// Create a clone of the template object and bind it to the specified view model.
101-
/// Place the new object under the parent at the specified index.
102-
/// </summary>
103-
protected void InstantiateTemplate(object templateViewModel, int index)
96+
protected void InstantiateTemplate(object templateViewModel, int index = 0)
10497
{
10598
Assert.IsNotNull(templateViewModel, "Cannot instantiate child with null view model");
10699

@@ -125,17 +118,18 @@ protected void InstantiateTemplate(object templateViewModel, int index)
125118
/// </summary>
126119
private Template FindTemplateForType(Type templateType)
127120
{
128-
var possibleMatches = FindTypesMatchingTemplate(templateType).ToList();
121+
var possibleMatches = FindTypesMatchingTemplate(templateType)
122+
.OrderBy(m => m.Key)
123+
.ToList();
129124

130125
if (!possibleMatches.Any())
131126
{
132127
throw new TemplateNotFoundException("Could not find any template matching type " + templateType);
133128
}
134129

135-
var sorted = possibleMatches.OrderBy(m => m.Key);
136-
var selectedType = sorted.First();
130+
var selectedType = possibleMatches.First();
137131

138-
if (sorted.Skip(1).Any(m => m.Key == selectedType.Key))
132+
if (possibleMatches.Skip(1).Any(m => m.Key == selectedType.Key))
139133
{
140134
throw new AmbiguousTypeException("Multiple templates were found that match type " + templateType
141135
+ ". This can be caused by providing multiple templates that match types " + templateType

UnityWeld/Binding/Adapters/StringCultureToDateTimeAdapterOptions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
51
using UnityEngine;
62

73
namespace UnityWeld.Binding.Adapters
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace UnityWeld.Binding.Adapters
2+
{
3+
/// <summary>
4+
/// String to bool adapter that returns false if the string is null or empty,
5+
/// otherwise true.
6+
/// </summary>
7+
[Adapter(typeof(string), typeof(bool))]
8+
public class StringEmptyToBoolAdapter : IAdapter
9+
{
10+
public object Convert(object valueIn, AdapterOptions options)
11+
{
12+
return !string.IsNullOrEmpty((string)valueIn);
13+
}
14+
}
15+
}

UnityWeld/Binding/AnimatorParameterBinding.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using UnityEngine;
63
using UnityEngine.Assertions;
7-
using UnityEngine.Serialization;
84
using UnityWeld.Binding.Internal;
95

106
namespace UnityWeld.Binding
@@ -171,7 +167,7 @@ public override void Connect()
171167
propertyName = "TriggerParameter";
172168
break;
173169
default:
174-
throw new ArgumentOutOfRangeException("Unexpected animator parameter type");
170+
throw new IndexOutOfRangeException("Unexpected animator parameter type");
175171
}
176172

177173
var viewModelEndPoint = MakeViewModelEndPoint(viewModelPropertyName, null, null);

UnityWeld/Binding/EventBinding.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ out viewModel
5656
ParseViewEndPointReference(viewEventName, out eventName, out view);
5757

5858
eventWatcher = new UnityEventWatcher(view, eventName,
59-
() => viewModelMethod.Invoke(viewModel, new object[0])
60-
);
59+
() =>
60+
{
61+
if (viewModelMethod != null)
62+
{
63+
viewModelMethod.Invoke(viewModel, new object[0]);
64+
}
65+
});
6166
}
6267

6368
public override void Disconnect()

UnityWeld/Binding/Internal/BindableMember.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Reflection;
33

44
namespace UnityWeld.Binding.Internal
@@ -9,12 +9,12 @@ namespace UnityWeld.Binding.Internal
99
/// returning the type of the view model if the property or method was declared in an
1010
/// interface that the view model inherits from.
1111
/// </summary>
12-
public class BindableMember<MemberType> where MemberType : MemberInfo
12+
public class BindableMember<TMemberType> where TMemberType : MemberInfo
1313
{
1414
/// <summary>
1515
/// The bindable member info (usually a PropertyInfo or MethodInfo)
1616
/// </summary>
17-
public readonly MemberType Member;
17+
public readonly TMemberType Member;
1818

1919
/// <summary>
2020
/// View model that the property or method belongs to.
@@ -43,7 +43,7 @@ public string MemberName
4343
}
4444
}
4545

46-
public BindableMember(MemberType member, Type viewModelType)
46+
public BindableMember(TMemberType member, Type viewModelType)
4747
{
4848
Member = member;
4949
ViewModelType = viewModelType;

UnityWeld/Binding/Internal/TypeResolver.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ private static bool HasCastDefined(Type from, Type to)
300300
new [] { typeof(Single) },
301301
new [] { typeof(Double) }
302302
};
303-
IEnumerable<Type> lowerTypes = Enumerable.Empty<Type>();
304303

305304
return typeHierarchy.Any(types => types.Contains(to)) &&
306305
typeHierarchy

UnityWeld/Binding/Internal/UnityEventBinder.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using UnityEngine.Events;
44
using UnityWeld.Binding.Exceptions;
@@ -18,17 +18,24 @@ public static UnityEventBinderBase Create(UnityEventBase unityEvent, Action acti
1818
{
1919
// Note that to find the paramaters of events on the UI, we need to see what
2020
// generic arguments were passed to the UnityEvent they inherit from.
21-
var eventArgumentTypes = unityEvent.GetType().BaseType.GetGenericArguments();
21+
var baseType = unityEvent.GetType().BaseType;
22+
var eventArgumentTypes = baseType != null
23+
? baseType.GetGenericArguments()
24+
: null;
2225

23-
if (!eventArgumentTypes.Any())
26+
if (eventArgumentTypes == null || !eventArgumentTypes.Any())
2427
{
2528
return new UnityEventBinder(unityEvent, action);
2629
}
2730

2831
try
2932
{
3033
var genericType = typeof(UnityEventBinder<>).MakeGenericType(eventArgumentTypes);
31-
return (UnityEventBinderBase)Activator.CreateInstance(genericType, unityEvent, action);
34+
return (UnityEventBinderBase)Activator.CreateInstance(
35+
genericType,
36+
unityEvent,
37+
action
38+
);
3239
}
3340
catch (ArgumentException ex)
3441
{

UnityWeld/Binding/ObservableList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace UnityWeld.Binding
77
{
8-
public class ObservableList<T> : IList<T>, IList, INotifyCollectionChanged, ITypedList
8+
public class ObservableList<T> : IList<T>, INotifyCollectionChanged, ITypedList
99
{
1010
/// <summary>
1111
/// Inner (non-obsevable) list.

UnityWeld/Binding/SubViewModelBinding.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using UnityEngine;
32
using UnityWeld.Binding.Exceptions;
43
using UnityWeld.Binding.Internal;

UnityWeld/Binding/TemplateBinding.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Reflection;
32
using UnityEngine;
43
using UnityWeld.Binding.Exceptions;

UnityWeld/Binding/ToggleActiveBinding.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Linq;
31
using UnityEngine;
4-
using UnityEngine.Assertions;
52
using UnityWeld.Binding.Internal;
63

74
namespace UnityWeld.Binding

UnityWeld/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("0.7.1.0")]
35-
[assembly: AssemblyFileVersion("0.7.1.0")]
34+
[assembly: AssemblyVersion("0.7.2")]
35+
[assembly: AssemblyFileVersion("0.7.2")]

UnityWeld/UnityWeld.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Binding\Adapters\FloatToDateTimeAdapter.cs" />
6969
<Compile Include="Binding\Adapters\StringCultureToDateTimeAdapter.cs" />
7070
<Compile Include="Binding\Adapters\StringCultureToDateTimeAdapterOptions.cs" />
71+
<Compile Include="Binding\Adapters\StringEmptyToBoolAdapter.cs" />
7172
<Compile Include="Binding\Adapters\StringToFloatAdapter.cs" />
7273
<Compile Include="Binding\Adapters\StringToIntAdapter.cs" />
7374
<Compile Include="Binding\AnimatorParameterBinding.cs" />

UnityWeld_Editor/AnimatorParameterBindingEditor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override void OnInspectorGUI()
5454

5555
var animatorParameters = GetAnimatorParameters();
5656

57-
if (animatorParameters == null || animatorParameters.Count() <= 0)
57+
if (animatorParameters == null || !animatorParameters.Any())
5858
{
5959
EditorGUILayout.HelpBox("Animator has no parameters!", MessageType.Warning);
6060
return;
@@ -151,7 +151,7 @@ private void ShowAnimatorParametersMenu(
151151
out Type selectedPropertyType
152152
)
153153
{
154-
if(properties == null || properties.Count() <= 0)
154+
if(properties == null || !properties.Any())
155155
{
156156
selectedPropertyType = null;
157157
return;
@@ -337,7 +337,7 @@ public override bool Equals(object obj)
337337

338338
public bool Equals(AnimatorParameterTypeAndName other)
339339
{
340-
return other.Name == Name && other.Type == Type;
340+
return other != null && other.Name == Name && other.Type == Type;
341341
}
342342
}
343343
}

UnityWeld_Editor/CollectionBindingEditor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ private void UpdatePrefabModifiedProperties()
7676
case "templatesRoot":
7777
templatesRootPrefabModified = property.prefabOverride;
7878
break;
79-
80-
default:
81-
break;
8279
}
8380
}
8481
while (property.Next(false));

UnityWeld_Editor/EventBindingEditor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ private void UpdatePrefabModifiedProperties()
102102
case "viewModelMethodName":
103103
viewModelMethodPrefabModified = property.prefabOverride;
104104
break;
105-
106-
default:
107-
break;
108105
}
109106
}
110107
while (property.Next(false));

UnityWeld_Editor/InspectorUtils.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using UnityEditor;
33
using UnityEditor.SceneManagement;
44
using UnityEngine;
@@ -77,9 +77,6 @@ private static void ShowPopupButton(Rect buttonRect, Rect labelRect, int control
7777
currentEvent.Use();
7878
}
7979
break;
80-
81-
default:
82-
break;
8380
}
8481
}
8582

UnityWeld_Editor/OneWayPropertyBindingEditor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ private void UpdatePrefabModifiedProperties()
180180
case "viewPropertyName":
181181
viewPropertyPrefabModified = property.prefabOverride;
182182
break;
183-
184-
default:
185-
break;
186183
}
187184
}
188185
while (property.Next(false));

UnityWeld_Editor/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("0.7.1.0")]
35-
[assembly: AssemblyFileVersion("0.7.1.0")]
34+
[assembly: AssemblyVersion("0.7.2")]
35+
[assembly: AssemblyFileVersion("0.7.2")]

UnityWeld_Editor/SubViewModelBindingEditor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public override void OnInspectorGUI()
5252
targetScript.ViewModelPropertyName = updatedValue;
5353

5454
targetScript.ViewModelTypeName = bindableProperties
55-
.Where(prop => prop.ToString() == updatedValue)
56-
.Single()
55+
.Single(prop => prop.ToString() == updatedValue)
5756
.Member.PropertyType.ToString();
5857
},
5958
targetScript.ViewModelPropertyName,
@@ -94,9 +93,6 @@ private void UpdatePrefabModifiedProperties()
9493
propertyPrefabModified = property.prefabOverride
9594
|| propertyPrefabModified;
9695
break;
97-
98-
default:
99-
break;
10096
}
10197
}
10298
while (property.Next(false));

UnityWeld_Editor/TemplateBindingEditor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ private void UpdatePrefabModifiedProperties()
8686
case "templatesRoot":
8787
templatesRootPrefabModified = property.prefabOverride;
8888
break;
89-
90-
default:
91-
break;
9289
}
9390
}
9491
while (property.Next(false));

UnityWeld_Editor/ToggleActiveBindingEditor.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using UnityEditor.AnimatedValues;
44
using UnityEngine;
55
using UnityWeld.Binding;
6-
using UnityWeld.Binding.Exceptions;
76
using UnityWeld.Binding.Internal;
87

98
namespace UnityWeld_Editor
@@ -48,11 +47,12 @@ public override void OnInspectorGUI()
4847

4948
var defaultLabelStyle = EditorStyles.label.fontStyle;
5049

51-
Type viewPropertyType = typeof(bool);
50+
var viewPropertyType = typeof(bool);
5251

5352
var viewAdapterTypeNames = GetAdapterTypeNames(
54-
type => viewPropertyType == null ||
55-
TypeResolver.IsTypeCastableTo(TypeResolver.FindAdapterAttribute(type).OutputType, viewPropertyType)
53+
type => TypeResolver.IsTypeCastableTo(
54+
TypeResolver.FindAdapterAttribute(type).OutputType, viewPropertyType
55+
)
5656
);
5757

5858
EditorStyles.label.fontStyle = viewAdapterPrefabModified
@@ -147,9 +147,6 @@ private void UpdatePrefabModifiedProperties()
147147
case "viewModelPropertyName":
148148
viewModelPropertyPrefabModified = property.prefabOverride;
149149
break;
150-
151-
default:
152-
break;
153150
}
154151
}
155152
while (property.Next(false));

UnityWeld_Editor/TwoWayPropertyBindingEditor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,6 @@ private void UpdatePrefabModifiedProperties()
359359
case "exceptionAdapterOptions":
360360
exceptionAdapterOptionsPrefabModified = property.prefabOverride;
361361
break;
362-
363-
default:
364-
break;
365362
}
366363
}
367364
while (property.Next(false));

0 commit comments

Comments
 (0)