Skip to content

Commit 1401ba3

Browse files
committed
Add new Timing01 data structure
1 parent 1a9c322 commit 1401ba3

File tree

7 files changed

+174
-12
lines changed

7 files changed

+174
-12
lines changed

.docfx/manual/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The **Animation Library** package contains assets and scripts for animating Unit
1919

2020
- [AnimatorParameter](xref:Zigurous.Animation.AnimatorParameter)
2121
- [Timing](xref:Zigurous.Animation.Timing)
22+
- [Timing01](xref:Zigurous.Animation.Timing01)
2223
- [TimingRange](xref:Zigurous.Animation.TimingRange)
2324
- [Vector2AnimationCurve](xref:Zigurous.Animation.Vector2AnimationCurve)
2425
- [Vector3AnimationCurve](xref:Zigurous.Animation.Vector3AnimationCurve)

Editor/Timing01PropertyDrawer.cs

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(Timing01))]
7+
public sealed class Timing01PropertyDrawer : PropertyDrawer
8+
{
9+
private const float horizontalSpacing = 4.0f;
10+
11+
private SerializedProperty _start;
12+
private SerializedProperty _end;
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 (_start == null) _start = property.FindPropertyRelative("_start");
31+
if (_end == null) _end = property.FindPropertyRelative("_end");
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(_start, rect, "Start");
39+
rect = SliderWithChangeCheck(_end, rect, "End");
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/Timing01PropertyDrawer.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/TimingPropertyDrawer.cs

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

11-
private SerializedProperty _startTime;
12-
private SerializedProperty _endTime;
11+
private SerializedProperty _start;
12+
private SerializedProperty _end;
1313

1414
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1515
{
@@ -27,16 +27,16 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2727
EditorGUI.indentLevel = 0;
2828

2929
// Create references to child fields
30-
if (_startTime == null) _startTime = property.FindPropertyRelative("startTime");
31-
if (_endTime == null) _endTime = property.FindPropertyRelative("endTime");
30+
if (_start == null) _start = property.FindPropertyRelative("start");
31+
if (_end == null) _end = property.FindPropertyRelative("end");
3232

3333
// Calculate the bounds of the child fields
3434
Rect rect = new Rect(position);
3535
rect.width = (position.width - horizontalSpacing) / 2;
3636

3737
// Draw the child fields
38-
rect = SliderWithChangeCheck(_startTime, rect, "Start");
39-
rect = SliderWithChangeCheck(_endTime, rect, "End");
38+
rect = FloatFieldWithChangeCheck(_start, rect);
39+
rect = FloatFieldWithChangeCheck(_end, rect);
4040

4141
// Set sizes back to their original values
4242
EditorGUI.indentLevel = originalIndent;
@@ -46,17 +46,17 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
4646
EditorGUI.EndProperty();
4747
}
4848

49-
private Rect SliderWithChangeCheck(SerializedProperty property, Rect position, string displayName)
49+
private Rect FloatFieldWithChangeCheck(SerializedProperty property, Rect position)
5050
{
5151
// Check for changes in the field's value
5252
EditorGUI.BeginChangeCheck();
5353

5454
// Calculate the width of the field label
55-
GUIContent label = new GUIContent(displayName);
55+
GUIContent label = new GUIContent(property.displayName);
5656
EditorGUIUtility.labelWidth = EditorStyles.label.CalcSize(label).x;
5757

5858
// Draw the field and store the field's value
59-
float value = EditorGUI.Slider(position, label, property.floatValue, 0.0f, 1.0f);
59+
float value = EditorGUI.FloatField(position, label, property.floatValue);
6060

6161
// Update the property's value from the field
6262
if (EditorGUI.EndChangeCheck()) {

Runtime/DataStructures/Timing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct Timing
2020
[Tooltip("The end time of the animation.")]
2121
public float end;
2222

23-
/// <summary>Constructs a new Timing with the given <paramref name="start"/> and <paramref name="end"/>.</summary>
23+
/// <summary>Constructs a new timing with the given <paramref name="start"/> and <paramref name="end"/>.</summary>
2424
/// <param name="start">The start time of the animation.</param>
2525
/// <param name="end">The end time of the animation.</param>
2626
public Timing(float start, float end)
@@ -37,7 +37,7 @@ public float Random()
3737
return UnityEngine.Random.Range(this.start, this.end);
3838
}
3939

40-
/// <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 start and end time.</returns>
4141
/// <param name="time">The time to check.</param>
4242
public bool Includes(float time)
4343
{

Runtime/DataStructures/Timing01.cs

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 the start and end time of an animation normalized between 0
7+
/// and 1.
8+
/// </summary>
9+
[System.Serializable]
10+
public struct Timing01
11+
{
12+
[SerializeField]
13+
[Tooltip("The start time of the animation, between 0 and 1.")]
14+
[Range(0.0f, 1.0f)]
15+
private float _start;
16+
17+
/// <summary>
18+
/// The start time of the animation, between 0 and 1.
19+
/// </summary>
20+
public float start
21+
{
22+
get => _start;
23+
set => _start = Mathf.Clamp01(value);
24+
}
25+
26+
[SerializeField]
27+
[Tooltip("The end time of the animation, between 0 and 1.")]
28+
[Range(0.0f, 1.0f)]
29+
private float _end;
30+
31+
/// <summary>
32+
/// The end time of the animation, between 0 and 1.
33+
/// </summary>
34+
public float end
35+
{
36+
get => _end;
37+
set => _end = Mathf.Clamp01(value);
38+
}
39+
40+
/// <summary>Constructs a new timing with the given <paramref name="start"/> and <paramref name="end"/>.</summary>
41+
/// <param name="start">The start time of the animation, between 0 and 1.</param>
42+
/// <param name="end">The end time of the animation, between 0 and 1.</param>
43+
public Timing01(float start, float end)
44+
{
45+
_start = Mathf.Clamp01(start);
46+
_end = Mathf.Clamp01(end);
47+
}
48+
49+
/// <returns>
50+
/// A random time within the start and end time.
51+
/// </returns>
52+
public float Random()
53+
{
54+
return UnityEngine.Random.Range(this.start, this.end);
55+
}
56+
57+
/// <returns>Whether the given <paramref name="time"/> is within the start and end time.</returns>
58+
/// <param name="time">The time to check.</param>
59+
public bool Includes(float time)
60+
{
61+
return time >= this.start && time <= this.end;
62+
}
63+
64+
}
65+
66+
}

Runtime/DataStructures/Timing01.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)