ValueDraggerEditor.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace TheraBytes.BetterUi.Editor
  9. {
  10. [CustomEditor(typeof(ValueDragger)), CanEditMultipleObjects]
  11. public class ValueDraggerEditor : UnityEditor.Editor
  12. {
  13. SerializedProperty
  14. fallbackDragSettings, customDragSettings,
  15. fallbackValueSettings, customValueSettings,
  16. fallbackDragDistance, customDragDistance,
  17. value, onValueChanged;
  18. // as ValueDragger derives from BetterSelectable, we need to use this.
  19. BetterElementHelper<Selectable, BetterSelectable> helper =
  20. new BetterElementHelper<Selectable, BetterSelectable>();
  21. void OnEnable()
  22. {
  23. fallbackDragSettings = serializedObject.FindProperty("fallbackDragSettings");
  24. customDragSettings = serializedObject.FindProperty("customDragSettings");
  25. fallbackValueSettings = serializedObject.FindProperty("fallbackValueSettings");
  26. customValueSettings = serializedObject.FindProperty("customValueSettings");
  27. fallbackDragDistance = serializedObject.FindProperty("fallbackDragDistance");
  28. customDragDistance = serializedObject.FindProperty("customDragDistance");
  29. value = serializedObject.FindProperty("value");
  30. onValueChanged = serializedObject.FindProperty("onValueChanged");
  31. }
  32. public override void OnInspectorGUI()
  33. {
  34. ScreenConfigConnectionHelper.DrawGui("Drag Settings",
  35. customDragSettings, ref fallbackDragSettings, DrawDragSettings);
  36. ScreenConfigConnectionHelper.DrawGui("Value Settings",
  37. customValueSettings, ref fallbackValueSettings, DrawValueSettings);
  38. ScreenConfigConnectionHelper.DrawSizerGui("Drag Distance per Integer Step",
  39. customDragDistance, ref fallbackDragDistance);
  40. EditorGUILayout.PropertyField(value);
  41. EditorGUILayout.PropertyField(onValueChanged);
  42. helper.DrawGui(serializedObject);
  43. serializedObject.ApplyModifiedProperties();
  44. }
  45. private void DrawDragSettings(string configName, SerializedProperty prop)
  46. {
  47. var direction = prop.FindPropertyRelative("Direction");
  48. var invert = prop.FindPropertyRelative("Invert");
  49. EditorGUILayout.PropertyField(direction);
  50. EditorGUILayout.PropertyField(invert);
  51. }
  52. private void DrawValueSettings(string configName, SerializedProperty prop)
  53. {
  54. var hasMinValue = prop.FindPropertyRelative("HasMinValue");
  55. var minValue = prop.FindPropertyRelative("MinValue");
  56. var hasMaxValue = prop.FindPropertyRelative("HasMaxValue");
  57. var maxValue = prop.FindPropertyRelative("MaxValue");
  58. var wholeNumbers = prop.FindPropertyRelative("WholeNumbers");
  59. DrawCheckboxField("Min Value", hasMinValue, minValue);
  60. DrawCheckboxField("Max Value", hasMaxValue, maxValue);
  61. EditorGUILayout.Space();
  62. EditorGUILayout.PropertyField(wholeNumbers);
  63. }
  64. private static void DrawCheckboxField(string label, SerializedProperty checkValue, SerializedProperty fieldValue)
  65. {
  66. var rect = EditorGUILayout.GetControlRect();
  67. var checkRect = checkValue.boolValue
  68. ? new Rect(rect.x, rect.y, EditorGUIUtility.labelWidth, rect.height)
  69. : rect;
  70. checkValue.boolValue = EditorGUI.ToggleLeft(checkRect, label, checkValue.boolValue);
  71. if (checkValue.boolValue)
  72. {
  73. var fieldRect = new Rect(checkRect.xMax, rect.y, rect.xMax - checkRect.xMax, rect.height);
  74. EditorGUI.PropertyField(fieldRect, fieldValue, GUIContent.none);
  75. }
  76. }
  77. }
  78. }