暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MatrixDrawer.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Unity.Mathematics.Editor
  7. {
  8. [CustomPropertyDrawer(typeof(bool2x2)), CustomPropertyDrawer(typeof(bool2x3)), CustomPropertyDrawer(typeof(bool2x4))]
  9. [CustomPropertyDrawer(typeof(bool3x2)), CustomPropertyDrawer(typeof(bool3x3)), CustomPropertyDrawer(typeof(bool3x4))]
  10. [CustomPropertyDrawer(typeof(bool4x2)), CustomPropertyDrawer(typeof(bool4x3)), CustomPropertyDrawer(typeof(bool4x4))]
  11. [CustomPropertyDrawer(typeof(double2x2)), CustomPropertyDrawer(typeof(double2x3)), CustomPropertyDrawer(typeof(double2x4))]
  12. [CustomPropertyDrawer(typeof(double3x2)), CustomPropertyDrawer(typeof(double3x3)), CustomPropertyDrawer(typeof(double3x4))]
  13. [CustomPropertyDrawer(typeof(double4x2)), CustomPropertyDrawer(typeof(double4x3)), CustomPropertyDrawer(typeof(double4x4))]
  14. [CustomPropertyDrawer(typeof(float2x2)), CustomPropertyDrawer(typeof(float2x3)), CustomPropertyDrawer(typeof(float2x4))]
  15. [CustomPropertyDrawer(typeof(float3x2)), CustomPropertyDrawer(typeof(float3x3)), CustomPropertyDrawer(typeof(float3x4))]
  16. [CustomPropertyDrawer(typeof(float4x2)), CustomPropertyDrawer(typeof(float4x3)), CustomPropertyDrawer(typeof(float4x4))]
  17. [CustomPropertyDrawer(typeof(int2x2)), CustomPropertyDrawer(typeof(int2x3)), CustomPropertyDrawer(typeof(int2x4))]
  18. [CustomPropertyDrawer(typeof(int3x2)), CustomPropertyDrawer(typeof(int3x3)), CustomPropertyDrawer(typeof(int3x4))]
  19. [CustomPropertyDrawer(typeof(int4x2)), CustomPropertyDrawer(typeof(int4x3)), CustomPropertyDrawer(typeof(int4x4))]
  20. [CustomPropertyDrawer(typeof(uint2x2)), CustomPropertyDrawer(typeof(uint2x3)), CustomPropertyDrawer(typeof(uint2x4))]
  21. [CustomPropertyDrawer(typeof(uint3x2)), CustomPropertyDrawer(typeof(uint3x3)), CustomPropertyDrawer(typeof(uint3x4))]
  22. [CustomPropertyDrawer(typeof(uint4x2)), CustomPropertyDrawer(typeof(uint4x3)), CustomPropertyDrawer(typeof(uint4x4))]
  23. class MatrixDrawer : PropertyDrawer
  24. {
  25. public override bool CanCacheInspectorGUI(SerializedProperty property)
  26. {
  27. return false;
  28. }
  29. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  30. {
  31. if (!property.isExpanded)
  32. return EditorGUIUtility.singleLineHeight;
  33. var rows = 1 + property.type[property.type.Length - 3] - '0';
  34. return rows * EditorGUIUtility.singleLineHeight + (rows - 1) * EditorGUIUtility.standardVerticalSpacing;
  35. }
  36. static ReadOnlyCollection<string> k_ColPropertyPaths =
  37. new ReadOnlyCollection<string>(new[] { "c0", "c1", "c2", "c3" });
  38. static ReadOnlyCollection<string> k_RowPropertyPaths =
  39. new ReadOnlyCollection<string>(new[] { "x", "y", "z", "w" });
  40. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  41. {
  42. position.height = EditorGUIUtility.singleLineHeight;
  43. EditorGUI.PropertyField(position, property, label, false);
  44. if (Event.current.type == EventType.ContextClick && position.Contains(Event.current.mousePosition))
  45. {
  46. DoUtilityMenu(property);
  47. Event.current.Use();
  48. }
  49. if (!property.isExpanded)
  50. return;
  51. var rows = property.type[property.type.Length - 3] - '0';
  52. var cols = property.type[property.type.Length - 1] - '0';
  53. ++EditorGUI.indentLevel;
  54. position = EditorGUI.IndentedRect(position);
  55. --EditorGUI.indentLevel;
  56. var elementType = property.FindPropertyRelative("c0.x").propertyType;
  57. for (var row = 0; row < rows; ++row)
  58. {
  59. position.y += position.height + EditorGUIUtility.standardVerticalSpacing;
  60. var elementRect = new Rect(position)
  61. {
  62. width = elementType == SerializedPropertyType.Boolean
  63. ? EditorGUIUtility.singleLineHeight
  64. : (position.width - (cols - 1) * EditorGUIUtility.standardVerticalSpacing) / cols
  65. };
  66. for (var col = 0; col < cols; ++col)
  67. {
  68. EditorGUI.PropertyField(
  69. elementRect,
  70. property.FindPropertyRelative($"{k_ColPropertyPaths[col]}.{k_RowPropertyPaths[row]}"),
  71. GUIContent.none
  72. );
  73. elementRect.x += elementRect.width + EditorGUIUtility.standardVerticalSpacing;
  74. }
  75. }
  76. }
  77. Dictionary<SerializedPropertyType, Action<SerializedProperty, bool>> k_UtilityValueSetters =
  78. new Dictionary<SerializedPropertyType, Action<SerializedProperty, bool>>
  79. {
  80. { SerializedPropertyType.Boolean, (property, b) => property.boolValue = b },
  81. { SerializedPropertyType.Float, (property, b) => property.floatValue = b ? 1f : 0f },
  82. { SerializedPropertyType.Integer, (property, b) => property.intValue = b ? 1 : 0 }
  83. };
  84. void DoUtilityMenu(SerializedProperty property)
  85. {
  86. var rows = property.type[property.type.Length - 3] - '0';
  87. var cols = property.type[property.type.Length - 1] - '0';
  88. var elementType = property.FindPropertyRelative("c0.x").propertyType;
  89. var setValue = k_UtilityValueSetters[elementType];
  90. var menu = new GenericMenu();
  91. property = property.Copy();
  92. menu.AddItem(
  93. EditorGUIUtility.TrTextContent("Set to Zero"),
  94. false,
  95. () =>
  96. {
  97. property.serializedObject.Update();;
  98. for (var row = 0; row < rows; ++row)
  99. for (var col = 0; col < cols; ++col)
  100. setValue(
  101. property.FindPropertyRelative($"{k_ColPropertyPaths[col]}.{k_RowPropertyPaths[row]}"),
  102. false
  103. );
  104. property.serializedObject.ApplyModifiedProperties();
  105. }
  106. );
  107. if (rows == cols)
  108. {
  109. menu.AddItem(
  110. EditorGUIUtility.TrTextContent("Reset to Identity"),
  111. false,
  112. () =>
  113. {
  114. property.serializedObject.Update();
  115. for (var row = 0; row < rows; ++row)
  116. for (var col = 0; col < cols; ++col)
  117. setValue(
  118. property.FindPropertyRelative($"{k_ColPropertyPaths[col]}.{k_RowPropertyPaths[row]}"),
  119. row == col
  120. );
  121. property.serializedObject.ApplyModifiedProperties();
  122. }
  123. );
  124. }
  125. menu.ShowAsContext();
  126. }
  127. }
  128. }