Skip to content

Commit

Permalink
Rename IsCompleted to IsRunning (#127)
Browse files Browse the repository at this point in the history
* Renamed IsComplete over to IsRunning
Also swapped to a BindingMode.OneWayToSource to allow this to provide values out to the VM or other controls in bindings with it not intended to control the running of the animation itself

* Add xml docs

* Update the docs
  • Loading branch information
bijington authored Jul 7, 2022
1 parent d6b3412 commit ce6c5c0
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 95 deletions.
6 changes: 3 additions & 3 deletions docs/api/ui-forms/skconfettiview.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The main property of a confetti view is the `Systems` property:
| :--------------------- | :---------------------------- | :---------- |
| **Systems** | `SKConfettiSystemCollection` | The collection of [systems](#system) in the view. |
| **IsAnimationEnabled** | `bool` | Determines whether the control will play the animation provided. |
| **IsComplete** | `bool` | A value that indicates whether all systems are complete. |
| **IsRunning** | `bool` | Determines whether the control is currently rendering confetti. |

## Parts

Expand Down Expand Up @@ -52,7 +52,7 @@ Every confetti view consists up one or more systems (`SKConfettiSystem`). Each s
| **FadeOut** | `bool` | Whether or not the particle should fade out at the end of its life. |
| **Lifetime** | `double` | The duration in seconds for how long the particle is allowed to live. |
| **IsAnimationEnabled** | `bool` | Controls whether the system is running or not. |
| **IsComplete** | `bool` | A value that indicates whether the system is complete and all systems and particles are also complete. |
| **IsRunning** | `bool` | Determines whether the system is complete and all systems and particles are also complete. |

# Emitter

Expand All @@ -63,7 +63,7 @@ Each system has an emitter instance that controls how the confetti particles are
| **ParticleRate** | `int` | The number of particles to generate each second. |
| **MaxParticles** | `int` | The maximum number of particles allowed by the emitter. A value of `-1` indicates no limit. |
| **Duration** | `double` | The duration in seconds of how long the emitter runs for. A value of `0` indicates that all particles are emitted instantly. |
| **IsComplete** | `bool` | A value that indicates whether the emitter has generated all the particles and they have all disappeared. |
| **IsRunning** | `bool` | Determines whether the emitter has generated all the particles and they have all disappeared. |

## Helper Emitters

Expand Down
2 changes: 1 addition & 1 deletion docs/api/ui-forms/sklottieview.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ There are several properties that can be used to control th animation playback:
| **RepeatCount** | `int` | The number of times to repeat the animation. Default is 0 (no repeat). |
| **RepeatMode** | `SKLottieRepeatMode` | The way in which to repeat the animation. Default is `Restart`. |
| **IsAnimationEnabled** | `bool` | Determines whether the control will play the animation provided. |
| **IsComplete** | `bool` | A value that indicates whether all systems are complete. |
| **IsRunning** | `bool` | Determines whether the control is currently rendering the animation. |

## Events

Expand Down
7 changes: 4 additions & 3 deletions docs/api/ui-maui/skconfettiview.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The main property of a confetti view is the `Systems` property:
| :--------------------- | :---------------------------- | :---------- |
| **Systems** | `SKConfettiSystemCollection` | The collection of [systems](#system) in the view. |
| **IsAnimationEnabled** | `bool` | Determines whether the control will play the animation provided. |
| **IsComplete** | `bool` | A value that indicates whether all systems are complete. |
| **IsRunning** | `bool` | Determines whether the control is currently rendering confetti. |

## Parts

Expand Down Expand Up @@ -52,7 +52,7 @@ Every confetti view consists up one or more systems (`SKConfettiSystem`). Each s
| **FadeOut** | `bool` | Whether or not the particle should fade out at the end of its life. |
| **Lifetime** | `double` | The duration in seconds for how long the particle is allowed to live. |
| **IsAnimationEnabled** | `bool` | Controls whether the system is running or not. |
| **IsComplete** | `bool` | A value that indicates whether the system is complete and all systems and particles are also complete. |
| **IsRunning** | `bool` | Determines whether the system is complete and all systems and particles are also complete. |

# Emitter

Expand All @@ -63,7 +63,8 @@ Each system has an emitter instance that controls how the confetti particles are
| **ParticleRate** | `int` | The number of particles to generate each second. |
| **MaxParticles** | `int` | The maximum number of particles allowed by the emitter. A value of `-1` indicates no limit. |
| **Duration** | `double` | The duration in seconds of how long the emitter runs for. A value of `0` indicates that all particles are emitted instantly. |
| **IsComplete** | `bool` | A value that indicates whether the emitter has generated all the particles and they have all disappeared. |
| **IsRunning** | `bool` | Determines whether the control is currently rendering confetti. |
| **IsRunning** | `bool` | Determines whether the emitter has generated all the particles and they have all disappeared. |

## Helper Emitters

Expand Down
2 changes: 1 addition & 1 deletion docs/api/ui-maui/sklottieview.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ There are several properties that can be used to control th animation playback:
| **RepeatCount** | `int` | The number of times to repeat the animation. Default is 0 (no repeat). |
| **RepeatMode** | `SKLottieRepeatMode` | The way in which to repeat the animation. Default is `Restart`. |
| **IsAnimationEnabled** | `bool` | Determines whether the control will play the animation provided. |
| **IsComplete** | `bool` | A value that indicates whether all systems are complete. |
| **IsRunning** | `bool` | Determines whether the control is currently rendering the animation. |

## Events

Expand Down
18 changes: 18 additions & 0 deletions samples/Forms/SkiaSharpDemo/Converters/InvertedBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Globalization;
using Xamarin.Forms;

namespace SkiaSharpDemo.Converters
{
public class InvertedBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
value switch
{
bool b => !b,
_ => throw new ArgumentException("Value was not a bool.", nameof(value)),
};

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Convert(value, targetType, parameter, culture);
}
}
7 changes: 6 additions & 1 deletion samples/Forms/SkiaSharpDemo/Demos/Confetti/ConfettiPage.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI"
xmlns:converters="clr-namespace:SkiaSharpDemo.Converters"
xmlns:views="clr-namespace:SkiaSharpDemo.Views"
x:Class="SkiaSharpDemo.Demos.ConfettiPage"
Title="Confetti">

<ContentPage.Resources>
<converters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
</ContentPage.Resources>

<Grid>

<Label Text="{Binding Message}" FontSize="32" FontAttributes="Bold"
Expand All @@ -20,7 +25,7 @@
<BoxView Color="Green" Opacity="0.5" CornerRadius="12"
WidthRequest="25" HeightRequest="24" Margin="24"
HorizontalOptions="End" VerticalOptions="Start"
IsVisible="{Binding IsComplete, Source={Reference confettiView}}" />
IsVisible="{Binding IsRunning, Source={Reference confettiView}, Converter={StaticResource InvertedBooleanConverter}}" />

<views:BottomTabBar SelectedIndex="{Binding SelectedTab}" PagePadding="12,6">
<views:BottomTabCollection>
Expand Down
8 changes: 6 additions & 2 deletions samples/Forms/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI"
xmlns:views="clr-namespace:SkiaSharpDemo.Views"
xmlns:converters="clr-namespace:SkiaSharpDemo.Converters"
x:Class="SkiaSharpDemo.Demos.LottiePage"
Title="Lottie">

<ContentPage.Resources>
<converters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
</ContentPage.Resources>

<Grid RowDefinitions="*,Auto">

<controls:SKLottieView x:Name="lottieView"
Expand All @@ -16,7 +20,7 @@
<BoxView Color="Green" Opacity="0.5" CornerRadius="12"
WidthRequest="25" HeightRequest="24" Margin="24"
HorizontalOptions="End" VerticalOptions="Start"
IsVisible="{Binding IsComplete, Source={Reference lottieView}}" />
IsVisible="{Binding IsRunning, Source={Reference confettiView}, Converter={StaticResource InvertedBooleanConverter}}" />

<StackLayout Spacing="12" Padding="12" Grid.Row="1">

Expand Down
16 changes: 16 additions & 0 deletions samples/Maui/SkiaSharpDemo/Converters/InvertedBoolConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Globalization;

namespace SkiaSharpDemo.Converters
{
public class InvertedBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
value switch
{
bool b => !b,
_ => throw new ArgumentException("Value was not a bool.", nameof(value)),
};

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Convert(value, targetType, parameter, culture);
}
}
7 changes: 6 additions & 1 deletion samples/Maui/SkiaSharpDemo/Demos/Confetti/ConfettiPage.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI"
xmlns:converters="clr-namespace:SkiaSharpDemo.Converters"
xmlns:views="clr-namespace:SkiaSharpDemo.Views"
x:Class="SkiaSharpDemo.Demos.ConfettiPage"
Title="Confetti">

<ContentPage.Resources>
<converters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
</ContentPage.Resources>

<Grid>

<Label Text="{Binding Message}" FontSize="32" FontAttributes="Bold"
Expand All @@ -20,7 +25,7 @@
<BoxView Color="Green" Opacity="0.5" CornerRadius="12"
WidthRequest="25" HeightRequest="24" Margin="24"
HorizontalOptions="End" VerticalOptions="Start"
IsVisible="{Binding IsComplete, Source={Reference confettiView}}" />
IsVisible="{Binding IsRunning, Source={Reference confettiView}, Converter={StaticResource InvertedBooleanConverter}}" />

<views:BottomTabBar SelectedIndex="{Binding SelectedTab}" PagePadding="12,6">
<views:BottomTabCollection>
Expand Down
7 changes: 6 additions & 1 deletion samples/Maui/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI"
xmlns:converters="clr-namespace:SkiaSharpDemo.Converters"
xmlns:views="clr-namespace:SkiaSharpDemo.Views"
x:Class="SkiaSharpDemo.Demos.LottiePage"
Title="Lottie">

<ContentPage.Resources>
<converters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
</ContentPage.Resources>

<Grid RowDefinitions="*,Auto">

<controls:SKLottieView x:Name="lottieView"
Expand All @@ -18,7 +23,7 @@
<BoxView Color="Green" Opacity="0.5" CornerRadius="12"
WidthRequest="25" HeightRequest="24" Margin="24"
HorizontalOptions="End" VerticalOptions="Start"
IsVisible="{Binding IsComplete, Source={Reference lottieView}}" />
IsVisible="{Binding IsRunning, Source={Reference confettiView}, Converter={StaticResource InvertedBooleanConverter}}" />

<VerticalStackLayout Spacing="12" Padding="12" Grid.Row="1">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public class SKConfettiEmitter : BindableObject
typeof(SKConfettiEmitter),
5.0);

