Skip to content

Commit 6d528b6

Browse files
committed
Add attach node gizmos
1 parent 7cbbff5 commit 6d528b6

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

Assets/KSP2UnityTools/Editor/ModuleDragEditor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ModuleDragEditor : Editor
1010
[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
1111
public static void DrawGizmosForDrag(Module_Drag moduleDrag, GizmoType gizmoType)
1212
{
13+
if (!PartEditor.DragCubeGizmos) return;
1314
var mat = moduleDrag.gameObject.transform.localToWorldMatrix;
1415
var dataDrag = moduleDrag.GetType().GetField("dataDrag", BindingFlags.Instance | BindingFlags.NonPublic)
1516
?.GetValue(moduleDrag) as Data_Drag;

Assets/KSP2UnityTools/Editor/PartEditor.cs

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616
using Newtonsoft.Json.UnityConverters.Configuration;
1717
using UnityEditor.VersionControl;
1818
using UnityEngine;
19-
using UnityEngine.Rendering;
2019

2120
[CustomEditor(typeof(CorePartData))]
2221
public class PartEditor : Editor
2322
{
2423

2524
private static bool _initialized = false;
2625
private static readonly Color ComColor = new Color(Color.yellow.r, Color.yellow.g, Color.yellow.b, 0.5f);
26+
27+
private static string _jsonPath = "%NAME%.json";
28+
29+
private static bool _centerOfMassGizmos = true;
30+
private static bool _centerOfLiftGizmos = true;
31+
private static bool _attachNodeGizmos = true;
32+
33+
public static bool DragCubeGizmos = true;
2734

2835
// Just initialize all the conversion stuff
2936
private static void Initialize()
@@ -37,18 +44,31 @@ private static void Initialize()
3744
private void OnSceneGUI()
3845
{
3946
}
47+
private GameObject TargetObject => TargetData.gameObject;
48+
private CorePartData TargetData => target as CorePartData;
49+
private PartCore TargetCore => TargetData.Core;
4050

4151
public override void OnInspectorGUI()
4252
{
4353
base.OnInspectorGUI();
54+
GUILayout.Label("Gizmo Settings", EditorStyles.boldLabel);
55+
EditorGUI.BeginChangeCheck();
56+
_centerOfMassGizmos = EditorGUILayout.Toggle("CoM gizmos", _centerOfMassGizmos);
57+
_centerOfLiftGizmos = EditorGUILayout.Toggle("CoL gizmos", _centerOfLiftGizmos);
58+
_attachNodeGizmos = EditorGUILayout.Toggle("Attach Node Gizmos", _attachNodeGizmos);
59+
DragCubeGizmos = EditorGUILayout.Toggle("Drag Cube Gizmos", DragCubeGizmos);
60+
if (EditorGUI.EndChangeCheck())
61+
{
62+
EditorUtility.SetDirty(target);
63+
}
64+
GUILayout.Label("Part Saving", EditorStyles.boldLabel);
65+
_jsonPath = EditorGUILayout.TextField("JSON Path",_jsonPath);
4466
if (!GUILayout.Button("Save Part JSON")) return;
4567
if (!_initialized) Initialize();
46-
var core = (serializedObject.targetObject as CorePartData)?.Core!;
47-
var targetGO = (serializedObject.targetObject as CorePartData).gameObject;
48-
if (core == null) return;
68+
if (TargetCore == null) return;
4969
// Clear out the serialized part modules and reserialize them
50-
core.data.serializedPartModules.Clear();
51-
foreach (var child in targetGO.GetComponents<Component>())
70+
TargetCore.data.serializedPartModules.Clear();
71+
foreach (var child in TargetObject.GetComponents<Component>())
5272
{
5373
if (!(child is PartBehaviourModule partBehaviourModule)) continue;
5474
var addMethod = child.GetType().GetMethod("AddDataModules", BindingFlags.Instance | BindingFlags.NonPublic) ??
@@ -59,22 +79,42 @@ public override void OnInspectorGUI()
5979
var rebuildMethod = data.GetType().GetMethod("RebuildDataContext", BindingFlags.Instance | BindingFlags.NonPublic) ?? data.GetType().GetMethod("RebuildDataContext", BindingFlags.Instance | BindingFlags.Public);
6080
rebuildMethod?.Invoke(data, new object[] { });
6181
}
62-
core.data.serializedPartModules.Add(new SerializedPartModule(partBehaviourModule,false));
82+
TargetCore.data.serializedPartModules.Add(new SerializedPartModule(partBehaviourModule,false));
6383
}
64-
var json = IOProvider.ToJson(core);
65-
File.WriteAllText($"{Application.dataPath}/{core.data.partName}.json", json);
84+
var json = IOProvider.ToJson(TargetCore);
85+
var path = $"{Application.dataPath}/{_jsonPath}";
86+
path = path.Replace("%NAME%", TargetCore.data.partName);
87+
File.WriteAllText($"{path}", json);
6688
AssetDatabase.Refresh();
67-
EditorUtility.DisplayDialog("Part Exported", $"Json is at: {Application.dataPath}/{core.data.partName}.json", "ok");
89+
EditorUtility.DisplayDialog("Part Exported", $"Json is at: {path}", "ok");
6890
}
91+
6992
[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
70-
static void DrawGizmoForPartCoreData(CorePartData data, GizmoType gizmoType)
93+
public static void DrawGizmoForPartCoreData(CorePartData data, GizmoType gizmoType)
7194
{
72-
var centerOfMassPosition = data.Data.coMassOffset;
7395
var localToWorldMatrix = data.transform.localToWorldMatrix;
74-
centerOfMassPosition = localToWorldMatrix.MultiplyPoint(centerOfMassPosition);
75-
Gizmos.DrawIcon(centerOfMassPosition, "com_icon.png",false);
76-
var centerOfLiftPosition = data.Data.coLiftOffset;
77-
centerOfLiftPosition = localToWorldMatrix.MultiplyPoint(centerOfLiftPosition);
78-
Gizmos.DrawIcon(centerOfLiftPosition, "col_icon.png",false);
96+
if (_centerOfMassGizmos)
97+
{
98+
var centerOfMassPosition = data.Data.coMassOffset;
99+
centerOfMassPosition = localToWorldMatrix.MultiplyPoint(centerOfMassPosition);
100+
Gizmos.DrawIcon(centerOfMassPosition, "com_icon.png", false);
101+
}
102+
if (_centerOfLiftGizmos)
103+
{
104+
var centerOfLiftPosition = data.Data.coLiftOffset;
105+
centerOfLiftPosition = localToWorldMatrix.MultiplyPoint(centerOfLiftPosition);
106+
Gizmos.DrawIcon(centerOfLiftPosition, "col_icon.png", false);
107+
}
108+
if (!_attachNodeGizmos) return;
109+
Gizmos.color = new Color(Color.green.r, Color.green.g, Color.green.b, 0.5f);
110+
foreach (var attachNode in data.Data.attachNodes)
111+
{
112+
var pos = attachNode.position;
113+
pos = localToWorldMatrix.MultiplyPoint(pos);
114+
var dir = attachNode.orientation;
115+
dir = localToWorldMatrix.MultiplyVector(dir);
116+
Gizmos.DrawRay(pos, dir * 0.25f);
117+
Gizmos.DrawSphere(pos,0.05f);
118+
}
79119
}
80120
}

Assets/Scenes/SampleScene.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ MonoBehaviour:
778778
- 1
779779
- 1
780780
- 1
781-
center: {x: 4.6299934, y: -24.184984, z: 10}
781+
center: {x: 0, y: 0, z: 0}
782782
size: {x: 0.625, y: 0.3946495, z: 0.625}
783783
name: Default
784784
weight: 1

0 commit comments

Comments
 (0)