Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions UnityWeld/Binding/AbstractMemberBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public virtual void Init()
private object FindViewModel(string viewModelName)
{
var trans = transform;

while (trans != null)
{
var components = trans.GetComponents<MonoBehaviour>();
var monoBehaviourViewModel = components
.FirstOrDefault(component => component.GetType().ToString() == viewModelName);

if (monoBehaviourViewModel != null)
{
return monoBehaviourViewModel;
Expand All @@ -42,8 +44,8 @@ private object FindViewModel(string viewModelName)
.Select(component => component as IViewModelProvider)
.Where(component => component != null)
.FirstOrDefault(
viewModelBinding => viewModelBinding.GetViewModelTypeName() == viewModelName &&
#pragma warning disable 252,253 // Warning says unintended reference comparison, but we do want to compare references
viewModelBinding => viewModelBinding.GetViewModelTypeName() == viewModelName &&
#pragma warning disable 252, 253 // Warning says unintended reference comparison, but we do want to compare references
(object)viewModelBinding != this
#pragma warning restore 252,253
);
Expand Down Expand Up @@ -73,6 +75,7 @@ protected static IAdapter CreateAdapter(string adapterTypeName)
}

var adapterType = TypeResolver.FindAdapterType(adapterTypeName);

if (adapterType == null)
{
throw new NoSuchAdapterException(adapterTypeName);
Expand Down Expand Up @@ -106,6 +109,7 @@ protected PropertyEndPoint MakeViewModelEndPoint(string viewModelPropertyName, s
protected static void ParseEndPointReference(string endPointReference, out string memberName, out string typeName)
{
var lastPeriodIndex = endPointReference.LastIndexOf('.');

if (lastPeriodIndex == -1)
{
throw new InvalidEndPointException(
Expand Down Expand Up @@ -171,15 +175,6 @@ protected void ParseViewEndPointReference(string endPointReference, out string m
/// </summary>
public abstract void Disconnect();

/// <summary>
/// Standard MonoBehaviour awake message, do not call this explicitly.
/// Initialises the binding.
/// </summary>
protected void Awake()
{
Init();
}

/// <summary>
/// Clean up when the game object is destroyed.
/// </summary>
Expand All @@ -188,4 +183,4 @@ public void OnDestroy()
Disconnect();
}
}
}
}
115 changes: 115 additions & 0 deletions UnityWeld/Binding/ToggleSelfBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using UnityEngine;
using UnityWeld.Binding.Internal;

namespace UnityWeld.Binding
{
/// <summary>
/// Bind to a boolean property on the view model and turn itself on
/// or off based on its value.
/// </summary>
[AddComponentMenu("Unity Weld/ToggleSelf Binding")]
[HelpURL("https://github.com/Real-Serious-Games/Unity-Weld")]
public class ToggleSelfBinding : AbstractMemberBinding
{
/// <summary>
/// Type of the adapter we're using to adapt between the view model property
/// and view property.
/// </summary>
public string ViewAdapterTypeName
{
get { return viewAdapterTypeName; }
set { viewAdapterTypeName = value; }
}

[SerializeField]
private string viewAdapterTypeName;

/// <summary>
/// Options for adapting from the view model to the view property.
/// </summary>
public AdapterOptions ViewAdapterOptions
{
get { return viewAdapterOptions; }
set { viewAdapterOptions = value; }
}

[SerializeField]
private AdapterOptions viewAdapterOptions;

/// <summary>
/// Name of the property in the view model to bind.
/// </summary>
public string ViewModelPropertyName
{
get { return viewModelPropertyName; }
set { viewModelPropertyName = value; }
}

[SerializeField]
private string viewModelPropertyName;

/// <summary>
/// Watcher the view-model for changes that must be propagated to the view.
/// </summary>
private PropertyWatcher viewModelWatcher;

/// <summary>
/// Property for the propertySync to set in order to activate and deactivate all children
/// </summary>
public bool SelfActive
{
set
{
SetActive(value);
}
}

/// <inheritdoc />
public override void Connect()
{
var viewModelEndPoint = MakeViewModelEndPoint(viewModelPropertyName, null, null);

var propertySync = new PropertySync(
// Source
viewModelEndPoint,

// Dest
new PropertyEndPoint(
this,
"SelfActive",
CreateAdapter(viewAdapterTypeName),
viewAdapterOptions,
"view",
this
),

// Errors, exceptions and validation.
null, // Validation not needed

this
);

viewModelWatcher = viewModelEndPoint.Watch(
() => propertySync.SyncFromSource()
);

// Copy the initial value over from the view-model.
propertySync.SyncFromSource();
}

/// <inheritdoc />
public override void Disconnect()
{
if (viewModelWatcher != null)
{
viewModelWatcher.Dispose();
viewModelWatcher = null;
}
}

private void SetActive(bool active)
{
transform.gameObject.SetActive(active);
}
}
}
2 changes: 2 additions & 0 deletions UnityWeld/UnityWeld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<Compile Include="Binding\SubViewModelBinding.cs" />
<Compile Include="Binding\TemplateBinding.cs" />
<Compile Include="Binding\ToggleActiveBinding.cs" />
<Compile Include="Binding\ToggleSelfBinding.cs" />
<Compile Include="Binding\TwoWayPropertyBinding.cs" />
<Compile Include="Binding\AbstractMemberBinding.cs" />
<Compile Include="Binding\Internal\UnityEventBinder.cs" />
Expand All @@ -120,6 +121,7 @@
<Compile Include="Ioc\WeldContainerAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Binding\AbstractTemplateSelector.cs" />
<Compile Include="WeldContext.cs" />
<Compile Include="Widgets\DropdownAdapter.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
33 changes: 33 additions & 0 deletions UnityWeld/WeldContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityWeld.Binding;

namespace UnityWeld
{
/// <summary>
/// Initialize all bindings when scene loaded
/// </summary>
public static class WeldContext
{
/// <summary>
/// Automatically called before scene load
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void WeldContextStartup()
{
SceneManager.sceneLoaded += SceneManager_SceneLoaded;
}

static void SceneManager_SceneLoaded(Scene scene, LoadSceneMode mode)
{
foreach (var parentT in scene.GetRootGameObjects())
{
//Call Init() on all bindings
foreach (var binding in parentT.GetComponentsInChildren<AbstractMemberBinding>(true)) //including inactive GameObjects
{
binding.Init();
}
}
}
}
}
153 changes: 153 additions & 0 deletions UnityWeld_Editor/ToggleSelfBindingEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine;
using UnityWeld.Binding;
using UnityWeld.Binding.Internal;

namespace UnityWeld_Editor
{
[CustomEditor(typeof(ToggleSelfBinding))]
public class ToggleSelfBindingEditor : BaseBindingEditor
{
private ToggleSelfBinding targetScript;

private AnimBool viewAdapterOptionsFade;

private bool viewAdapterPrefabModified;
private bool viewAdapterOptionsPrefabModified;
private bool viewModelPropertyPrefabModified;

private void OnEnable()
{
targetScript = (ToggleSelfBinding)target;

Type adapterType;

viewAdapterOptionsFade = new AnimBool(
ShouldShowAdapterOptions(targetScript.ViewAdapterTypeName, out adapterType)
);

viewAdapterOptionsFade.valueChanged.AddListener(Repaint);
}

private void OnDisable()
{
viewAdapterOptionsFade.valueChanged.RemoveListener(Repaint);
}

public override void OnInspectorGUI()
{
if (CannotModifyInPlayMode())
{
GUI.enabled = false;
}

UpdatePrefabModifiedProperties();

var defaultLabelStyle = EditorStyles.label.fontStyle;

var viewPropertyType = typeof(bool);

var viewAdapterTypeNames = GetAdapterTypeNames(
type => TypeResolver.FindAdapterAttribute(type).OutputType == viewPropertyType
);

EditorStyles.label.fontStyle = viewAdapterPrefabModified
? FontStyle.Bold
: defaultLabelStyle;

ShowAdapterMenu(
new GUIContent(
"View adapter",
"Adapter that converts values sent from the view-model to the view."
),
viewAdapterTypeNames,
targetScript.ViewAdapterTypeName,
newValue =>
{
// Get rid of old adapter options if we changed the type of the adapter.
if (newValue != targetScript.ViewAdapterTypeName)
{
Undo.RecordObject(targetScript, "Set view adapter options");
targetScript.ViewAdapterOptions = null;
}

UpdateProperty(
updatedValue => targetScript.ViewAdapterTypeName = updatedValue,
targetScript.ViewAdapterTypeName,
newValue,
"Set view adapter"
);
}
);

Type adapterType;
viewAdapterOptionsFade.target = ShouldShowAdapterOptions(
targetScript.ViewAdapterTypeName,
out adapterType
);

EditorStyles.label.fontStyle = viewAdapterOptionsPrefabModified
? FontStyle.Bold
: defaultLabelStyle;

ShowAdapterOptionsMenu(
"View adapter options",
adapterType,
options => targetScript.ViewAdapterOptions = options,
targetScript.ViewAdapterOptions,
viewAdapterOptionsFade.faded
);

EditorGUILayout.Space();

EditorStyles.label.fontStyle = viewModelPropertyPrefabModified
? FontStyle.Bold
: defaultLabelStyle;

var adaptedViewPropertyType = AdaptTypeBackward(
viewPropertyType,
targetScript.ViewAdapterTypeName
);
ShowViewModelPropertyMenu(
new GUIContent(
"View-model property",
"Property on the view-model to bind to."
),
TypeResolver.FindBindableProperties(targetScript),
updatedValue => targetScript.ViewModelPropertyName = updatedValue,
targetScript.ViewModelPropertyName,
property => property.PropertyType == adaptedViewPropertyType
);

EditorStyles.label.fontStyle = defaultLabelStyle;
}

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.
property.Next(true);
do
{
switch (property.name)
{
case "viewAdapterTypeName":
viewAdapterPrefabModified = property.prefabOverride;
break;

case "viewAdapterOptions":
viewAdapterOptionsPrefabModified = property.prefabOverride;
break;

case "viewModelPropertyName":
viewModelPropertyPrefabModified = property.prefabOverride;
break;
}
}
while (property.Next(false));
}
}
}
1 change: 1 addition & 0 deletions UnityWeld_Editor/UnityWeld_Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="TemplateEditor.cs" />
<Compile Include="TemplateBindingEditor.cs" />
<Compile Include="ToggleActiveBindingEditor.cs" />
<Compile Include="ToggleSelfBindingEditor.cs" />
<Compile Include="TwoWayPropertyBindingEditor.cs" />
<Compile Include="EventBindingEditor.cs" />
<Compile Include="InspectorUtils.cs" />
Expand Down