// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using FlaxEditor.Scripting; using FlaxEngine; namespace FlaxEditor.CustomEditors { /// /// Custom for any type of storage and data management logic. /// /// [HideInEditor] public class CustomValueContainer : ValueContainer { /// /// Get value delegate. /// /// The parent object instance. /// The index (for multi selected objects). /// The value. public delegate object GetDelegate(object instance, int index); /// /// Set value delegate. /// /// The parent object instance. /// The index (for multi selected objects). /// The value. public delegate void SetDelegate(object instance, int index, object value); private readonly GetDelegate _getter; private readonly SetDelegate _setter; private readonly object[] _attributes; /// /// Initializes a new instance of the class. /// /// Type of the value. /// The value getter. /// The value setter. /// The custom type attributes used to override the value editor logic or appearance (eg. instance of ). public CustomValueContainer(ScriptType valueType, GetDelegate getter, SetDelegate setter, object[] attributes = null) : base(ScriptMemberInfo.Null, valueType) { if (getter == null || setter == null) throw new ArgumentNullException(); _getter = getter; _setter = setter; _attributes = attributes; } /// /// Initializes a new instance of the class. /// /// Type of the value. /// The initial value. /// The value getter. /// The value setter. /// The custom type attributes used to override the value editor logic or appearance (eg. instance of ). public CustomValueContainer(ScriptType valueType, object initialValue, GetDelegate getter, SetDelegate setter, object[] attributes = null) : this(valueType, getter, setter, attributes) { Add(initialValue); } /// public override object[] GetAttributes() { return _attributes ?? base.GetAttributes(); } /// public override void Refresh(ValueContainer instanceValues) { if (instanceValues == null || instanceValues.Count != Count) throw new ArgumentException(); for (int i = 0; i < Count; i++) { var v = instanceValues[i]; this[i] = _getter(v, i); } } /// public override void Set(ValueContainer instanceValues, object value) { if (instanceValues == null || instanceValues.Count != Count) throw new ArgumentException(); for (int i = 0; i < Count; i++) { var v = instanceValues[i]; _setter(v, i, value); this[i] = value; } } /// public override void Set(ValueContainer instanceValues, ValueContainer values) { if (instanceValues == null || instanceValues.Count != Count) throw new ArgumentException(); if (values == null || values.Count != Count) throw new ArgumentException(); for (int i = 0; i < Count; i++) { var v = instanceValues[i]; var value = ((CustomValueContainer)values)[i]; _setter(v, i, value); this[i] = value; } } /// public override void Set(ValueContainer instanceValues) { if (instanceValues == null || instanceValues.Count != Count) throw new ArgumentException(); for (int i = 0; i < Count; i++) { var v = instanceValues[i]; _setter(v, i, _getter(v, i)); } } /// public override void RefreshReferenceValue(object instanceValue) { // Not supported } } }