Exemplo n.º 1
0
        private void AddElement_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.MenuItem mi = e.Source as System.Windows.Controls.MenuItem;

            TypeLoadData tld = builtinTypes[mi.Header.ToString()] as TypeLoadData;

            if (tld != null)
            {
                dynElement newEl = AddDynElement(tld.t, tld.assembly, mi.Header.ToString(), Guid.NewGuid(), 0.0, 0.0);
                if (newEl != null)
                {
                    newEl.CheckInputs();
                    return;
                }
            }

            tld = userTypes[mi.Header.ToString()] as TypeLoadData;
            if (tld != null)
            {
                dynElement newEl = AddDynElement(tld.t, tld.assembly, mi.Header.ToString(), Guid.NewGuid(), 0.0, 0.0);
                if (newEl != null)
                {
                    newEl.CheckInputs();
                    return;
                }
            }
        }
Exemplo n.º 2
0
        void OnMouseRightButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            hitResultsList.Clear();
            TestClick(e.GetPosition(workBench));

            dynElement dynEl = null;

            if (hitResultsList.Count > 0)
            {
                foreach (DependencyObject depObj in hitResultsList)
                {
                    //traverse the tree through all the
                    //hit elements to see if you get a port
                    dynEl = ElementClicked(depObj, typeof(dynElement)) as dynElement;
                    if (dynEl != null)
                    {
                        break;
                    }
                }
            }

            if (dynEl != null)
            {
                //this.statusText.Text = "DynElement selected...";

                workBench.elementBeingDragged = dynEl;
                workBench.DragElement();
            }
        }
