using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reflection; using SkiaSharp; namespace Artemis.Core.Nodes; /// /// Allows you to register one or more s usable by node scripts. /// public abstract class NodeProvider : PluginFeature { private readonly List _nodeDescriptors; /// /// Creates a new instance of the class. /// public NodeProvider() { _nodeDescriptors = new List(); NodeDescriptors = new ReadOnlyCollection(_nodeDescriptors); Disabled += OnDisabled; } /// /// A read-only collection of all nodes added with /// public ReadOnlyCollection NodeDescriptors { get; set; } /// /// Adds a node descriptor for a given node, so that it appears in the UI. /// Note: You do not need to manually remove these on disable /// /// The type of the node you wish to register protected void RegisterNodeType() where T : INode { RegisterNodeType(typeof(T)); } /// /// Adds a node descriptor for a given node, so that it appears in the UI. /// Note: You do not need to manually remove these on disable /// /// The type of the node you wish to register protected void RegisterNodeType(Type nodeType) { if (!IsEnabled) throw new ArtemisPluginFeatureException(this, "Can only add a node descriptor when the plugin is enabled"); if (nodeType == null) throw new ArgumentNullException(nameof(nodeType)); if (!nodeType.IsAssignableTo(typeof(INode))) throw new ArgumentException("Node has to be a base type of the Node-Type.", nameof(nodeType)); NodeAttribute? nodeAttribute = nodeType.GetCustomAttribute(); string name = nodeAttribute?.Name ?? nodeType.Name; string description = nodeAttribute?.Description ?? string.Empty; string category = nodeAttribute?.Category ?? string.Empty; string helpUrl = nodeAttribute?.HelpUrl ?? string.Empty; NodeData nodeData = new(this, nodeType, name, description, category, helpUrl, nodeAttribute?.InputType, nodeAttribute?.OutputType); _nodeDescriptors.Add(nodeData); NodeTypeStore.Add(nodeData); } /// /// Adds a color for lines of the provided type. /// /// The color to add. /// The type to use the color for. protected TypeColorRegistration RegisterTypeColor(SKColor color) { return NodeTypeStore.AddColor(typeof(T), color, this); } private void OnDisabled(object? sender, EventArgs e) { // The store will clean up the registrations by itself, the plugin feature just needs to clear its own list _nodeDescriptors.Clear(); } }