mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Implemented layer element saving
Added a working setting to the brush element
This commit is contained in:
parent
be03167f7e
commit
8ba2e58c5d
@ -212,6 +212,7 @@
|
||||
<Compile Include="Services\Storage\ProfileService.cs" />
|
||||
<Compile Include="Services\Storage\Interfaces\ISurfaceService.cs" />
|
||||
<Compile Include="Services\Storage\SurfaceService.cs" />
|
||||
<Compile Include="Utilities\EnumUtilities.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
|
||||
@ -6,6 +6,7 @@ using Artemis.Core.Extensions;
|
||||
using Artemis.Core.Models.Profile.Abstract;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using Newtonsoft.Json;
|
||||
using SkiaSharp;
|
||||
@ -61,7 +62,7 @@ namespace Artemis.Core.Models.Profile
|
||||
|
||||
public override void Render(double deltaTime, ArtemisSurface surface, SKCanvas canvas)
|
||||
{
|
||||
if (RenderRectangle == null || AbsoluteRenderRectangle == null || RenderPath == null)
|
||||
if (RenderPath == null)
|
||||
return;
|
||||
|
||||
canvas.Save();
|
||||
@ -127,6 +128,7 @@ namespace Artemis.Core.Models.Profile
|
||||
{
|
||||
var layerElementEntity = new LayerElementEntity
|
||||
{
|
||||
Id = layerElement.Guid,
|
||||
PluginGuid = layerElement.Descriptor.LayerElementProvider.PluginInfo.Guid,
|
||||
LayerElementType = layerElement.GetType().Name,
|
||||
Configuration = JsonConvert.SerializeObject(layerElement.Settings)
|
||||
@ -164,7 +166,12 @@ namespace Artemis.Core.Models.Profile
|
||||
_layerElements.Add(layerElement);
|
||||
}
|
||||
|
||||
public void ApplySurface(ArtemisSurface surface)
|
||||
internal void RemoveLayerElement(LayerElement layerElement)
|
||||
{
|
||||
_layerElements.Remove(layerElement);
|
||||
}
|
||||
|
||||
internal void PopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
var leds = new List<ArtemisLed>();
|
||||
|
||||
@ -181,14 +188,11 @@ namespace Artemis.Core.Models.Profile
|
||||
_leds = leds;
|
||||
CalculateRenderProperties();
|
||||
}
|
||||
|
||||
|
||||
internal void CalculateRenderProperties()
|
||||
{
|
||||
if (!Leds.Any())
|
||||
{
|
||||
// TODO: Create an empty rectangle and path
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine to top-left and bottom-right
|
||||
var minX = Leds.Min(l => l.AbsoluteRenderRectangle.Left);
|
||||
|
||||
@ -85,7 +85,6 @@ namespace Artemis.Core.Models.Profile
|
||||
ProfileEntity.Layers.AddRange(GetAllLayers().Select(f => f.LayerEntity));
|
||||
}
|
||||
|
||||
|
||||
internal void Activate(ArtemisSurface surface)
|
||||
{
|
||||
lock (this)
|
||||
@ -93,7 +92,7 @@ namespace Artemis.Core.Models.Profile
|
||||
if (IsActivated)
|
||||
return;
|
||||
|
||||
ApplySurface(surface);
|
||||
PopulateLeds(surface);
|
||||
OnActivated();
|
||||
IsActivated = true;
|
||||
}
|
||||
@ -115,10 +114,10 @@ namespace Artemis.Core.Models.Profile
|
||||
return $"{nameof(Order)}: {Order}, {nameof(Name)}: {Name}, {nameof(PluginInfo)}: {PluginInfo}";
|
||||
}
|
||||
|
||||
public void ApplySurface(ArtemisSurface surface)
|
||||
internal void PopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
foreach (var layer in GetAllLayers())
|
||||
layer.ApplySurface(surface);
|
||||
layer.PopulateLeds(surface);
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
@ -1,19 +1,22 @@
|
||||
using Artemis.Core.Models.Profile;
|
||||
using System;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Plugins.LayerElement
|
||||
{
|
||||
public abstract class LayerElement
|
||||
public abstract class LayerElement : IDisposable
|
||||
{
|
||||
protected LayerElement(Layer layer, LayerElementSettings settings, LayerElementDescriptor descriptor)
|
||||
protected LayerElement(Layer layer, Guid guid, LayerElementSettings settings, LayerElementDescriptor descriptor)
|
||||
{
|
||||
Layer = layer;
|
||||
Guid = guid;
|
||||
Settings = settings;
|
||||
Descriptor = descriptor;
|
||||
}
|
||||
|
||||
public Layer Layer { get; }
|
||||
public Guid Guid { get; }
|
||||
public LayerElementSettings Settings { get; }
|
||||
public LayerElementDescriptor Descriptor { get; }
|
||||
|
||||
@ -65,5 +68,9 @@ namespace Artemis.Core.Plugins.LayerElement
|
||||
{
|
||||
return shader;
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,21 @@
|
||||
using Stylet;
|
||||
using System;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.Core.Plugins.LayerElement
|
||||
{
|
||||
public abstract class LayerElementSettings : PropertyChangedBase
|
||||
{
|
||||
private Action<Action> _propertyChangedDispatcher = Execute.DefaultPropertyChangedDispatcher;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the dispatcher to use to dispatch PropertyChanged events. Defaults to Execute.DefaultPropertyChangedDispatcher
|
||||
/// </summary>
|
||||
[System.Xml.Serialization.XmlIgnore]
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
public override Action<Action> PropertyChangedDispatcher
|
||||
{
|
||||
get { return this._propertyChangedDispatcher; }
|
||||
set { this._propertyChangedDispatcher = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Artemis.Core.Models.Profile;
|
||||
using System;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
@ -13,6 +14,8 @@ namespace Artemis.Core.Services.Interfaces
|
||||
/// <param name="layerElementDescriptor">The descriptor of the new layer element</param>
|
||||
/// <param name="settings">JSON settings to be deserialized and injected into the layer element</param>
|
||||
/// <returns></returns>
|
||||
LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings = null);
|
||||
LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings = null, Guid? guid = null);
|
||||
|
||||
void RemoveLayerElement(Layer layer, LayerElement layerElement);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,7 @@
|
||||
using Artemis.Core.Models.Profile;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Plugins.Exceptions;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
@ -17,28 +20,41 @@ namespace Artemis.Core.Services
|
||||
_kernel = kernel;
|
||||
}
|
||||
|
||||
public LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings)
|
||||
public LayerElement InstantiateLayerElement(Layer layer, LayerElementDescriptor layerElementDescriptor, string settings, Guid? guid)
|
||||
{
|
||||
// Deserialize the settings, if provided
|
||||
object deserializedSettings = null;
|
||||
if (settings != null)
|
||||
if (guid == null)
|
||||
guid = Guid.NewGuid();
|
||||
|
||||
// Determine the settings type declared by the layer element
|
||||
object settingsInstance = null;
|
||||
var properties = layerElementDescriptor.LayerElementType.GetProperties();
|
||||
var settingsType = properties.FirstOrDefault(p => p.Name == "Settings" &&
|
||||
p.DeclaringType == layerElementDescriptor.LayerElementType)?.PropertyType;
|
||||
|
||||
// Deserialize the settings if provided, check for null in JSON as well
|
||||
if (settings != null && settings != "null")
|
||||
{
|
||||
var settingsType = layerElementDescriptor.LayerElementType.GetProperty(nameof(LayerElement.Settings))?.PropertyType;
|
||||
// Setting where provided but no settings type was found, something is wrong
|
||||
if (settingsType == null)
|
||||
{
|
||||
throw new ArtemisPluginException(
|
||||
layerElementDescriptor.LayerElementProvider.PluginInfo,
|
||||
$"Layer element of type {layerElementDescriptor.LayerElementType} has no Settings property."
|
||||
$"Settings where provided but layer element of type {layerElementDescriptor.LayerElementType.Name} has no Settings property."
|
||||
);
|
||||
}
|
||||
|
||||
deserializedSettings = JsonConvert.DeserializeObject(settings, settingsType);
|
||||
settingsInstance = JsonConvert.DeserializeObject(settings, settingsType);
|
||||
}
|
||||
// If no settings found, provide a fresh instance of the settings type
|
||||
else if (settingsType != null)
|
||||
{
|
||||
settingsInstance = Activator.CreateInstance(settingsType);
|
||||
}
|
||||
|
||||
var arguments = new IParameter[]
|
||||
{
|
||||
new ConstructorArgument("layer", layer),
|
||||
new ConstructorArgument("settings", deserializedSettings),
|
||||
new ConstructorArgument("guid", guid.Value),
|
||||
new ConstructorArgument("settings", settingsInstance),
|
||||
new ConstructorArgument("descriptor", layerElementDescriptor)
|
||||
};
|
||||
var layerElement = (LayerElement) _kernel.Get(layerElementDescriptor.LayerElementType, arguments);
|
||||
@ -46,5 +62,11 @@ namespace Artemis.Core.Services
|
||||
|
||||
return layerElement;
|
||||
}
|
||||
|
||||
public void RemoveLayerElement(Layer layer, LayerElement layerElement)
|
||||
{
|
||||
layer.RemoveLayerElement(layerElement);
|
||||
layerElement.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@ using Artemis.Core.Events;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.Abstract;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Core.Services.Storage.Interfaces;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
@ -18,17 +19,21 @@ namespace Artemis.Core.Services.Storage
|
||||
private readonly IPluginService _pluginService;
|
||||
private readonly IProfileRepository _profileRepository;
|
||||
private readonly ISurfaceService _surfaceService;
|
||||
private readonly ILayerService _layerService;
|
||||
|
||||
internal ProfileService(IPluginService pluginService, ISurfaceService surfaceService, IProfileRepository profileRepository)
|
||||
internal ProfileService(IPluginService pluginService, ISurfaceService surfaceService, ILayerService layerService, IProfileRepository profileRepository)
|
||||
{
|
||||
_pluginService = pluginService;
|
||||
_surfaceService = surfaceService;
|
||||
_layerService = layerService;
|
||||
_profileRepository = profileRepository;
|
||||
|
||||
_surfaceService.ActiveSurfaceConfigurationChanged += SurfaceServiceOnActiveSurfaceConfigurationChanged;
|
||||
_surfaceService.SurfaceConfigurationUpdated += SurfaceServiceOnSurfaceConfigurationUpdated;
|
||||
_surfaceService.ActiveSurfaceConfigurationChanged += OnActiveSurfaceConfigurationChanged;
|
||||
_surfaceService.SurfaceConfigurationUpdated += OnSurfaceConfigurationUpdated;
|
||||
_pluginService.PluginLoaded += OnPluginLoaded;
|
||||
}
|
||||
|
||||
|
||||
public List<Profile> GetProfiles(ProfileModule module)
|
||||
{
|
||||
var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
|
||||
@ -64,7 +69,7 @@ namespace Artemis.Core.Services.Storage
|
||||
_profileRepository.Add(profile.ProfileEntity);
|
||||
|
||||
if (_surfaceService.ActiveSurface != null)
|
||||
profile.ApplySurface(_surfaceService.ActiveSurface);
|
||||
profile.PopulateLeds(_surfaceService.ActiveSurface);
|
||||
return profile;
|
||||
}
|
||||
|
||||
@ -72,6 +77,7 @@ namespace Artemis.Core.Services.Storage
|
||||
public void ActivateProfile(ProfileModule module, Profile profile)
|
||||
{
|
||||
module.ChangeActiveProfile(profile, _surfaceService.ActiveSurface);
|
||||
InstantiateProfileLayerElements(profile);
|
||||
}
|
||||
|
||||
public void DeleteProfile(Profile profile)
|
||||
@ -90,29 +96,68 @@ namespace Artemis.Core.Services.Storage
|
||||
layer.ApplyToEntity();
|
||||
|
||||
if (_surfaceService.ActiveSurface != null)
|
||||
profile.ApplySurface(_surfaceService.ActiveSurface);
|
||||
profile.PopulateLeds(_surfaceService.ActiveSurface);
|
||||
}
|
||||
|
||||
_profileRepository.Save(profile.ProfileEntity);
|
||||
}
|
||||
|
||||
private void SurfaceServiceOnActiveSurfaceConfigurationChanged(object sender, SurfaceConfigurationEventArgs e)
|
||||
private void InstantiateProfileLayerElements(Profile profile)
|
||||
{
|
||||
ApplySurfaceToProfiles(e.Surface);
|
||||
var layerElementProviders = _pluginService.GetPluginsOfType<LayerElementProvider>();
|
||||
var descriptors = layerElementProviders.SelectMany(l => l.LayerElementDescriptors).ToList();
|
||||
|
||||
foreach (var layer in profile.GetAllLayers())
|
||||
{
|
||||
foreach (var elementEntity in layer.LayerEntity.Elements)
|
||||
{
|
||||
// Skip already instantiated layer elements
|
||||
if (layer.LayerElements.Any(e => e.Guid == elementEntity.Id))
|
||||
continue;
|
||||
|
||||
// Get a matching descriptor
|
||||
var descriptor = descriptors.FirstOrDefault(d => d.LayerElementProvider.PluginInfo.Guid == elementEntity.PluginGuid &&
|
||||
d.LayerElementType.Name == elementEntity.LayerElementType);
|
||||
|
||||
// If a descriptor that matches if found, instantiate it with the GUID of the element entity
|
||||
if (descriptor != null)
|
||||
_layerService.InstantiateLayerElement(layer, descriptor, elementEntity.Configuration, elementEntity.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SurfaceServiceOnSurfaceConfigurationUpdated(object sender, SurfaceConfigurationEventArgs e)
|
||||
{
|
||||
if (!e.Surface.IsActive)
|
||||
return;
|
||||
ApplySurfaceToProfiles(e.Surface);
|
||||
}
|
||||
|
||||
private void ApplySurfaceToProfiles(ArtemisSurface surface)
|
||||
private void ActiveProfilesPopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
|
||||
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
|
||||
profileModule.ActiveProfile.ApplySurface(surface);
|
||||
profileModule.ActiveProfile.PopulateLeds(surface);
|
||||
}
|
||||
|
||||
private void ActiveProfilesInstantiateProfileLayerElements()
|
||||
{
|
||||
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
|
||||
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
|
||||
InstantiateProfileLayerElements(profileModule.ActiveProfile);
|
||||
}
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void OnActiveSurfaceConfigurationChanged(object sender, SurfaceConfigurationEventArgs e)
|
||||
{
|
||||
ActiveProfilesPopulateLeds(e.Surface);
|
||||
}
|
||||
|
||||
private void OnSurfaceConfigurationUpdated(object sender, SurfaceConfigurationEventArgs e)
|
||||
{
|
||||
if (e.Surface.IsActive)
|
||||
ActiveProfilesPopulateLeds(e.Surface);
|
||||
}
|
||||
|
||||
private void OnPluginLoaded(object sender, PluginEventArgs e)
|
||||
{
|
||||
if (e.PluginInfo.Instance is LayerElementProvider)
|
||||
ActiveProfilesInstantiateProfileLayerElements();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
37
src/Artemis.Core/Utilities/EnumUtilities.cs
Normal file
37
src/Artemis.Core/Utilities/EnumUtilities.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Artemis.Core.Utilities
|
||||
{
|
||||
public static class EnumUtilities
|
||||
{
|
||||
public static string Description(this Enum value)
|
||||
{
|
||||
var attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (attributes.Any())
|
||||
return (attributes.First() as DescriptionAttribute).Description;
|
||||
|
||||
// If no description is found, the least we can do is replace underscores with spaces
|
||||
// You can add your own custom default formatting logic here
|
||||
var ti = CultureInfo.CurrentCulture.TextInfo;
|
||||
return ti.ToTitleCase(ti.ToLower(value.ToString().Replace("_", " ")));
|
||||
}
|
||||
|
||||
public static IEnumerable<ValueDescription> GetAllValuesAndDescriptions(Type t)
|
||||
{
|
||||
if (!t.IsEnum)
|
||||
throw new ArgumentException($"{nameof(t)} must be an enum type");
|
||||
|
||||
return Enum.GetValues(t).Cast<Enum>().Select(e => new ValueDescription {Value = e, Description = e.Description()}).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public class ValueDescription
|
||||
{
|
||||
public object Value { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Artemis.Core.Models.Profile;
|
||||
using System;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using SkiaSharp;
|
||||
@ -7,7 +8,7 @@ namespace Artemis.Plugins.LayerElements.Animations
|
||||
{
|
||||
public class RotationLayerElement : LayerElement
|
||||
{
|
||||
public RotationLayerElement(Layer layer, LayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, settings, descriptor)
|
||||
public RotationLayerElement(Layer layer, Guid guid, LayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, guid, settings, descriptor)
|
||||
{
|
||||
}
|
||||
|
||||
@ -20,17 +21,20 @@ namespace Artemis.Plugins.LayerElements.Animations
|
||||
|
||||
public override void Update(double deltaTime)
|
||||
{
|
||||
Rotation += (float)(deltaTime * 100);
|
||||
Rotation += (float) (deltaTime * 100);
|
||||
if (Rotation > 360)
|
||||
Rotation = 0;
|
||||
}
|
||||
|
||||
public override SKShader RenderPostProcess(ArtemisSurface surface, SKBitmap bitmap, SKShader shader)
|
||||
{
|
||||
var center = new SKPoint(Layer.AbsoluteRenderRectangle.MidX, Layer.AbsoluteRenderRectangle.MidY);
|
||||
|
||||
// TODO Scale so that the rectangle is covered in every rotation, instead of just putting it at 2
|
||||
return SKShader.CreateLocalMatrix(SKShader.CreateLocalMatrix(shader, SKMatrix.MakeScale(2, 2, center.X, center.Y)), SKMatrix.MakeRotationDegrees(Rotation, center.X, center.Y));
|
||||
var rect = Layer.AbsoluteRenderRectangle;
|
||||
var center = new SKPoint(rect.MidX, rect.MidY);
|
||||
|
||||
var required = (float) Math.Sqrt(rect.Width * rect.Width + rect.Height * rect.Height);
|
||||
var minSide = Math.Min(rect.Width, rect.Height);
|
||||
var scale = required / minSide;
|
||||
return SKShader.CreateLocalMatrix(SKShader.CreateLocalMatrix(shader, SKMatrix.MakeScale(scale, scale, center.X, center.Y)), SKMatrix.MakeRotationDegrees(Rotation, center.X, center.Y));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Artemis.Core.Models.Profile;
|
||||
using System;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using SkiaSharp;
|
||||
@ -7,7 +8,7 @@ namespace Artemis.Plugins.LayerElements.Animations
|
||||
{
|
||||
public class SlideLayerElement : LayerElement
|
||||
{
|
||||
public SlideLayerElement(Layer layer, LayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, settings, descriptor)
|
||||
public SlideLayerElement(Layer layer, Guid guid, LayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, guid, settings, descriptor)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@ -113,6 +113,7 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PostBuildEvent>echo Copying plugin to Artemis.UI output directory
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
@ -11,9 +13,9 @@ namespace Artemis.Plugins.LayerElements.Brush
|
||||
private SKShader _shader;
|
||||
private List<SKColor> _testColors;
|
||||
|
||||
public BrushLayerElement(Layer layer, BrushLayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, settings, descriptor)
|
||||
public BrushLayerElement(Layer layer, Guid guid, BrushLayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, guid, settings, descriptor)
|
||||
{
|
||||
Settings = settings ?? new BrushLayerElementSettings();
|
||||
Settings = settings;
|
||||
|
||||
_testColors = new List<SKColor>();
|
||||
for (var i = 0; i < 9; i++)
|
||||
@ -26,13 +28,31 @@ namespace Artemis.Plugins.LayerElements.Brush
|
||||
|
||||
CreateShader();
|
||||
Layer.RenderPropertiesUpdated += (sender, args) => CreateShader();
|
||||
Settings.PropertyChanged += (sender, args) => CreateShader();
|
||||
}
|
||||
|
||||
private void CreateShader()
|
||||
{
|
||||
var center = new SKPoint(Layer.AbsoluteRenderRectangle.MidX, Layer.AbsoluteRenderRectangle.MidY);
|
||||
var shader = SKShader.CreateSweepGradient(center, _testColors.ToArray(), null, SKShaderTileMode.Clamp, 0, 360);
|
||||
|
||||
SKShader shader;
|
||||
switch (Settings.BrushType)
|
||||
{
|
||||
case BrushType.Solid:
|
||||
shader = SKShader.CreateColor(_testColors.First());
|
||||
break;
|
||||
case BrushType.LinearGradient:
|
||||
shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(Layer.AbsoluteRenderRectangle.Width, 0), _testColors.ToArray(), SKShaderTileMode.Clamp);
|
||||
break;
|
||||
case BrushType.RadialGradient:
|
||||
shader = SKShader.CreateRadialGradient(center, Math.Min(Layer.AbsoluteRenderRectangle.Width, Layer.AbsoluteRenderRectangle.Height), _testColors.ToArray(), SKShaderTileMode.Clamp);
|
||||
break;
|
||||
case BrushType.SweepGradient:
|
||||
shader = SKShader.CreateSweepGradient(center, _testColors.ToArray(), null, SKShaderTileMode.Clamp, 0, 360);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
var oldShader = _shader;
|
||||
_shader = shader;
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using SkiaSharp;
|
||||
|
||||
@ -6,11 +7,40 @@ namespace Artemis.Plugins.LayerElements.Brush
|
||||
{
|
||||
public class BrushLayerElementSettings : LayerElementSettings
|
||||
{
|
||||
private BrushType _brushType;
|
||||
private List<SKColor> _colors;
|
||||
|
||||
public BrushLayerElementSettings()
|
||||
{
|
||||
BrushType = BrushType.Solid;
|
||||
Colors = new List<SKColor>();
|
||||
}
|
||||
|
||||
public List<SKColor> Colors { get; set; }
|
||||
public BrushType BrushType
|
||||
{
|
||||
get => _brushType;
|
||||
set => SetAndNotify(ref _brushType, value);
|
||||
}
|
||||
|
||||
public List<SKColor> Colors
|
||||
{
|
||||
get => _colors;
|
||||
set => SetAndNotify(ref _colors, value);
|
||||
}
|
||||
}
|
||||
|
||||
public enum BrushType
|
||||
{
|
||||
[Description("Solid")]
|
||||
Solid,
|
||||
|
||||
[Description("Linear Gradient")]
|
||||
LinearGradient,
|
||||
|
||||
[Description("Radial Gradient")]
|
||||
RadialGradient,
|
||||
|
||||
[Description("Sweep Gradient")]
|
||||
SweepGradient
|
||||
}
|
||||
}
|
||||
@ -20,35 +20,116 @@
|
||||
</UserControl.Resources>
|
||||
|
||||
<Grid Margin="12">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Setting 1 -->
|
||||
<StackPanel Grid.Row="0" Grid.Column="0">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
|
||||
<!-- Brush type -->
|
||||
<Grid Grid.Row="0">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0" VerticalAlignment="Center">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Brush type</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ComboBox HorizontalAlignment="Left"
|
||||
ItemsSource="{Binding Path=BrushTypes}"
|
||||
SelectedValuePath="Value"
|
||||
DisplayMemberPath="Description"
|
||||
SelectedValue="{Binding Path=LayerElement.Settings.BrushType}" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
|
||||
</Grid>
|
||||
|
||||
<!-- Sample 1 -->
|
||||
<Grid Grid.Row="2">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
|
||||
</Grid>
|
||||
|
||||
<!-- Sample 1 -->
|
||||
<Grid Grid.Row="3">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
|
||||
</Grid>
|
||||
|
||||
<!-- Setting 2 -->
|
||||
<StackPanel Grid.Row="2" Grid.Column="0">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="2" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignLightSeparator}" />
|
||||
<Grid Grid.Row="4">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
|
||||
</Grid>
|
||||
|
||||
<!-- Setting 2 -->
|
||||
<Grid Grid.Row="5">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
|
||||
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
|
||||
</StackPanel>
|
||||
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@ -1,4 +1,6 @@
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.Core.Utilities;
|
||||
|
||||
namespace Artemis.Plugins.LayerElements.Brush
|
||||
{
|
||||
@ -10,5 +12,6 @@ namespace Artemis.Plugins.LayerElements.Brush
|
||||
}
|
||||
|
||||
public new BrushLayerElement LayerElement { get; }
|
||||
public IEnumerable<ValueDescription> BrushTypes => EnumUtilities.GetAllValuesAndDescriptions(typeof(BrushType));
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.Abstract;
|
||||
using Artemis.Core.Plugins.Models;
|
||||
|
||||
@ -4,6 +4,8 @@ namespace Artemis.Storage.Entities.Profile
|
||||
{
|
||||
public class LayerElementEntity
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public Guid PluginGuid { get; set; }
|
||||
public string LayerElementType { get; set; }
|
||||
public string Configuration { get; set; }
|
||||
|
||||
@ -172,6 +172,7 @@
|
||||
<Compile Include="Bootstrapper.cs" />
|
||||
<Compile Include="Converters\ColorToDrawingColorConverter.cs" />
|
||||
<Compile Include="Converters\ColorToSolidColorBrushConverter.cs" />
|
||||
<Compile Include="Converters\EnumToCollectionConverter.cs" />
|
||||
<Compile Include="Converters\InverseBooleanConverter.cs" />
|
||||
<Compile Include="Converters\NullToImageConverter.cs" />
|
||||
<Compile Include="Converters\NullToVisibilityConverter.cs" />
|
||||
|
||||
48
src/Artemis.UI/Converters/EnumToCollectionConverter.cs
Normal file
48
src/Artemis.UI/Converters/EnumToCollectionConverter.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace Artemis.UI.Converters
|
||||
{
|
||||
[ValueConversion(typeof(Enum), typeof(IEnumerable<Tuple<object, object>>))]
|
||||
public class EnumToCollectionConverter : MarkupExtension, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return GetAllValuesAndDescriptions(value.GetType());
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
private static string Description(Enum value)
|
||||
{
|
||||
var attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (attributes.Any())
|
||||
return (attributes.First() as DescriptionAttribute)?.Description;
|
||||
|
||||
// If no description is found, the least we can do is replace underscores with spaces
|
||||
var ti = CultureInfo.CurrentCulture.TextInfo;
|
||||
return ti.ToTitleCase(ti.ToLower(value.ToString().Replace("_", " ")));
|
||||
}
|
||||
|
||||
private static IEnumerable<Tuple<object, object>> GetAllValuesAndDescriptions(Type t)
|
||||
{
|
||||
if (!t.IsEnum)
|
||||
throw new ArgumentException($"{nameof(t)} must be an enum type");
|
||||
|
||||
return Enum.GetValues(t).Cast<Enum>().Select(e => new Tuple<object, object>(e, Description(e))).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -19,7 +19,9 @@
|
||||
</TextBlock>
|
||||
<Separator Style="{StaticResource MaterialDesignDarkSeparator}" Margin="8 0" />
|
||||
</StackPanel>
|
||||
<ContentControl Grid.Row="1" s:View.Model="{Binding LayerElementViewModel}" />
|
||||
<ScrollViewer Grid.Row="1">
|
||||
<ContentControl s:View.Model="{Binding LayerElementViewModel}" Margin="0 0 10 0"/>
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
|
||||
</UserControl>
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
|
||||
@ -19,7 +20,18 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.ElementProperties
|
||||
|
||||
private void OnSelectedLayerElementChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (LayerElementViewModel?.LayerElement?.Settings != null)
|
||||
LayerElementViewModel.LayerElement.Settings.PropertyChanged -= SettingsOnPropertyChanged;
|
||||
|
||||
LayerElementViewModel = _profileEditorService.SelectedLayerElement?.GetViewModel();
|
||||
|
||||
if (LayerElementViewModel?.LayerElement?.Settings != null)
|
||||
LayerElementViewModel.LayerElement.Settings.PropertyChanged += SettingsOnPropertyChanged;
|
||||
}
|
||||
|
||||
private void SettingsOnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
_profileEditorService.UpdateSelectedProfileElement();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -31,12 +31,16 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerElements.Dialogs
|
||||
|
||||
public void Accept()
|
||||
{
|
||||
if (Session.IsEnded)
|
||||
return;
|
||||
var layerElement = _layerService.InstantiateLayerElement(Layer, SelectedLayerElementDescriptor);
|
||||
Session.Close(layerElement);
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
if (Session.IsEnded)
|
||||
return;
|
||||
Session.Close();
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ using System.Linq;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Profile.Abstract;
|
||||
using Artemis.Core.Plugins.LayerElement;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.UI.Screens.Module.ProfileEditor.LayerElements.Dialogs;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
using Stylet;
|
||||
@ -14,11 +15,13 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerElements
|
||||
{
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly IProfileEditorService _profileEditorService;
|
||||
private readonly ILayerService _layerService;
|
||||
private LayerElementViewModel _selectedLayerElement;
|
||||
|
||||
public LayerElementsViewModel(IProfileEditorService profileEditorService, IDialogService dialogService)
|
||||
public LayerElementsViewModel(IProfileEditorService profileEditorService, ILayerService layerService, IDialogService dialogService)
|
||||
{
|
||||
_profileEditorService = profileEditorService;
|
||||
_layerService = layerService;
|
||||
_dialogService = dialogService;
|
||||
|
||||
LayerElements = new BindableCollection<LayerElementViewModel>();
|
||||
@ -74,8 +77,11 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerElements
|
||||
new Dictionary<string, object> {{"layer", (Layer) SelectedProfileElement}}
|
||||
);
|
||||
|
||||
if (result is LayerElement layerElement)
|
||||
LayerElements.Add(new LayerElementViewModel(layerElement));
|
||||
if (!(result is LayerElement layerElement))
|
||||
return;
|
||||
|
||||
LayerElements.Add(new LayerElementViewModel(layerElement));
|
||||
_profileEditorService.UpdateSelectedProfileElement();
|
||||
}
|
||||
|
||||
public async void DeleteSelectedLayerElement()
|
||||
@ -88,8 +94,18 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerElements
|
||||
"Delete layer element",
|
||||
"Are you sure you want to delete the selected layer element?"
|
||||
);
|
||||
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
var layerElement = SelectedLayerElement.LayerElement;
|
||||
var layer = (Layer) SelectedProfileElement;
|
||||
|
||||
LayerElements.Remove(SelectedLayerElement);
|
||||
SelectedLayerElement = null;
|
||||
|
||||
_layerService.RemoveLayerElement(layer, layerElement);
|
||||
_profileEditorService.UpdateSelectedProfileElement();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user