Skip to content

Commit 3d47520

Browse files
authored
feat: Improve progress bar UI
1 parent 4a3a191 commit 3d47520

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

Editor/Scripts/Node/AnimationClipPlayableNode.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
22
using GBG.PlayableGraphMonitor.Editor.GraphView;
3+
using GBG.PlayableGraphMonitor.Editor.Utility;
34
using UnityEditor.UIElements;
45
using UnityEngine;
56
using UnityEngine.Animations;
@@ -59,16 +60,18 @@ protected override void OnUpdate(PlayableGraphViewUpdateContext updateContext, b
5960
{
6061
if (clip.isLooping)
6162
{
62-
var progress = (float)(Playable.GetTime() / clip.length % 1.0f * 100);
63+
var progress = Playable.GetTime() / clip.length;
64+
progress = GraphTool.Wrap01(progress) * 100;
6365
// Expensive operations
64-
_progressBar.SetValueWithoutNotify(progress);
66+
_progressBar.SetValueWithoutNotify((float)progress);
6567
}
6668
else
6769
{
68-
var progress = (float)(Playable.GetTime() / clip.length * 100);
69-
progress = Mathf.Clamp(progress, 0, 100);
70+
var progress = Playable.GetTime() / clip.length;
71+
progress = Mathf.Clamp((float)progress, -1, 1);
72+
progress = GraphTool.Wrap01(progress) * 100;
7073
// Expensive operations
71-
_progressBar.SetValueWithoutNotify(progress);
74+
_progressBar.SetValueWithoutNotify((float)progress);
7275
}
7376
}
7477
else

Editor/Scripts/Node/AudioClipPlayableNode.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
22
using GBG.PlayableGraphMonitor.Editor.GraphView;
3+
using GBG.PlayableGraphMonitor.Editor.Utility;
34
using UnityEditor.UIElements;
45
using UnityEngine;
56
using UnityEngine.Audio;
@@ -58,16 +59,18 @@ protected override void OnUpdate(PlayableGraphViewUpdateContext updateContext, b
5859
{
5960
if (clipPlayable.GetLooped())
6061
{
61-
var progress = (float)(Playable.GetTime() / clip.length % 1.0f * 100);
62+
var progress = Playable.GetTime() / clip.length;
63+
progress = GraphTool.Wrap01(progress) * 100;
6264
// Expensive operations
63-
_progressBar.SetValueWithoutNotify(progress);
65+
_progressBar.SetValueWithoutNotify((float)progress);
6466
}
6567
else
6668
{
67-
var progress = (float)(Playable.GetTime() / clip.length * 100);
68-
progress = Mathf.Clamp(progress, 0, 100);
69+
var progress = Playable.GetTime() / clip.length;
70+
progress = Mathf.Clamp((float)progress, -1, 1);
71+
progress = GraphTool.Wrap01(progress) * 100;
6972
// Expensive operations
70-
_progressBar.SetValueWithoutNotify(progress);
73+
_progressBar.SetValueWithoutNotify((float)progress);
7174
}
7275
}
7376
else

Editor/Scripts/Utility/GraphTool.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,40 @@ public static string DurationToString(this Playable playable, string format = "F
4040

4141
return durationStr;
4242
}
43+
44+
/// <summary>
45+
/// Wrap the value to the range of [0,1].
46+
/// </summary>
47+
/// <param name="value"></param>
48+
/// <returns></returns>
49+
public static double Wrap01(double value)
50+
{
51+
if (value < 0)
52+
{
53+
var result = 1.0 + value % 1;
54+
// Prevent wrapping -0 to 1
55+
if (result == 1)
56+
{
57+
result = 0;
58+
}
59+
60+
return result;
61+
}
62+
63+
if (value > 1)
64+
{
65+
var result = value % 1;
66+
// Prevent wrapping 1 to 0
67+
if (result == 0)
68+
{
69+
result = 1;
70+
}
71+
72+
return result;
73+
}
74+
75+
return value;
76+
}
4377

4478
public static void SetNodeStyle(this GraphViewNode node, Color nodeColor,
4579
float titleFontSize = 15, Color? titleColor = null)

0 commit comments

Comments
 (0)