Skip to content

Commit

Permalink
fixed some style and missing assembly ref
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Aug 26, 2024
1 parent 4a857a4 commit 4603fd3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ public class CraftingRecipeEditor : Editor
private SerializedProperty _stepLinksProperty;
private RecipeStep _recipeStep;

private void OnEnable()
{
// Initialize SerializedProperties
CraftingRecipe recipe = serializedObject.targetObject as CraftingRecipe;
_targetProperty = serializedObject.FindProperty(nameof(recipe.Target));
_stepsProperty = serializedObject.FindProperty(nameof(recipe.Steps));
_stepLinksProperty = serializedObject.FindProperty(nameof(recipe.StepLinks));
}

public override void OnInspectorGUI()
{
serializedObject.Update();
Expand All @@ -27,6 +18,14 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_stepLinksProperty, true);
serializedObject.ApplyModifiedProperties();
}

protected void OnEnable()
{
// Initialize SerializedProperties
CraftingRecipe recipe = serializedObject.targetObject as CraftingRecipe;
_targetProperty = serializedObject.FindProperty(nameof(recipe.Target));
_stepsProperty = serializedObject.FindProperty(nameof(recipe.Steps));
_stepLinksProperty = serializedObject.FindProperty(nameof(recipe.StepLinks));
}
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using UnityEditor;
using UnityEngine;
using QuikGraph;
using QuikGraph;
using SS3D.Utils;
using System.Collections;
using Unity.EditorCoroutines.Editor;
using SS3D.Utils;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;

namespace SS3D.Systems.Crafting
Expand All @@ -18,38 +18,33 @@ public class CraftingRecipeVisualizerEditorWindow : EditorWindow
/// How much vertices are repulsive to each other.
/// </summary>
private const float RepulsiveConstant = 100;

/// <summary>
/// How much vertices linked by an edge attract each other.
/// </summary>
private const float AttractiveConstant = 3;

/// <summary>
/// Ideal lenght between vertices.
/// </summary>
private const float IdealLenght = 80;

/// <summary>
/// Maximum of interation the force algorithm will make.
/// </summary>
private const int MaxIteration = 500;

/// <summary>
/// "Speed" factor for the algorithm, the higher it is, the faster it converges
/// toward the solution, but values too high can lead to divergence.
/// </summary>
private const float Delta = 10f;

/// <summary>
/// Another criteria to stop the algorithm is what's the max force exerted on any vertices is at a given iteration.
/// When lower than a given amount we consider it won't move much, and we stop.
/// </summary>
private const float ForceToStop = 0.1f;

/// <summary>
/// Enhanced recipe Graph with position for vertices.
/// </summary>
private AdjacencyGraph<VerticeWithPosition<RecipeStep>, TaggedEdge<VerticeWithPosition<RecipeStep>, RecipeStepLink>> _graphWithPosition;

/// <summary>
/// Minimum zoom allowed
Expand All @@ -61,32 +56,60 @@ public class CraftingRecipeVisualizerEditorWindow : EditorWindow
/// </summary>
private const float KZoomMax = 10.0f;

/// <summary>
/// Size of vertices drawn in the window.
/// </summary>
private const float CircleSize = 5f;

/// <summary>
/// Area in which the zooming will occur.
/// </summary>
private readonly Rect _zoomArea = new(0.0f, 100.0f, 1200.0f, 600.0f);

/// <summary>
/// Enhanced recipe Graph with position for vertices.
/// </summary>
private AdjacencyGraph<VerticeWithPosition<RecipeStep>, TaggedEdge<VerticeWithPosition<RecipeStep>, RecipeStepLink>> _graphWithPosition;

/// <summary>
/// The current value of the zoom.
/// </summary>
private float _zoom = 1.0f;

private Vector2 _zoomCoordsOrigin = Vector2.zero;

[SerializeField]
private AssetReferenceT<CraftingRecipe> _recipe;

