Skip to content

Commit

Permalink
QOL
Browse files Browse the repository at this point in the history
  • Loading branch information
ensapra committed Mar 5, 2024
1 parent 0f27c5e commit 535c23b
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 195 deletions.
104 changes: 31 additions & 73 deletions Editor/ModuleDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void SerializeModule(SerializedProperty module, Rect position)
}
protected void ObjectList(SerializedProperty module)
{
SerializedProperty prop = module.FindPropertyRelative("onlyEnabledRoutines");
SerializedProperty prop = module.FindPropertyRelative("baseAllRoutines");
EditorGUI.indentLevel += 1;
GUILayout.Space(5);
if(prop.isExpanded)
Expand All @@ -92,19 +92,7 @@ protected void ObjectList(SerializedProperty module)
{
EditorGUILayout.LabelField("No components enabled on this module");
GUILayout.Space(EditorGUIUtility.singleLineHeight);
}
//Load cached routines
SerializedProperty createdRoutines = module.FindPropertyRelative("cachedRoutines");
if(createdRoutines.arraySize > 0)
{
GUILayout.Space(EditorGUIUtility.singleLineHeight);
EditorGUILayout.LabelField("Saved routines");
}
for(int i = 0; i < createdRoutines.arraySize; i++)
{
SerializedProperty item = createdRoutines.GetArrayElementAtIndex(i);
LoadAbstractRoutine(item);
}
}
}
EditorGUI.indentLevel -= 1;
}
Expand Down Expand Up @@ -138,7 +126,7 @@ private void ModuleHeader(Rect position, SerializedProperty module)
}
GUI.color = current;

