Skip to content

Commit d9d1e67

Browse files
committed
update: added matrix drawers
1 parent e63354d commit d9d1e67

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

Editor/MatrixDrawer.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#if UNITY_EDITOR
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace FixedMathSharp.Editor
6+
{
7+
/// <summary>
8+
/// A read-only matrix drawer for Fixed3x3 and Fixed4x4 values.
9+
/// </summary>
10+
[CustomPropertyDrawer(typeof(Fixed3x3))]
11+
[CustomPropertyDrawer(typeof(Fixed4x4))]
12+
public class MatrixDrawer : PropertyDrawer
13+
{
14+
private static readonly string[][] Fixed3x3FieldNames =
15+
{
16+
new[] { "m00", "m01", "m02" },
17+
new[] { "m10", "m11", "m12" },
18+
new[] { "m20", "m21", "m22" }
19+
};
20+
21+
private static readonly string[][] Fixed4x4FieldNames =
22+
{
23+
new[] { "m00", "m01", "m02", "m03" },
24+
new[] { "m10", "m11", "m12", "m13" },
25+
new[] { "m20", "m21", "m22", "m23" },
26+
new[] { "m30", "m31", "m32", "m33" }
27+
};
28+
29+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
30+
{
31+
if (!property.isExpanded)
32+
return EditorGUIUtility.singleLineHeight;
33+
34+
int rowCount = IsFixed4x4(property) ? 4 : 3;
35+
return EditorGUIUtility.singleLineHeight +
36+
rowCount * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
37+
}
38+
39+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
40+
{
41+
EditorGUI.BeginProperty(position, label, property);
42+
43+
Rect foldoutRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
44+
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, true);
45+
46+
if (property.isExpanded)
47+
{
48+
string[][] fieldNames = IsFixed4x4(property) ? Fixed4x4FieldNames : Fixed3x3FieldNames;
49+
Rect rowRect = new Rect(
50+
position.x,
51+
position.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing,
52+
position.width,
53+
EditorGUIUtility.singleLineHeight);
54+
55+
for (int row = 0; row < fieldNames.Length; row++)
56+
{
57+
Rect indentedRowRect = EditorGUI.IndentedRect(rowRect);
58+
FMSEditorUtility.DrawReadOnlyMatrixRow(indentedRowRect, $"R{row}", GetRowValues(property, fieldNames[row]));
59+
rowRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
60+
}
61+
}
62+
63+
EditorGUI.EndProperty();
64+
}
65+
66+
private bool IsFixed4x4(SerializedProperty property)
67+
{
68+
return fieldInfo != null
69+
? fieldInfo.FieldType == typeof(Fixed4x4)
70+
: property.type == nameof(Fixed4x4);
71+
}
72+
73+
private static Fixed64[] GetRowValues(SerializedProperty property, string[] rowFieldNames)
74+
{
75+
Fixed64[] values = new Fixed64[rowFieldNames.Length];
76+
for (int i = 0; i < rowFieldNames.Length; i++)
77+
{
78+
values[i] = FMSEditorUtility.GetFixed64Value(property.FindPropertyRelative(rowFieldNames[i]));
79+
}
80+
81+
return values;
82+
}
83+
}
84+
}
85+
#endif

Editor/MatrixDrawer.cs.meta

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

Editor/Utility/FMSEditorUtility.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace FixedMathSharp.Editor
77
{
88
public static class FMSEditorUtility
99
{
10+
private const float MatrixRowLabelWidth = 24f;
11+
private const float MatrixCellSpacing = 2f;
12+
1013
#region EditorGUI
1114

1215
public static void DoubleField(Rect position, GUIContent label, ref Fixed64 value, double scale = 1d)
@@ -64,6 +67,29 @@ public static void Vector3dField(Rect position, GUIContent label, ref Vector3d v
6467
EditorGUIUtility.labelWidth = labelWidth;
6568
}
6669

70+
public static Fixed64 GetFixed64Value(SerializedProperty property)
71+
{
72+
SerializedProperty rawValue = property?.FindPropertyRelative("m_rawValue");
73+
return rawValue == null ? Fixed64.Zero : Fixed64.FromRaw(rawValue.longValue);
74+
}
75+
76+
public static void DrawReadOnlyMatrixRow(Rect position, string rowLabel, params Fixed64[] values)
77+
{
78+
Rect rowLabelRect = new Rect(position.x, position.y, MatrixRowLabelWidth, position.height);
79+
EditorGUI.LabelField(rowLabelRect, rowLabel);
80+
81+
float totalSpacing = MatrixCellSpacing * (values.Length - 1);
82+
float cellWidth = (position.width - MatrixRowLabelWidth - totalSpacing) / values.Length;
83+
Rect cellRect = new Rect(rowLabelRect.xMax, position.y, cellWidth, position.height);
84+
85+
using var disabled = new EditorGUI.DisabledScope(true);
86+
for (int i = 0; i < values.Length; i++)
87+
{
88+
EditorGUI.TextField(cellRect, values[i].ToFormattedDouble(3).ToString("0.###"));
89+
cellRect.x += cellWidth + MatrixCellSpacing;
90+
}
91+
}
92+
6793
#endregion
6894

6995
#region EditorGUILayout
@@ -109,4 +135,4 @@ public static void Vector3dField(string Label, ref Vector3d vector)
109135
#endregion
110136
}
111137
}
112-
#endif
138+
#endif

0 commit comments

Comments
 (0)