/// <summary>
/// Size of vertices drawn in the window.
/// Show this window with the right size and parameters.
/// </summary>
private const float CircleSize = 5f;
[MenuItem("Window/SS3D/Crafting Recipe Display")]
public static void ShowWindow()
{
CraftingRecipeVisualizerEditorWindow window = GetWindow<CraftingRecipeVisualizerEditorWindow>("Crafting Recipe Display");
window.minSize = new(600.0f, 300.0f);
window.wantsMouseMove = true;
}

protected void OnGUI()
{
HandleEvents();
DrawNonZoomArea();

// The zoom area clipping is sometimes not fully confined to the passed in rectangle. At certain
// zoom levels you will get a line of pixels rendered outside of the passed in area because of
// floating point imprecision in the scaling. Therefore, it is recommended to draw the zoom
// area first and then draw everything else so that there is no undesired overlap.
DrawZoomArea();
}

/// <summary>
/// Helper method to find coordinates when zooming from the screen coordinates.
/// </summary>
private Vector2 ConvertScreenCoordsToZoomCoords(Vector2 screenCoords)
{
return (screenCoords - _zoomArea.TopLeft()) / _zoom + _zoomCoordsOrigin;
return ((screenCoords - _zoomArea.TopLeft()) / _zoom) + _zoomCoordsOrigin;
}

/// <summary>
Expand All @@ -98,12 +121,12 @@ private void DrawZoomArea()
// with the width and height being scaled versions of the original/unzoomed area's width and height.
EditorZoomArea.Begin(_zoom, _zoomArea);
GUILayout.BeginArea(new(-_zoomCoordsOrigin.x, -_zoomCoordsOrigin.y, 1600.0f, 900.0f));

if (_graphWithPosition != null)
{
DrawGraph(_graphWithPosition);
}

GUILayout.EndArea();
EditorZoomArea.End();
}
Expand All @@ -118,11 +141,15 @@ private void DrawNonZoomArea()

if (GUILayout.Button("Draw graph"))
{
if (_recipe == null) return;
if (_recipe == null)
{
return;
}

EditorCoroutineUtility.StartCoroutine(ComputeGraphPositions(_recipe.editorAsset), this);
}
SerializedObject so = new (this);

SerializedObject so = new(this);
EditorGUILayout.PropertyField(so.FindProperty(nameof(_recipe)), new GUIContent("Recipe"));
}

Expand All @@ -133,17 +160,20 @@ private IEnumerator ComputeGraphPositions(CraftingRecipe recipe)
{
_graphWithPosition = SpringEmbedderAlgorithm<RecipeStep, TaggedEdge<RecipeStep, RecipeStepLink>, RecipeStepLink>
.InitializeGraphWithPositions(recipe.RecipeGraph);

for (int i = 0; i < MaxIteration; i++)
{
SpringEmbedderAlgorithm<RecipeStep, TaggedEdge<RecipeStep, RecipeStepLink>, RecipeStepLink>
.SetParameters(RepulsiveConstant, AttractiveConstant,
IdealLenght, Delta, ForceToStop);

.SetParameters(RepulsiveConstant, AttractiveConstant, IdealLenght, Delta, ForceToStop);

bool forceReachedMinimum = SpringEmbedderAlgorithm<RecipeStep, TaggedEdge<RecipeStep, RecipeStepLink>, RecipeStepLink>
.ComputeOneStep(_graphWithPosition);

if (forceReachedMinimum) break;

if (forceReachedMinimum)
{
break;
}

Repaint();
yield return null;
}
Expand All @@ -166,11 +196,11 @@ private void HandleEvents()
float oldZoom = _zoom;
_zoom += zoomDelta;
_zoom = Mathf.Clamp(_zoom, KZoomMin, KZoomMax);
_zoomCoordsOrigin += zoomCoordsMousePos - _zoomCoordsOrigin - (oldZoom / _zoom) * (zoomCoordsMousePos - _zoomCoordsOrigin);
_zoomCoordsOrigin += zoomCoordsMousePos - _zoomCoordsOrigin - ((oldZoom / _zoom) * (zoomCoordsMousePos - _zoomCoordsOrigin));

Event.current.Use();
}

