Browse Source

NodeFactory refactor

CPKreuz 1 year ago
parent
commit
003c4a13c7

+ 2 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Factories/ImageLayerNodeFactory.cs

@@ -5,8 +5,8 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Factories;
 
 
 public class ImageLayerNodeFactory : NodeFactory<ImageLayerNode>
 public class ImageLayerNodeFactory : NodeFactory<ImageLayerNode>
 {
 {
-    public override T CreateNode<T>(IReadOnlyDocument document)
+    public override ImageLayerNode CreateNode(IReadOnlyDocument document)
     {
     {
-        return (T)(object)new ImageLayerNode(document.Size);
+        return new ImageLayerNode(document.Size);
     }
     }
 }
 }

+ 8 - 15
src/PixiEditor.ChangeableDocument/Changeables/Graph/NodeFactory.cs

@@ -3,28 +3,21 @@ using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 
 
 namespace PixiEditor.ChangeableDocument.Changeables.Graph;
 namespace PixiEditor.ChangeableDocument.Changeables.Graph;
 
 
-public abstract class NodeFactory
+public interface INodeFactory
 {
 {
     public Type NodeType { get; }
     public Type NodeType { get; }
 
 
-    public NodeFactory(Type nodeType)
-    {
-        NodeType = nodeType;
-    }
-
-    public abstract Node CreateNode(IReadOnlyDocument document);
+    public Node CreateNode(IReadOnlyDocument document);
 }
 }
 
 
-public abstract class NodeFactory<T> : NodeFactory where T : Node
+public abstract class NodeFactory<T> : INodeFactory where T : Node
 {
 {
-    public NodeFactory() : base(typeof(T))
-    {
-    }
-
-    public abstract T CreateNode<T>(IReadOnlyDocument document) where T : Node;
+    public Type NodeType { get; } = typeof(T);
+    
+    public abstract T CreateNode(IReadOnlyDocument document);
 
 
-    public override Node CreateNode(IReadOnlyDocument document)
+    Node INodeFactory.CreateNode(IReadOnlyDocument document)
     {
     {
-        return CreateNode<T>(document);
+        return CreateNode(document);
     }
     }
 }
 }

+ 5 - 5
src/PixiEditor.ChangeableDocument/Changes/NodeGraph/CreateNode_Change.cs

@@ -13,7 +13,7 @@ internal class CreateNode_Change : Change
 {
 {
     private Type nodeType;
     private Type nodeType;
     private Guid id;
     private Guid id;
-    private static Dictionary<Type, NodeFactory> allFactories;
+    private static Dictionary<Type, INodeFactory> allFactories;
     
     
     [GenerateMakeChangeAction]
     [GenerateMakeChangeAction]
     public CreateNode_Change(Type nodeType, Guid id)
     public CreateNode_Change(Type nodeType, Guid id)
@@ -23,11 +23,11 @@ internal class CreateNode_Change : Change
 
 
         if (allFactories == null)
         if (allFactories == null)
         {
         {
-            allFactories = new Dictionary<Type, NodeFactory>();
-            var factoryTypes = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsSubclassOf(typeof(NodeFactory)) && !x.IsAbstract && !x.IsInterface).ToImmutableArray();
+            allFactories = new Dictionary<Type, INodeFactory>();
+            var factoryTypes = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsSubclassOf(typeof(INodeFactory)) && !x.IsAbstract && !x.IsInterface).ToImmutableArray();
             foreach (var factoryType in factoryTypes)
             foreach (var factoryType in factoryTypes)
             {
             {
-                NodeFactory factory = (NodeFactory)Activator.CreateInstance(factoryType);
+                INodeFactory factory = (INodeFactory)Activator.CreateInstance(factoryType);
                 allFactories.Add(factory.NodeType, factory);
                 allFactories.Add(factory.NodeType, factory);
             }
             }
         }
         }
@@ -44,7 +44,7 @@ internal class CreateNode_Change : Change
             id = Guid.NewGuid();
             id = Guid.NewGuid();
 
 
         Node node = null;
         Node node = null;
-        if (allFactories.TryGetValue(nodeType, out NodeFactory factory))
+        if (allFactories.TryGetValue(nodeType, out INodeFactory factory))
         {
         {
             node = factory.CreateNode(target);
             node = factory.CreateNode(target);
         }
         }