restructure
This commit is contained in:
@ -0,0 +1,133 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Convai.Scripts.Runtime.Features;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Editor.NarrativeDesign
|
||||
{
|
||||
[CustomEditor(typeof(NarrativeDesignManager))]
|
||||
public class NarrativeDesignManagerEditor : UnityEditor.Editor
|
||||
{
|
||||
private NarrativeDesignManager _narrativeDesignManager;
|
||||
private SerializedProperty _sectionChangeEvents;
|
||||
private bool _sectionEventsExpanded = true;
|
||||
private Dictionary<string, bool> _sectionIdExpanded = new();
|
||||
private SerializedObject _serializedObject;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_serializedObject = new SerializedObject(target);
|
||||
_narrativeDesignManager = (NarrativeDesignManager)target;
|
||||
FindProperties();
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
_serializedObject.Update();
|
||||
|
||||
DrawUpdateButton();
|
||||
DrawSectionEvents();
|
||||
|
||||
_serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
private void DrawUpdateButton()
|
||||
{
|
||||
if (GUILayout.Button("Check for Updates")) OnUpdateNarrativeDesignButtonClicked();
|
||||
GUILayout.Space(10);
|
||||
}
|
||||
|
||||
private void DrawSectionEvents()
|
||||
{
|
||||
if (_narrativeDesignManager.sectionDataList.Count == 0) return;
|
||||
|
||||
_sectionEventsExpanded = EditorGUILayout.Foldout(_sectionEventsExpanded, "Section Events", true, EditorStyles.foldoutHeader);
|
||||
if (!_sectionEventsExpanded) return;
|
||||
|
||||
EditorGUI.indentLevel++;
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
for (int i = 0; i < _narrativeDesignManager.sectionDataList.Count; i++) DrawSectionEvent(i);
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
_serializedObject.ApplyModifiedProperties();
|
||||
_narrativeDesignManager.OnSectionEventListChange();
|
||||
}
|
||||
|
||||
EditorGUI.indentLevel--;
|
||||
}
|
||||
|
||||
private void DrawSectionEvent(int index)
|
||||
{
|
||||
SectionData sectionData = _narrativeDesignManager.sectionDataList[index];
|
||||
string sectionId = sectionData.sectionId;
|
||||
|
||||
EnsureSectionChangeEventsDataExists(sectionId);
|
||||
|
||||
_sectionIdExpanded.TryAdd(sectionId, false);
|
||||
|
||||
GUIStyle sectionIdStyle = CreateSectionIdStyle();
|
||||
|
||||
string sectionIdText = $"{sectionData.sectionName} - {sectionId}";
|
||||
_sectionIdExpanded[sectionId] = EditorGUILayout.Foldout(_sectionIdExpanded[sectionId], sectionIdText, true, sectionIdStyle);
|
||||
|
||||
if (_sectionIdExpanded[sectionId]) DrawSectionChangeEvents(index);
|
||||
}
|
||||
|
||||
private void EnsureSectionChangeEventsDataExists(string sectionId)
|
||||
{
|
||||
if (!_narrativeDesignManager.sectionChangeEventsDataList.Exists(x => x.id == sectionId))
|
||||
_narrativeDesignManager.sectionChangeEventsDataList.Add(new SectionChangeEventsData { id = sectionId });
|
||||
}
|
||||
|
||||
private GUIStyle CreateSectionIdStyle()
|
||||
{
|
||||
return new GUIStyle(EditorStyles.foldoutHeader)
|
||||
{
|
||||
fontStyle = FontStyle.Bold,
|
||||
fontSize = 14
|
||||
};
|
||||
}
|
||||
|
||||
private void DrawSectionChangeEvents(int index)
|
||||
{
|
||||
EditorGUI.indentLevel++;
|
||||
SerializedProperty sectionChangeEventsProperty = _sectionChangeEvents.GetArrayElementAtIndex(index);
|
||||
EditorGUILayout.PropertyField(sectionChangeEventsProperty, GUIContent.none, true);
|
||||
EditorGUI.indentLevel--;
|
||||
}
|
||||
|
||||
private async void OnUpdateNarrativeDesignButtonClicked()
|
||||
{
|
||||
await Task.WhenAll(_narrativeDesignManager.UpdateSectionListAsync(), _narrativeDesignManager.UpdateTriggerListAsync());
|
||||
|
||||
// Remove section change events for deleted sections
|
||||
_narrativeDesignManager.sectionChangeEventsDataList = _narrativeDesignManager.sectionChangeEventsDataList
|
||||
.Where(changeEvent => _narrativeDesignManager.sectionDataList.Any(section => section.sectionId == changeEvent.id))
|
||||
.ToList();
|
||||
|
||||
// Remove expanded states for deleted sections
|
||||
_sectionIdExpanded = _sectionIdExpanded
|
||||
.Where(kvp => _narrativeDesignManager.sectionDataList.Any(section => section.sectionId == kvp.Key))
|
||||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
|
||||
|
||||
_serializedObject.Update(); // Update the serialized object
|
||||
_serializedObject.ApplyModifiedProperties();
|
||||
_narrativeDesignManager.OnSectionEventListChange();
|
||||
|
||||
// Force the inspector to repaint
|
||||
Repaint();
|
||||
}
|
||||
|
||||
|
||||
private void FindProperties()
|
||||
{
|
||||
_serializedObject.FindProperty(nameof(NarrativeDesignManager.sectionDataList));
|
||||
_serializedObject.FindProperty(nameof(NarrativeDesignManager.triggerDataList));
|
||||
_sectionChangeEvents = _serializedObject.FindProperty(nameof(NarrativeDesignManager.sectionChangeEventsDataList));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1537f32558f6e8042878f1a95c1fe984
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,43 @@
|
||||
using Convai.Scripts.Runtime.Features;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Editor.NarrativeDesign
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(SectionChangeEventsData))]
|
||||
public class NarrativeDesignSectionChangeEventsDataPropertyDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
EditorGUI.BeginProperty(position, label, property);
|
||||
|
||||
SerializedProperty sectionIdProperty = property.FindPropertyRelative("id");
|
||||
SerializedProperty onSectionStartProperty = property.FindPropertyRelative("onSectionStart");
|
||||
SerializedProperty onSectionEndProperty = property.FindPropertyRelative("onSectionEnd");
|
||||
|
||||
Rect sectionIdRect = new(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
|
||||
EditorGUI.LabelField(sectionIdRect, "Section ID", sectionIdProperty.stringValue);
|
||||
|
||||
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
|
||||
EditorGUI.PropertyField(position, onSectionStartProperty, true);
|
||||
position.y += EditorGUI.GetPropertyHeight(onSectionStartProperty) + EditorGUIUtility.standardVerticalSpacing;
|
||||
|
||||
EditorGUI.PropertyField(position, onSectionEndProperty, true);
|
||||
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
SerializedProperty onSectionStartProperty = property.FindPropertyRelative("onSectionStart");
|
||||
SerializedProperty onSectionEndProperty = property.FindPropertyRelative("onSectionEnd");
|
||||
|
||||
float height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
height += EditorGUI.GetPropertyHeight(onSectionStartProperty) + EditorGUIUtility.standardVerticalSpacing;
|
||||
height += EditorGUI.GetPropertyHeight(onSectionEndProperty);
|
||||
|
||||
return height;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23fcdcc9acc44804e991455dcd85953d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,34 @@
|
||||
using System.Threading.Tasks;
|
||||
using Convai.Scripts.Runtime.Features;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convai.Scripts.Editor.NarrativeDesign
|
||||
{
|
||||
[CustomEditor(typeof(NarrativeDesignTrigger))]
|
||||
public class NarrativeDesignTriggerEditor : UnityEditor.Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
NarrativeDesignTrigger narrativeDesignTrigger = (NarrativeDesignTrigger)target;
|
||||
|
||||
if (GUILayout.Button("Update Triggers"))
|
||||
if (narrativeDesignTrigger.convaiNPC != null)
|
||||
{
|
||||
NarrativeDesignManager manager = narrativeDesignTrigger.convaiNPC.GetComponent<NarrativeDesignManager>();
|
||||
if (manager != null)
|
||||
manager.UpdateTriggerListAsync().ContinueWith(_ =>
|
||||
{
|
||||
narrativeDesignTrigger.UpdateAvailableTriggers();
|
||||
EditorUtility.SetDirty(narrativeDesignTrigger);
|
||||
}, TaskScheduler.FromCurrentSynchronizationContext());
|
||||
}
|
||||
|
||||
GUILayout.Space(10);
|
||||
DrawDefaultInspector();
|
||||
if (narrativeDesignTrigger.availableTriggers is { Count: > 0 })
|
||||
narrativeDesignTrigger.selectedTriggerIndex =
|
||||
EditorGUILayout.Popup("Trigger", narrativeDesignTrigger.selectedTriggerIndex, narrativeDesignTrigger.availableTriggers.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cdf68c1817bd4102879052a18d773fec
|
||||
timeCreated: 1706873993
|
||||
Reference in New Issue
Block a user