Skip to content

Commit

Permalink
Merge pull request #318 from crashkonijn/fix/toggle-config-in-graph-v…
Browse files Browse the repository at this point in the history
…iewer

Toggle show config during play
  • Loading branch information
crashkonijn authored Dec 5, 2024
2 parents 659546c + 9c73d1e commit ecef2c7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace CrashKonijn.Goap.Editor
{
Expand All @@ -8,25 +9,27 @@ public class EditorWindowValues
public int Zoom { get; set; } = 100;
public VisualElement RootElement { get; set; }
public DragDrawer DragDrawer { get; set; }
public UnityEngine.Object SelectedObject { get; set; }

public Object SelectedObject { get; set; }
public bool ShowConfig { get; set; }

public void UpdateZoom(int zoom)
{
if (zoom > 0)
{
this.Zoom = Math.Min(100, this.Zoom + zoom);
return;
}

this.Zoom = Math.Max(50, this.Zoom + zoom);
}

public delegate void UpdateEvent();

public event UpdateEvent OnUpdate;

public void Update()
{
this.OnUpdate?.Invoke();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private static void ShowWindow()

private void OnPlayModeChange(PlayModeStateChange obj)
{
this.values.ShowConfig = obj != PlayModeStateChange.EnteredPlayMode;

this.OnSelectionChange();
}

Expand Down
51 changes: 35 additions & 16 deletions Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,20 @@ public NodeElement(INode graphNode, VisualElement bezierRoot, EditorWindowValues
if (graphNode.Action is IGoapAction goapAction)
{
this.TargetCircle.SetColor(this.GetCircleColor(goapAction, null));
this.Target.text = $"Target: {goapAction.Config.Target?.GetType().GetGenericTypeName()}";
this.Target.text = this.Target.text = this.GetTargetText(values, null, goapAction);
this.Cost.text = $"Cost: {goapAction.Config.BaseCost}";
}
}

this.Effects = new VisualElement();
this.Effects.AddToClassList("effects");
this.Effects.Add(new Label("Effects"));
this.Node.Add(this.Effects);
this.Effects = new VisualElement();
this.Effects.AddToClassList("effects");
this.Effects.Add(new Label("Effects"));
this.Node.Add(this.Effects);

foreach (var effect in graphNode.Effects)
{
var effectElement = new EffectElement(effect);
this.Effects.Add(effectElement);
}
foreach (var effect in graphNode.Effects)
{
var effectElement = new EffectElement(effect);
this.Effects.Add(effectElement);
}

this.schedule.Execute(() =>
Expand All @@ -144,21 +144,34 @@ public NodeElement(INode graphNode, VisualElement bezierRoot, EditorWindowValues
if (!provider.isActiveAndEnabled)
return;

this.UpdateClasses(graphNode, provider);
this.UpdateClasses(values, graphNode, provider);

this.Cost.text = $"Cost: {graphNode.GetCost(provider):0.00}";

if (graphNode.Action is IGoapAction action)
{
this.TargetCircle.SetColor(this.GetCircleColor(action, provider));
var target = provider.WorldData.GetTarget(action);
var targetText = target != null ? target.Position.ToString() : "null";

this.Target.text = $"Target: {targetText}";
this.Target.text = this.GetTargetText(values, provider, action);
}
}).Every(33);
}

private string GetTargetText(EditorWindowValues values, IMonoGoapActionProvider provider, IGoapAction action)
{
var targetConfig = action.Config.Target?.GetType().GetGenericTypeName();

if (!Application.isPlaying || provider == null)
return $"Target: {targetConfig}";

var target = provider.WorldData.GetTarget(action);
var targetText = target != null ? target.Position.ToString() : "null";

if (values.ShowConfig)
return $"Target: {targetText} ({targetConfig})";

return $"Target: {targetText}";
}

private Color GetCircleColor(IGoapAction goapAction, IMonoGoapActionProvider provider)
{
if (!Application.isPlaying)
Expand All @@ -173,11 +186,17 @@ private Color GetCircleColor(IGoapAction goapAction, IMonoGoapActionProvider pro
return Color.green;
}

private void UpdateClasses(INode graphNode, IMonoGoapActionProvider provider)
private void UpdateClasses(EditorWindowValues values, INode graphNode, IMonoGoapActionProvider provider)
{
this.Node.RemoveFromClassList("active");
this.Node.RemoveFromClassList("disabled");
this.Node.RemoveFromClassList("path");
this.Node.RemoveFromClassList("hide-effects");

if (!values.ShowConfig)
{
this.Node.AddToClassList("hide-effects");
}

if (provider.CurrentPlan?.Goal == this.GraphNode.Action)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public ToolbarElement(EditorWindowValues values)
Selection.activeObject = values.SelectedObject;
})
{
text = values.SelectedObject.name
text = values.SelectedObject.name,
});

this.Add(new ToolbarButton(() =>
{
var elementsWithClass = values.RootElement.Query<VisualElement>(className: "node").ToList();
Expand All @@ -26,9 +26,9 @@ public ToolbarElement(EditorWindowValues values)
}
})
{
text = "collapse"
text = "collapse",
});

this.Add(new ToolbarButton(() =>
{
var elementsWithClass = values.RootElement.Query<VisualElement>(className: "node").ToList();
Expand All @@ -39,35 +39,47 @@ public ToolbarElement(EditorWindowValues values)
}
})
{
text = "open"
text = "open",
});


ToolbarButton configToggle = null;
configToggle = new ToolbarButton(() =>
{
values.ShowConfig = !values.ShowConfig;
configToggle.text = values.ShowConfig ? "Config (true)" : "Config (false)";
})
{
text = "toggle config",
};

this.Add(configToggle);

var spacer = new VisualElement();
spacer.style.flexGrow = 1; // This makes the spacer flexible, filling available space
this.Add(spacer);

this.Add(new ToolbarButton(() =>
{
values.UpdateZoom(10);
})
{
text = "+"
text = "+",
});
this.Add(new ToolbarButton(() =>
{
values.UpdateZoom(-10);
})
{
text = "-"
text = "-",
});
this.Add(new ToolbarButton(() =>
{
values.Zoom = 100;
values.DragDrawer.Reset();
})
{
text = "reset"
text = "reset",
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
border-width: 2px;
}

.node.collapsed .effects {
.node.hide-effects .effects {
display: none;
}

.node.collapsed .effects{
/*min-height: 40px;*/
max-height: 0;
margin: 0;
Expand Down

0 comments on commit ecef2c7

Please sign in to comment.