Skip to content

Commit

Permalink
feat: Improve progress bar UI
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarianZ authored May 4, 2023
1 parent 4a3a191 commit 3d47520
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
13 changes: 8 additions & 5 deletions Editor/Scripts/Node/AnimationClipPlayableNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using GBG.PlayableGraphMonitor.Editor.GraphView;
using GBG.PlayableGraphMonitor.Editor.Utility;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Animations;
Expand Down Expand Up @@ -59,16 +60,18 @@ protected override void OnUpdate(PlayableGraphViewUpdateContext updateContext, b
{
if (clip.isLooping)
{
var progress = (float)(Playable.GetTime() / clip.length % 1.0f * 100);
var progress = Playable.GetTime() / clip.length;
progress = GraphTool.Wrap01(progress) * 100;
// Expensive operations
_progressBar.SetValueWithoutNotify(progress);
_progressBar.SetValueWithoutNotify((float)progress);
}
else
{
var progress = (float)(Playable.GetTime() / clip.length * 100);
progress = Mathf.Clamp(progress, 0, 100);
var progress = Playable.GetTime() / clip.length;
progress = Mathf.Clamp((float)progress, -1, 1);
progress = GraphTool.Wrap01(progress) * 100;
// Expensive operations
_progressBar.SetValueWithoutNotify(progress);
_progressBar.SetValueWithoutNotify((float)progress);
}
}
else
Expand Down
13 changes: 8 additions & 5 deletions Editor/Scripts/Node/AudioClipPlayableNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using GBG.PlayableGraphMonitor.Editor.GraphView;
using GBG.PlayableGraphMonitor.Editor.Utility;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Audio;
Expand Down Expand Up @@ -58,16 +59,18 @@ protected override void OnUpdate(PlayableGraphViewUpdateContext updateContext, b
{
if (clipPlayable.GetLooped())
{
var progress = (float)(Playable.GetTime() / clip.length % 1.0f * 100);
var progress = Playable.GetTime() / clip.length;
progress = GraphTool.Wrap01(progress) * 100;
// Expensive operations
_progressBar.SetValueWithoutNotify(progress);
_progressBar.SetValueWithoutNotify((float)progress);
}
else
{
var progress = (float)(Playable.GetTime() / clip.length * 100);
progress = Mathf.Clamp(progress, 0, 100);
var progress = Playable.GetTime() / clip.length;
progress = Mathf.Clamp((float)progress, -1, 1);
progress = GraphTool.Wrap01(progress) * 100;
// Expensive operations
_progressBar.SetValueWithoutNotify(progress);
_progressBar.SetValueWithoutNotify((float)progress);
}
}
else
Expand Down
34 changes: 34 additions & 0 deletions Editor/Scripts/Utility/GraphTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ public static string DurationToString(this Playable playable, string format = "F

return durationStr;
}

/// <summary>
/// Wrap the value to the range of [0,1].
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static double Wrap01(double value)
{
if (value < 0)
{
var result = 1.0 + value % 1;
// Prevent wrapping -0 to 1
if (result == 1)
{
result = 0;
}

return result;
}

if (value > 1)
{
var result = value % 1;
// Prevent wrapping 1 to 0
if (result == 0)
{
result = 1;
}

return result;
}

return value;
}

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

0 comments on commit 3d47520

Please sign in to comment.