Skip to content

Commit

Permalink
Major Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
knowlife4 committed Mar 9, 2023
1 parent 2fc9c4e commit c84d0c1
Show file tree
Hide file tree
Showing 30 changed files with 295 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace ECT.UnityEditor
{
public class CreationEditor : EditorWindow
public class ComponentWizard : EditorWindow
{
[MenuItem("Assets/Create/ECT/Component Instance", false, -10)]
[MenuItem("Assets/ECT/Component Wizard", false, -10)]
static void Open()
{
CreationEditor window = CreateInstance<CreationEditor>();
window.position = new Rect(Screen.width / 2f, Screen.height / 2f, 250, 150);
ComponentWizard window = CreateInstance<ComponentWizard>();

Vector2 size = new Vector2(250, 150);
Vector2 position = new Vector2((Screen.currentResolution.width - size.x) / 2, (Screen.currentResolution.height - size.y) / 2);
window.position = new Rect(position, size);

window.ShowPopup();
}

Expand Down Expand Up @@ -44,6 +48,8 @@ static void Open()

string Name { get; set; }

protected void OnLostFocus() => Close();

protected void OnEnable()
{
(AllComponents, ComponentEntries) = GetType(typeof(IComponent));
Expand All @@ -52,9 +58,16 @@ protected void OnEnable()
void OnGUI()
{
GUILayout.BeginHorizontal("box");
GUIStyle style = EditorStyles.boldLabel;
style.alignment = TextAnchor.UpperCenter;
style.fontSize = 18;
GUIStyle style = new()
{
alignment = TextAnchor.UpperCenter,
fontSize = 18,
normal =
{
textColor = Color.white
},
fontStyle = FontStyle.Bold
};

EditorGUILayout.LabelField("Component Wizard", style);
GUILayout.EndHorizontal();
Expand All @@ -76,9 +89,8 @@ void OnGUI()

GUILayout.BeginHorizontal();

if (GUILayout.Button("Create")) CreateComponent();
if (GUILayout.Button("Cancel")) Close();

if (GUILayout.Button("Create") && !string.IsNullOrWhiteSpace(Name)) CreateComponent();

GUILayout.EndHorizontal();

GUILayout.EndVertical();
Expand All @@ -90,7 +102,7 @@ void CreateComponent()

ScriptableObject scriptableObject = CreateInstance(selected);

string filePath = Selection.assetGUIDs.Length == 0 ? "Assets/New TMP Color Gradient.asset" : AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
string filePath = Selection.assetGUIDs.Length == 0 ? "Assets" : AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);

AssetDatabase.CreateAsset(scriptableObject, $"{filePath}/{Name}.asset");
AssetDatabase.SaveAssets();
Expand Down
File renamed without changes.
13 changes: 10 additions & 3 deletions ECT/Assets/Editor/EntityEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public class EntityEditor : Editor

int previousComponentLength;

public void OnEnable()
{
UpdateInfo();
}

public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
Expand All @@ -28,7 +33,7 @@ public override void OnInspectorGUI()
void UpdateInfo()
{
entity = (IEntity)target;
components = entity.GetComponents();
components = entity.GetComponentsRecursively().ToArray();

referenceGroup = entity.ReferenceGroup;

Expand All @@ -41,7 +46,9 @@ void Populate()

foreach (IComponent component in components)
{
Type type = component.SceneReferenceConstructor.Type;
IDynamicConstructor sceneReferenceConstructor = component.GetSceneReferenceConstructor();

Type type = sceneReferenceConstructor?.Type;

if(type == null) continue;

Expand All @@ -55,7 +62,7 @@ void Populate()
continue;
}

ISceneReference reference = (ISceneReference)component.SceneReferenceConstructor.Create();
ISceneReference reference = (ISceneReference)sceneReferenceConstructor.Create();

references.Add(reference);
}
Expand Down
2 changes: 1 addition & 1 deletion ECT/Assets/Editor/SceneReferenceGroupEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
public void DrawElement(Rect rect, int index, SerializedProperty listProperty)
{
SerializedProperty baseProp = listProperty.GetArrayElementAtIndex(index);
string name = baseProp.managedReferenceFullTypename.Split('.').Last();
string name = baseProp.managedReferenceFullTypename.Split(' ').Last().Split('.').Last();

GUI.Label(rect, name);

Expand Down
20 changes: 20 additions & 0 deletions ECT/Assets/Editor/ScriptTemplates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEditor;
using UnityEngine;

namespace ECT.UnityEditor
{
public class ScriptTemplates
{
[MenuItem("Assets/ECT/C# Script/Entity", false, -10)]
static void EntityTemplate()
{
ProjectWindowUtil.CreateScriptAssetFromTemplateFile("Assets/ScriptTemplates/ECTEntityTemplate.txt", "MyEntity.cs");
}

[MenuItem("Assets/ECT/C# Script/Component", false, -10)]
static void ComponentTemplate()
{
ProjectWindowUtil.CreateScriptAssetFromTemplateFile("Assets/ScriptTemplates/ECTComponentTemplate.txt", "MyComponent.cs");
}
}
}
11 changes: 11 additions & 0 deletions ECT/Assets/Editor/ScriptTemplates.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion ECT/Assets/Runtime/Attributes/SceneReferenceAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public static Type Find(Type componentType)
{
Type[] nested = componentType.GetNestedTypes();

return nested.FirstOrDefault(type => type.IsDefined(typeof(SceneReferenceAttribute), false));
Type firstOrDefault = nested.FirstOrDefault(type => type.IsDefined(typeof(SceneReferenceAttribute), false));

return firstOrDefault;
}
}
}
7 changes: 4 additions & 3 deletions ECT/Assets/Runtime/ECTAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

namespace ECT
{
public struct ECTAction
public struct ECTAction : IAction
{
private List<Action> callbacks;
List<Action> Callbacks => callbacks ??= new();
public ECTAction(params Action[] callbacks) => Callbacks = new(callbacks);

HashSet<Action> Callbacks { get; }

public bool Contains(Action function) => Callbacks.Contains(function);

Expand Down
39 changes: 23 additions & 16 deletions ECT/Assets/Runtime/ECTComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using UnityEngine;

namespace ECT
Expand All @@ -11,41 +11,48 @@ public abstract class ECTComponent<TRoot, TParent> : ScriptableObject, IComponen
protected ECTComponent()
{
Type type = GetType();

SystemConstructor = new(ComponentSystemAttribute.Find(type));
SceneReferenceConstructor = new(SceneReferenceAttribute.Find(type));
}

ECTDynamicConstructor<ISystem> SystemConstructor { get; }
ECTConstructor<ISystem> SystemConstructor { get; }

IDynamicConstructor IComponent.SceneReferenceConstructor => SceneReferenceConstructor;
ECTDynamicConstructor<ISceneReference> SceneReferenceConstructor { get; }

protected virtual ISystem CreateSystem() => SystemConstructor.Create();

public virtual ECTSystemData CreateSystemData(IRoot root, IParent parent)
ECTConstructor<ISceneReference> SceneReferenceConstructor { get; }

public IDynamicConstructor GetSystemConstructor() => SystemConstructor;

public IDynamicConstructor GetSceneReferenceConstructor() => SceneReferenceConstructor;

public ECTSystemData CreateSystemData(IRoot root, IParent parent)
{
ISystem system = CreateSystem();
ECTSystemData data = CreateSystemData((TRoot)root, (TParent)parent, system);
ECTSystemData data = CreateSystemData(new((TRoot)root, (TParent)parent, system));
system.SetData(data);

return data;
}

protected virtual ECTSystemData CreateSystemData(TRoot root, TParent parent, ISystem system) => new ECTSystemData(root, parent, this, system);
protected virtual ISystem CreateSystem() => SystemConstructor.Create();

protected virtual ECTSystemData CreateSystemData(ECTSystemData.SystemInfo info) => new(info, this);

public virtual IEnumerable<IComponent> GetComponentsRecursively()
{
yield return this;
}

public abstract class System<TComponent> : ECTSystem<TRoot, TParent, TComponent, ECTSystemData> where TComponent : class, IComponent { }

public abstract class Parent : ECTComponentParent<TRoot, TParent, Parent> { }

internal virtual IComponent[] GetComponents() => new[] { this };
IComponent[] IReferenceComponent.GetComponents() => GetComponents();
}

public interface IComponent : IReferenceComponent
{
public IDynamicConstructor SceneReferenceConstructor { get; }
IDynamicConstructor GetSystemConstructor();

IDynamicConstructor GetSceneReferenceConstructor();

public ECTSystemData CreateSystemData(IRoot root, IParent parent);
ECTSystemData CreateSystemData(IRoot root, IParent parent);
}
}
40 changes: 22 additions & 18 deletions ECT/Assets/Runtime/ECTComponentParent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using UnityEngine;

namespace ECT
{
Expand All @@ -7,43 +8,46 @@ public abstract class ECTComponentParent<TRoot, TParent, TComponentParent> : ECT
where TParent : class, IParent
where TComponentParent : class, IComponent, IParent
{
public ECTComponentGroup<ECTComponent<TRoot, TComponentParent>> ComponentGroup;
IComponentGroup IParent.ComponentGroup => ComponentGroup;
[SerializeField]
ECTComponentGroup<ECTComponent<TRoot, TComponentParent>> componentGroup;

internal override IComponent[] GetComponents()
public IComponentGroup ComponentGroup => componentGroup;

public override IEnumerable<IComponent> GetComponentsRecursively()
{
List<IComponent> components = new();

components.AddRange(ComponentGroup.GetComponents());
components.Add(this);
yield return this;

return components.ToArray();
foreach (IComponent component in ComponentGroup.GetComponentsRecursively()) yield return component;
}

protected override ECTSystemData CreateSystemData(TRoot root, TParent parent, ISystem system) => new ComponentParentSystemData(root, parent, this, system, ComponentGroup);
protected override ECTSystemData CreateSystemData(ECTSystemData.SystemInfo info) => new ComponentParentSystemData(info, this, componentGroup);

public abstract class Component : ECTComponent<TRoot, TComponentParent> { }

public class ComponentParentSystemData : ECTSystemData
{
public ECTSystemDataGroup DataGroup;
public ComponentParentSystemData(SystemInfo info, IComponent component, ECTComponentGroup<ECTComponent<TRoot, TComponentParent>> componentGroup) : base(info, component)
{
DataGroup = new(componentGroup.Components);
}

public ECTSystemDataGroup DataGroup { get; }

public override TSystem GetSystem<TSystem>()
{
if (System is TSystem system) return system;
if (Info.System is TSystem system) return system;

return DataGroup.GetSystem<TSystem>();
}

public ComponentParentSystemData(IRoot root, IParent parent, IComponent component, ISystem system, ECTComponentGroup<ECTComponent<TRoot, TComponentParent>> componentGroup) : base(root, parent, component, system)
{
DataGroup = new(componentGroup.Components);
}
}

public new abstract class System<TComponent> : ECTSystem<TRoot, TParent, TComponent, ComponentParentSystemData> where TComponent : class, IComponent, IParent
{
protected override void OnUpdate() => Data.DataGroup.Update(Root, Component);
protected override void OnUpdate()
{
Data.DataGroup.Update(Root, Component);
}
}

public abstract class Component : ECTComponent<TRoot, TComponentParent> { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace ECT
{
public class ECTDynamicConstructor<TReturn> : IDynamicConstructor
public class ECTConstructor<TReturn> : IDynamicConstructor
{
public ECTDynamicConstructor(Type type)
public ECTConstructor(Type type)
{
Type = type;
}

public Type Type { get; }

Func<TReturn> ConstructorFunc
{
get
Expand All @@ -27,11 +27,11 @@ Func<TReturn> ConstructorFunc
void Compile()
{
NewExpression constructorExpression = Expression.New(Type);

Expression conversionExpression = Expression.Convert(constructorExpression, typeof(TReturn));

Expression<Func<TReturn>> lambdaExpression = Expression.Lambda<Func<TReturn>>(conversionExpression);

constructorFunc = lambdaExpression.Compile();
}

Expand Down
File renamed without changes.
Loading

0 comments on commit c84d0c1

Please sign in to comment.