using UnityEditor;
using System.Collections.Generic;
namespace UnityEngine.AzureSky
{
[ExecuteInEditMode]
[AddComponentMenu("Azure[Sky] Dynamic Skybox/Azure Weather Preset")]
public sealed class AzureWeatherPreset : MonoBehaviour
{
/// The reference to the core system this preset belong to.
public AzureCoreSystem azureCoreSystem { get => m_azureCoreSystem; set => m_azureCoreSystem = value; }
[SerializeField] private AzureCoreSystem m_azureCoreSystem;
/// The list containing all the property groups data of this preset.
public List propertyGroupDataList { get => m_propertyGroupDataList; set => m_propertyGroupDataList = value; }
[SerializeField] private List m_propertyGroupDataList = new List();
private void Reset()
{
#if UNITY_EDITOR
Undo.RecordObject(this, "Reset Weather Preset");
InitPropertyList();
#endif
}
/// Registering to the events.
private void Awake()
{
#if UNITY_EDITOR
AzureNotificationCenterEditor.OnAddWeatherPropertyGroup += OnAddPropertyGroup;
AzureNotificationCenterEditor.OnAddWeatherProperty += OnAddWeatherProperty;
AzureNotificationCenterEditor.OnRemoveWeatherPropertyGroup += OnRemoveWeatherPropertyGroup;
AzureNotificationCenterEditor.OnRemoveWeatherProperty += OnRemoveWeatherProperty;
AzureNotificationCenterEditor.OnReorderWeatherPropertyGroupList += OnReorderWeatherPropertyGroupList;
AzureNotificationCenterEditor.OnReorderWeatherPropertyList += OnReorderWeatherPropertyList;
#endif
}
/// Unregistering to the events.
private void OnDestroy()
{
#if UNITY_EDITOR
AzureNotificationCenterEditor.OnAddWeatherPropertyGroup -= OnAddPropertyGroup;
AzureNotificationCenterEditor.OnAddWeatherProperty -= OnAddWeatherProperty;
AzureNotificationCenterEditor.OnRemoveWeatherPropertyGroup -= OnRemoveWeatherPropertyGroup;
AzureNotificationCenterEditor.OnRemoveWeatherProperty -= OnRemoveWeatherProperty;
AzureNotificationCenterEditor.OnReorderWeatherPropertyGroupList -= OnReorderWeatherPropertyGroupList;
AzureNotificationCenterEditor.OnReorderWeatherPropertyList -= OnReorderWeatherPropertyList;
#endif
}
/// Initialize the property list to match with the custom property list from the core system.
public void InitPropertyList()
{
// If the preset game object is a child of the Azure Core System
m_azureCoreSystem = GetComponentInParent();
// If the m_azureCoreSystem is still null, get the first game object in the scene using the AzureCoreSystem component
if (m_azureCoreSystem == null)
{
m_azureCoreSystem = FindObjectOfType();
}
// If the m_azureCoreSystem is still null, just give up
// We can still use the Unity's component copy/paste feature to initialize it manually...
// from another weather preset already configured in the scene
if (m_azureCoreSystem == null)
{
return;
}
if (m_azureCoreSystem)
{
if (m_azureCoreSystem.weatherSystem.weatherPropertyGroupList.Count == m_propertyGroupDataList.Count)
{
for (int i = 0; i < m_propertyGroupDataList.Count; i++)
{
if (m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList.Count == m_propertyGroupDataList[i].propertyDataList.Count)
{
for (int j = 0; j < m_propertyGroupDataList[i].propertyDataList.Count; j++)
{
m_propertyGroupDataList[i].propertyDataList[j].weatherPropertyOwner = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList[j];
}
}
else
{
for (int j = 0; j < m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList.Count; j++)
{
if (m_propertyGroupDataList[i].propertyDataList.Count < m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList.Count)
{
AzurePropertyData newPropertyData = new AzurePropertyData();
m_propertyGroupDataList[i].propertyDataList.Add(newPropertyData);
newPropertyData.weatherPropertyOwner = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList[j];
InitNewPropertyData(newPropertyData);
}
else
{
m_propertyGroupDataList[i].propertyDataList.RemoveAt(j);
}
m_propertyGroupDataList[i].propertyDataList[j].weatherPropertyOwner = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList[j];
}
}
m_propertyGroupDataList[i].name = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].name;
}
}
else
{
for (int i = 0; i < m_azureCoreSystem.weatherSystem.weatherPropertyGroupList.Count; i++)
{
if (m_propertyGroupDataList.Count < m_azureCoreSystem.weatherSystem.weatherPropertyGroupList.Count)
{
AzurePropertyGroupData newPropertyGroup = new AzurePropertyGroupData();
m_propertyGroupDataList.Add(newPropertyGroup);
for (int j = 0; j < m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList.Count; j++)
{
AzurePropertyData newPropertyData = new AzurePropertyData();
newPropertyGroup.propertyDataList.Add(newPropertyData);
newPropertyData.weatherPropertyOwner = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList[j];
InitNewPropertyData(newPropertyData);
}
}
else
{
m_propertyGroupDataList.RemoveAt(i);
}
m_propertyGroupDataList[i].name = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].name;
}
}
}
}
//#if UNITY_EDITOR
//[ContextMenu("CheckOwners()")]
//private void CheckOwners()
//{
// for (int i = 0; i < m_propertyGroupDataList.Count; i++)
// {
// for (int j = 0; j < m_propertyGroupDataList[i].propertyDataList.Count; j++)
// {
// if (m_propertyGroupDataList[i].propertyDataList[j].weatherPropertyOwner != m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[i].weatherPropertyList[j])
// {
// Debug.Log("The custom property: " + m_propertyGroupDataList[i].propertyDataList[j].weatherPropertyOwner.name + " is missing its owner!");
// }
//
// if (m_propertyGroupDataList[i].propertyDataList[j].weatherPropertyOwner == null)
// {
// Debug.Log("The custom property is missing its owner! Group Index: " + i + " Property Index: " + j);
// }
// }
// }
//}
//#endif
/// Initialize the new property data according to its owner default values.
private void InitNewPropertyData(AzurePropertyData propertyData)
{
propertyData.floatData = propertyData.weatherPropertyOwner.defaultFloatValue;
propertyData.colorData = propertyData.weatherPropertyOwner.defaultColorValue;
propertyData.curveData = new AnimationCurve();
propertyData.curveData.CopyFrom(propertyData.weatherPropertyOwner.defaultCurveValue);
propertyData.gradientData = new Gradient();
propertyData.gradientData.SetKeys(propertyData.weatherPropertyOwner.defaultGradientValue.colorKeys, propertyData.weatherPropertyOwner.defaultGradientValue.alphaKeys);
propertyData.vector3Data = propertyData.weatherPropertyOwner.defaultVector3Value;
}
#if UNITY_EDITOR
/// Add a new element to the weather property group list.
private void OnAddPropertyGroup(AzureCoreSystem azureCoreSystem)
{
if (m_azureCoreSystem == azureCoreSystem)
{
Undo.RecordObject(this, "Add Weather Property Group");
AzurePropertyGroupData newPropertyGroup = new AzurePropertyGroupData();
int lastIndex = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList.Count - 1;
newPropertyGroup.name = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[lastIndex].name;
m_propertyGroupDataList.Add(newPropertyGroup);
//InitPropertyList();
}
}
/// Add a new element to the weather property list.
private void OnAddWeatherProperty(AzureCoreSystem azureCoreSystem, int groupIndex)
{
if (m_azureCoreSystem == azureCoreSystem)
{
Undo.RecordObject(this, "Add Weather Property");
AzurePropertyData newPropertyData = new AzurePropertyData();
int lastIndex = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[groupIndex].weatherPropertyList.Count - 1;
newPropertyData.weatherPropertyOwner = m_azureCoreSystem.weatherSystem.weatherPropertyGroupList[groupIndex].weatherPropertyList[lastIndex];
InitNewPropertyData(newPropertyData);
m_propertyGroupDataList[groupIndex].propertyDataList.Add(newPropertyData);
//InitPropertyList();
}
}
/// Remove an element of the weather property group list.
private void OnRemoveWeatherPropertyGroup(AzureCoreSystem azureCoreSystem, int groupIndex)
{
if (m_azureCoreSystem == azureCoreSystem)
{
Undo.RecordObject(this, "Remove Weather Property Group");
AzurePropertyGroupData data = m_propertyGroupDataList[groupIndex];
m_propertyGroupDataList.RemoveAt(groupIndex);
data = null;
//InitPropertyList();
}
}
/// Remove an element from a weather property list.
private void OnRemoveWeatherProperty(AzureCoreSystem azureCoreSystem, int groupIndex, int propertyIndex)
{
if (m_azureCoreSystem == azureCoreSystem)
{
Undo.RecordObject(this, "Remove Weather Property");
AzurePropertyData data = m_propertyGroupDataList[groupIndex].propertyDataList[propertyIndex];
m_propertyGroupDataList[groupIndex].propertyDataList.RemoveAt(propertyIndex);
data = null;
//InitPropertyList();
}
}
/// Reorder the weather property group list.
private void OnReorderWeatherPropertyGroupList(AzureCoreSystem azureCoreSystem, int oldIndex, int newIndex)
{
if (m_azureCoreSystem == azureCoreSystem)
{
Undo.RecordObject(this, "Reorder Weather Property Group");
AzurePropertyGroupData item = m_propertyGroupDataList[oldIndex];
m_propertyGroupDataList.RemoveAt(oldIndex);
m_propertyGroupDataList.Insert(newIndex, item);
//InitPropertyList();
}
}
/// Reorder the weather property list.
private void OnReorderWeatherPropertyList(AzureCoreSystem azureCoreSystem, int groupIndex, int oldIndex, int newIndex)
{
if (m_azureCoreSystem == azureCoreSystem)
{
Undo.RecordObject(this, "Reorder Weather Property");
AzurePropertyData item = m_propertyGroupDataList[groupIndex].propertyDataList[oldIndex];
m_propertyGroupDataList[groupIndex].propertyDataList.RemoveAt(oldIndex);
m_propertyGroupDataList[groupIndex].propertyDataList.Insert(newIndex, item);
//InitPropertyList();
}
}
#endif
}
}