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

Feature/ui toolkit debugger #26

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
saver works for UI Toolkit editor
  • Loading branch information
MystikalPooka committed Jul 8, 2020
commit 10e18be339e64f6cd390e91dc3aab06c11623816
3 changes: 1 addition & 2 deletions Assets/Scripts/AI/Tree/TreeElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public int ID
public int Depth { get => _Depth; set => _Depth = value; }

public TreeElement ()
{
}
{}

public TreeElement (string name, int depth, int id)
{
Expand Down
10 changes: 4 additions & 6 deletions Assets/Visual Behavior Tree/Editor/TreeSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,17 @@ public void SaveTree(List<EditorNode> nodes, string path)
{
EditorNode rootNode = nodes.Find(node => node.inPoint.connections.Count == 0);

Debug.Log(rootNode.TreeElement.Name);

rootNode.TreeElement.Depth = -1;

positions = new List<Rect>();
positions.Add(rootNode.layout);
positions = new List<Rect>
{
rootNode.layout
};

RecursiveAddChildren(rootNode);

var json = GetJsonSaveDataFromRoot(rootNode.TreeElement);

Debug.Log(json);

SaveAssetToDatabase(path, positions, json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class Connection : VisualElement
public ConnectionPoint outPoint;
public Action<Connection> OnClickRemoveConnection;

Button removeButton;
readonly Button removeButton;

public float lineWidth = 3;

public Connection(ConnectionPoint inPoint, ConnectionPoint outPoint, Action<Connection> OnClickRemoveConnection)
public Connection(ConnectionPoint inPoint, ConnectionPoint outPoint, Action<Connection> onClickRemoveConnection)
{
this.inPoint = inPoint;
this.outPoint = outPoint;
this.OnClickRemoveConnection = OnClickRemoveConnection;
this.OnClickRemoveConnection = onClickRemoveConnection;

this.AddToClassList("Connection");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void OnEnable()
{
VisualElement root = rootVisualElement;

// Import UXML
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Visual Behavior Tree/Editor/UIENodeEditor/Editor Window/BehaviorEditorWindow.uxml");
VisualElement uxmlRoot = visualTree.CloneTree();

Expand All @@ -48,8 +47,6 @@ public void OnEnable()
var loadButton = (Button)rootVisualElement.Q<VisualElement>("LoadButton");
loadButton.clicked += LoadNodesFromFile;

// A stylesheet can be added to a VisualElement.
// The style will be applied to the VisualElement and all of its children.
var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Visual Behavior Tree/Editor/UIENodeEditor/Editor Window/BehaviorEditorWindow.uss");
root.styleSheets.Add(styleSheet);

Expand Down Expand Up @@ -87,12 +84,13 @@ where type.Name.Contains(selectedName)
treeElement.Name = selectedName;
treeElement.ElementType = typeName.First().ToString();

EditorNode item = new EditorNode(treeElement, action.eventInfo.localMousePosition, OnClickInPoint, OnClickOutPoint, OnClickRemoveNode);
EditorNode item = new EditorNode(treeElement, action.eventInfo.localMousePosition, OnClickInPoint, OnClickOutPoint, OnClickAddNode, OnClickRemoveNode);
OnClickAddNode(item);
}

void OnClickAddNode(EditorNode node)
{
Debug.Log("Adding " + node.TreeElement.Name);
nodes.Add(node);
rootVisualElement.Q<VisualElement>("GridContainer").Add(node);
}
Expand Down Expand Up @@ -201,7 +199,7 @@ protected void LoadNodesFromFile()

UIETreeLoader loader = new UIETreeLoader();
var root = loader.LoadFromAsset(path);
nodes = loader.GetNodes(OnClickInPoint, OnClickOutPoint, OnClickRemoveNode);
nodes = loader.GetNodes(OnClickInPoint, OnClickOutPoint, OnClickAddNode, OnClickRemoveNode);
connections = loader.GetConnectionsFromRoot(root, OnClickRemoveConnection);

foreach(var node in nodes)
Expand All @@ -223,6 +221,8 @@ private bool IsValidTree()

private void OnClickRemoveNode(EditorNode node)
{
Debug.Log("Removing " + node.TreeElement.Name);
var container = rootVisualElement.Q<VisualElement>("GridContainer");
if (connections != null)
{
List<Connection> connectionsToRemove = new List<Connection>();
Expand All @@ -238,24 +238,21 @@ private void OnClickRemoveNode(EditorNode node)
for (int i = 0; i < connectionsToRemove.Count; i++)
{
var connection = connectionsToRemove[i];
connections.Remove(connection);

if (connection.inPoint.connections.Contains(connection))
{
connection.inPoint.connections.Remove(connection);
rootVisualElement.Q<VisualElement>("GridContainer").Remove(connection);

}

if (connection.outPoint.connections.Contains(connection))
{
connection.outPoint.connections.Remove(connection);
rootVisualElement.Q<VisualElement>("GridContainer").Remove(connection);
}
connections.Remove(connection);
container.Remove(connection);
}

connectionsToRemove = null;
}

container.Remove(node);
nodes.Remove(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
background-color: rgba(242, 246, 250,1);
width: auto;
height: auto;
overflow: hidden;
}
66 changes: 25 additions & 41 deletions Assets/Visual Behavior Tree/Editor/UIENodeEditor/Node/EditorNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public class EditorNode : Box
public ConnectionPoint inPoint;
public ConnectionPoint outPoint;

public EditorNode(BehaviorTreeElement wrappedElement, Vector2 position, Action<ConnectionPoint> OnClickInPoint, Action<ConnectionPoint> OnClickOutPoint, Action<EditorNode> OnClickRemoveNode)
private Action<EditorNode> OnClickAddNode;
private Action<EditorNode> OnClickRemoveNode;

public EditorNode(BehaviorTreeElement wrappedElement, Vector2 position, Action<ConnectionPoint> onClickInPoint, Action<ConnectionPoint> onClickOutPoint, Action<EditorNode> onClickAddNode, Action<EditorNode> onClickRemoveNode)
{
var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Visual Behavior Tree/Editor/UIENodeEditor/Node/EditorNode.uss");
this.styleSheets.Add(styleSheet);
Expand All @@ -33,11 +36,11 @@ public EditorNode(BehaviorTreeElement wrappedElement, Vector2 position, Action<C
this.Add(uxmlRoot);

var parentContainer = uxmlRoot.Q<VisualElement>("ParentConnectorContainer");
inPoint = new ConnectionPoint(this, ConnectionPointType.In, OnClickInPoint);
inPoint = new ConnectionPoint(this, ConnectionPointType.In, onClickInPoint);
inPoint.AddToClassList("NodeButton");
parentContainer.Insert(1,inPoint);

outPoint = new ConnectionPoint(this, ConnectionPointType.Out, OnClickOutPoint);
outPoint = new ConnectionPoint(this, ConnectionPointType.Out, onClickOutPoint);

this.AddToClassList("EditorNode");

Expand All @@ -51,6 +54,9 @@ public EditorNode(BehaviorTreeElement wrappedElement, Vector2 position, Action<C
TreeElement = wrappedElement;
ElementObject = new SerializedObject(wrappedElement);

OnClickAddNode = onClickAddNode;
OnClickRemoveNode = onClickRemoveNode;

ReBindAllProperties();
}

Expand All @@ -62,23 +68,32 @@ private void ContextMenu(ContextualMenuPopulateEvent evt)
{
var menuStrings = type.ToString().Split('.');
evt.menu.AppendAction("Change/" + menuStrings[menuStrings.Length - 2] +
"/" + menuStrings.Last(), OnMenuAction);
"/" + menuStrings.Last(), OnMenuChangeAction);
evt.menu.AppendAction("Remove", OnMenuRemoveAction);
}
}

void OnMenuAction(DropdownMenuAction action)
private void OnMenuRemoveAction(DropdownMenuAction obj)
{
OnClickRemoveNode(this);
}

void OnMenuChangeAction(DropdownMenuAction action)
{
string selectedName = action.name.Split('/').Last();
var typeName = from type in typeof(BehaviorTreeElement).Assembly.GetTypes()
where type.Name.Contains(selectedName)
select type;

OnClickRemoveNode(this);

var treeElement = (BehaviorTreeElement)ScriptableObject.CreateInstance(typeName.First());
treeElement.ID = 0;
treeElement.Name = selectedName;
treeElement.ElementType = typeName.First().ToString();
this.TreeElement = treeElement;
ElementObject = new SerializedObject(TreeElement);
OnClickAddNode(this);
ReBindAllProperties();
}

Expand All @@ -87,55 +102,24 @@ internal void ReBindAllProperties()
this.Bind(ElementObject);

var nodeContainer = this.Q<VisualElement>("NodeContainer");
foreach(var child in nodeContainer.Children())
{
Debug.Log("Removing 1 element");
nodeContainer.Remove(child);
}

nodeContainer.Clear();

foreach (var element in GetAllPropertyFields())
{
Debug.Log("Adding one");
nodeContainer.Add(element);
element.Bind(ElementObject);
}

var childContainer = this.Q<VisualElement>("ChildrenConnectorContainer");

if (childContainer.Contains(outPoint)) childContainer.Remove(outPoint);

if (this.TreeElement.CanHaveChildren)
{
outPoint.AddToClassList("NodeButton");
childContainer.Add(outPoint);
}
else if(childContainer.Contains(outPoint))
{
Debug.Log("Removing connections");
var connections = outPoint.connections;
List<Connection> connectionsToRemove = new List<Connection>();

for (int i = 0; i < connections.Count; i++)
{
if (connections[i].inPoint.Equals(this.inPoint) || connections[i].outPoint.Equals(this.outPoint))
{
connectionsToRemove.Add(connections[i]);
}
}

for (int i = 0; i < connectionsToRemove.Count; i++)
{
var connection = connectionsToRemove[i];
connections.Remove(connection);
if (connection.inPoint.connections.Contains(connection))
{
connection.inPoint.connections.Remove(connection);
}

if (connection.outPoint.connections.Contains(connection))
{
connection.outPoint.connections.Remove(connection);
}
}
childContainer.Remove(outPoint);
}
}

public List<VisualElement> GetAllPropertyFields()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace Assets.Visual_Behavior_Tree.Editor.UIENodeEditor
{
Expand All @@ -32,22 +33,23 @@ public BehaviorTreeElement LoadFromAsset(string path)
{
string typeName = el.ElementType;
Type type = Assembly.GetAssembly(typeof(BehaviorTreeElement)).GetType(typeName);
dynamic newBehavior = Activator.CreateInstance(type, (string)el.Name, (int)el.Depth, (int)el.ID);
//dynamic newBehavior = Activator.CreateInstance(type, (string)el.Name, (int)el.Depth, (int)el.ID);
dynamic newBehavior = ScriptableObject.CreateInstance(type);
JsonConvert.PopulateObject(JsonConvert.SerializeObject(el), newBehavior);
behaviorElements.Add(newBehavior);
}
root = TreeElementUtility.ListToTree(behaviorElements);
return root;
}

public List<EditorNode> GetNodes(Action<ConnectionPoint> OnClickInPoint, Action<ConnectionPoint> OnClickOutPoint, Action<EditorNode> OnClickRemoveNode)
public List<EditorNode> GetNodes(Action<ConnectionPoint> onClickInPoint, Action<ConnectionPoint> onClickOutPoint, Action<EditorNode> onClickAddNode, Action<EditorNode> onClickRemoveNode)
{
nodes = new List<EditorNode>();

for(int i = 0; i < behaviorElements.Count; ++i)
{
var nodeRect = asset.positions[i];
var newNode = new EditorNode(behaviorElements[i], nodeRect.position, OnClickInPoint, OnClickOutPoint, OnClickRemoveNode);
var newNode = new EditorNode(behaviorElements[i], nodeRect.position, onClickInPoint, onClickOutPoint, onClickAddNode, onClickRemoveNode);

nodes.Add(newNode);
}
Expand Down
36 changes: 18 additions & 18 deletions Assets/Visual Behavior Tree/Examples/New Behavior Tree4.asset
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,48 @@ MonoBehaviour:
m_EditorClassIdentifier:
positions:
- serializedVersion: 2
x: 424
y: 38
x: 301
y: 48
width: 200
height: 100
- serializedVersion: 2
x: 439
y: 218
x: 316
y: 228
width: 200
height: 100
- serializedVersion: 2
x: 339
y: 389
x: 216
y: 399
width: 200
height: 100
- serializedVersion: 2
x: 547
y: 390
x: 424
y: 400
width: 200
height: 100
- serializedVersion: 2
x: 215
y: 219
x: 92
y: 229
width: 200
height: 100
- serializedVersion: 2
x: 783
y: 211
x: 644
y: 212
width: 200
height: 100
- serializedVersion: 2
x: 787
y: 394
x: 647
y: 397
width: 200
height: 100
- serializedVersion: 2
x: 882
y: 560
x: 778
y: 558
width: 200
height: 100
- serializedVersion: 2
x: 658
y: 557
x: 563
y: 559
width: 200
height: 100
treeElements: "[\r\n {\r\n \"SucceedPercent\": 100.0,\r\n \"ElementType\":
Expand Down