Skip to content

Commit

Permalink
feat: added the abstracted interval logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kurone-kito committed Jul 19, 2024
1 parent f4351c9 commit cca1088
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 75 deletions.
52 changes: 52 additions & 0 deletions Packages/black.kit.toybox/Runtime/Scripts/UI/IntervalBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using UdonSharp;
using UnityEngine;

namespace black.kit.toybox
{
/// <summary>The base class of the interval logic.</summary>
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public abstract class IntervalBase : UdonSharpBehaviour
{
/// <summary>The safe minimum interval.</summary>
private const float SAFE_MIN_INTERVAL = 0.01f;

/// <summary>The warning of the null.</summary>
private const string WARN_NULL = "Some inspector values is null.";

/// <summary>The interval of the progress.</summary>
/// <remarks>It ignores less than 0.01f.</remarks>
[SerializeField, Range(SAFE_MIN_INTERVAL, 60f)]
[Tooltip("Specifies the interval of the progress")]
private float interval = 1f;

/// <summary>The interval of the progress.</summary>
public float Interval => interval;

/// <summary>Update the view of the UI.</summary>
public void InternalUpdateView()
{
UpdateView();
var safeInterval = Mathf.Max(Interval, SAFE_MIN_INTERVAL);
SendCustomEventDelayedSeconds(nameof(InternalUpdateView), safeInterval);
}

/// <summary>Update the view of the UI.</summary>
[ContextMenu("Update view")]
public abstract void UpdateView();

/// <summary>Validate the inspector.</summary>
/// <returns>Whether the inspectors are valid.</returns>
protected abstract bool ValidateInspector();

/// <summary>The callback when the object is initialized.</summary>
protected virtual void Start()
{
if (!ValidateInspector())
{
Debug.LogWarning(WARN_NULL);
return;
}
SendCustomEventDelayedFrames(nameof(InternalUpdateView), 1);
}
}
}
11 changes: 11 additions & 0 deletions Packages/black.kit.toybox/Runtime/Scripts/UI/IntervalBase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 28 additions & 29 deletions Packages/black.kit.toybox/Runtime/Scripts/UI/TickingDown.asset
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: format
Data: interval
- Name: $v
Entry: 7
Data: 2|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: format
Data: interval
- Name: <UserType>k__BackingField
Entry: 7
Data: 3|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.String, mscorlib
Data: System.Single, mscorlib
- Name:
Entry: 8
Data:
Expand All @@ -85,7 +85,7 @@ MonoBehaviour:
Data: 4|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 2
Data: 3
- Name:
Entry: 7
Data: 5|UnityEngine.SerializeField, UnityEngine.CoreModule
Expand All @@ -94,10 +94,22 @@ MonoBehaviour:
Data:
- Name:
Entry: 7
Data: 6|UnityEngine.TooltipAttribute, UnityEngine.CoreModule
Data: 6|UnityEngine.RangeAttribute, UnityEngine.CoreModule
- Name: min
Entry: 4
Data: 0.01
- Name: max
Entry: 4
Data: 60
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data: 7|UnityEngine.TooltipAttribute, UnityEngine.CoreModule
- Name: tooltip
Entry: 1
Data: Specify the format to display the date and time.
Data: Specifies the interval of the progress
- Name:
Entry: 8
Data:
Expand All @@ -118,25 +130,25 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: interval
Data: format
- Name: $v
Entry: 7
Data: 7|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 8|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: interval
Data: format
- Name: <UserType>k__BackingField
Entry: 7
Data: 8|System.RuntimeType, mscorlib
Data: 9|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Single, mscorlib
Data: System.String, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 8
Data: 9
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
Expand All @@ -151,25 +163,13 @@ MonoBehaviour:
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 9|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 10|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 3
- Name:
Entry: 7
Data: 10|UnityEngine.SerializeField, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
Data: 2
- Name:
Entry: 7
Data: 11|UnityEngine.RangeAttribute, UnityEngine.CoreModule
- Name: min
Entry: 4
Data: 0.01
- Name: max
Entry: 4
Data: 60
Data: 11|UnityEngine.SerializeField, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
Expand All @@ -178,8 +178,7 @@ MonoBehaviour:
Data: 12|UnityEngine.TooltipAttribute, UnityEngine.CoreModule
- Name: tooltip
Entry: 1
Data: Specify the interval to update the date and time by seconds. It ignores
less than 0.01f.
Data: Specify the format to display the date and time.
- Name:
Entry: 8
Data:
Expand Down
52 changes: 6 additions & 46 deletions Packages/black.kit.toybox/Runtime/Scripts/UI/TickingDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,20 @@ namespace black.kit.toybox
/// </remarks>
[AddComponentMenu("UdonSharp Toybox/UI/Ticking Down")]
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public sealed class TickingDown : UdonSharpBehaviour
public sealed class TickingDown : IntervalBase
{
/// <summary>The property name of the format.</summary>
public const string NAME_TEXT = nameof(text);

/// <summary>The property name of the format.</summary>
public const string NAME_TEXT_MESH = nameof(textMesh);

/// <summary>
/// The warning message when the Text component is not found.
/// </summary>
private const string WARN_NO_TEXT = "Text component is not found.";

#pragma warning disable IDE0044
/// <summary>The format to display the date and time.</summary>
[SerializeField]
[Tooltip("Specify the format to display the date and time.")]
private string format = "yyyy-MM-ddTHH:mm:ss";

/// <summary>The interval to update the date and time.</summary>
/// <remarks>It ignores less than 0.01f.</remarks>
[SerializeField, Range(0.01f, 60f)]
[Tooltip("Specify the interval to update the date and time by seconds. It ignores less than 0.01f.")]
private float interval = 1f;

/// <summary>
/// The text component to display the date and time.
/// </summary>
Expand All @@ -78,18 +67,9 @@ public sealed class TickingDown : UdonSharpBehaviour
private TextMeshProUGUI textMesh;
#pragma warning restore IDE0044

/// <summary>The callback to update the date and time.</summary>
/// <remarks>
/// This method is automatically called from within.
/// <em>DO NOT CALL IT DIRECTLY</em>; it'll duplicate the calling
/// cycle and can result in overloading.
/// </remarks>
public void Tick()
/// <summary>Update the view of the UI.</summary>
public override void UpdateView()
{
if (DetectNull())
{
return;
}
var now = DateTime.Now.ToString(format);
if (text)
{
Expand All @@ -99,30 +79,10 @@ public void Tick()
{
textMesh.text = now;
}
var safeInterval = Mathf.Max(interval, 0.01f);
SendCustomEventDelayedSeconds(nameof(Tick), safeInterval);
}

/// <summary>
/// Determine whether the Text or TextMeshProUGUI component is null.
/// </summary>
/// <returns>
/// <c>true</c> if the Text or TextMeshProUGUI component is null;
/// otherwise, <c>false</c>.
/// </returns>
private bool DetectNull()
{
var result = !(text || textMesh);
if (result)
{
Log.Warn(WARN_NO_TEXT);
}
return result;
}

#pragma warning disable IDE0051
/// <summary>The callback when the object is enabled.</summary>
void Start() => Tick();
#pragma warning restore IDE0051
/// <summary>Validate the inspector.</summary>
/// <returns>Whether the inspectors are valid.</returns>
protected override bool ValidateInspector() => text || textMesh;
}
}

0 comments on commit cca1088

Please sign in to comment.