Skip to content

Commit

Permalink
SerializableState implementation from #33
Browse files Browse the repository at this point in the history
it's a bit opinonated, so maybe it should go into a separate package and/or namespace, not to conflict with custom implementations people have (e.g. Lythom has its own with different serialization methods)
  • Loading branch information
nadako committed Dec 18, 2024
1 parent 9b2481f commit 3585523
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 4 deletions.
8 changes: 4 additions & 4 deletions playground-unity/Assets/HelloWorld/HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class HelloWorld : MonoBehaviour
[SerializeField] TMP_InputField nameInput;
[SerializeField] TMP_Text greetingLabel;

// define piece of mutable observable state
[SerializeField] SerializableState<string> name = new("World");

void Start()
{
// define piece of mutable observable state
var name = Observable.State("World");

// bind the state two-ways to an input field
name.Bind(nameInput.SetTextWithoutNotify);
nameInput.onValueChanged.AddListener(newValue => name.Value = newValue);
Expand All @@ -22,4 +22,4 @@ void Start()
// bind the auto-observable to a text field
greeting.Bind(text => greetingLabel.text = text);
}
}
}
8 changes: 8 additions & 0 deletions src/TinkState-Unity/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Nadako.TinkState.Unity.Editor",
"references": [
"Nadako.TinkState.Unity"
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/TinkState-Unity/Editor/SerializableStateDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UnityEditor;
using UnityEngine;

namespace TinkState
{
[CustomPropertyDrawer(typeof(SerializableState<>))]
class SerializableStateDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property.FindPropertyRelative("value"), label);
EditorGUI.EndProperty();
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property.FindPropertyRelative("value"), label);
}
}
}
11 changes: 11 additions & 0 deletions src/TinkState-Unity/Editor/SerializableStateDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions src/TinkState-Unity/Runtime/SerializableState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using TinkState;
using UnityEngine;

namespace TinkState
{
[Serializable]
public class SerializableState<T> : State<T>, ISerializationCallbackReceiver
{
State<T> state;
[SerializeField] T value;

public SerializableState(T initialValue)
{
state = Observable.State(initialValue);
value = initialValue;
}

public T Value
{
get => state.Value;
set
{
state.Value = value;
this.value = value;
}
}

public override string ToString() => state.ToString();

public IDisposable Bind(Action<T> callback, IEqualityComparer<T> comparer = null, Scheduler scheduler = null) =>
state.Bind(callback, comparer, scheduler);

public Observable<TOut> Map<TOut>(Func<T, TOut> transform, IEqualityComparer<TOut> comparer = null) => state.Map(transform, comparer);

void ISerializationCallbackReceiver.OnBeforeSerialize() {}

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
state ??= Observable.State(value);
Value = value;
}
}
}
11 changes: 11 additions & 0 deletions src/TinkState-Unity/Runtime/SerializableState.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3585523

Please sign in to comment.