Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit cbc927c

Browse files
committed
Add DerivedProperty
1 parent 0511ce2 commit cbc927c

File tree

3 files changed

+180
-18
lines changed

3 files changed

+180
-18
lines changed

Bindings/Packages/com.virtualmaker.bindings/Editor/PropertyEditor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
8787
return;
8888
}
8989

90+
property.serializedObject.Update();
91+
9092
EditorGUI.PropertyField(position, value, label, true);
9193

9294
if (property.serializedObject.ApplyModifiedProperties())

Bindings/Packages/com.virtualmaker.bindings/Runtime/Property.cs

Lines changed: 175 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IPropertyNotify
99
void NotifyChanged();
1010
}
1111

12-
public interface IProperty<TValue>
12+
public interface IProperty<out TValue>
1313
{
1414
TValue Value { get; }
1515
event Action<TValue> OnChange;
@@ -70,30 +70,85 @@ private Derived() { }
7070
public static Derived<TDerived> From<TValue>(IProperty<TValue> property, Func<TValue, TDerived> func)
7171
{
7272
var derived = new Derived<TDerived>();
73-
Action update = () => derived._property.Value = func(property.Value);
74-
property.OnChange += _ => update();
75-
update();
73+
74+
void Update()
75+
{
76+
derived._property.Value = func(property.Value);
77+
}
78+
79+
property.OnChange += _ => Update();
80+
81+
Update();
82+
7683
return derived;
7784
}
7885

79-
public static Derived<TDerived> From<TValue1, TValue2>(IProperty<TValue1> property1, IProperty<TValue2> property2, Func<TValue1, TValue2, TDerived> func)
86+
public static Derived<TDerived> From<TValue1, TValue2>(
87+
IProperty<TValue1> property1,
88+
IProperty<TValue2> property2,
89+
Func<TValue1, TValue2, TDerived> func
90+
)
8091
{
8192
var derived = new Derived<TDerived>();
82-
Action update = () => derived._property.Value = func(property1.Value, property2.Value);
83-
property1.OnChange += _ => update();
84-
property2.OnChange += _ => update();
85-
update();
93+
94+
void Update()
95+
{
96+
derived._property.Value = func(property1.Value, property2.Value);
97+
}
98+
99+
property1.OnChange += _ => Update();
100+
property2.OnChange += _ => Update();
101+
102+
Update();
103+
104+
return derived;
105+
}
106+
107+
public static Derived<TDerived> From<TValue1, TValue2, TValue3>(
108+
IProperty<TValue1> property1,
109+
IProperty<TValue2> property2,
110+
IProperty<TValue3> property3,
111+
Func<TValue1, TValue2, TValue3, TDerived> func
112+
)
113+
{
114+
var derived = new Derived<TDerived>();
115+
116+
void Update()
117+
{
118+
derived._property.Value = func(property1.Value, property2.Value, property3.Value);
119+
}
120+
121+
property1.OnChange += _ => Update();
122+
property2.OnChange += _ => Update();
123+
property3.OnChange += _ => Update();
124+
125+
Update();
126+
86127
return derived;
87128
}
88129

89-
public static Derived<TDerived> From<TValue1, TValue2, TValue3>(IProperty<TValue1> property1, IProperty<TValue2> property2, IProperty<TValue3> property3, Func<TValue1, TValue2, TValue3, TDerived> func)
130+
public static Derived<TDerived> From<TValue1, TValue2, TValue3, TValue4>(
131+
IProperty<TValue1> property1,
132+
IProperty<TValue2> property2,
133+
IProperty<TValue3> property3,
134+
IProperty<TValue4> property4,
135+
Func<TValue1, TValue2, TValue3, TValue4, TDerived> func
136+
)
90137
{
91138
var derived = new Derived<TDerived>();
92-
Action update = () => derived._property.Value = func(property1.Value, property2.Value, property3.Value);
93-
property1.OnChange += _ => update();
94-
property2.OnChange += _ => update();
95-
property3.OnChange += _ => update();
96-
update();
139+
140+
void Update()
141+
{
142+
derived._property.Value = func(property1.Value, property2.Value, property3.Value, property4.Value);
143+
}
144+
145+
property1.OnChange += _ => Update();
146+
property2.OnChange += _ => Update();
147+
property3.OnChange += _ => Update();
148+
property4.OnChange += _ => Update();
149+
150+
Update();
151+
97152
return derived;
98153
}
99154
}
@@ -105,14 +160,117 @@ public static Derived<TDerived> From<TValue, TDerived>(IProperty<TValue> propert
105160
return Derived<TDerived>.From(property, func);
106161
}
107162

