Assignment for RMIT Mixed Reality in 2020
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.

79 lines
3.2 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Globalization;
  6. using Supyrb;
  7. [CustomPropertyDrawer(typeof(MinMaxRangeAttribute))]
  8. class MinMaxRangeDrawer : PropertyDrawer
  9. {
  10. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  11. {
  12. label.tooltip = VRTK_EditorUtilities.GetTooltipAttribute(fieldInfo).tooltip;
  13. bool foundGeneric = false;
  14. bool valid = false;
  15. try
  16. {
  17. Vector2 input = SerializedPropertyExtensions.GetValue<Limits2D>(property).AsVector2();
  18. Vector2 output = BuildSlider(position, label, input, out valid);
  19. if (valid)
  20. {
  21. SerializedPropertyExtensions.SetValue<Limits2D>(property, new Limits2D(output));
  22. }
  23. foundGeneric = true;
  24. }
  25. catch (System.Exception)
  26. {
  27. Error(position, label);
  28. }
  29. if (!foundGeneric)
  30. {
  31. switch (property.propertyType)
  32. {
  33. case SerializedPropertyType.Vector2:
  34. Vector2 input = property.vector2Value;
  35. Vector2 output = BuildSlider(position, label, input, out valid);
  36. if (valid)
  37. {
  38. property.vector2Value = output;
  39. }
  40. break;
  41. default:
  42. Error(position, label);
  43. break;
  44. }
  45. }
  46. }
  47. private Vector2 BuildSlider(Rect position, GUIContent label, Vector2 range, out bool valid)
  48. {
  49. float fieldWidth = GUI.skin.textField.CalcSize(new GUIContent(1.23456f.ToString(CultureInfo.InvariantCulture))).x; ;
  50. float fieldPadding = 5f;
  51. float min = range.x;
  52. float max = range.y;
  53. MinMaxRangeAttribute attr = attribute as MinMaxRangeAttribute;
  54. EditorGUI.BeginChangeCheck();
  55. Rect updatedPosition = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  56. min = EditorGUI.FloatField(new Rect(updatedPosition.x, updatedPosition.y, fieldWidth, updatedPosition.height), Mathf.Clamp(min, attr.min, attr.max));
  57. EditorGUI.MinMaxSlider(new Rect(updatedPosition.x + (fieldWidth + fieldPadding), updatedPosition.y, updatedPosition.width - ((fieldWidth + fieldPadding) * 2f), updatedPosition.height), ref min, ref max, attr.min, attr.max);
  58. max = EditorGUI.FloatField(new Rect(updatedPosition.x + (updatedPosition.width - fieldWidth), updatedPosition.y, fieldWidth, updatedPosition.height), Mathf.Clamp(max, attr.min, attr.max));
  59. if (EditorGUI.EndChangeCheck())
  60. {
  61. range.x = min;
  62. range.y = max;
  63. valid = true;
  64. return range;
  65. }
  66. valid = false;
  67. return Vector2.zero;
  68. }
  69. private void Error(Rect position, GUIContent label)
  70. {
  71. EditorGUI.LabelField(position, label, new GUIContent("Use only with Vector2 or Limits2D"));
  72. }
  73. }
  74. }