Skip to content

Commit

Permalink
* First implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Leopotam committed Jun 5, 2021
1 parent 02e6a05 commit f3671d5
Show file tree
Hide file tree
Showing 34 changed files with 920 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Editor.meta

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

123 changes: 123 additions & 0 deletions Editor/EcsEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityEditor integration https://github.com/Leopotam/ecslite-unityeditor
// for LeoECS Lite https://github.com/Leopotam/ecslite
// Copyright (c) 2021 Leopotam <[email protected]>
// ----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace Leopotam.EcsLite.UnityEditor {
[CustomEditor (typeof (EcsEntityObserver))]
sealed class EcsEntityObserverInspector : Editor {
const int MaxFieldToStringLength = 128;

static object[] _componentsCache = new object[32];

EcsEntityObserver _observer;

public override void OnInspectorGUI () {
if (_observer.World != null) {
var guiEnabled = GUI.enabled;
GUI.enabled = true;
DrawComponents ();
GUI.enabled = guiEnabled;
EditorUtility.SetDirty (target);
}
}

void OnEnable () {
_observer = target as EcsEntityObserver;
}

void OnDisable () {
_observer = null;
}

void DrawComponents () {
if (_observer.gameObject.activeSelf) {
var count = _observer.World.GetComponents (_observer.Entity, ref _componentsCache);
for (var i = 0; i < count; i++) {
var component = _componentsCache[i];
_componentsCache[i] = null;
var type = component.GetType ();
GUILayout.BeginVertical (GUI.skin.box);
var typeName = EditorHelpers.GetCleanGenericTypeName (type);
if (!EcsComponentInspectors.Render (typeName, type, component, _observer)) {
EditorGUILayout.LabelField (typeName, EditorStyles.boldLabel);
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel++;
foreach (var field in type.GetFields (BindingFlags.Instance | BindingFlags.Public)) {
DrawTypeField (component, field, _observer);
}
EditorGUI.indentLevel = indent;
}
GUILayout.EndVertical ();
EditorGUILayout.Space ();
}
}
}

void DrawTypeField (object instance, FieldInfo field, EcsEntityObserver entity) {
var fieldValue = field.GetValue (instance);
var fieldType = field.FieldType;
if (!EcsComponentInspectors.Render (field.Name, fieldType, fieldValue, entity)) {
if (fieldType == typeof (UnityEngine.Object) || fieldType.IsSubclassOf (typeof (UnityEngine.Object))) {
GUILayout.BeginHorizontal ();
EditorGUILayout.LabelField (field.Name, GUILayout.MaxWidth (EditorGUIUtility.labelWidth - 16));
var guiEnabled = GUI.enabled;
GUI.enabled = false;
EditorGUILayout.ObjectField (fieldValue as UnityEngine.Object, fieldType, false);
GUI.enabled = guiEnabled;
GUILayout.EndHorizontal ();
return;
}
var strVal = fieldValue != null ? string.Format (System.Globalization.CultureInfo.InvariantCulture, "{0}", fieldValue) : "null";
if (strVal.Length > MaxFieldToStringLength) {
strVal = strVal.Substring (0, MaxFieldToStringLength);
}
GUILayout.BeginHorizontal ();
EditorGUILayout.LabelField (field.Name, GUILayout.MaxWidth (EditorGUIUtility.labelWidth - 16));
EditorGUILayout.SelectableLabel (strVal, GUILayout.MaxHeight (EditorGUIUtility.singleLineHeight));
GUILayout.EndHorizontal ();
}
}
}

static class EcsComponentInspectors {
static readonly Dictionary<Type, IEcsComponentInspector> Inspectors = new Dictionary<Type, IEcsComponentInspector> ();

static EcsComponentInspectors () {
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies ()) {
foreach (var type in assembly.GetTypes ()) {
if (typeof (IEcsComponentInspector).IsAssignableFrom (type) && !type.IsInterface) {
if (Activator.CreateInstance (type) is IEcsComponentInspector inspector) {
var componentType = inspector.GetFieldType ();
if (Inspectors.ContainsKey (componentType)) {
Debug.LogWarningFormat ("Inspector for \"{0}\" already exists, new inspector will be used instead.", componentType.Name);
}
Inspectors[componentType] = inspector;
}
}
}
}
}

public static bool Render (string label, Type type, object value, EcsEntityObserver observer) {
if (Inspectors.TryGetValue (type, out var inspector)) {
inspector.OnGUI (label, value, observer.World, observer.Entity);
return true;
}
return false;
}
}

public interface IEcsComponentInspector {
Type GetFieldType ();
void OnGUI (string label, object value, EcsWorld world, int entityId);
}
}
11 changes: 11 additions & 0 deletions Editor/EcsEntity.cs.meta

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