SerializedProperty prop = module.FindPropertyRelative("onlyEnabledRoutines");
SerializedProperty prop = module.FindPropertyRelative("baseAllRoutines");
prop.isExpanded = EditorGUI.Foldout(toggleRect, prop.isExpanded, UpperSplit(module.name), true, headerStyle);
if(GUI.Button(buttonRect, "Clear"))
{
Expand All @@ -155,7 +143,9 @@ void ClearList(SerializedProperty list, SerializedProperty property)
for(int i = 0; i < list.arraySize; i++)
{
SerializedProperty item = list.GetArrayElementAtIndex(i);
item.FindPropertyRelative("_isEnabled").boolValue = false;
SerializedProperty boolVal = item.FindPropertyRelative("_isEnabled");
if(boolVal != null)
boolVal.boolValue = false;
}
}
/// <summary>
Expand All @@ -165,9 +155,7 @@ protected void GenerateFoldoutMenu(SerializedProperty list, SerializedProperty p
{
Module module = property.GetSerializedObject() as Module;
List<System.Type> types = module.GetAssemblyRoutines();
Routine[] routines = module.EnabledRoutinesObject;
Routine[] cached = module.ChachedRoutinesObject;
SerializedProperty cachedList = property.FindPropertyRelative("cachedRoutines");
List<Routine> routines = module.baseAllRoutines;

GenericMenu newMenu = new GenericMenu();
for(int i = 0; i < types.Count; i++)
Expand All @@ -184,58 +172,22 @@ protected void GenerateFoldoutMenu(SerializedProperty list, SerializedProperty p
}

GUIContent content = new GUIContent(routeName);
bool exists = routines != null && routines.Any(a => a != null && a.GetType().IsEquivalentTo(target));
if(exists)
{
(Routine rot, int index) foundRoutine = routines.Select((obj, index) => (obj,index)).First(a => a.obj != null && a.obj.GetType().IsEquivalentTo(target));
if(foundRoutine.rot.isEnabled)
{
//The object exists and is enabled
newMenu.AddDisabledItem(content);
}
else
{
//The object exists but is not enabled
newMenu.AddItem(content, false, ()=>{
SerializedProperty routineFound = list.GetArrayElementAtIndex(foundRoutine.index);
routineFound.FindPropertyRelative("_isEnabled").boolValue = true;
property.serializedObject.ApplyModifiedProperties();
});
}
(Routine rot, int index) foundRoutine = routines.Select((obj, index) => (obj,index)).FirstOrDefault(a => a.obj != null && a.obj.GetType().IsEquivalentTo(target));
if(foundRoutine.rot != null && foundRoutine.rot.isEnabled){
newMenu.AddDisabledItem(content);
}
else
{
exists = cached != null && cached.Any(a => a != null && a.GetType().IsEquivalentTo(target));
if(exists)
{
//Exists on cache
(Routine rot, int index) foundRoutine = cached.Select((obj, index) => (obj,index)).First(a => a.obj != null && a.obj.GetType().IsEquivalentTo(target));
newMenu.AddItem(content, false, ()=>{
if(list.arraySize <= 0)
list.arraySize = 1;
else
list.InsertArrayElementAtIndex(list.arraySize-1);
SerializedProperty newClass = list.GetArrayElementAtIndex(list.arraySize-1);
newClass.managedReferenceValue = cachedList.GetArrayElementAtIndex(foundRoutine.index).managedReferenceValue;
newClass.FindPropertyRelative("_isEnabled").boolValue = true;
cachedList.DeleteArrayElementAtIndex(foundRoutine.index);
property.serializedObject.ApplyModifiedProperties();
});
}
else
{
//The object doesn't exist on the main array, so create it again
newMenu.AddItem(content, false, ()=>{
if(list.arraySize <= 0)
list.arraySize = 1;
else
list.InsertArrayElementAtIndex(list.arraySize-1);
SerializedProperty newClass = list.GetArrayElementAtIndex(list.arraySize-1);
newClass.managedReferenceValue = System.Activator.CreateInstance(target);
newClass.FindPropertyRelative("_isEnabled").boolValue = true;
property.serializedObject.ApplyModifiedProperties();
});
}
else{
newMenu.AddItem(content, false, ()=>{
if(list.arraySize <= 0)
list.arraySize = 1;
else
list.InsertArrayElementAtIndex(list.arraySize-1);
SerializedProperty newClass = list.GetArrayElementAtIndex(list.arraySize-1);
newClass.managedReferenceValue = System.Activator.CreateInstance(target);
newClass.FindPropertyRelative("_isEnabled").boolValue = true;
module.InternalSort();
property.serializedObject.ApplyModifiedProperties();
});
}
}
newMenu.ShowAsContext();
Expand All @@ -260,6 +212,9 @@ protected void AbstractRoutineHeader(Rect position, SerializedProperty AbstractR
SerializedProperty enabledBool = AbstractRoutineProperty.FindPropertyRelative("_isEnabled");
SerializedProperty awakenedBool = AbstractRoutineProperty.FindPropertyRelative("_isAwake");

if(enabledBool == null || awakenedBool == null)
return;

Rect boxPosition = position;
boxPosition.height = 22;
Rect togglePosition = boxPosition;
Expand Down Expand Up @@ -327,8 +282,11 @@ protected string ObjectName(string name)
string lastBit = propertyName[propertyName.Length-1];
propertyName = lastBit.Split(" ");
lastBit = propertyName[propertyName.Length-1];
string noFirst = lastBit.Substring(1);
return UpperSplit(noFirst);
if(lastBit.Length > 1){
string noFirst = lastBit.Substring(1);
return UpperSplit(noFirst);
}
return name;
}

private void OpenFile(System.Type type)
Expand All @@ -340,7 +298,7 @@ private void OpenFile(System.Type type)
}
bool IsScript(string guid, string name){
string path = AssetDatabase.GUIDToAssetPath(guid);
string fileName = Path.GetFileNameWithoutExtension(path);//path.Split("/").LastOrDefault().Replace(".cs","");
string fileName = Path.GetFileNameWithoutExtension(path);
return fileName.Equals(name);
}
}
Expand Down
193 changes: 79 additions & 114 deletions Runtime/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,39 @@
using System.Reflection;
using System.Linq;
using System;

using sapra.ObjectController;
namespace sapra.ObjectController
{

[System.Serializable]
public abstract class Module<T> : Module where T : Routine
{
[SerializeReference] [HideInInspector] protected ObjectController controller;

{
/// <summary>
/// Enabled components
/// <summary/>

[SerializeReference] [HideInInspector] protected List<T> onlyEnabledRoutines = new List<T>();
[SerializeReference] [HideInInspector] private List<T> cachedRoutines = new List<T>();

private Dictionary<Type, T> onlyRoutines = new Dictionary<Type, T>();
public void UpdateList(){
onlyEnabledRoutines.Clear();
foreach(KeyValuePair<Type, T> keyValue in onlyRoutines){
onlyEnabledRoutines.Add(keyValue.Value);
}
}


public override Routine[] EnabledRoutinesObject => onlyEnabledRoutines.ToArray();
internal override Routine[] ChachedRoutinesObject => cachedRoutines.ToArray();

[SerializeField] [HideInInspector] private bool RemoveUnused = false;

public List<T> EnabledRoutines = new List<T>();

#region Initialization
internal override sealed void SleepRoutines(ObjectController controller)
{
for(int i = onlyEnabledRoutines.Count-1; i>= 0; i--)
{
T routine = onlyEnabledRoutines[i];
routine.SleepRoutine();
}
}
internal override sealed void InitializeRoutines(ObjectController controller)
public Z GetComponent<Z>(bool required = false) where Z : Component
{
this.controller = controller;
if(this.controller == null)
{
Debug.Log("Error initializing components, no CObject was set");
return;
}

//Check cached routines to recover info
for(int i = cachedRoutines.Count-1; i>= 0; i--)
{
T routine = cachedRoutines[i];
if(routine != null && routine.isEnabled)
{
onlyEnabledRoutines.Add(routine);
cachedRoutines.RemoveAt(i);
}
}

//Remove all the rest disabled routines if needed
if(RemoveUnused)
cachedRoutines.Clear();

for(int i = onlyEnabledRoutines.Count-1; i>= 0; i--)
{
T routine = onlyEnabledRoutines[i];
if(routine == null)
{
onlyEnabledRoutines.RemoveAt(i);
continue;
}

if(routine.isEnabled)
routine.AwakeRoutine(controller);
if(!routine.isEnabled)
{
routine.SleepRoutine();
//Store to cache if needed later
CacheRoutine(routine);
}
}

onlyEnabledRoutines.Sort((a,b)=> a.GetType().ToString().CompareTo(b.GetType().ToString()));
InitializeModule();
}
private void CacheRoutine(T routine){
T existsOne = cachedRoutines.Find(a => a.GetType().Equals(routine.GetType()));
if(existsOne != null && !RemoveUnused){
cachedRoutines.Add(routine);
}
onlyEnabledRoutines.RemoveAll(a => a.GetType().Equals(routine.GetType()));

return controller.GetComponent<Z>(required);
}
public Z GetComponent<Z>(bool required = false) where Z : Component
internal override void EnableRoutines()
{
return controller.GetComponent<Z>(required);
EnabledRoutines.Clear();
EnabledRoutines = baseAllRoutines.FindAll(a => a._isEnabled).Cast<T>().ToList();
}
#endregion
#region Components requests
private object GenerateRoutine(Type type)
public override Routine GenerateRoutine(Type type)
{
T newRoutine = Activator.CreateInstance(type) as T;
newRoutine.AwakeRoutine(controller);
onlyEnabledRoutines.Add(newRoutine);
onlyEnabledRoutines.Sort((a,b)=> a.GetType().ToString().CompareTo(b.GetType().ToString()));
newRoutine._isEnabled = true;
AddRoutine(newRoutine);
baseAllRoutines.Sort((a,b)=> a.GetType().ToString().CompareTo(b.GetType().ToString()));
return newRoutine;
}

internal override Routine GetRoutine(System.Type type, bool required){
T foundRoutine = onlyEnabledRoutines.Find(x => x != null && x.GetType().IsEquivalentTo(type));
if(foundRoutine != null)
return foundRoutine;
if(!required)
return null;

foundRoutine = cachedRoutines.Find(x => x != null && x.GetType().IsEquivalentTo(type));
if(foundRoutine != null)
{
foundRoutine.AwakeRoutine(controller);
cachedRoutines.Remove(foundRoutine);
onlyEnabledRoutines.Add(foundRoutine);
onlyEnabledRoutines.Sort((a,b)=> a.GetType().ToString().CompareTo(b.GetType().ToString()));
return foundRoutine;
}

return GenerateRoutine(type) as Routine;
}

/// <summary>
/// Returns the requested Routine if it has been enabled, otherwise returns true. If required is True, and the component hasn't been enabled, it will automatically enable it
/// <summary/>
Expand Down Expand Up @@ -165,18 +71,77 @@ public override Type GetModuleType()
[System.Serializable]
public abstract class Module
{
//public abstract AbstractRoutine RequestRoutine(System.Type routineType, bool required);
public abstract Routine[] EnabledRoutinesObject{get;}
internal abstract Routine[] ChachedRoutinesObject{get;}
internal abstract void InitializeRoutines(ObjectController controller);
internal abstract void SleepRoutines(ObjectController controller);
[SerializeReference] [HideInInspector] protected ObjectController controller;
[SerializeField] [HideInInspector] protected bool RemoveUnused = false;
[SerializeReference] public List<Routine> baseAllRoutines = new List<Routine>();
internal abstract void EnableRoutines();
internal void InitializeRoutines(ObjectController controller){
this.controller = controller;
if(this.controller == null)
{
Debug.Log("Error initializing components, no CObject was set");
return;
}
UpdateList();
InitializeModule();
}
internal abstract List<Type> GetAssemblyRoutines();
internal abstract Routine GetRoutine(System.Type type, bool required);
public abstract Type GetModuleType();
public abstract Routine GenerateRoutine(Type type);
public Routine GetRoutine(Type type, bool required){
Routine foundRoutine = baseAllRoutines.FirstOrDefault(a => type.IsEquivalentTo(a.GetType()));
if(foundRoutine == default && required)
foundRoutine = GenerateRoutine(type);

if(foundRoutine != null)
{
if(foundRoutine._isEnabled || required)
foundRoutine.AwakeRoutine(controller);
}
return foundRoutine;
}

internal void UpdateList(){
baseAllRoutines.RemoveAll(a => a == null);
List<Routine> Copy = new List<Routine>(baseAllRoutines);
foreach(Routine target in Copy){
if(!target.isEnabled){
target.SleepRoutine();
if(RemoveUnused)
baseAllRoutines.Remove(target);
}
else{
target.AwakeRoutine(controller);
}

}

baseAllRoutines.Sort((a,b)=> a.GetType().ToString().CompareTo(b.GetType().ToString()));
EnableRoutines();
}

internal void SleepRoutines(ObjectController controller)
{
for(int i = baseAllRoutines.Count-1; i>= 0; i--)
{
Routine routine = baseAllRoutines[i];
routine.SleepRoutine();
}
}

public void AddRoutine(Routine routine){
if(!baseAllRoutines.Contains(routine))
baseAllRoutines.Add(routine);
}

public void InternalSort(){
baseAllRoutines.Sort((a,b)=> a.GetType().ToString().CompareTo(b.GetType().ToString()));

}
/// <summary>
/// Method called after all routines have been enabled. Equivalent to Awake of Monobehaviours
/// <summary/>
protected virtual void InitializeModule(){}
}
}

Loading

0 comments on commit 535c23b

Please sign in to comment.