108-
public static Derived<TDerived> From<TValue1, TValue2, TDerived>(IProperty<TValue1> property1, IProperty<TValue2> property2, Func<TValue1, TValue2, TDerived> func)
163+
public static Derived<TDerived> From<TValue1, TValue2, TDerived>(
164+
IProperty<TValue1> property1,
165+
IProperty<TValue2> property2,
166+
Func<TValue1, TValue2, TDerived> func
167+
)
109168
{
110169
return Derived<TDerived>.From(property1, property2, func);
111170
}
112171

113-
public static Derived<TDerived> From<TValue1, TValue2, TValue3, TDerived>(IProperty<TValue1> property1, IProperty<TValue2> property2, IProperty<TValue3> property3, Func<TValue1, TValue2, TValue3, TDerived> func)
172+
public static Derived<TDerived> From<TValue1, TValue2, TValue3, TDerived>(
173+
IProperty<TValue1> property1,
174+
IProperty<TValue2> property2,
175+
IProperty<TValue3> property3,
176+
Func<TValue1, TValue2, TValue3, TDerived> func
177+
)
114178
{
115179
return Derived<TDerived>.From(property1, property2, property3, func);
116180
}
181+
182+
public static Derived<TDerived> From<TValue1, TValue2, TValue3, TValue4, TDerived>(
183+
IProperty<TValue1> property1,
184+
IProperty<TValue2> property2,
185+
IProperty<TValue3> property3,
186+
IProperty<TValue4> property4,
187+
Func<TValue1, TValue2, TValue3, TValue4, TDerived> func
188+
)
189+
{
190+
return Derived<TDerived>.From(property1, property2, property3, property4, func);
191+
}
192+
193+
public static Derived<bool> Inverted(this IProperty<bool> property)
194+
{
195+
return Derived<bool>.From(property, value => !value);
196+
}
197+
}
198+
199+
public class DerivedProperty<TDerived> : IProperty<TDerived>
200+
{
201+
private IProperty<TDerived> _derived;
202+
203+
private Property<TDerived> _property = new();
204+
public TDerived Value => _property.Value;
205+
public event Action<TDerived> OnChange
206+
{
207+
add => _property.OnChange += value;
208+
remove => _property.OnChange -= value;
209+
}
210+
211+
private void SetDerived(IProperty<TDerived> derived)
212+
{
213+
if (_derived != null)
214+
{
215+
_derived.OnChange -= OnDerivedChanged;
216+
}
217+
218+
_derived = derived;
219+
220+
if (_derived != null)
221+
{
222+
_derived.OnChange += OnDerivedChanged;
223+
OnDerivedChanged(_derived.Value);
224+
}
225+
else
226+
{
227+
OnDerivedChanged(default);
228+
}
229+
}
230+
231+
private void OnDerivedChanged(TDerived derived)
232+
{
233+
_property.Value = derived;
234+
}
235+
236+
private DerivedProperty() { }
237+
238+
public static DerivedProperty<TDerived> From<TValue>(IProperty<TValue> property, Func<TValue, IProperty<TDerived>> func)
239+
{
240+
var derivedProperty = new DerivedProperty<TDerived>();
241+
242+
void Update()
243+
{
244+
derivedProperty.SetDerived(func(property.Value));
245+
}
246+
247+
property.OnChange += _ => Update();
248+
249+
Update();
250+
251+
return derivedProperty;
252+
}
253+
}
254+
255+
public static class DerivedProperty
256+
{
257+
public static DerivedProperty<TDerived> From<TValue, TDerived>(IProperty<TValue> property, Func<TValue, IProperty<TDerived>> func)
258+
{
259+
return DerivedProperty<TDerived>.From<TValue>(property, func);
260+
}
261+
}
262+
263+
public class PropertyConverter<T> : Newtonsoft.Json.JsonConverter<Property<T>>
264+
{
265+
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, Property<T> value, Newtonsoft.Json.JsonSerializer serializer)
266+
{
267+
serializer.Serialize(writer, value.Value);
268+
}
269+
270+
public override Property<T> ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, Property<T> existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer)
271+
{
272+
T value = serializer.Deserialize<T>(reader);
273+
return new Property<T> { Value = value };
274+
}
117275
}
118276
}

Bindings/Packages/com.virtualmaker.bindings/Runtime/VirtualMaker.Bindings.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"excludePlatforms": [],
99
"allowUnsafeCode": false,
1010
"overrideReferences": true,
11-
"precompiledReferences": [],
11+
"precompiledReferences": [
12+
"Newtonsoft.Json.dll"
13+
],
1214
"autoReferenced": true,
1315
"defineConstraints": [],
1416
"versionDefines": [],

0 commit comments

Comments
 (0)