using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
namespace UnityEditor.Rendering.HighDefinition
{
///
/// Serialized version of .
///
internal class SerializedScalableSetting
{
public SerializedProperty values;
public SerializedProperty schemaId;
public SerializedScalableSetting(SerializedProperty property)
{
values = property.FindPropertyRelative("m_Values");
schemaId = property.FindPropertyRelative("m_SchemaId.m_Id");
}
/// Get the value of level .
/// The type of the scalable setting.
/// The level to get.
///
/// The value of the level if the level was found.
///
/// default(T) when:
/// - The level does not exists (level index is out of range)
/// - The level value has multiple different values
///
/// true when the value was evaluated, false when the value could not be evaluated.
public bool TryGetLevelValue(int level, out T value)
where T : struct
{
if (level < values.arraySize && level >= 0)
{
var levelValue = values.GetArrayElementAtIndex(level);
if (levelValue.hasMultipleDifferentValues)
{
value = default;
return false;
}
else
{
value = levelValue.GetInline();
return true;
}
}
else
{
value = default;
return false;
}
}
public int GetSchemaLevelCount()
{
var schema = ScalableSettingSchema.GetSchemaOrNull(new ScalableSettingSchemaId(schemaId.stringValue))
?? ScalableSettingSchema.GetSchemaOrNull(ScalableSettingSchemaId.With3Levels);
return schema.levelCount;
}
}
internal static class SerializedScalableSettingUI
{
///
/// Draw the scalable setting as a single line field with multiple values.
///
/// There will be one value per level.
///
/// The type of the scalable setting.
/// The scalable setting to draw.
/// The label of the field.
public static void ValueGUI(this SerializedScalableSetting self, GUIContent label)
where T : struct
{
var schema = ScalableSettingSchema.GetSchemaOrNull(new ScalableSettingSchemaId(self.schemaId.stringValue))
?? ScalableSettingSchema.GetSchemaOrNull(ScalableSettingSchemaId.With3Levels);
EditorGUI.showMixedValue = self.values.hasMultipleDifferentValues;
var count = schema.levelCount;
if (self.values.arraySize != count)
self.values.arraySize = count;
LevelValuesFieldGUI(label, self, count, schema);
EditorGUI.showMixedValue = false;
}
///
/// Draw the value fields for each levels of the scalable setting.
///
/// Assumes that the generic type is the type stored in the .
///
/// The type of the scalable setting.
/// Rect used to draw the GUI.
/// The scalable setting to draw.
/// The number of level to draw.
/// The schema to use when drawing the levels.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void LevelValuesFieldGUI(
GUIContent label,
SerializedScalableSetting scalableSetting,
int count,
ScalableSettingSchema schema
)
where T : struct
{
var labels = new GUIContent[count];
Array.Copy(schema.levelNames, labels, count);
var values = new T[count];
for (var i = 0; i < count; ++i)
values[i] = scalableSetting.values.GetArrayElementAtIndex(i).GetInline();
using (var scope = new EditorGUI.ChangeCheckScope())
{
CoreEditorUtils.DrawMultipleFields(label, labels, values);
if (scope.changed)
{
for (var i = 0; i < count; ++i)
scalableSetting.values.GetArrayElementAtIndex(i).SetInline(values[i]);
}
}
}
}
}