Skip to content

Commit 6e65944

Browse files
committed
Add new TimingRange01 data structure
1 parent 1401ba3 commit 6e65944

File tree

7 files changed

+180
-34
lines changed

7 files changed

+180
-34
lines changed

.docfx/manual/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The **Animation Library** package contains assets and scripts for animating Unit
2121
- [Timing](xref:Zigurous.Animation.Timing)
2222
- [Timing01](xref:Zigurous.Animation.Timing01)
2323
- [TimingRange](xref:Zigurous.Animation.TimingRange)
24+
- [TimingRange01](xref:Zigurous.Animation.TimingRange01)
2425
- [Vector2AnimationCurve](xref:Zigurous.Animation.Vector2AnimationCurve)
2526
- [Vector3AnimationCurve](xref:Zigurous.Animation.Vector3AnimationCurve)
2627
- [Vector4AnimationCurve](xref:Zigurous.Animation.Vector4AnimationCurve)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.Animation.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(TimingRange01))]
7+
public sealed class TimingRange01PropertyDrawer : PropertyDrawer
8+
{
9+
private const float horizontalSpacing = 4.0f;
10+
11+
private SerializedProperty _min;
12+
private SerializedProperty _max;
13+
14+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
15+
{
16+
// Start drawing the property
17+
EditorGUI.BeginProperty(position, label, property);
18+
19+
// Draw the property label
20+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
21+
22+
// Store orginal sizes so they can be set back to their original values
23+
int originalIndent = EditorGUI.indentLevel;
24+
float originalLabelWidth = EditorGUIUtility.labelWidth;
25+
26+
// Set indent level of child fields
27+
EditorGUI.indentLevel = 0;
28+
29+
// Create references to child fields
30+
if (_min == null) _min = property.FindPropertyRelative("_min");
31+
if (_max == null) _max = property.FindPropertyRelative("_max");
32+
33+
// Calculate the bounds of the child fields
34+
Rect rect = new Rect(position);
35+
rect.width = (position.width - horizontalSpacing) / 2;
36+
37+
// Draw the child fields
38+
rect = SliderWithChangeCheck(_min, rect, "Min");
39+
rect = SliderWithChangeCheck(_max, rect, "Max");
40+
41+
// Set sizes back to their original values
42+
EditorGUI.indentLevel = originalIndent;
43+
EditorGUIUtility.labelWidth = originalLabelWidth;
44+
45+
// Finish drawing the property
46+
EditorGUI.EndProperty();
47+
}
48+
49+
private Rect SliderWithChangeCheck(SerializedProperty property, Rect position, string displayName)
50+
{
51+
// Check for changes in the field's value
52+
EditorGUI.BeginChangeCheck();
53+
54+
// Calculate the width of the field label
55+
GUIContent label = new GUIContent(displayName);
56+
EditorGUIUtility.labelWidth = EditorStyles.label.CalcSize(label).x;
57+
58+
// Draw the field and store the field's value
59+
float value = EditorGUI.Slider(position, label, property.floatValue, 0.0f, 1.0f);
60+
61+
// Update the property's value from the field
62+
if (EditorGUI.EndChangeCheck()) {
63+
property.floatValue = value;
64+
}
65+
66+
// Advance the position for the next child field
67+
position.x += position.width + horizontalSpacing;
68+
return position;
69+
}
70+
71+
}
72+
73+
}

