Skip to content

Commit

Permalink
Merge pull request #14 from theowiik/property-fixes
Browse files Browse the repository at this point in the history
Add timer factory
  • Loading branch information
theowiik authored Sep 17, 2023
2 parents 78d5dbb + 5fde931 commit a972517
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion GodotSharper.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xml:space="preserve">
<s:Boolean
Expand Down
2 changes: 1 addition & 1 deletion GodotSharper/AutoGetNode/GetNodeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class GetNodeAttribute : Attribute
{
private readonly string _path;

private GetNodeAttribute(string nodePath)
public GetNodeAttribute(string nodePath)
{
_path = nodePath;
}
Expand Down
52 changes: 52 additions & 0 deletions GodotSharper/TimerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Timer = Godot.Timer;

namespace GodotSharper;

/// <summary>
/// A factory class for creating Timer objects with various configurations.
/// </summary>
public static class TimerFactory
{
/// <summary>
/// Creates a new Timer object that starts automatically and triggers only once after the specified wait time. Note:
/// also destroys the timer after it triggers!
/// </summary>
/// <param name="waitTime">The time to wait before triggering the timer, in seconds.</param>
/// <param name="onTimeout">An optional action to execute when the timer triggers.</param>
/// <returns>The newly created Timer object.</returns>
public static Timer StartedOneShot(double waitTime, Action onTimeout = null)
{
var timer = InternalCreateTimer(true, true, waitTime);
timer.Timeout += () =>
{
onTimeout?.Invoke();
timer.QueueFree();
};

return timer;
}

/// <summary>
/// Creates a new Timer object that starts automatically and triggers repeatedly after the specified wait time.
/// </summary>
/// <param name="waitTime">The time to wait before each trigger of the timer, in seconds.</param>
/// <param name="onTimeout">An optional action to execute when the timer triggers.</param>
/// <returns>The newly created Timer object.</returns>
public static Timer StartedRepeating(double waitTime, Action onTimeout = null)
{
var timer = InternalCreateTimer(true, false, waitTime);
timer.Timeout += () => onTimeout?.Invoke();

return timer;
}

private static Timer InternalCreateTimer(bool autostart, bool oneShot, double waitTime)
{
return new Timer
{
Autostart = autostart,
OneShot = oneShot,
WaitTime = waitTime
};
}
}

0 comments on commit a972517

Please sign in to comment.