129 lines
4.1 KiB
C#
129 lines
4.1 KiB
C#
using UnityEditor;
|
|
using UnityEditor.UIElements;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
[CustomEditor(typeof(NanoBrain))]
|
|
public class NanoBrainComponent_Editor : Editor {
|
|
protected static VisualElement mainContainer;
|
|
protected static VisualElement inspectorContainer;
|
|
|
|
protected NanoBrain component;
|
|
private SerializedProperty brainProp;
|
|
|
|
ClusterInspector.GraphView board;
|
|
|
|
public void OnEnable() {
|
|
component = target as NanoBrain;
|
|
|
|
if (Application.isPlaying == false)
|
|
brainProp = serializedObject.FindProperty(nameof(NanoBrain.defaultBrain));
|
|
|
|
}
|
|
|
|
public override VisualElement CreateInspectorGUI() {
|
|
//ClusterPrefab brain = Application.isPlaying ? component.brain.prefab : component.defaultBrain;
|
|
Cluster brain = component.brain;
|
|
|
|
if (Application.isPlaying == false)
|
|
serializedObject.Update();
|
|
|
|
|
|
VisualElement root = new();
|
|
root.style.flexDirection = FlexDirection.Column; // side-by-side layout
|
|
root.style.flexGrow = 1;
|
|
root.style.minHeight = 600;
|
|
root.style.paddingLeft = 0;
|
|
root.style.paddingRight = 0;
|
|
root.style.paddingTop = 0;
|
|
root.style.paddingBottom = 0;
|
|
|
|
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
|
|
|
|
if (Application.isPlaying == false) {
|
|
PropertyField brainField = new(brainProp) {
|
|
label = "Nano Brain"
|
|
};
|
|
root.Add(brainField);
|
|
}
|
|
|
|
mainContainer = new() {
|
|
name = "main",
|
|
style = {
|
|
flexDirection = FlexDirection.Row,
|
|
flexGrow = 1,
|
|
minHeight = 500,
|
|
}
|
|
};
|
|
board = new ClusterInspector.GraphView();
|
|
board.style.flexGrow = 1;
|
|
mainContainer.Add(board);
|
|
|
|
inspectorContainer = new VisualElement {
|
|
name = "inspector",
|
|
style = {
|
|
width = 400,
|
|
}
|
|
};
|
|
|
|
mainContainer.Add(inspectorContainer);
|
|
root.Add(mainContainer);
|
|
|
|
// Run once for initial state (use resolved style width if available)
|
|
float initialWidth = root.layout.width > 0 ? root.layout.width : root.contentRect.width;
|
|
UpdateLayout(initialWidth);
|
|
|
|
// React to size changes of root (or parent if appropriate)
|
|
root.RegisterCallback<GeometryChangedEvent>(evt => {
|
|
UpdateLayout(evt.newRect.width);
|
|
});
|
|
|
|
if (brain != null && board != null)
|
|
board.SetGraph(component.gameObject, brain.prefab, brain.output, inspectorContainer);
|
|
// else
|
|
// Debug.LogWarning(" No brain!");
|
|
|
|
if (Application.isPlaying == false)
|
|
serializedObject.ApplyModifiedProperties();
|
|
return root;
|
|
}
|
|
|
|
// void OnSceneGUI() {
|
|
// if (Application.isPlaying && board != null)
|
|
// board.OnIMGUI();
|
|
// }
|
|
|
|
void OnSceneGui(SceneView sv) {
|
|
if (Application.isPlaying == false)
|
|
return;
|
|
// May need some throttling here...
|
|
if (board != null) {
|
|
Debug.Log(".");
|
|
board.OnIMGUI();
|
|
}
|
|
|
|
// EditorApplication.delayCall = UpdateInspectorUI;
|
|
}
|
|
|
|
void UpdateInspectorUI() {
|
|
if (board != null) {
|
|
Debug.Log(".");
|
|
board.OnIMGUI();
|
|
}
|
|
}
|
|
|
|
private void UpdateLayout(float containerWidth) {
|
|
if (containerWidth > 800f) {
|
|
mainContainer.style.flexDirection = FlexDirection.Row;
|
|
inspectorContainer.style.width = 400; // fixed sidebar width
|
|
inspectorContainer.style.flexGrow = 0;
|
|
}
|
|
else {
|
|
mainContainer.style.flexDirection = FlexDirection.Column;
|
|
inspectorContainer.style.width = Length.Percent(100); // full width below
|
|
inspectorContainer.style.flexDirection = FlexDirection.Column;
|
|
inspectorContainer.style.flexGrow = 1; // can set 0 or keep as needed
|
|
}
|
|
}
|
|
|
|
} |