Skip to content

Commit

Permalink
Add attach node gizmos
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Mar 30, 2023
1 parent 7cbbff5 commit 6d528b6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
1 change: 1 addition & 0 deletions Assets/KSP2UnityTools/Editor/ModuleDragEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ModuleDragEditor : Editor
[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
public static void DrawGizmosForDrag(Module_Drag moduleDrag, GizmoType gizmoType)
{
if (!PartEditor.DragCubeGizmos) return;
var mat = moduleDrag.gameObject.transform.localToWorldMatrix;
var dataDrag = moduleDrag.GetType().GetField("dataDrag", BindingFlags.Instance | BindingFlags.NonPublic)
?.GetValue(moduleDrag) as Data_Drag;
Expand Down
74 changes: 57 additions & 17 deletions Assets/KSP2UnityTools/Editor/PartEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@
using Newtonsoft.Json.UnityConverters.Configuration;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.Rendering;

[CustomEditor(typeof(CorePartData))]
public class PartEditor : Editor
{

private static bool _initialized = false;
private static readonly Color ComColor = new Color(Color.yellow.r, Color.yellow.g, Color.yellow.b, 0.5f);

private static string _jsonPath = "%NAME%.json";

private static bool _centerOfMassGizmos = true;
private static bool _centerOfLiftGizmos = true;
private static bool _attachNodeGizmos = true;

public static bool DragCubeGizmos = true;

// Just initialize all the conversion stuff
private static void Initialize()
Expand All @@ -37,18 +44,31 @@ private static void Initialize()
private void OnSceneGUI()
{
}
private GameObject TargetObject => TargetData.gameObject;
private CorePartData TargetData => target as CorePartData;
private PartCore TargetCore => TargetData.Core;

public override void OnInspectorGUI()
{
base.OnInspectorGUI();
GUILayout.Label("Gizmo Settings", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
_centerOfMassGizmos = EditorGUILayout.Toggle("CoM gizmos", _centerOfMassGizmos);
_centerOfLiftGizmos = EditorGUILayout.Toggle("CoL gizmos", _centerOfLiftGizmos);
_attachNodeGizmos = EditorGUILayout.Toggle("Attach Node Gizmos", _attachNodeGizmos);
DragCubeGizmos = EditorGUILayout.Toggle("Drag Cube Gizmos", DragCubeGizmos);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(target);
}
GUILayout.Label("Part Saving", EditorStyles.boldLabel);
_jsonPath = EditorGUILayout.TextField("JSON Path",_jsonPath);
if (!GUILayout.Button("Save Part JSON")) return;
if (!_initialized) Initialize();
var core = (serializedObject.targetObject as CorePartData)?.Core!;
var targetGO = (serializedObject.targetObject as CorePartData).gameObject;
if (core == null) return;
if (TargetCore == null) return;
// Clear out the serialized part modules and reserialize them
core.data.serializedPartModules.Clear();
foreach (var child in targetGO.GetComponents<Component>())
TargetCore.data.serializedPartModules.Clear();
foreach (var child in TargetObject.GetComponents<Component>())
{
if (!(child is PartBehaviourModule partBehaviourModule)) continue;
var addMethod = child.GetType().GetMethod("AddDataModules", BindingFlags.Instance | BindingFlags.NonPublic) ??
Expand All @@ -59,22 +79,42 @@ public override void OnInspectorGUI()
var rebuildMethod = data.GetType().GetMethod("RebuildDataContext", BindingFlags.Instance | BindingFlags.NonPublic) ?? data.GetType().GetMethod("RebuildDataContext", BindingFlags.Instance | BindingFlags.Public);
rebuildMethod?.Invoke(data, new object[] { });
}
core.data.serializedPartModules.Add(new SerializedPartModule(partBehaviourModule,false));
TargetCore.data.serializedPartModules.Add(new SerializedPartModule(partBehaviourModule,false));
}
var json = IOProvider.ToJson(core);
File.WriteAllText($"{Application.dataPath}/{core.data.partName}.json", json);
var json = IOProvider.ToJson(TargetCore);
var path = $"{Application.dataPath}/{_jsonPath}";
path = path.Replace("%NAME%", TargetCore.data.partName);
File.WriteAllText($"{path}", json);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Part Exported", $"Json is at: {Application.dataPath}/{core.data.partName}.json", "ok");
EditorUtility.DisplayDialog("Part Exported", $"Json is at: {path}", "ok");
}

[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
static void DrawGizmoForPartCoreData(CorePartData data, GizmoType gizmoType)
public static void DrawGizmoForPartCoreData(CorePartData data, GizmoType gizmoType)
{
var centerOfMassPosition = data.Data.coMassOffset;
var localToWorldMatrix = data.transform.localToWorldMatrix;
centerOfMassPosition = localToWorldMatrix.MultiplyPoint(centerOfMassPosition);
Gizmos.DrawIcon(centerOfMassPosition, "com_icon.png",false);
var centerOfLiftPosition = data.Data.coLiftOffset;
centerOfLiftPosition = localToWorldMatrix.MultiplyPoint(centerOfLiftPosition);
Gizmos.DrawIcon(centerOfLiftPosition, "col_icon.png",false);
if (_centerOfMassGizmos)
{
var centerOfMassPosition = data.Data.coMassOffset;
centerOfMassPosition = localToWorldMatrix.MultiplyPoint(centerOfMassPosition);
Gizmos.DrawIcon(centerOfMassPosition, "com_icon.png", false);
}
if (_centerOfLiftGizmos)
{
var centerOfLiftPosition = data.Data.coLiftOffset;
centerOfLiftPosition = localToWorldMatrix.MultiplyPoint(centerOfLiftPosition);
Gizmos.DrawIcon(centerOfLiftPosition, "col_icon.png", false);
}
if (!_attachNodeGizmos) return;
Gizmos.color = new Color(Color.green.r, Color.green.g, Color.green.b, 0.5f);
foreach (var attachNode in data.Data.attachNodes)
{
var pos = attachNode.position;
pos = localToWorldMatrix.MultiplyPoint(pos);
var dir = attachNode.orientation;
dir = localToWorldMatrix.MultiplyVector(dir);
Gizmos.DrawRay(pos, dir * 0.25f);
Gizmos.DrawSphere(pos,0.05f);
}
}
}
2 changes: 1 addition & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ MonoBehaviour:
- 1
- 1
- 1
center: {x: 4.6299934, y: -24.184984, z: 10}
center: {x: 0, y: 0, z: 0}
size: {x: 0.625, y: 0.3946495, z: 0.625}
name: Default
weight: 1
Expand Down

0 comments on commit 6d528b6

Please sign in to comment.