// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections;
using FlaxEditor.Scripting;
using FlaxEngine;
namespace FlaxEditor.CustomEditors
{
///
/// Custom for .
///
///
[HideInEditor]
public class DictionaryValueContainer : ValueContainer
{
///
/// The key in the collection.
///
public readonly object Key;
///
/// Initializes a new instance of the class.
///
/// Type of the collection elements.
/// The key.
public DictionaryValueContainer(ScriptType elementType, object key)
: base(ScriptMemberInfo.Null, elementType)
{
Key = key;
}
///
/// Initializes a new instance of the class.
///
/// Type of the collection elements.
/// The key.
/// The collection values.
public DictionaryValueContainer(ScriptType elementType, object key, ValueContainer values)
: this(elementType, key)
{
Capacity = values.Count;
for (int i = 0; i < values.Count; i++)
{
var v = (IDictionary)values[i];
Add(v[Key]);
}
if (values.HasReferenceValue)
{
var v = (IDictionary)values.ReferenceValue;
if (v.Contains(key))
{
_referenceValue = v[key];
_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 = (IDictionary)instanceValues[i];
this[i] = v[Key];
}
}
///
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 = (IDictionary)instanceValues[i];
v[Key] = 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 = (IDictionary)instanceValues[i];
var value = ((DictionaryValueContainer)values)[i];
v[Key] = 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 = (IDictionary)instanceValues[i];
v[Key] = this[i];
}
}
///
public override void RefreshReferenceValue(object instanceValue)
{
if (instanceValue is IDictionary v)
{
_referenceValue = v[Key];
_hasReferenceValue = true;
}
}
}
}