Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

DSGraphEdit: A Reasonable Facsimile of Microsoft's GraphEdit in .NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (79 votes)
28 Jan 2018MIT7 min read 297.2K   10K   142  
A library for adding DirectShow GraphEdit-like abilities to .NET applications
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;

namespace DaggerLib.Core
{
    [Serializable]
    internal class DaggerNodeNonSerializationAssistant : ISerializable
    {
        public List<DaggerInputPin> InputPins;
        public List<DaggerOutputPin> OutputPins;
        public Type NodeType;
        public Guid NodeInstanceGuid;

        public DaggerNodeNonSerializationAssistant(DaggerNode node)
        {
            NodeType = node.GetType();

            NodeInstanceGuid = node.InstanceGuid;

            InputPins = new List<DaggerInputPin>();
            foreach (DaggerInputPin pin in node.InputPins)
            {
                InputPins.Add(pin);
            }

            OutputPins = new List<DaggerOutputPin>();
            foreach (DaggerOutputPin pin in node.OutputPins)
            {
                OutputPins.Add(pin);
            }

            FieldInfo[] fi = NodeType.GetFields();

            for (int i = 0; i < fi.Length; i++)
            {
                if (fi[i].FieldType.IsSubclassOf(typeof(DaggerBasePin)))
                {
                    DaggerBasePin val = (DaggerBasePin)fi[i].GetValue(node);

                    if (val != null)
                    {
                        //if this pin is stored in the collection, serialize it's reflected FieldInfo
                        if (node.InputPins[val.InstanceGuid] != null || node.OutputPins[val.InstanceGuid] != null)
                        {
                            val._reflectedTargets.Add(fi[i]);
                        }
                    }
                }
            }
        }

        public DaggerNode CreateNode()
        {
            DaggerNode node = (DaggerNode)Activator.CreateInstance(NodeType);

            node.InputPins.Clear();
            node.OutputPins.Clear();
            
            //set it'a InstanceGuid
            node.InstanceGuid = NodeInstanceGuid;

            //give it the pins
            foreach (DaggerInputPin pin in InputPins)
            {
                node.InputPins.Add(pin);
            }

            foreach (DaggerOutputPin pin in OutputPins)
            {
                node.OutputPins.Add(pin);
            }

            // put pins into thier reflected fields
            foreach (DaggerInputPin pin in InputPins)
            {
                foreach (FieldInfo fi in pin._reflectedTargets)
                {
                    fi.SetValue(node, pin);
                }
            }
            foreach (DaggerOutputPin pin in OutputPins)
            {
                foreach (FieldInfo fi in pin._reflectedTargets)
                {
                    fi.SetValue(node, pin);
                }
            }

            return node;
        }

        public DaggerNodeNonSerializationAssistant(SerializationInfo info, StreamingContext ctxt)
        {
            NodeType = (Type)info.GetValue("NodeType", typeof(Type));
            InputPins = (List<DaggerInputPin>)info.GetValue("InputPins", typeof(List<DaggerInputPin>));
            OutputPins = (List<DaggerOutputPin>)info.GetValue("OutputPins", typeof(List<DaggerOutputPin>));
            NodeInstanceGuid = (Guid)info.GetValue("NodeGuid", typeof(Guid));
        }

        //Serialization function.
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("NodeType", NodeType);
            info.AddValue("NodeGuid", NodeInstanceGuid);
            info.AddValue("InputPins", InputPins);
            info.AddValue("OutputPins", OutputPins);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
United States United States
AKA Rich Insley.

I have over 25 years experience in programming, and I'm completely self taught. (Except for one year at California State University Fresno where I had to learn the God awful language Miranda (http://miranda.org.uk/). I've spent 10 years as a Paratrooper in the US Army during the Clinton Administration.

Comments and Discussions