if (Event.current.type == EventType.MouseDrag && Event.current.button == 0)
{
Vector2 delta = Event.current.delta;
Expand All @@ -180,51 +210,43 @@ private void HandleEvents()
}
}

/// <summary>
/// Show this window with the right size and parameters.
/// </summary>
[MenuItem("Window/SS3D/Crafting Recipe Display")]
public static void ShowWindow()
{
CraftingRecipeVisualizerEditorWindow window = GetWindow<CraftingRecipeVisualizerEditorWindow>("Crafting Recipe Display");
window.minSize = new(600.0f, 300.0f);
window.wantsMouseMove = true;
}

private void OnGUI()
{
HandleEvents();
DrawNonZoomArea();

// The zoom area clipping is sometimes not fully confined to the passed in rectangle. At certain
// zoom levels you will get a line of pixels rendered outside of the passed in area because of
// floating point imprecision in the scaling. Therefore, it is recommended to draw the zoom
// area first and then draw everything else so that there is no undesired overlap.
DrawZoomArea();
}

/// <summary>
/// Draw the graph
/// </summary>
private void DrawGraph(AdjacencyGraph<VerticeWithPosition<RecipeStep>, TaggedEdge<VerticeWithPosition<RecipeStep>, RecipeStepLink>> graphWithPosition)
{
foreach (VerticeWithPosition<RecipeStep> stepWithPosition in graphWithPosition.Vertices)
{
Color color = stepWithPosition.Vertice.IsTerminal ? Color.red : stepWithPosition.Vertice.IsInitialState ? Color.green : Color.gray;
Color color;

if (stepWithPosition.Vertice.IsTerminal)
{
color = Color.red;
}
else if (stepWithPosition.Vertice.IsInitialState)
{
color = Color.green;
}
else
{
color = Color.gray;
}

Handles.color = color;
Handles.DrawSolidDisc(new (stepWithPosition.Position.x, stepWithPosition.Position.y, 0), Vector3.forward, CircleSize);
Handles.DrawSolidDisc(new(stepWithPosition.Position.x, stepWithPosition.Position.y, 0), Vector3.forward, CircleSize);
Handles.color = Color.black;
Handles.DrawWireDisc(new (stepWithPosition.Position.x, stepWithPosition.Position.y, 0), Vector3.forward, CircleSize);
Handles.DrawWireDisc(new(stepWithPosition.Position.x, stepWithPosition.Position.y, 0), Vector3.forward, CircleSize);

GUIStyle style = new(GUI.skin.label)
{
fontSize = (int)Mathf.Clamp(12f / _zoom, 4f, 25f),
};

EditorGUI.LabelField(new(stepWithPosition.Position.x, stepWithPosition.Position.y, 200, 20), stepWithPosition.Vertice.Name, style);
}

Handles.color = Color.white;

foreach (TaggedEdge<VerticeWithPosition<RecipeStep>, RecipeStepLink> edge in graphWithPosition.Edges)
{
Handles.DrawAAPolyLine(3, edge.Source.Position, edge.Target.Position);
Expand All @@ -246,4 +268,3 @@ private void DrawArrowhead(Vector2 start, Vector2 end, float arrowheadAngle, flo
}
}
}

3 changes: 2 additions & 1 deletion Assets/Scripts/SS3D/Systems/SS3D.Systems.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"GUID:cb1568c00c2cb384889306082481b4dc",
"GUID:3cf19ecc459d5694491bb233fc4fccbd",
"GUID:493bb11add906094faa30425cab7ce2e",
"GUID:eec0964c48f6f4e40bc3ec2257ccf8c5"
"GUID:eec0964c48f6f4e40bc3ec2257ccf8c5",
"GUID:776d03a35f1b52c4a9aed9f56d7b4229"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down

0 comments on commit 4603fd3

Please sign in to comment.