private static readonly BindablePropertyKey IsCompletePropertyKey = BindableProperty.CreateReadOnly(
nameof(IsComplete),
private static readonly BindablePropertyKey IsRunningPropertyKey = BindableProperty.CreateReadOnly(
nameof(IsRunning),
typeof(bool),
typeof(SKConfettiEmitter),
false);
true,
defaultBindingMode: BindingMode.OneWayToSource);

public static readonly BindableProperty IsCompleteProperty = IsCompletePropertyKey.BindableProperty;
public static readonly BindableProperty IsRunningProperty = IsRunningPropertyKey.BindableProperty;

private int totalParticles = 0;
private double totalDuration = 0;
Expand Down Expand Up @@ -64,17 +65,20 @@ public double Duration
set => SetValue(DurationProperty, value);
}

public bool IsComplete
/// <summary>
/// Gets a value indicating whether confetti is being emitted.
/// </summary>
public bool IsRunning
{
get => (bool)GetValue(IsCompleteProperty);
private set => SetValue(IsCompletePropertyKey, value);
get => (bool)GetValue(IsRunningProperty);
private set => SetValue(IsRunningPropertyKey, value);
}

public event Action<int>? ParticlesCreated;

public void Update(TimeSpan deltaTime)
{
if (IsComplete)
if (!IsRunning)
return;

var prevDuration = totalDuration;
Expand All @@ -96,10 +100,7 @@ public void Update(TimeSpan deltaTime)

ParticlesCreated?.Invoke(particles);

IsComplete =
Duration == 0 || // burst mode
(MaxParticles > 0 && totalParticles >= MaxParticles) || // reached the max particles
(Duration > 0 && totalDuration >= Duration); // reached the max duration
UpdateIsRunning();
}

