// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections;
using FlaxEditor.Scripting;
namespace FlaxEditor.CustomEditors
{
///
/// Custom for (used for and .
///
///
public class ListValueContainer : ValueContainer
{
private readonly object[] _attributes;
///
/// The index in the collection.
///
public readonly int Index;
///
/// Initializes a new instance of the class.
///
/// Type of the collection elements.
/// The index.
public ListValueContainer(ScriptType elementType, int index)
: base(ScriptMemberInfo.Null, elementType)
{
Index = index;
}
///
/// Initializes a new instance of the class.
///
/// Type of the collection elements.
/// The index.
/// The collection values.
/// The collection property attributes to inherit.
public ListValueContainer(ScriptType elementType, int index, ValueContainer values, object[] attributes = null)
: this(elementType, index)
{
_attributes = attributes;
Capacity = values.Count;
for (int i = 0; i < values.Count; i++)
{
var v = (IList)values[i];
Add(v[index]);
}
if (values.HasReferenceValue)
{
if (values.ReferenceValue is IList v && v.Count > index)
{
_referenceValue = v[index];
_hasReferenceValue = true;
}
}
}
///
public override void Refresh(ValueContainer instanceValues)
{
if (instanceValues == null || instanceValues.Count != Count)
throw new ArgumentException();
for (int i = 0; i < Count; i++)
{
var v = (IList)instanceValues[i];
this[i] = v[Index];
}
}
///
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 = (IList)instanceValues[i];
v[Index] = 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 = (IList)instanceValues[i];
var value = ((ListValueContainer)values)[i];
v[Index] = 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 = (IList)instanceValues[i];
v[Index] = this[i];
}
}
///
public override void RefreshReferenceValue(object instanceValue)
{
if (instanceValue is IList v)
{
_referenceValue = v[Index];
_hasReferenceValue = true;
}
}
///
public override object[] GetAttributes()
{
return _attributes ?? base.GetAttributes();
}
}
}