28 changes: 28 additions & 0 deletions Editor/EcsWorld.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityEditor integration https://github.com/Leopotam/ecslite-unityeditor
// for LeoECS Lite https://github.com/Leopotam/ecslite
// Copyright (c) 2021 Leopotam <[email protected]>
// ----------------------------------------------------------------------------

using UnityEditor;
using UnityEngine;

namespace Leopotam.EcsLite.UnityEditor {
[CustomEditor (typeof (EcsWorldObserver))]
sealed class EcsWorldObserverInspector : Editor {
public override void OnInspectorGUI () {
// var observer = (Runtime.EcsWorldObserver) target;
// var stats = observer.GetStats ();
var guiEnabled = GUI.enabled;
GUI.enabled = true;
GUILayout.BeginVertical (GUI.skin.box);
// EditorGUILayout.LabelField ("Components", stats.Components.ToString ());
// EditorGUILayout.LabelField ("Filters", stats.Filters.ToString ());
// EditorGUILayout.LabelField ("Active entities", stats.ActiveEntities.ToString ());
// EditorGUILayout.LabelField ("Reserved entities", stats.ReservedEntities.ToString ());
GUILayout.EndVertical ();
GUI.enabled = guiEnabled;
}
}
}
11 changes: 11 additions & 0 deletions Editor/EcsWorld.cs.meta

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

8 changes: 8 additions & 0 deletions Editor/Inspectors.meta

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

22 changes: 22 additions & 0 deletions Editor/Inspectors/Quaternion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityEditor integration https://github.com/Leopotam/ecslite-unityeditor
// for LeoECS Lite https://github.com/Leopotam/ecslite
// Copyright (c) 2021 Leopotam <[email protected]>
// ----------------------------------------------------------------------------

using System;
using UnityEditor;
using UnityEngine;

namespace Leopotam.EcsLite.UnityEditor.Inspectors {
sealed class QuaternionInspector : IEcsComponentInspector {
public Type GetFieldType () {
return typeof (Quaternion);
}

public void OnGUI (string label, object value, EcsWorld world, int entityId) {
EditorGUILayout.Vector3Field (label, ((Quaternion) value).eulerAngles);
}
}
}
13 changes: 13 additions & 0 deletions Editor/Inspectors/Quaternion.cs.meta

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

22 changes: 22 additions & 0 deletions Editor/Inspectors/Vector3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityEditor integration https://github.com/Leopotam/ecslite-unityeditor
// for LeoECS Lite https://github.com/Leopotam/ecslite
// Copyright (c) 2021 Leopotam <[email protected]>
// ----------------------------------------------------------------------------

using System;
using UnityEditor;
using UnityEngine;

namespace Leopotam.EcsLite.UnityEditor.Inspectors {
sealed class Vector3Inspector : IEcsComponentInspector {
public Type GetFieldType () {
return typeof (Vector3);
}

public void OnGUI (string label, object value, EcsWorld world, int entityId) {
EditorGUILayout.Vector3Field (label, (Vector3) value);
}
}
}
13 changes: 13 additions & 0 deletions Editor/Inspectors/Vector3.cs.meta

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

19 changes: 19 additions & 0 deletions Editor/Leopotam.EcsLite.UnityEditor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Leopotam.EcsLite.UnityEditor",
"rootNamespace": "Leopotam.EcsLite.UnityEditor",
"references": [
"Leopotam.EcsLite",
"Leopotam.EcsLite.UnityEditor.Runtime"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/Leopotam.EcsLite.UnityEditor.asmdef.meta

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

8 changes: 8 additions & 0 deletions Editor/Templates.meta

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

5 changes: 5 additions & 0 deletions Editor/Templates/Component.cs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace #NS# {
struct #SCRIPTNAME# {
// add your data here.
}
}
7 changes: 7 additions & 0 deletions Editor/Templates/Component.cs.txt.meta

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

9 changes: 9 additions & 0 deletions Editor/Templates/InitSystem.cs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Leopotam.EcsLite;

namespace #NS# {
sealed class #SCRIPTNAME# : IEcsInitSystem {
public void Init (EcsSystems systems) {
// add your initialize code here.
}
}
}
7 changes: 7 additions & 0 deletions Editor/Templates/InitSystem.cs.txt.meta

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

9 changes: 9 additions & 0 deletions Editor/Templates/RunSystem.cs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Leopotam.EcsLite;

namespace #NS# {
sealed class #SCRIPTNAME# : IEcsRunSystem {
public void Run (EcsSystems systems) {
// add your run code here.
}
}
}
7 changes: 7 additions & 0 deletions Editor/Templates/RunSystem.cs.txt.meta

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

Loading

0 comments on commit f3671d5

Please sign in to comment.