Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixes various SO config errors #309

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 875fbfa2d1734285b353878b31467c98, type: 3}
m_Name: TestCapability
m_EditorClassIdentifier:
goals: []
actions: []
worldSensors: []
targetSensors: []
multiSensors: []
generatorScriptable: {fileID: 0}
references:
version: 2
RefIds: []

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

61 changes: 40 additions & 21 deletions Package/Editor/CrashKonijn.Goap.Editor/Elements/ListElementBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ public void Rebuild()
{
return;
}

for (var i = 0; i < this.property.arraySize; i++)
{
this.CreateElement(i);
}

if (this.buttonContainer is not null)
return;

this.buttonContainer = new VisualElement();
this.buttonContainer.style.flexDirection = FlexDirection.Row;
this.buttonContainer.style.justifyContent = Justify.FlexEnd;

var addButton = new Button(this.AddItem)
{
text = "+"
text = "+",
};
this.buttonContainer.Add(addButton);

var removeButton = new Button(this.RemoveSelectedItem)
{
text = "-"
text = "-",
};
this.buttonContainer.Add(removeButton);

Expand All @@ -60,16 +60,19 @@ public void Rebuild()

private void CreateElement(int i)
{
if (i < 0 || i >= this.list.Count)
return;

var value = this.list[i];
var prop = this.property.GetArrayElementAtIndex(i);

var element = this.CreateListItem(prop, value);
element.RegisterCallback<ClickEvent>(evt =>
{
this.selectedItemIndex = i;
});
this.elementsRoot.Add(element);

this.BindListItem(prop, element, value, i);
}

Expand All @@ -79,46 +82,62 @@ private void BindListItem(SerializedProperty property, VisualElement element, in
{
this.BindListItem(property, element as TRenderType, this.list[index], index);
}

protected abstract void BindListItem(SerializedProperty property, TRenderType element, TItemType item, int index);

private void AddItem()
{
// Add your item to the scriptable's list
// Example: scriptable.items.Add(newItem);
this.property.arraySize++;

// var element = this.property.GetArrayElementAtIndex(this.property.arraySize -1);
// element.managedReferenceValue = new TItemType();

this.list.Add(new TItemType());
this.CreateElement(this.property.arraySize -1);

this.CreateElement(this.property.arraySize - 1);

this.CloseAll();

this.GetFoldable(this.property.arraySize -1).Foldout.value = true;

// this.Rebuild(); // Refresh the ListView

var foldable = this.GetFoldable(this.property.arraySize - 1);

if (foldable is null)
return;

foldable.Foldout.value = true;

this.Rebuild(); // Refresh the ListView
}

private void RemoveSelectedItem()
{
if (this.selectedItemIndex < 0 || this.selectedItemIndex >= this.property.arraySize)
return;

this.list.RemoveAt(this.selectedItemIndex);

this.Rebuild();
}

private void CloseAll()
{
for (int i = 0; i < this.property.arraySize; i++)
for (var i = 0; i < this.property.arraySize; i++)
{
this.GetFoldable(i).Foldout.value = false;
var foldable = this.GetFoldable(i);

if (foldable is null)
continue;

foldable.Foldout.value = false;
}
}

private IFoldable GetFoldable(int index)
{
if (index < 0 || index >= this.elementsRoot.childCount)
return null;

return this.elementsRoot.ElementAt(index) as IFoldable;
}
}
Expand All @@ -127,4 +146,4 @@ public interface IFoldable
{
public Foldout Foldout { get; }
}
}
}
37 changes: 20 additions & 17 deletions Package/Editor/CrashKonijn.Goap.Editor/Elements/SensorList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SensorList(SerializedObject serializedObject, CapabilityConfigScriptable
{
this.scriptable = scriptable;
this.generator = generator;

this.Rebuild();
}

Expand All @@ -28,7 +28,7 @@ protected override CapabilitySensorElement CreateListItem(SerializedProperty pro
protected override void BindListItem(SerializedProperty property, CapabilitySensorElement element, TSensorType item, int index)
{
element.Foldout.text = this.GetName(item);

this.Bind(element, item);
}

Expand All @@ -54,7 +54,7 @@ private void BindSensor(CapabilitySensorElement element, CapabilityWorldSensor i
{
element.Foldout.text = this.GetName(item);
});

element.KeyField.Bind(this.scriptable, item.worldKey, this.generator.GetWorldKeys(), classRef =>
{
element.Foldout.text = this.GetName(item);
Expand All @@ -67,7 +67,7 @@ private void BindSensor(CapabilitySensorElement element, CapabilityTargetSensor
{
element.Foldout.text = this.GetName(item);
});

element.KeyField.Bind(this.scriptable, item.targetKey, this.generator.GetTargetKeys(), classRef =>
{
element.Foldout.text = this.GetName(item);
Expand All @@ -80,24 +80,27 @@ private void BindSensor(CapabilitySensorElement element, CapabilityMultiSensor i
{
element.Foldout.text = this.GetName(item);
});
var sensors = this.GetMultiSensors(item.sensor, this.generator.GetMultiSensors());

var sensors = this.GetMultiSensors(item.sensor, this.generator.GetMultiSensors());

element.LabelField.text = "- " + string.Join("\n- ", sensors);

element.Foldout.text = $"{item} ({sensors.Length})";
}

public string[] GetMultiSensors(ClassRef classRef, Script[] scripts)
{
var match = classRef.GetMatch(scripts);

if (match.status == ClassRefStatus.None)
return Array.Empty<string>();


if (match.script == null)
return Array.Empty<string>();

// Create instance of type
var instance = (IMultiSensor) Activator.CreateInstance(match.script.Type);

return instance.GetSensors();
}

Expand All @@ -110,15 +113,15 @@ public string GetName(CapabilitySensor item)

if (item is CapabilityWorldSensor worldSensor)
{
var scopes = new List<string>(){ worldSensor.worldKey.Name };
var scopes = new List<string>() { worldSensor.worldKey.Name };
scopes.AddRange(this.GetScopes(worldSensor.sensor, this.generator.GetWorldSensors()));

return $"{worldSensor.sensor.Name} ({string.Join(", ", scopes)})";
}

if (item is CapabilityTargetSensor targetSensor)
{
var scopes = new List<string>(){ targetSensor.targetKey.Name };
var scopes = new List<string>() { targetSensor.targetKey.Name };
scopes.AddRange(this.GetScopes(targetSensor.sensor, this.generator.GetTargetSensors()));

return $"{targetSensor.sensor.Name} ({string.Join(", ", scopes)})";
Expand All @@ -133,19 +136,19 @@ private string[] GetScopes(ClassRef classRef, Script[] scripts)

if (status == ClassRefStatus.None)
return Array.Empty<string>();

if (status == ClassRefStatus.Empty)
return Array.Empty<string>();

var scopes = new List<string>();

if (typeof(ILocalSensor).IsAssignableFrom(match.Type))
scopes.Add("local");

if (typeof(IGlobalSensor).IsAssignableFrom(match.Type))
scopes.Add("global");

return scopes.ToArray();
}
}
}
}
Loading