fix foldouts

This commit is contained in:
Mikhail 2024-03-04 03:00:45 +08:00
parent 963cd2a286
commit f88b07950f
8 changed files with 218 additions and 52 deletions

View File

@ -0,0 +1,75 @@
using UnityEngine;
namespace DCFApixels.DragonECS
{
public static class EcsDefaultWorldBuilder
{
public static EcsDefaultWorld Build()
{
return new EcsDefaultWorld();
}
}
namespace Project.Unity
{
public class AutoEntityCreator : MonoBehaviour
{
[SerializeField]
private EcsEntityConnect _connect;
[SerializeField]
private EcsWorldProviderBase _world;
private bool _created;
#region Properties
public EcsEntityConnect Connect => _connect;
#endregion
#region UnityEvents
private void OnValidate()
{
if (_world == null)
{
AutoResolveWorldProviderDependensy();
}
}
private void Start()
{
CreateEntity();
}
#endregion
private void AutoResolveWorldProviderDependensy()
{
_world = EcsDefaultWorldSingletonProvider.Instance;
}
public void ManualStart()
{
CreateEntity();
}
private void CreateEntity()
{
if (_created)
{
return;
}
if (_world == null)
{
AutoResolveWorldProviderDependensy();
}
else
{
InitConnect(_connect, _world.GetRaw());
}
_created = true;
}
private void InitConnect(EcsEntityConnect connect, EcsWorld world)
{
connect.ConnectWith(world.NewEntityLong());
connect.ApplyTemplates();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e244d7ed454067a4bb82fecd87513856
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,6 +1,7 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using DCFApixels.DragonECS.Unity.Internal; using DCFApixels.DragonECS.Unity.Internal;
using System; using System;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -38,10 +39,10 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (nextWorld == false && prewIsUpper == false) if (nextWorld == false && prewIsUpper == false)
{ {
b.Append(' '); b.Append(' ');
nextWorld = true;
} }
} }
else
{
if (nextWorld) if (nextWorld)
{ {
b.Append(char.ToUpper(c)); b.Append(char.ToUpper(c));
@ -51,7 +52,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
b.Append(c); b.Append(c);
} }
nextWorld = false; nextWorld = false;
}
prewIsUpper = isUpper; prewIsUpper = isUpper;
} }
@ -121,21 +121,13 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
GUILayout.Box("", options); GUILayout.Box("", options);
Rect lastRect = GUILayoutUtility.GetLastRect(); Rect lastRect = GUILayoutUtility.GetLastRect();
if (status) Color color = status ? _greenColor : _redColor;
{ string text = status ? CONNECTED : NOT_CONNECTED;
Color color = _greenColor;
color.a = 0.6f; color.a = 0.6f;
EditorGUI.DrawRect(lastRect, color); EditorGUI.DrawRect(lastRect, color);
GUI.Box(lastRect, CONNECTED); GUI.Box(lastRect, text);
}
else
{
Color color = _redColor;
color.a = 0.6f;
EditorGUI.DrawRect(lastRect, color);
GUI.Box(lastRect, NOT_CONNECTED);
}
} }
public static void DrawUndeterminedConnectStatus(params GUILayoutOption[] options) public static void DrawUndeterminedConnectStatus(params GUILayoutOption[] options)
{ {
Init(); Init();
@ -174,8 +166,9 @@ namespace DCFApixels.DragonECS.Unity.Editors
GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, 0.22f)); GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, 0.22f));
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
bool changed = DrawData(pool.ComponentType, new GUIContent(meta.Name), data, out object resultData); Type componentType = pool.ComponentType;
ExpandMatrix expandMatrix = ExpandMatrix.Take(componentType);
bool changed = DrawData(componentType, new GUIContent(meta.Name), expandMatrix, data, out object resultData);
if (changed) if (changed)
{ {
pool.SetRaw(entityID, resultData); pool.SetRaw(entityID, resultData);
@ -193,26 +186,26 @@ namespace DCFApixels.DragonECS.Unity.Editors
GUILayout.Space(2f); GUILayout.Space(2f);
} }
private static bool DrawData(Type fieldType, GUIContent label, object data, out object outData) private static bool DrawData(Type fieldType, GUIContent label, ExpandMatrix expandMatrix, object data, out object outData)
{ {
Type type = data.GetType(); Type type = data.GetType();
var uobj = data as UnityEngine.Object; var uobj = data as UnityEngine.Object;
ref bool isExpanded = ref expandMatrix.Down();
if ((uobj == false && type.IsGenericType) || if ((uobj == false && type.IsGenericType) ||
(uobj == false && !type.IsSerializable)) (uobj == false && !type.IsSerializable))
{ {
bool result = false; bool result = false;
WrapperBase w = RefEditorWrapper.Take(EmptyDummy.Instance);
//w.SO.Update(); isExpanded = EditorGUILayout.Foldout(isExpanded, label);
//EditorGUILayout.PropertyField(w.Property, label, true);
w.Property.isExpanded = EditorGUILayout.Foldout(w.Property.isExpanded, label); if (isExpanded)
if (w.Property.isExpanded)
{ {
EditorGUI.indentLevel++; EditorGUI.indentLevel++;
foreach (var field in type.GetFields(fieldFlags)) foreach (var field in type.GetFields(fieldFlags))
{ {
GUIContent subLabel = new GUIContent(EcsUnityEditorUtility.TransformFieldName(field.Name)); GUIContent subLabel = new GUIContent(EcsUnityEditorUtility.TransformFieldName(field.Name));
if (DrawData(field.FieldType, subLabel, field.GetValue(data), out object fieldData)) if (DrawData(field.FieldType, subLabel, expandMatrix, field.GetValue(data), out object fieldData))
{ {
field.SetValue(data, fieldData); field.SetValue(data, fieldData);
result = true; result = true;
@ -220,26 +213,23 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
EditorGUI.indentLevel--; EditorGUI.indentLevel--;
} }
w.Release();
expandMatrix.Up();
outData = data; outData = data;
return result; return result;
} }
else else
{ {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
WrapperBase w; WrapperBase w = uobj == null ? RefEditorWrapper.Take(data) : UnityObjEditorWrapper.Take(uobj);
if (uobj == null)
{
w = RefEditorWrapper.Take(data);
}
else
{
w = UnityObjEditorWrapper.Take(uobj);
}
w.SO.Update();
w.IsExpanded = isExpanded;
EditorGUILayout.PropertyField(w.Property, label, true); EditorGUILayout.PropertyField(w.Property, label, true);
isExpanded = w.IsExpanded;
w.Release(); w.Release();
expandMatrix.Up();
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
{ {

View File

@ -0,0 +1,57 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
namespace DCFApixels.DragonECS.Unity.Editors
{
internal class ExpandMatrix
{
private const bool TOP_DEFAULT = true;
private const bool DEFAULT = false;
private static Dictionary<Type, ExpandMatrix> _instances = new Dictionary<Type, ExpandMatrix>();
public static ExpandMatrix Take(Type type)
{
if(_instances.TryGetValue(type, out ExpandMatrix result) == false)
{
result = new ExpandMatrix();
_instances.Add(type, result);
}
return result;
}
private bool[] _flags = new bool[8];
private int _count = 0;
private int _ptr = 0;
public int Count
{
get { return _count; }
}
public ref bool CurrentIsExpanded
{
get { return ref _flags[_ptr]; }
}
public void Up()
{
if (_ptr < 0)
{
throw new Exception("нарушение баланса инкремент/декремент");
}
_ptr--;
}
public ref bool Down()
{
_ptr++;
if (_ptr >= _count)
{
if (_count >= _flags.Length)
{
Array.Resize(ref _flags, _flags.Length << 1);
}
_flags[_count++] = _ptr <= 1 ? TOP_DEFAULT : DEFAULT;
}
return ref _flags[_ptr];
}
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fef04508ed32be24386f6b2a43e01b2c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,6 +9,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
[SerializeReference] [SerializeReference]
public object data; public object data;
public override object Data public override object Data
{ {
get { return data; } get { return data; }
@ -17,8 +18,16 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
var result = Take(); var result = Take();
result.data = data; result.data = data;
result.SO.Update();
return result; return result;
} }
} }
} }
[Serializable]
public class EmptyDummy
{
public static readonly EmptyDummy Instance = new EmptyDummy();
private EmptyDummy() { }
}
#endif #endif

View File

@ -9,6 +9,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
[SerializeField] [SerializeField]
public UnityEngine.Object data; public UnityEngine.Object data;
public override object Data public override object Data
{ {
get { return data; } get { return data; }
@ -17,6 +18,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
var result = Take(); var result = Take();
result.data = data; result.data = data;
result.SO.Update();
return result; return result;
} }
} }

View File

@ -10,6 +10,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
internal abstract class WrapperBase : ScriptableObject internal abstract class WrapperBase : ScriptableObject
{ {
public abstract object Data { get; } public abstract object Data { get; }
public abstract bool IsExpanded { get; set; }
public abstract SerializedObject SO { get; } public abstract SerializedObject SO { get; }
public abstract SerializedProperty Property { get; } public abstract SerializedProperty Property { get; }
public abstract void Release(); public abstract void Release();
@ -21,8 +22,16 @@ namespace DCFApixels.DragonECS.Unity.Editors
private SerializedObject _so; private SerializedObject _so;
private SerializedProperty _property; private SerializedProperty _property;
private bool _isDestroyed = false;
private bool _isReleased = false; private bool _isReleased = false;
private static Stack<TSelf> _wrappers = new Stack<TSelf>(); private static Stack<TSelf> _wrappers = new Stack<TSelf>();
public override bool IsExpanded
{
get { return Property.isExpanded; }
set { Property.isExpanded = value; }
}
public override SerializedObject SO public override SerializedObject SO
{ {
get { return _so; } get { return _so; }
@ -31,7 +40,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
get { return _property; } get { return _property; }
} }
public static TSelf Take() public static TSelf Take()
{ {
TSelf result; TSelf result;
@ -44,30 +52,33 @@ namespace DCFApixels.DragonECS.Unity.Editors
else else
{ {
result = _wrappers.Pop(); result = _wrappers.Pop();
if (result._isDestroyed)
{
result = Take();
} }
}
result._isReleased = false;
return result; return result;
} }
public static void Release(TSelf wrapper) public static void Release(TSelf wrapper)
{ {
if (wrapper._isReleased) if (wrapper._isReleased)
{ {
return; throw new Exception();
} }
wrapper._isReleased = true; wrapper._isReleased = true;
_wrappers.Push(wrapper); _wrappers.Push(wrapper);
} }
private void OnDestroy()
{
_isDestroyed = true;
}
public override void Release() public override void Release()
{ {
Release((TSelf)this); Release((TSelf)this);
} }
} }
[Serializable]
public class EmptyDummy
{
public static readonly EmptyDummy Instance = new EmptyDummy();
private EmptyDummy() { }
}
} }
#endif #endif