Editor/TimingRange01PropertyDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/TimingRangePropertyDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Zigurous.Animation.Editor
66
[CustomPropertyDrawer(typeof(TimingRange))]
77
public sealed class TimingRangePropertyDrawer : PropertyDrawer
88
{
9-
private static readonly float horizontalSpacing = 4.0f;
9+
private const float horizontalSpacing = 4.0f;
1010

1111
private SerializedProperty _min;
1212
private SerializedProperty _max;

Runtime/DataStructures/TimingRange.cs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,45 @@
33
namespace Zigurous.Animation
44
{
55
/// <summary>
6-
/// Represents an animation timing range normalized between 0 and 1.
6+
/// Represents an animation timing range between a lower and upper bound.
77
/// </summary>
88
[System.Serializable]
99
public struct TimingRange
1010
{
11-
[SerializeField]
12-
[Tooltip("The start time of the animation, between 0 and 1.")]
13-
[Range(0.0f, 1.0f)]
14-
private float _start;
15-
1611
/// <summary>
17-
/// The start time of the animation, between 0 and 1.
12+
/// The lower bound of the timing range.
1813
/// </summary>
19-
public float start
20-
{
21-
get => _start;
22-
set => _start = Mathf.Clamp01(value);
23-
}
24-
25-
[SerializeField]
26-
[Tooltip("The end time of the animation, between 0 and 1.")]
27-
[Range(0.0f, 1.0f)]
28-
private float _end;
14+
[Tooltip("The lower bound of the timing range.")]
15+
public float min;
2916

3017
/// <summary>
31-
/// The end time of the animation, between 0 and 1.
18+
/// The upper bound of the timing range.
3219
/// </summary>
33-
public float end
34-
{
35-
get => _end;
36-
set => _end = Mathf.Clamp01(value);
37-
}
20+
[Tooltip("The upper bound of the timing range.")]
21+
public float max;
3822

39-
/// <summary>Constructs a new TimingRange with the given <paramref name="start"/> and <paramref name="end"/>.</summary>
40-
/// <param name="start">The start time of the animation, between 0 and 1.</param>
41-
/// <param name="end">The end time of the animation, between 0 and 1.</param>
42-
public TimingRange(float start, float end)
23+
/// <summary>Constructs a new timing range with the given <paramref name="min"/> and <paramref name="max"/>.</summary>
24+
/// <param name="min">The lower bound of the timing range.</param>
25+
/// <param name="max">The upper bound of the timing range.</param>
26+
public TimingRange(float min, float max)
4327
{
44-
_start = Mathf.Clamp01(start);
45-
_end = Mathf.Clamp01(end);
28+
this.min = min;
29+
this.max = max;
4630
}
4731

4832
/// <returns>
49-
/// A random time within the start and end time.
33+
/// A random time within the min and max time.
5034
/// </returns>
5135
public float Random()
5236
{
53-
return UnityEngine.Random.Range(this.start, this.end);
37+
return UnityEngine.Random.Range(this.min, this.max);
5438
}
5539

56-
/// <returns>Whether the given <paramref name="time"/> is within range of the animation timing.</returns>
40+
/// <returns>Whether the given <paramref name="time"/> is within the min and max time.</returns>
5741
/// <param name="time">The time to check.</param>
5842
public bool Includes(float time)
5943
{
60-
return time >= this.start && time <= this.end;
44+
return time >= this.min && time <= this.max;
6145
}
6246

6347
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Animation
4+
{
5+
/// <summary>
6+
/// Represents an animation timing range between a lower and upper bound
7+
/// normalized between 0 and 1.
8+
/// </summary>
9+
[System.Serializable]
10+
public struct TimingRange01
11+
{
12+
[SerializeField]
13+
[Tooltip("The lower bound of the timing range, between 0 and 1.")]
14+
[Range(0.0f, 1.0f)]
15+
private float _min;
16+
17+
/// <summary>
18+
/// The lower bound of the timing range, between 0 and 1.
19+
/// </summary>
20+
public float min
21+
{
22+
get => _min;
23+
set => _min = Mathf.Clamp01(value);
24+
}
25+
26+
[SerializeField]
27+
[Tooltip("The upper bound of the timing range, between 0 and 1.")]
28+
[Range(0.0f, 1.0f)]
29+
private float _max;
30+
31+
/// <summary>
32+
/// The upper bound of the timing range, between 0 and 1.
33+
/// </summary>
34+
public float max
35+
{
36+
get => _max;
37+
set => _max = Mathf.Clamp01(value);
38+
}
39+
40+
/// <summary>Constructs a new timing range with the given <paramref name="min"/> and <paramref name="max"/>.</summary>
41+
/// <param name="min">The lower bound of the timing range, between 0 and 1.</param>
42+
/// <param name="max">The upper bound of the timing range, between 0 and 1.</param>
43+
public TimingRange01(float min, float max)
44+
{
45+
_min = Mathf.Clamp01(min);
46+
_max = Mathf.Clamp01(max);
47+
}
48+
49+
/// <returns>
50+
/// A random time within the min and max time.
51+
/// </returns>
52+
public float Random()
53+
{
54+
return UnityEngine.Random.Range(this.min, this.max);
55+
}
56+
57+
/// <returns>Whether the given <paramref name="time"/> is within the min and max time.</returns>
58+
/// <param name="time">The time to check.</param>
59+
public bool Includes(float time)
60+
{
61+
return time >= this.min && time <= this.max;
62+
}
63+
64+
}
65+
66+
}

Runtime/DataStructures/TimingRange01.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)