1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Artemis/src/Artemis.Storage/Migrations/M0021GradientNodes.cs
Robert 9c117d2773 Nodes - Added gradient nodes
Nodes - Added color gradient pin type
Data bindings - Changed color gradient data bindings to now take a color gradient
2022-09-23 21:41:08 +02:00

87 lines
3.0 KiB
C#

using System;
using System.Linq;
using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Entities.Profile.Nodes;
using Artemis.Storage.Migrations.Interfaces;
using LiteDB;
namespace Artemis.Storage.Migrations;
public class M0021GradientNodes : IStorageMigration
{
private void MigrateDataBinding(PropertyEntity property)
{
NodeScriptEntity script = property.DataBinding.NodeScript;
NodeEntity exitNode = script.Nodes.FirstOrDefault(s => s.IsExitNode);
if (exitNode == null)
return;
// Create a new node at the same position of the exit node
NodeEntity gradientNode = new()
{
Id = Guid.NewGuid(),
Type = "ColorGradientNode",
PluginId = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff"),
Name = "Color Gradient",
Description = "Outputs a color gradient with the given colors",
X = exitNode.X,
Y = exitNode.Y,
Storage = property.Value // Copy the value of the property into the node storage
};
script.Nodes.Add(gradientNode);
// Move all connections of the exit node to the new node
foreach (NodeConnectionEntity connection in script.Connections)
{
if (connection.SourceNode == exitNode.Id)
{
connection.SourceNode = gradientNode.Id;
connection.SourcePinId++;
}
}
// Connect the data binding node to the source node
script.Connections.Add(new NodeConnectionEntity
{
SourceType = "ColorGradient",
SourceNode = exitNode.Id,
SourcePinCollectionId = -1,
SourcePinId = 0,
TargetType = "ColorGradient",
TargetNode = gradientNode.Id,
TargetPinCollectionId = -1,
TargetPinId = 0,
});
// Move the exit node to the right
exitNode.X += 300;
exitNode.Y += 30;
}
public int UserVersion => 21;
public void Apply(LiteRepository repository)
{
// Find all color gradient data bindings, there's no really good way to do this so infer it from the value
ILiteCollection<ProfileEntity> collection = repository.Database.GetCollection<ProfileEntity>();
foreach (ProfileEntity profileEntity in collection.FindAll())
{
foreach (LayerEntity layer in profileEntity.Layers)
MigrateDataBinding(layer.LayerBrush.PropertyGroup);
collection.Update(profileEntity);
}
}
private void MigrateDataBinding(PropertyGroupEntity propertyGroup)
{
foreach (PropertyGroupEntity propertyGroupPropertyGroup in propertyGroup.PropertyGroups)
MigrateDataBinding(propertyGroupPropertyGroup);
foreach (PropertyEntity property in propertyGroup.Properties)
{
if (property.Value.StartsWith("[{\"Color\":\"") && property.DataBinding?.NodeScript != null && property.DataBinding.IsEnabled)
MigrateDataBinding(property);
}
}
}