Core - Added fading in and out of profiles

This commit is contained in:
Diogo Trindade committed 2022-11-27 15:20:17 +00:00
1 parent f30cc31ead
commit 194780b25c
6 files changed
+87 -3

No files matched your search

+44 -1
View File
@@ -8,6 +8,15 @@ using SkiaSharp;
namespace Artemis.Core;
internal enum FadingStatus
{
Enabled,
FadingIn,
FadingOut,
Disabled
}
/// <summary>
/// Represents a profile containing folders and layers
/// </summary>
@@ -18,12 +27,15 @@ public sealed class Profile : ProfileElement
private readonly ObservableCollection<ProfileScript> _scripts;
private bool _isFreshImport;
private ProfileElement? _lastSelectedProfileElement;
private double _opacity;
internal Profile(ProfileConfiguration configuration, ProfileEntity profileEntity) : base(null!)
{
_scripts = new ObservableCollection<ProfileScript>();
_scriptConfigurations = new ObservableCollection<ScriptConfiguration>();
_opacity = 0d;
FadingStatus = FadingStatus.FadingIn;
Configuration = configuration;
Profile = this;
ProfileEntity = profileEntity;
@@ -81,6 +93,8 @@ public sealed class Profile : ProfileElement
internal List<Exception> Exceptions { get; }
internal FadingStatus FadingStatus { get; private set; }
/// <inheritdoc />
public override void Update(double deltaTime)
{
@@ -97,6 +111,16 @@ public sealed class Profile : ProfileElement
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileUpdated(deltaTime);
const double OPACITY_PER_SECOND = 1;
if (FadingStatus == FadingStatus.FadingIn)
_opacity = Math.Clamp(_opacity + OPACITY_PER_SECOND * deltaTime, 0d, 1d);
if (FadingStatus == FadingStatus.FadingOut)
_opacity = Math.Clamp(_opacity - OPACITY_PER_SECOND * deltaTime, 0d, 1d);
if (_opacity == 0d)
FadingStatus = FadingStatus.Disabled;
if (_opacity == 1d)
FadingStatus = FadingStatus.Enabled;
}
}
@@ -111,9 +135,17 @@ public sealed class Profile : ProfileElement
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileRendering(canvas, canvas.LocalClipBounds);
using var opacityPaint = new SKPaint();
if (Configuration.FadeInAndOut && FadingStatus != FadingStatus.Enabled)
opacityPaint.Color = new SKColor(0, 0, 0, (byte)(255d * Easings.CubicEaseInOut(_opacity)));
canvas.SaveLayer(opacityPaint);
foreach (ProfileElement profileElement in Children)
profileElement.Render(canvas, basePosition, editorFocus);
canvas.Restore();
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileRendered(canvas, canvas.LocalClipBounds);
@@ -165,6 +197,17 @@ public sealed class Profile : ProfileElement
layer.PopulateLeds(devices);
}
/// <summary>
/// Starts the fade out process.
/// </summary>
public void FadeOut()
{
if (Disposed)
throw new ObjectDisposedException("Profile");
FadingStatus = FadingStatus.FadingOut;
}
#region Overrides of BreakableModel
/// <inheritdoc />
@@ -21,6 +21,7 @@ public class ProfileConfiguration : BreakableModel, IStorageModel, IDisposable
private bool _isBeingEdited;
private bool _isMissingModule;
private bool _isSuspended;
private bool _fadeInAndOut;
private Module? _module;
private string _name;
@@ -160,6 +161,15 @@ public class ProfileConfiguration : BreakableModel, IStorageModel, IDisposable
internal set => SetAndNotify(ref _profile, value);
}
/// <summary>
/// Gets or sets a boolean indicating whether this profile should fade in and out when enabling or disabling
/// </summary>
public bool FadeInAndOut
{
get => _fadeInAndOut;
set => SetAndNotify(ref _fadeInAndOut, value);
}
/// <summary>
/// Gets or sets the module this profile uses
/// </summary>
@@ -272,6 +282,7 @@ public class ProfileConfiguration : BreakableModel, IStorageModel, IDisposable
IsSuspended = Entity.IsSuspended;
ActivationBehaviour = (ActivationBehaviour) Entity.ActivationBehaviour;
HotkeyMode = (ProfileConfigurationHotkeyMode) Entity.HotkeyMode;
FadeInAndOut = Entity.FadeInAndOut;
Order = Entity.Order;
Icon.Load();
@@ -294,6 +305,7 @@ public class ProfileConfiguration : BreakableModel, IStorageModel, IDisposable
Entity.ActivationBehaviour = (int) ActivationBehaviour;
Entity.HotkeyMode = (int) HotkeyMode;
Entity.ProfileCategoryId = Category.Entity.Id;
Entity.FadeInAndOut = FadeInAndOut;
Entity.Order = Order;
Icon.Save();