public static SKConfettiEmitter Burst(int particles) =>
Expand All @@ -113,5 +114,13 @@ public static SKConfettiEmitter Infinite(int particleRate) =>

public static SKConfettiEmitter Infinite(int particleRate, int maxParticles) =>
new SKConfettiEmitter(particleRate, maxParticles, -1);

private void UpdateIsRunning()
{
IsRunning =
Duration != 0 && // burst mode
(MaxParticles > 0 && totalParticles < MaxParticles) || // reached the max particles
(Duration > 0 && totalDuration < Duration); // reached the max duration
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public float Size

public SKRect Bounds { get; private set; }

public bool IsComplete { get; private set; }
public bool IsRunning { get; private set; } = true;

public void Draw(SKCanvas canvas)
{
if (IsComplete || Shape == null)
if (!IsRunning || Shape == null)
return;

canvas.Save();
Expand All @@ -81,7 +81,7 @@ public void Draw(SKCanvas canvas)

public void ApplyForce(SKPoint force, TimeSpan deltaTime)
{
if (IsComplete)
if (!IsRunning)
return;

var secs = (float)deltaTime.TotalSeconds;
Expand Down Expand Up @@ -119,11 +119,11 @@ public void ApplyForce(SKPoint force, TimeSpan deltaTime)
var c = Color;
var alpha = c.Alpha - secs;
Color = c.WithAlpha(alpha);
IsComplete = alpha <= 0;
IsRunning = alpha > 0;
}
else
{
IsComplete = true;
IsRunning = false;
}
}

Expand Down
Loading

0 comments on commit ce6c5c0

Please sign in to comment.