Exemplo n.º 3
0
        public dynElement FindDynElementByGuid(Guid guid)
        {
            foreach (UIElement uiel in dynElementSettings.SharedInstance.Workbench.Children)
            {
                dynElement testEl = null;

                //walk up through the inheritance to find whether the base type is a dynElement
                Type startType = uiel.GetType();
                while (startType.BaseType != null)
                {
                    startType = startType.BaseType;
                    if (startType == typeof(dynElement))
                    {
                        testEl = uiel as dynElement;
                        break;
                    }
                }

                if (testEl != null)
                {
                    if (testEl.GUID == guid)
                    {
                        return(testEl);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        void UpdateElement(object sender, MouseButtonEventArgs e)
        {
            dynElement el = sender as dynElement;

            foreach (dynPort p in el.InPorts)
            {
                p.Update();
            }
            foreach (dynPort p in el.OutPorts)
            {
                p.Update();
            }
        }
Exemplo n.º 5
0
        public void SelectElement(dynElement sel)
        {
            if (!selectedElements.Contains(sel))
            {
                //set all other items to the unselected state
                foreach (dynElement el in selectedElements)
                {
                    el.Deselect();
                }

                selectedElements.Clear();
                selectedElements.Add(sel);
                sel.Select();
            }
        }
Exemplo n.º 6
0
        public void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isConnecting && activeConnector != null)
            {
                activeConnector.Redraw(e.GetPosition(workBench));
            }
            if (workBench.isDragInProgress)
            {
                dynElement el = workBench.elementBeingDragged as dynElement;
                if (el != null)
                {
                    foreach (dynPort p in el.InPorts)
                    {
                        p.Update();
                    }
                    foreach (dynPort p in el.OutPorts)
                    {
                        p.Update();
                    }
                    foreach (dynPort p in el.StatePorts)
                    {
                        p.Update();
                    }
                }
            }

            if (isPanning)
            {
                if (oldX == 0.0)
                {
                    oldX = e.GetPosition(border).X;
                    oldY = e.GetPosition(border).Y;
                }
                else
                {
                    newX           = e.GetPosition(border).X;
                    newY           = e.GetPosition(border).Y;
                    this.CurrentX += newX - oldX;
                    this.CurrentY += newY - oldY;
                    oldX           = newX;
                    oldY           = newY;
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// This method adds dynElements when opening from a file
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="nickName"></param>
        /// <param name="guid"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public dynElement AddDynElement(Type elementType, string nickName, Guid guid, double x, double y)
        {
            try
            {
                //create a new object from a type
                //that is passed in
                //dynElement el = (dynElement)Activator.CreateInstance(elementType, new object[] { nickName });
                dynElement el = (dynElement)Activator.CreateInstance(elementType);

                if (!string.IsNullOrEmpty(nickName))
                {
                    el.NickName = nickName;
                }
                else
                {
                    ElementNameAttribute elNameAttrib = this.GetType().GetCustomAttributes(typeof(ElementNameAttribute), true)[0] as ElementNameAttribute;
                    if (elNameAttrib != null)
                    {
                        el.NickName = elNameAttrib.ElementName;
                    }
                }
                el.GUID = guid;

                //store the element in the elements list
                elements.Add(el);

                workBench.Children.Add(el);

                Canvas.SetLeft(el, x);
                Canvas.SetTop(el, y);

                //create an event on the element itself
                //to update the elements ports and connectors
                el.PreviewMouseRightButtonDown += new MouseButtonEventHandler(UpdateElement);

                return(el);
            }
            catch (Exception e)
            {
                dynElementSettings.SharedInstance.Bench.Log("Could not create an instance of the selected type.");
                return(null);
            }
        }
Exemplo n.º 8
0
        private void DeleteElement(dynElement el)
        {
            foreach (dynPort p in el.OutPorts)
            {
                for (int i = p.Connectors.Count - 1; i >= 0; i--)
                {
                    p.Connectors[i].Kill();
                }
            }
            foreach (dynPort p in el.InPorts)
            {
                for (int i = p.Connectors.Count - 1; i >= 0; i--)
                {
                    p.Connectors[i].Kill();
                }
            }

            selectedElements.Remove(el);
            elements.Remove(el);
            dynElementSettings.SharedInstance.Workbench.Children.Remove(el);
            el = null;
        }
Exemplo n.º 9
0
        /// <summary>
        /// This method adds dynElements when selected in the menu
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="nickName"></param>
        /// <param name="guid"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public dynElement AddDynElement(Type elementType, Assembly assem, string nickName, Guid guid, double x, double y)
        {
            try
            {
                //http://msdn.microsoft.com/en-us/library/ms173139.aspx
                //http://stackoverflow.com/questions/4993098/wpf-control-throwing-resource-identified-by-the-uri-missing-exception
                //http://www.matthidinger.com/archive/2008/10/12/managed-addin-framework-system.addin-with-wpf.aspx

                //create a new object from a type
                //that is passed in
                //dynElement el = (dynElement)Activator.CreateInstance(elementType, new object[] { nickName });
                System.Runtime.Remoting.ObjectHandle obj = Activator.CreateInstanceFrom(assem.Location, elementType.FullName);
                dynElement el = (dynElement)obj.Unwrap();

                el.GUID = guid;

                //store the element in the elements list
                elements.Add(el);

                workBench.Children.Add(el);

                Canvas.SetLeft(el, x);
                Canvas.SetTop(el, y);

                //create an event on the element itself
                //to update the elements ports and connectors
                el.PreviewMouseRightButtonDown += new MouseButtonEventHandler(UpdateElement);

                return(el);
            }
            catch (Exception e)
            {
                dynElementSettings.SharedInstance.Bench.Log(e.Message);
                return(null);
            }
        }
Exemplo n.º 10
0
        void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //Keyboard.Focus(this);

            hitResultsList.Clear();
            TestClick(e.GetPosition(workBench));

            #region test for a port
            dynPort p = null;
            if (hitResultsList.Count > 0)
            {
                foreach (DependencyObject depObj in hitResultsList)
                {
                    //traverse the tree through all the
                    //hit elements to see if you get a port
                    p = ElementClicked(depObj, typeof(dynPort)) as dynPort;
                    if (p != null)
                    {
                        break;
                    }
                }
            }


            if (p != null)
            {
                if (!isConnecting)
                {
                    //test if port already has a connection if so grab it
                    //and begin connecting to somewhere else
                    //don't allow the grabbing of the start connector
                    if (p.Connectors.Count > 0 && p.Connectors[0].Start != p)
                    {
                        activeConnector = p.Connectors[0];
                        activeConnector.Disconnect(p);
                        isConnecting           = true;
                        workBench.isConnecting = true;
                    }
                    else
                    {
                        try
                        {
                            //you've begun creating a connector
                            dynConnector c = new dynConnector(p, workBench, e.GetPosition(workBench));
                            activeConnector        = c;
                            isConnecting           = true;
                            workBench.isConnecting = true;
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }
                    }
                }
                else
                {
                    //attempt a connection between the port
                    //and the connector
                    if (!activeConnector.Connect(p))
                    {
                        activeConnector.Kill();
                        isConnecting           = false;
                        workBench.isConnecting = false;
                        activeConnector        = null;
                    }
                    else
                    {
                        //you've already started connecting
                        //now you're going to stop
                        isConnecting           = false;
                        workBench.isConnecting = false;
                        activeConnector        = null;
                    }
                }

                //set the handled flag so that the element doesn't get dragged
                e.Handled = true;
            }
            else
            {
                //if you click on the canvas and you're connecting
                //then drop the connector, otherwise do nothing
                if (activeConnector != null)
                {
                    activeConnector.Kill();
                    isConnecting           = false;
                    workBench.isConnecting = false;
                    activeConnector        = null;
                }
            }
            #endregion

            #region test for canvas
            hitResultsList.Clear();
            TestClick(e.GetPosition(workBench));

            DragCanvas dc = null;
            if (hitResultsList.Count > 0)
            {
                foreach (DependencyObject depObj in hitResultsList)
                {
                    //traverse the tree through all the
                    //hit elements to see if you get a port
                    dc = ElementClicked(depObj, typeof(DragCanvas)) as DragCanvas;
                    if (dc != null)
                    {
                        Debug.WriteLine("Canvas clicked");
                        ClearSelection();
                        break;
                    }
                }
            }
            #endregion

            #region test for dyn element
            hitResultsList.Clear();
            TestClick(e.GetPosition(workBench));

            dynElement element = null;
            if (hitResultsList.Count > 0)
            {
                foreach (DependencyObject depObj in hitResultsList)
                {
                    //traverse the tree through all the
                    //hit elements to see if you get an element
                    element = ElementClicked(depObj, typeof(dynElement)) as dynElement;
                    if (element != null)
                    {
                        SelectElement(element);
                        break;
                    }
                }
            }
            #endregion
        }
Exemplo n.º 11
0
        bool OpenWorkbench(string xmlPath)
        {
            Log("Opening workbench " + xmlPath + "...");
            CleanWorkbench();

            try
            {
                #region read xml file
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlPath);

                XmlNodeList elNodes = xmlDoc.GetElementsByTagName("dynElements");
                XmlNodeList cNodes  = xmlDoc.GetElementsByTagName("dynConnectors");

                XmlNode elNodesList = elNodes[0] as XmlNode;
                XmlNode cNodesList  = cNodes[0] as XmlNode;

                foreach (XmlNode elNode in elNodesList.ChildNodes)
                {
                    XmlAttribute typeAttrib     = elNode.Attributes[0];
                    XmlAttribute guidAttrib     = elNode.Attributes[1];
                    XmlAttribute nicknameAttrib = elNode.Attributes[2];
                    XmlAttribute xAttrib        = elNode.Attributes[3];
                    XmlAttribute yAttrib        = elNode.Attributes[4];

                    string typeName = typeAttrib.Value.ToString();
                    Guid   guid     = new Guid(guidAttrib.Value.ToString());
                    string nickname = nicknameAttrib.Value.ToString();

                    double x = Convert.ToDouble(xAttrib.Value.ToString());
                    double y = Convert.ToDouble(yAttrib.Value.ToString());

                    Type t = Type.GetType(typeName);

                    dynElement el = AddDynElement(t, nickname, guid, x, y);

                    //read the sub elements
                    //set any numeric values
                    foreach (XmlNode subNode in elNode.ChildNodes)
                    {
                        if (subNode.Name == "System.Double")
                        {
                            double val = Convert.ToDouble(subNode.Attributes[0].Value);
                            el.OutPortData[0].Object = val;
                            el.Update();
                        }
                        else if (subNode.Name == "System.Int32")
                        {
                            int val = Convert.ToInt32(subNode.Attributes[0].Value);
                            el.OutPortData[0].Object = val;
                            el.Update();
                        }
                    }
                }

                dynElementSettings.SharedInstance.Workbench.UpdateLayout();

                foreach (XmlNode connector in cNodesList.ChildNodes)
                {
                    XmlAttribute guidStartAttrib = connector.Attributes[0];
                    XmlAttribute intStartAttrib  = connector.Attributes[1];
                    XmlAttribute guidEndAttrib   = connector.Attributes[2];
                    XmlAttribute intEndAttrib    = connector.Attributes[3];
                    XmlAttribute portTypeAttrib  = connector.Attributes[4];

                    Guid guidStart  = new Guid(guidStartAttrib.Value.ToString());
                    Guid guidEnd    = new Guid(guidEndAttrib.Value.ToString());
                    int  startIndex = Convert.ToInt16(intStartAttrib.Value.ToString());
                    int  endIndex   = Convert.ToInt16(intEndAttrib.Value.ToString());
                    int  portType   = Convert.ToInt16(portTypeAttrib.Value.ToString());

                    //find the elements to connect
                    dynElement start = null;
                    dynElement end   = null;

                    foreach (dynElement e in dynElementSettings.SharedInstance.Bench.Elements)
                    {
                        if (e.GUID == guidStart)
                        {
                            start = e;
                        }
                        else if (e.GUID == guidEnd)
                        {
                            end = e;
                        }
                        if (start != null && end != null)
                        {
                            break;
                        }
                    }

                    //don't connect if the end element is an instance map
                    //those have a morphing set of inputs
                    //dynInstanceParameterMap endTest = end as dynInstanceParameterMap;

                    //if (endTest != null)
                    //{
                    //    continue;
                    //}

                    if (start != null && end != null && start != end)
                    {
                        dynConnector newConnector = new dynConnector(start, end, startIndex,
                                                                     endIndex, portType);

                        //connectors.Add(newConnector);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Log("There was an error opening the workbench.");
                Log(ex.Message);
                Log(ex.StackTrace);
                Debug.WriteLine(ex.Message + ":" + ex.StackTrace);
                CleanWorkbench();
                return(false);
            }
            return(true);
        }
Exemplo n.º 12
0
        public dynConnector(dynElement start, dynElement end, int startIndex, int endIndex, int portType)
        {
            //this.workBench = settings.WorkBench;

            //if (start != null && end != null && start != end)
            //{
            //in the start element, find the out port at the startIndex
            pStart = start.OutPorts[startIndex];

            dynPort endPort = null;

            if (portType == 0)
            {
                endPort = end.InPorts[endIndex];
            }
            else if (portType == 1)
            {
                endPort = end.StatePorts[endIndex];
            }

            //connect the two ports
            //get start point

            pStart.Connect(this);

            #region bezier creation
            connector                 = new Path();
            connector.Stroke          = Brushes.Black;
            connector.StrokeThickness = STROKE_THICKNESS;
            connector.Opacity         = .8;

            DoubleCollection dashArray = new DoubleCollection();
            dashArray.Add(5); dashArray.Add(2);
            connector.StrokeDashArray = dashArray;

            PathGeometry connectorGeometry = new PathGeometry();
            connectorPoints = new PathFigure();
            connectorCurve  = new BezierSegment();

            connectorPoints.StartPoint = new Point(pStart.Center.X, pStart.Center.Y);
            connectorCurve.Point1      = connectorPoints.StartPoint;
            connectorCurve.Point2      = connectorPoints.StartPoint;
            connectorCurve.Point3      = connectorPoints.StartPoint;

            connectorPoints.Segments.Add(connectorCurve);
            connectorGeometry.Figures.Add(connectorPoints);
            connector.Data = connectorGeometry;
            dynElementSettings.SharedInstance.Workbench.Children.Add(connector);

            #endregion

            isDrawing = true;

            //set this to not draggable
            Dynamo.Controls.DragCanvas.SetCanBeDragged(this, false);
            Dynamo.Controls.DragCanvas.SetCanBeDragged(connector, false);

            //set the z order to the front
            Canvas.SetZIndex(this, 300);

            this.Connect(endPort);

            //}
        }