Click here to Skip to main content
15,892,927 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 301.8K   10K   142  
A library for adding DirectShow GraphEdit-like abilities to .NET applications
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using DaggerLib.Core;

namespace DaggerLib.UI.Windows
{
    public class PinUI
    {
        #region Fields

        // pin these UI elements belongs to
        private DaggerBasePin _pin;

        // style of tooltip to display for pin
        private PinToolTipStyle _pinToolTipStyle = PinToolTipStyle.NameShortType;

        // Pin's ToolTip text
        private string _toolTipText = string.Empty;

        // User defined context menu strip
        private ContextMenuStrip _userContextMenu;

        //the top left physical location of this pin in it's Parent UINode
        private Point _pinLocation = new Point(0, 0);

        //regions for hit testing and drawing
        internal Region targetRegion;

        #endregion

        #region ctor

        public PinUI(DaggerBasePin pin)
        {
            _pin = pin;
        }

        #endregion

        #region Events

        /// <summary>
        /// User defined ToolTip Popup Handler
        /// </summary>
        public event PopupEventHandler PinToolTipPopup;

        /// <summary>
        /// User defined ToolTip Draw Handler
        /// </summary>
        public event DrawToolTipEventHandler PinToolTipDraw;

        /// <summary>
        /// Allows DaggerNodeUI to call user defined PinToolTipPopup
        /// </summary>
        public void InvokeToolTipPopup(object sender, PopupEventArgs e)
        {
            if (PinToolTipPopup != null)
            {
                PinToolTipPopup(sender, e);
            }
        }

        /// <summary>
        /// Allows DaggerNodeUI to call user defined PinToolTipDraw
        /// </summary>
        public void InvokeToolTipDraw(object sender, DrawToolTipEventArgs e)
        {
            if (PinToolTipDraw != null)
            {
                PinToolTipDraw(sender, e);
            }
        }

        #endregion

        #region Properties

        public Region TargetRegion
        {
            get
            {
                return targetRegion;
            }
            set
            {
                targetRegion = value;
            }
        }

        public Point PinLocation
        {
            get
            {
                return _pinLocation;
            }
            set
            {
                _pinLocation = value;
            }
        }

        public Color NoodleColor
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].NoodleColor;
                }
                else
                {
                    return Color.Black;
                }
            }
        }

        /// <summary>
        /// Gets or sets the Pin's ToolTip text
        /// </summary>
        public string ToolTipText
        {
            set
            {
                _toolTipText = value;
            }
            get
            {
                if (_toolTipText == string.Empty)
                {
                    return _pin.Name;
                }
                else
                {
                    return _toolTipText;
                }
            }
        }

        /// <summary>
        /// Gets or Sets how the ToolTip is Displayed
        /// </summary>
        public PinToolTipStyle PinToolTipStyle
        {
            get
            {
                return _pinToolTipStyle;
            }
            set
            {
                _pinToolTipStyle = value;
            }
        }

        /// <summary>
        /// Returns true if the user has defined PinToolTipPopup
        /// </summary>
        public bool UserDefinedToolTipPopup
        {
            get
            {
                return PinToolTipPopup != null;
            }
        }

        /// <summary>
        /// Returns true if the user has defined PinToolTipDraw
        /// </summary>
        public bool UserDefinedToolTipDraw
        {
            get
            {
                return PinToolTipDraw != null;
            }
        }

        /// <summary>
        /// Context Menu to show for this Pin
        /// </summary>
        public ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return _userContextMenu;
            }
            set
            {
                _userContextMenu = value;
            }
        }

        public Bitmap PinImageConnected
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    if (_pin is DaggerInputPin)
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].InputPinImageConnected;
                    }
                    else
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].OutputPinImageConnected;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

        public Bitmap PinImageDisconnected
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    if (_pin is DaggerInputPin)
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].InputPinImageDisconnected;
                    }
                    else
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].OutputPinImageDisconnected;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

        public Color PinImageConnectedTransparent
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    if (_pin is DaggerInputPin)
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].InputPinImageConnectedTransparent;
                    }
                    else
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].OutputPinImageConnectedTransparent;
                    }
                }
                else
                {
                    return Color.Red;
                }
            }
        }

        public Color PinImageDisconnectedTransparent
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    if (_pin is DaggerInputPin)
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].InputPinImageDisconnectedTransparent;
                    }
                    else
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].OutputPinImageDisconnectedTransparent;
                    }
                }
                else
                {
                    return Color.Red;
                }
            }
        }

        public Region PinConnectedRegion
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    if (_pin is DaggerInputPin)
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].InputPinRegionConnected;
                    }
                    else
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].OutputPinRegionConnected;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

        public Region PinDisconnectedRegion
        {
            get
            {
                if (_pin.ParentUIGraph != null)
                {
                    if (_pin is DaggerInputPin)
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].InputPinRegionDisconnected;
                    }
                    else
                    {
                        return (_pin.ParentUIGraph as DaggerUIGraph).PinLegend[_pin.DataType].OutputPinRegionDisconnected;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

        #endregion
    }
}

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