|
| 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 |
0 commit comments