ScrollerEditor.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * FancyScrollView (https://github.com/setchi/FancyScrollView)
  3. * Copyright (c) 2020 setchi
  4. * Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
  5. */
  6. using UnityEditor;
  7. using UnityEditor.AnimatedValues;
  8. // For manteinance, every new [SerializeField] variable in Scroller must be declared here
  9. namespace FancyScrollView
  10. {
  11. [CustomEditor(typeof(Scroller))]
  12. [CanEditMultipleObjects]
  13. public class ScrollerEditor : Editor
  14. {
  15. SerializedProperty viewport;
  16. SerializedProperty scrollDirection;
  17. SerializedProperty movementType;
  18. SerializedProperty elasticity;
  19. SerializedProperty scrollSensitivity;
  20. SerializedProperty inertia;
  21. SerializedProperty decelerationRate;
  22. SerializedProperty snap;
  23. SerializedProperty draggable;
  24. SerializedProperty scrollbar;
  25. AnimBool showElasticity;
  26. AnimBool showInertiaRelatedValues;
  27. void OnEnable()
  28. {
  29. viewport = serializedObject.FindProperty("viewport");
  30. scrollDirection = serializedObject.FindProperty("scrollDirection");
  31. movementType = serializedObject.FindProperty("movementType");
  32. elasticity = serializedObject.FindProperty("elasticity");
  33. scrollSensitivity = serializedObject.FindProperty("scrollSensitivity");
  34. inertia = serializedObject.FindProperty("inertia");
  35. decelerationRate = serializedObject.FindProperty("decelerationRate");
  36. snap = serializedObject.FindProperty("snap");
  37. draggable = serializedObject.FindProperty("draggable");
  38. scrollbar = serializedObject.FindProperty("scrollbar");
  39. showElasticity = new AnimBool(Repaint);
  40. showInertiaRelatedValues = new AnimBool(Repaint);
  41. SetAnimBools(true);
  42. }
  43. void OnDisable()
  44. {
  45. showElasticity.valueChanged.RemoveListener(Repaint);
  46. showInertiaRelatedValues.valueChanged.RemoveListener(Repaint);
  47. }
  48. void SetAnimBools(bool instant)
  49. {
  50. SetAnimBool(showElasticity, !movementType.hasMultipleDifferentValues && movementType.enumValueIndex == (int)MovementType.Elastic, instant);
  51. SetAnimBool(showInertiaRelatedValues, !inertia.hasMultipleDifferentValues && inertia.boolValue, instant);
  52. }
  53. void SetAnimBool(AnimBool a, bool value, bool instant)
  54. {
  55. if (instant)
  56. {
  57. a.value = value;
  58. }
  59. else
  60. {
  61. a.target = value;
  62. }
  63. }
  64. public override void OnInspectorGUI()
  65. {
  66. SetAnimBools(false);
  67. serializedObject.Update();
  68. EditorGUILayout.PropertyField(viewport);
  69. EditorGUILayout.PropertyField(scrollDirection);
  70. EditorGUILayout.PropertyField(movementType);
  71. DrawMovementTypeRelatedValue();
  72. EditorGUILayout.PropertyField(scrollSensitivity);
  73. EditorGUILayout.PropertyField(inertia);
  74. DrawInertiaRelatedValues();
  75. EditorGUILayout.PropertyField(draggable);
  76. EditorGUILayout.PropertyField(scrollbar);
  77. serializedObject.ApplyModifiedProperties();
  78. }
  79. void DrawMovementTypeRelatedValue()
  80. {
  81. using (var group = new EditorGUILayout.FadeGroupScope(showElasticity.faded))
  82. {
  83. if (!group.visible)
  84. {
  85. return;
  86. }
  87. using (new EditorGUI.IndentLevelScope())
  88. {
  89. EditorGUILayout.PropertyField(elasticity);
  90. }
  91. }
  92. }
  93. void DrawInertiaRelatedValues()
  94. {
  95. using (var group = new EditorGUILayout.FadeGroupScope(showInertiaRelatedValues.faded))
  96. {
  97. if (!group.visible)
  98. {
  99. return;
  100. }
  101. using (new EditorGUI.IndentLevelScope())
  102. {
  103. EditorGUILayout.PropertyField(decelerationRate);
  104. EditorGUILayout.PropertyField(snap);
  105. }
  106. }
  107. }
  108. }
  109. }