Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions UnityWeld/Binding/AbstractMemberBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ protected void ParseViewEndPointReference(string endPointReference, out string m
/// Standard MonoBehaviour awake message, do not call this explicitly.
/// Initialises the binding.
/// </summary>
protected void Awake()
{
Init();
}
//protected void Awake()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just delete this. We try to avoid leaving blocks of commented-out code because it can be confusing to someone else reading the code.

//{
// Init();
//}

/// <summary>
/// Clean up when the game object is destroyed.
Expand Down
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