Core - Reworked brush/effect property storage

Profile editor - Added brush selection
Profile editor - Added playback controls
This commit is contained in:
Robert committed 2022-01-16 00:46:19 +01:00
1 parent 022beb6a48
commit 1832a25426
64 files changed
+1168 -481

No files matched your search

+34 -49
View File
@@ -94,7 +94,7 @@ namespace Artemis.Core
/// <summary>
/// Gets the general properties of the layer
/// </summary>
[PropertyGroupDescription(Name = "General", Description = "A collection of general properties")]
[PropertyGroupDescription(Identifier = "General", Name = "General", Description = "A collection of general properties")]
public LayerGeneralProperties General
{
get => _general;
@@ -104,7 +104,7 @@ namespace Artemis.Core
/// <summary>
/// Gets the transform properties of the layer
/// </summary>
[PropertyGroupDescription(Name = "Transform", Description = "A collection of transformation properties")]
[PropertyGroupDescription(Identifier = "Transform", Name = "Transform", Description = "A collection of transformation properties")]
public LayerTransformProperties Transform
{
get => _transform;
@@ -208,19 +208,20 @@ namespace Artemis.Core
LayerBrushStore.LayerBrushRemoved += LayerBrushStoreOnLayerBrushRemoved;
// Layers have two hardcoded property groups, instantiate them
Attribute generalAttribute = Attribute.GetCustomAttribute(
PropertyGroupDescriptionAttribute generalAttribute = (PropertyGroupDescriptionAttribute) Attribute.GetCustomAttribute(
GetType().GetProperty(nameof(General))!,
typeof(PropertyGroupDescriptionAttribute)
)!;
Attribute transformAttribute = Attribute.GetCustomAttribute(
PropertyGroupDescriptionAttribute transformAttribute = (PropertyGroupDescriptionAttribute) Attribute.GetCustomAttribute(
GetType().GetProperty(nameof(Transform))!,
typeof(PropertyGroupDescriptionAttribute)
)!;
General.GroupDescription = (PropertyGroupDescriptionAttribute) generalAttribute;
General.Initialize(this, "General.", Constants.CorePluginFeature);
Transform.GroupDescription = (PropertyGroupDescriptionAttribute) transformAttribute;
Transform.Initialize(this, "Transform.", Constants.CorePluginFeature);
LayerEntity.GeneralPropertyGroup ??= new PropertyGroupEntity {Identifier = generalAttribute.Identifier};
LayerEntity.TransformPropertyGroup ??= new PropertyGroupEntity {Identifier = transformAttribute.Identifier};
General.Initialize(this, null, generalAttribute, LayerEntity.GeneralPropertyGroup);
Transform.Initialize(this, null, transformAttribute, LayerEntity.TransformPropertyGroup);
General.ShapeType.CurrentValueSet += ShapeTypeOnCurrentValueSet;
ApplyShapeType();
@@ -241,8 +242,7 @@ namespace Artemis.Core
return;
LayerBrushReference? current = General.BrushReference.CurrentValue;
if (e.Registration.PluginFeature.Id == current?.LayerBrushProviderId &&
e.Registration.LayerBrushDescriptor.LayerBrushType.Name == current.BrushType)
if (e.Registration.PluginFeature.Id == current?.LayerBrushProviderId && e.Registration.LayerBrushDescriptor.LayerBrushType.Name == current.BrushType)
ActivateLayerBrush();
}
@@ -282,7 +282,13 @@ namespace Artemis.Core
General.ApplyToEntity();
Transform.ApplyToEntity();
LayerBrush?.BaseProperties?.ApplyToEntity();
// Don't override the old value of LayerBrush if the current value is null, this avoid losing settings of an unavailable brush
if (LayerBrush != null)
{
LayerBrush.Save();
LayerEntity.LayerBrush = LayerBrush.LayerBrushEntity;
}
// LEDs
LayerEntity.Leds.Clear();
@@ -712,53 +718,32 @@ namespace Artemis.Core
#region Brush management
/// <summary>
/// Changes the current layer brush to the brush described in the provided <paramref name="descriptor" />
/// Changes the current layer brush to the provided layer brush and activates it
/// </summary>
public void ChangeLayerBrush(LayerBrushDescriptor descriptor)
public void ChangeLayerBrush(BaseLayerBrush? layerBrush)
{
if (descriptor == null)
throw new ArgumentNullException(nameof(descriptor));
General.BrushReference.SetCurrentValue(layerBrush != null ? new LayerBrushReference(layerBrush.Descriptor) : null, null);
LayerBrush = layerBrush;
if (LayerBrush != null)
{
BaseLayerBrush brush = LayerBrush;
LayerBrush = null;
brush.Dispose();
}
// Ensure the brush reference matches the brush
LayerBrushReference? current = General.BrushReference.BaseValue;
if (!descriptor.MatchesLayerBrushReference(current))
General.BrushReference.SetCurrentValue(new LayerBrushReference(descriptor), null);
ActivateLayerBrush();
}
/// <summary>
/// Removes the current layer brush from the layer
/// </summary>
public void RemoveLayerBrush()
{
if (LayerBrush == null)
return;
BaseLayerBrush brush = LayerBrush;
DeactivateLayerBrush();
LayerEntity.PropertyEntities.RemoveAll(p => p.FeatureId == brush.ProviderId && p.Path.StartsWith("LayerBrush."));
ActivateLayerBrush();
else
OnLayerBrushUpdated();
}
internal void ActivateLayerBrush()
{
try
{
LayerBrushReference? current = General.BrushReference.CurrentValue;
if (current == null)
if (LayerBrush == null)
{
// If the brush is null, try to instantiate it
LayerBrushReference? brushReference = General.BrushReference.CurrentValue;
if (brushReference?.LayerBrushProviderId != null && brushReference.BrushType != null)
ChangeLayerBrush(LayerBrushStore.Get(brushReference.LayerBrushProviderId, brushReference.BrushType)?.LayerBrushDescriptor.CreateInstance(this, LayerEntity.LayerBrush));
// If that's not possible there's nothing to do
return;
LayerBrushDescriptor? descriptor = current.LayerBrushProviderId != null && current.BrushType != null
? LayerBrushStore.Get(current.LayerBrushProviderId, current.BrushType)?.LayerBrushDescriptor
: null;
descriptor?.CreateInstance(this);
}
General.ShapeType.IsHidden = LayerBrush != null && !LayerBrush.SupportsTransformation;
General.BlendMode.IsHidden = LayerBrush != null && !LayerBrush.SupportsTransformation;
@@ -778,9 +763,9 @@ namespace Artemis.Core
if (LayerBrush == null)
return;
BaseLayerBrush brush = LayerBrush;
BaseLayerBrush? brush = LayerBrush;
LayerBrush = null;
brush.Dispose();
brush?.Dispose();
OnLayerBrushUpdated();
}
@@ -7,6 +7,11 @@ namespace Artemis.Core
/// </summary>
public class PropertyDescriptionAttribute : Attribute
{
/// <summary>
/// The identifier of this property used for storage, if not set one will be generated property name in code
/// </summary>
public string? Identifier { get; set; }
/// <summary>
/// The user-friendly name for this property, shown in the UI
/// </summary>
@@ -8,12 +8,17 @@ namespace Artemis.Core
public class PropertyGroupDescriptionAttribute : Attribute
{
/// <summary>
/// The user-friendly name for this property, shown in the UI.
/// The identifier of this property group used for storage, if not set one will be generated based on the group name in code
/// </summary>
public string? Name { get; set; }
public string? Identifier { get; set; }
/// <summary>
/// The user-friendly description for this property, shown in the UI.
/// The user-friendly name for this property group, shown in the UI.
/// </summary>
public string? Name { get; set; }
/// <summary>
/// The user-friendly description for this property group, shown in the UI.
/// </summary>
public string? Description { get; set; }
}
@@ -16,6 +16,11 @@ namespace Artemis.Core
/// Gets the description attribute applied to this property
/// </summary>
PropertyDescriptionAttribute PropertyDescription { get; }
/// <summary>
/// Gets the profile element (such as layer or folder) this property is applied to
/// </summary>
RenderProfileElement ProfileElement { get; }
/// <summary>
/// The parent group of this layer property, set after construction
@@ -43,7 +48,7 @@ namespace Artemis.Core
public bool DataBindingsSupported { get; }
/// <summary>
/// Gets the unique path of the property on the layer
/// Gets the unique path of the property on the render element
/// </summary>
string Path { get; }
@@ -56,7 +61,7 @@ namespace Artemis.Core
/// Indicates whether the BaseValue was loaded from storage, useful to check whether a default value must be applied
/// </summary>
bool IsLoadedFromStorage { get; }
/// <summary>
/// Initializes the layer property
/// <para>
@@ -64,7 +69,7 @@ namespace Artemis.Core
/// <see cref="LayerProperty{T}" />
/// </para>
/// </summary>
void Initialize(RenderProfileElement profileElement, LayerPropertyGroup group, PropertyEntity entity, bool fromStorage, PropertyDescriptionAttribute description, string path);
void Initialize(RenderProfileElement profileElement, LayerPropertyGroup group, PropertyEntity entity, bool fromStorage, PropertyDescriptionAttribute description);
/// <summary>
/// Attempts to load and add the provided keyframe entity to the layer property
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@@ -27,10 +28,10 @@ namespace Artemis.Core
// These are set right after construction to keep the constructor (and inherited constructs) clean
ProfileElement = null!;
LayerPropertyGroup = null!;
Path = null!;
Entity = null!;
PropertyDescription = null!;
DataBinding = null!;
Path = "";
CurrentValue = default!;
DefaultValue = default!;
@@ -117,13 +118,11 @@ namespace Artemis.Core
}
}
/// <summary>
/// Gets the profile element (such as layer or folder) this property is applied to
/// </summary>
public RenderProfileElement ProfileElement { get; internal set; }
/// <inheritdoc />
public RenderProfileElement ProfileElement { get; private set; }
/// <inheritdoc />
public LayerPropertyGroup LayerPropertyGroup { get; internal set; }
public LayerPropertyGroup LayerPropertyGroup { get; private set; }
#endregion
@@ -457,16 +456,18 @@ namespace Artemis.Core
internal PropertyEntity Entity { get; set; }
/// <inheritdoc />
public void Initialize(RenderProfileElement profileElement, LayerPropertyGroup group, PropertyEntity entity, bool fromStorage, PropertyDescriptionAttribute description, string path)
public void Initialize(RenderProfileElement profileElement, LayerPropertyGroup group, PropertyEntity entity, bool fromStorage, PropertyDescriptionAttribute description)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (description.Identifier == null)
throw new ArtemisCoreException("Can't initialize a property group without an identifier");
_isInitialized = true;
ProfileElement = profileElement ?? throw new ArgumentNullException(nameof(profileElement));
LayerPropertyGroup = group ?? throw new ArgumentNullException(nameof(group));
Path = path;
Entity = entity ?? throw new ArgumentNullException(nameof(entity));
PropertyDescription = description ?? throw new ArgumentNullException(nameof(description));
IsLoadedFromStorage = fromStorage;
@@ -475,6 +476,9 @@ namespace Artemis.Core
if (PropertyDescription.DisableKeyframes)
KeyframesSupported = false;
// Create the path to this property by walking up the tree
Path = LayerPropertyGroup.Path + "." + description.Identifier;
OnInitialize();
}
@@ -31,9 +31,7 @@ namespace Artemis.Core
{
// These are set right after construction to keep the constructor (and inherited constructs) clean
GroupDescription = null!;
Feature = null!;
ProfileElement = null!;
Path = null!;
Path = "";
_layerProperties = new List<ILayerProperty>();
_layerPropertyGroups = new List<LayerPropertyGroup>();
@@ -42,47 +40,32 @@ namespace Artemis.Core
LayerPropertyGroups = new ReadOnlyCollection<LayerPropertyGroup>(_layerPropertyGroups);
}
/// <summary>
/// Gets the description of this group
/// </summary>
public PropertyGroupDescriptionAttribute GroupDescription { get; internal set; }
/// <summary>
/// Gets the plugin feature this group is associated with
/// </summary>
public PluginFeature Feature { get; set; }
/// <summary>
/// Gets the profile element (such as layer or folder) this group is associated with
/// </summary>
public RenderProfileElement ProfileElement { get; internal set; }
public RenderProfileElement ProfileElement { get; private set; }
/// <summary>
/// Gets the description of this group
/// </summary>
public PropertyGroupDescriptionAttribute GroupDescription { get; private set; }
/// <summary>
/// The parent group of this group
/// </summary>
[LayerPropertyIgnore]
[LayerPropertyIgnore] // Ignore the parent when selecting child groups
public LayerPropertyGroup? Parent { get; internal set; }
/// <summary>
/// The path of this property group
/// Gets the unique path of the property on the render element
/// </summary>
public string Path { get; internal set; }
public string Path { get; private set; }
/// <summary>
/// Gets whether this property groups properties are all initialized
/// </summary>
public bool PropertiesInitialized { get; private set; }
/// <summary>
/// The layer brush this property group belongs to
/// </summary>
public BaseLayerBrush? LayerBrush { get; internal set; }
/// <summary>
/// The layer effect this property group belongs to
/// </summary>
public BaseLayerEffect? LayerEffect { get; internal set; }
/// <summary>
/// Gets or sets whether the property is hidden in the UI
/// </summary>
@@ -96,6 +79,11 @@ namespace Artemis.Core
}
}
/// <summary>
/// Gets the entity this property group uses for persistent storage
/// </summary>
public PropertyGroupEntity? PropertyGroupEntity { get; internal set; }
/// <summary>
/// A list of all layer properties in this group
/// </summary>
@@ -194,17 +182,20 @@ namespace Artemis.Core
}
}
internal void Initialize(RenderProfileElement profileElement, string path, PluginFeature feature)
internal void Initialize(RenderProfileElement profileElement, LayerPropertyGroup? parent, PropertyGroupDescriptionAttribute groupDescription, PropertyGroupEntity? propertyGroupEntity)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (groupDescription.Identifier == null)
throw new ArtemisCoreException("Can't initialize a property group without an identifier");
// Doubt this will happen but let's make sure
if (PropertiesInitialized)
throw new ArtemisCoreException("Layer property group already initialized, wut");
Feature = feature ?? throw new ArgumentNullException(nameof(feature));
ProfileElement = profileElement ?? throw new ArgumentNullException(nameof(profileElement));
Path = path.TrimEnd('.');
ProfileElement = profileElement;
Parent = parent;
GroupDescription = groupDescription;
PropertyGroupEntity = propertyGroupEntity ?? new PropertyGroupEntity {Identifier = groupDescription.Identifier};
Path = parent != null ? parent.Path + "." + groupDescription.Identifier : groupDescription.Identifier;
// Get all properties implementing ILayerProperty or LayerPropertyGroup
foreach (PropertyInfo propertyInfo in GetType().GetProperties())
@@ -271,61 +262,68 @@ namespace Artemis.Core
private void InitializeProperty(PropertyInfo propertyInfo, PropertyDescriptionAttribute propertyDescription)
{
string path = $"{Path}.{propertyInfo.Name}";
if (!typeof(ILayerProperty).IsAssignableFrom(propertyInfo.PropertyType))
throw new ArtemisPluginException($"Layer property with PropertyDescription attribute must be of type LayerProperty at {path}");
if (!(Activator.CreateInstance(propertyInfo.PropertyType, true) is ILayerProperty instance))
throw new ArtemisPluginException($"Failed to create instance of layer property at {path}");
// Ensure the description has a name, if not this is a good point to set it based on the property info
// Ensure the description has an identifier and name, if not this is a good point to set it based on the property info
if (string.IsNullOrWhiteSpace(propertyDescription.Identifier))
propertyDescription.Identifier = propertyInfo.Name;
if (string.IsNullOrWhiteSpace(propertyDescription.Name))
propertyDescription.Name = propertyInfo.Name.Humanize();
PropertyEntity entity = GetPropertyEntity(ProfileElement, path, out bool fromStorage);
instance.Initialize(ProfileElement, this, entity, fromStorage, propertyDescription, path);
if (!typeof(ILayerProperty).IsAssignableFrom(propertyInfo.PropertyType))
throw new ArtemisPluginException($"Property with PropertyDescription attribute must be of type ILayerProperty: {propertyDescription.Identifier}");
if (Activator.CreateInstance(propertyInfo.PropertyType, true) is not ILayerProperty instance)
throw new ArtemisPluginException($"Failed to create instance of layer property: {propertyDescription.Identifier}");
PropertyEntity entity = GetPropertyEntity(propertyDescription.Identifier, out bool fromStorage);
instance.Initialize(ProfileElement, this, entity, fromStorage, propertyDescription);
propertyInfo.SetValue(this, instance);
_layerProperties.Add(instance);
}
private void InitializeChildGroup(PropertyInfo propertyInfo, PropertyGroupDescriptionAttribute propertyGroupDescription)
{
string path = Path + ".";
if (!typeof(LayerPropertyGroup).IsAssignableFrom(propertyInfo.PropertyType))
throw new ArtemisPluginException("Layer property with PropertyGroupDescription attribute must be of type LayerPropertyGroup");
if (!(Activator.CreateInstance(propertyInfo.PropertyType) is LayerPropertyGroup instance))
throw new ArtemisPluginException($"Failed to create instance of layer property group at {path + propertyInfo.Name}");
// Ensure the description has a name, if not this is a good point to set it based on the property info
// Ensure the description has an identifier and name name, if not this is a good point to set it based on the property info
if (string.IsNullOrWhiteSpace(propertyGroupDescription.Identifier))
propertyGroupDescription.Identifier = propertyInfo.Name;
if (string.IsNullOrWhiteSpace(propertyGroupDescription.Name))
propertyGroupDescription.Name = propertyInfo.Name.Humanize();
instance.Parent = this;
instance.GroupDescription = propertyGroupDescription;
instance.LayerBrush = LayerBrush;
instance.LayerEffect = LayerEffect;
instance.Initialize(ProfileElement, $"{path}{propertyInfo.Name}.", Feature);
if (!typeof(LayerPropertyGroup).IsAssignableFrom(propertyInfo.PropertyType))
throw new ArtemisPluginException($"Property with PropertyGroupDescription attribute must be of type LayerPropertyGroup: {propertyGroupDescription.Identifier}");
if (!(Activator.CreateInstance(propertyInfo.PropertyType) is LayerPropertyGroup instance))
throw new ArtemisPluginException($"Failed to create instance of layer property group: {propertyGroupDescription.Identifier}");
PropertyGroupEntity entity = GetPropertyGroupEntity(propertyGroupDescription.Identifier);
instance.Initialize(ProfileElement, this, propertyGroupDescription, entity);
propertyInfo.SetValue(this, instance);
_layerPropertyGroups.Add(instance);
}
private PropertyEntity GetPropertyEntity(RenderProfileElement profileElement, string path, out bool fromStorage)
private PropertyEntity GetPropertyEntity(string identifier, out bool fromStorage)
{
PropertyEntity? entity = profileElement.RenderElementEntity.PropertyEntities.FirstOrDefault(p => p.FeatureId == Feature.Id && p.Path == path);
if (PropertyGroupEntity == null)
throw new ArtemisCoreException($"Can't execute {nameof(GetPropertyEntity)} without {nameof(PropertyGroupEntity)} being setup");
PropertyEntity? entity = PropertyGroupEntity.Properties.FirstOrDefault(p => p.Identifier == identifier);
fromStorage = entity != null;
if (entity == null)
{
entity = new PropertyEntity {FeatureId = Feature.Id, Path = path};
profileElement.RenderElementEntity.PropertyEntities.Add(entity);
entity = new PropertyEntity {Identifier = identifier};
PropertyGroupEntity.Properties.Add(entity);
}
return entity;
}
private PropertyGroupEntity GetPropertyGroupEntity(string identifier)
{
if (PropertyGroupEntity == null)
throw new ArtemisCoreException($"Can't execute {nameof(GetPropertyGroupEntity)} without {nameof(PropertyGroupEntity)} being setup");
return PropertyGroupEntity.PropertyGroups.FirstOrDefault(g => g.Identifier == identifier) ?? new PropertyGroupEntity() {Identifier = identifier};
}
/// <inheritdoc />
public void Dispose()
{
@@ -97,20 +97,10 @@ namespace Artemis.Core
internal void SaveRenderElement()
{
RenderElementEntity.LayerEffects.Clear();
foreach (BaseLayerEffect layerEffect in LayerEffects)
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
{
LayerEffectEntity layerEffectEntity = new()
{
Id = layerEffect.EntityId,
ProviderId = layerEffect.Descriptor?.PlaceholderFor ?? layerEffect.ProviderId,
EffectType = layerEffect.GetEffectTypeName(),
Name = layerEffect.Name,
Suspended = layerEffect.Suspended,
HasBeenRenamed = layerEffect.HasBeenRenamed,
Order = layerEffect.Order
};
RenderElementEntity.LayerEffects.Add(layerEffectEntity);
layerEffect.BaseProperties?.ApplyToEntity();
baseLayerEffect.Save();
RenderElementEntity.LayerEffects.Add(baseLayerEffect.LayerEffectEntity);
}
// Condition
@@ -310,7 +300,7 @@ namespace Artemis.Core
foreach (LayerEffectEntity layerEffectEntity in RenderElementEntity.LayerEffects)
{
// If there is a non-placeholder existing effect, skip this entity
BaseLayerEffect? existing = LayerEffectsList.FirstOrDefault(e => e.EntityId == layerEffectEntity.Id);
BaseLayerEffect? existing = LayerEffectsList.FirstOrDefault(e => e.LayerEffectEntity.Id == layerEffectEntity.Id);
if (existing != null && existing.Descriptor.PlaceholderFor == null)
continue;
@@ -351,7 +341,7 @@ namespace Artemis.Core
List<BaseLayerEffect> pluginEffects = LayerEffectsList.Where(ef => ef.ProviderId == e.Registration.PluginFeature.Id).ToList();
foreach (BaseLayerEffect pluginEffect in pluginEffects)
{
LayerEffectEntity entity = RenderElementEntity.LayerEffects.First(en => en.Id == pluginEffect.EntityId);
LayerEffectEntity entity = RenderElementEntity.LayerEffects.First(en => en.Id == pluginEffect.LayerEffectEntity.Id);
LayerEffectsList.Remove(pluginEffect);
pluginEffect.Dispose();