@@ -6,6 +6,61 @@ namespace Toolbox.Editor.Drawers
66 [CustomPropertyDrawer(typeof(ProgressBarAttribute))]
77 public class ProgressBarAttributeDrawer : PropertyDrawerBase
88 {
9+ private static readonly int drawerHash = nameof(ProgressBarAttributeDrawer).GetHashCode();
10+
11+ private void HandleGuiEvents(SerializedProperty property, Rect progressBarRect)
12+ {
13+ var mousePosition = Event.current.mousePosition;
14+ var id = GUIUtility.GetControlID(drawerHash, FocusType.Passive, progressBarRect);
15+ switch (Event.current.GetTypeForControl(id))
16+ {
17+ case EventType.MouseDown:
18+ if (progressBarRect.Contains(mousePosition))
19+ {
20+ GUIUtility.hotControl = id;
21+ SetProgressValue(property, progressBarRect, mousePosition.x);
22+ Event.current.Use();
23+ }
24+ break;
25+ case EventType.MouseDrag:
26+ if (GUIUtility.hotControl == id)
27+ {
28+ SetProgressValue(property, progressBarRect, mousePosition.x);
29+ Event.current.Use();
30+ }
31+ break;
32+ case EventType.MouseUp:
33+ if (GUIUtility.hotControl == id)
34+ {
35+ GUIUtility.hotControl = 0;
36+ Event.current.Use();
37+ }
38+ break;
39+ }
40+ }
41+
42+ private void SetProgressValue(SerializedProperty property, Rect progressBarRect, float xPosition)
43+ {
44+ var minValue = Attribute.MinValue;
45+ var maxValue = Attribute.MaxValue;
46+
47+ var range = progressBarRect.xMax - progressBarRect.xMin;
48+ xPosition = Mathf.Clamp(xPosition - progressBarRect.xMin, 0, range);
49+
50+ var fill = Mathf.Clamp01(xPosition / range);
51+ var newValue = (maxValue - minValue) * fill + minValue;
52+
53+ switch (property.propertyType)
54+ {
55+ case SerializedPropertyType.Integer:
56+ property.intValue = Mathf.RoundToInt(newValue);
57+ break;
58+ case SerializedPropertyType.Float:
59+ property.floatValue = newValue;
60+ break;
61+ }
62+ }
63+
964 protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
1065 {
1166 return Style.barHeight;
@@ -51,19 +106,23 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
51106 position.y -= Style.textOffset;
52107 //finally draw the progress bar label
53108 EditorGUI.DropShadowLabel(position, labelText);
54- }
55109
110+ if (!attribute.IsInteractable)
111+ {
112+ return;
113+ }
114+
115+ HandleGuiEvents(property, position);
116+ }
56117
57118 public override bool IsPropertyValid(SerializedProperty property)
58119 {
59120 return property.propertyType == SerializedPropertyType.Float ||
60121 property.propertyType == SerializedPropertyType.Integer;
61122 }
62123
63-
64124 private ProgressBarAttribute Attribute => attribute as ProgressBarAttribute;
65125
66-
67126 private static class Style
68127 {
69128 internal static readonly float rowHeight = EditorGUIUtility.singleLineHeight;
0 commit comments