Skip to content

Commit e762e8e

Browse files
committed
fix(behavior-tree): recursive global position changes don't double up
1 parent e72cc54 commit e762e8e

23 files changed

+389
-347
lines changed

Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/BehaviorTreePrinter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public class BehaviorTreePrinter {
33
private readonly VisualTask _root;
44

55
public BehaviorTreePrinter (IBehaviorTree tree) {
6-
var container = new TreeContainerVertical();
6+
var container = new GraphContainerVertical();
77
// @TODO Temporary start position for debugging, replace with a proper scroll view
88
container.SetGlobalPosition(300, 0);
99
_root = new VisualTask(tree.Root, container);

Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/Containers.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace CleverCrow.Fluid.BTs.Trees.Editors {
2+
public class GraphBox : IGraphBox {
3+
public float LocalPositionX { get; private set; }
4+
public float LocalPositionY { get; private set; }
5+
6+
public float GlobalPositionX { get; private set; }
7+
public float GlobalPositionY { get; private set; }
8+
9+
public float Width { get; private set; }
10+
public float Height { get; private set; }
11+
12+
public void SetSize (float width, float height) {
13+
Width = width;
14+
Height = height;
15+
}
16+
17+
public void SetLocalPosition (float x, float y) {
18+
LocalPositionX = x;
19+
LocalPositionY = y;
20+
}
21+
22+
public void AddGlobalPosition (float x, float y) {
23+
GlobalPositionX += x;
24+
GlobalPositionY += y;
25+
}
26+
}
27+
}

Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/Containers/GraphBox.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
3+
namespace CleverCrow.Fluid.BTs.Trees.Editors {
4+
public class GraphContainerHorizontal : IGraphContainer {
5+
protected readonly List<IGraphBox> _childContainers = new List<IGraphBox>();
6+
7+
public float LocalPositionX { get; private set; }
8+
public float LocalPositionY { get; private set; }
9+
public float GlobalPositionX { get; private set; }
10+
public float GlobalPositionY { get; private set; }
11+
public float Width { get; protected set; }
12+
public float Height { get; protected set; }
13+
public List<IGraphBox> ChildContainers => _childContainers;
14+
15+
public void SetLocalPosition (float x, float y) {
16+
LocalPositionX = x;
17+
LocalPositionY = y;
18+
}
19+
20+
public virtual void AddBox (IGraphBox child) {
21+
CalculateChild(child);
22+
_childContainers.Add(child);
23+
}
24+
25+
private void CalculateChild (IGraphBox child) {
26+
child.SetLocalPosition(Width, 0);
27+
child.AddGlobalPosition(GlobalPositionX + child.LocalPositionX, GlobalPositionY + child.LocalPositionY);
28+
29+
Width += child.Width;
30+
if (child.Height > Height) Height = child.Height;
31+
}
32+
33+
public void SetSize (float width, float height) {
34+
throw new System.NotImplementedException();
35+
}
36+
37+
public void SetGlobalPosition (float x, float y) {
38+
GlobalPositionX = x;
39+
GlobalPositionY = y;
40+
}
41+
42+
public void AddGlobalPosition (float x, float y) {
43+
GlobalPositionX += x;
44+
GlobalPositionY += y;
45+
46+
foreach (var child in ChildContainers) {
47+
child.AddGlobalPosition(x, y);
48+
}
49+
}
50+
51+
public override string ToString () {
52+
return
53+
$"Size: {Width}, {Height}; Local: {LocalPositionX}, {LocalPositionY}; Global: {GlobalPositionX}, {GlobalPositionY};";
54+
}
55+
}
56+
}

Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/Containers/GraphContainerHorizontal.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace CleverCrow.Fluid.BTs.Trees.Editors {
2+
public class GraphContainerVertical : GraphContainerHorizontal {
3+
public override void AddBox (IGraphBox child) {
4+
CalculateChild(child);
5+
_childContainers.Add(child);
6+
}
7+
8+
private void CalculateChild (IGraphBox child) {
9+
child.SetLocalPosition(0, Height);
10+
child.AddGlobalPosition(GlobalPositionX + child.LocalPositionX, GlobalPositionY + child.LocalPositionY);
11+
12+
Height += child.Height;
13+
if (child.Width > Width) Width = child.Width;
14+
}
15+
}
16+
}

Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/Containers/GraphContainerVertical.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace CleverCrow.Fluid.BTs.Trees.Editors {
2+
public interface IGraphBox {
3+
float LocalPositionX { get; }
4+
float LocalPositionY { get; }
5+
float GlobalPositionX { get; }
6+
float GlobalPositionY { get; }
7+
8+
float Width { get; }
9+
float Height { get; }
10+
11+
void SetSize (float width, float height);
12+
void SetLocalPosition (float x, float y);
13+
void AddGlobalPosition (float x, float y);
14+
}
15+
}

Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/Containers/IGraphBox.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)