Fix clusterarray

This commit is contained in:
Pascal Serrarens 2026-01-21 15:31:48 +01:00
parent 4b72e6c712
commit f309d4cec6
3 changed files with 47 additions and 30 deletions

View File

@ -1,46 +1,64 @@
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class NucleusArray {
[SerializeReference]
public INucleus[] nuclei;
public Cluster[] clusters;
private INucleus[] _nuclei;
private Cluster[] _clusters;
public IEnumerable<INucleus> nuclei {
get {
if (_nuclei == null)
return _clusters;
else if (_clusters == null)
return _nuclei;
else
return _nuclei.Concat(_clusters);
}
}
public string name;
public NucleusArray(INucleus nucleus) {
this.name = nucleus.name;
this.nuclei = new INucleus[1];
this.nuclei[0] = nucleus;
this._nuclei = new INucleus[1];
this._nuclei[0] = nucleus;
}
public NucleusArray(Cluster cluster) {
this.name = cluster.name;
this._clusters = new Cluster[1];
this._clusters[0] = cluster;
}
public void AddNucleus() {
if (this.nuclei.Length == 0) {
if (this._nuclei.Length == 0) {
Debug.LogError("Empty perceptoid array, cannot add");
return;
}
int newLength = this.nuclei.Length + 1;
int newLength = this._nuclei.Length + 1;
INucleus[] newArray = new INucleus[newLength];
for (int i = 0; i < this.nuclei.Length; i++)
newArray[i] = this.nuclei[i];
newArray[newLength - 1] = this.nuclei[0].Clone();
for (int i = 0; i < this._nuclei.Length; i++)
newArray[i] = this._nuclei[i];
newArray[newLength - 1] = this._nuclei[0].Clone();
this.nuclei = newArray;
this._nuclei = newArray;
}
public void RemoveNucleus() {
int newLength = this.nuclei.Length - 1;
int newLength = this._nuclei.Length - 1;
if (newLength == 0) {
Debug.LogWarning("Perceptoid array cannot be empty");
return;
}
INucleus[] newPerceptei = new INucleus[newLength];
for (int i = 0; i < newLength; i++)
newPerceptei[i] = this.nuclei[i];
newPerceptei[i] = this._nuclei[i];
// Delete the last perception
Neuron.Delete(this.nuclei[newLength]);
this.nuclei = newPerceptei;
Neuron.Delete(this._nuclei[newLength]);
this._nuclei = newPerceptei;
}
}

View File

@ -231,7 +231,7 @@ public class ClusterInspector : Editor {
maxValue = value;
}
float spacing = 400f / this.currentNucleus.array.nuclei.Length;
float spacing = 400f / this.currentNucleus.array.nuclei.Count();
float margin = 10 + spacing / 2;
float xMin = 150 - size;
float xMax = 150 + size;
@ -384,13 +384,13 @@ public class ClusterInspector : Editor {
fontStyle = FontStyle.Bold,
};
if (nucleus is INucleus neuron) {
if (neuron.array == null || neuron.array.nuclei == null || neuron.array.nuclei.Length == 0)
if (neuron.array == null || neuron.array.nuclei == null || neuron.array.nuclei.Count() == 0)
neuron.array = new NucleusArray(neuron);
if ((!expandArray || neuron.array.nuclei[0] != this.currentNucleus) && neuron.array.nuclei.Length > 1) {
Handles.Label(labelPosition, neuron.array.nuclei.Length.ToString(), style);
if ((!expandArray || neuron.array.nuclei.First() != this.currentNucleus) && neuron.array.nuclei.Count() > 1) {
Handles.Label(labelPosition, neuron.array.nuclei.Count().ToString(), style);
}
if (expandArray && neuron.array.nuclei[0] == this.currentNucleus) {
if (expandArray && neuron.array.nuclei.First() == this.currentNucleus) {
int arrayIx = 0;
foreach (INucleus n in neuron.array.nuclei) {
if (n == neuron)
@ -497,10 +497,10 @@ public class ClusterInspector : Editor {
neuroid.curvePreset = (Neuron.CurvePresets)EditorGUILayout.EnumPopup(neuroid.curvePreset, GUILayout.Width(100));
EditorGUILayout.EndHorizontal();
if (neuroid.array == null || neuroid.array.nuclei == null || neuroid.array.nuclei.Length == 0)
if (neuroid.array == null || neuroid.array.nuclei == null || neuroid.array.nuclei.Count() == 0)
neuroid.array = new NucleusArray(neuroid);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Length);
EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Count());
if (GUILayout.Button("Add"))
neuroid.array.AddNucleus();
if (GUILayout.Button("Del"))

View File

@ -13,19 +13,19 @@ MonoBehaviour:
m_Name: New Cluster
m_EditorClassIdentifier: Assembly-CSharp::Cluster
nuclei:
- rid: 2243601242842202217
- rid: 2243601242842202225
subClusters: []
references:
version: 2
RefIds:
- rid: 2243601242842202217
- rid: 2243601242842202225
type: {class: Neuron, ns: , asm: Assembly-CSharp}
data:
_name: Output
_synapses: []
_receivers: []
_array:
rid: 2243601242842202218
rid: 2243601242842202226
_curvePreset: 0
curve:
serializedVersion: 2
@ -53,10 +53,9 @@ MonoBehaviour:
m_RotationOrder: 4
curveMax: 1
average: 0
- rid: 2243601242842202218
- rid: 2243601242842202226
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
data:
nuclei:
- rid: 2243601242842202217
clusters: []
_nuclei:
- rid: 2243601242842202225
name: Output