Пример #1
0
        private void UpdateSelection()
        {
            designerCanvas.SelectionService.ClearSelection();

            Rect rubberBand = new Rect(startPoint.Value, endPoint.Value);

            foreach (Control item in designerCanvas.Children)
            {
                Rect itemRect   = VisualTreeHelper.GetDescendantBounds(item);
                Rect itemBounds = item.TransformToAncestor(designerCanvas).TransformBounds(itemRect);

                if (rubberBand.Contains(itemBounds))
                {
                    if (item is Connection)
                    {
                        designerCanvas.SelectionService.AddToSelection(item as ISelectable);
                    }
                    else
                    {
                        CircuitPart di = item as CircuitPart;
                        if (di.ParentID == Guid.Empty)
                        {
                            designerCanvas.SelectionService.AddToSelection(di);
                        }
                    }
                }
            }
        }
Пример #2
0
        private void Group_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var items = from item in this.SelectionService.CurrentSelection.OfType <CircuitPart>()
                        where item.ParentID == Guid.Empty
                        select item;

            Rect rect = GetBoundingRectangle(items);

            CircuitPart groupItem = new CircuitPart();

            groupItem.IsGroup = true;
            groupItem.Width   = rect.Width;
            groupItem.Height  = rect.Height;
            Canvas.SetLeft(groupItem, rect.Left);
            Canvas.SetTop(groupItem, rect.Top);
            Canvas groupCanvas = new Canvas();

            groupItem.Content = groupCanvas;
            Canvas.SetZIndex(groupItem, this.Children.Count);
            this.Children.Add(groupItem);

            foreach (CircuitPart item in items)
            {
                item.ParentID = groupItem.ID;
            }

            this.SelectionService.SelectItem(groupItem);
        }
Пример #3
0
 void CircuitPart_Loaded(object sender, RoutedEventArgs e)
 {
     if (base.Template != null)
     {
         ContentPresenter contentPresenter =
             this.Template.FindName("PART_ContentPresenter", this) as ContentPresenter;
         if (contentPresenter != null)
         {
             UIElement contentVisual = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;
             if (contentVisual != null)
             {
                 DragThumb thumb = this.Template.FindName("PART_DragThumb", this) as DragThumb;
                 if (thumb != null)
                 {
                     ControlTemplate template =
                         CircuitPart.GetDragThumbTemplate(contentVisual) as ControlTemplate;
                     if (template != null)
                     {
                         thumb.Template = template;
                     }
                 }
             }
         }
     }
 }
Пример #4
0
 private void SetConnectorDecoratorTemplate(CircuitPart item)
 {
     if (item.ApplyTemplate() && item.Content is UIElement)
     {
         ControlTemplate template  = CircuitPart.GetConnectorDecoratorTemplate(item.Content as UIElement);
         Control         decorator = item.Template.FindName("PART_ConnectorDecorator", item) as Control;
         if (decorator != null && template != null)
         {
             decorator.Template = template;
         }
     }
 }
Пример #5
0
        private Connector GetConnector(Guid itemID, String connectorName)
        {
            CircuitPart circuitPart = (from item in this.Children.OfType <CircuitPart>()
                                       where item.ID == itemID
                                       select item).FirstOrDefault();

            Control connectorDecorator = circuitPart.Template.FindName("PART_ConnectorDecorator", circuitPart) as Control;

            connectorDecorator.ApplyTemplate();

            return(connectorDecorator.Template.FindName(connectorName, connectorDecorator) as Connector);
        }
Пример #6
0
        private static CircuitPart DeserializeCircuitPart(XElement itemXML, Guid id, double OffsetX, double OffsetY)
        {
            CircuitPart item = new CircuitPart(id);

            item.Width    = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture);
            item.Height   = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture);
            item.ParentID = new Guid(itemXML.Element("ParentID").Value);
            item.IsGroup  = Boolean.Parse(itemXML.Element("IsGroup").Value);
            Canvas.SetLeft(item, Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + OffsetX);
            Canvas.SetTop(item, Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + OffsetY);
            Canvas.SetZIndex(item, Int32.Parse(itemXML.Element("zIndex").Value));
            Object content = XamlReader.Load(XmlReader.Create(new StringReader(itemXML.Element("Content").Value)));

            item.Content = content;
            return(item);
        }
Пример #7
0
        private void CopyCurrentSelection()
        {
            IEnumerable <CircuitPart> selectedCircuitParts =
                this.SelectionService.CurrentSelection.OfType <CircuitPart>();

            List <Connection> selectedConnections =
                this.SelectionService.CurrentSelection.OfType <Connection>().ToList();

            foreach (Connection connection in this.Children.OfType <Connection>())
            {
                if (!selectedConnections.Contains(connection))
                {
                    CircuitPart sourceItem = (from item in selectedCircuitParts
                                              where item.ID == connection.Source.ParentCircuitPart.ID
                                              select item).FirstOrDefault();

                    CircuitPart sinkItem = (from item in selectedCircuitParts
                                            where item.ID == connection.Sink.ParentCircuitPart.ID
                                            select item).FirstOrDefault();

                    if (sourceItem != null &&
                        sinkItem != null &&
                        BelongToSameGroup(sourceItem, sinkItem))
                    {
                        selectedConnections.Add(connection);
                    }
                }
            }

            XElement circuitPartsXML = SerializeCircuitParts(selectedCircuitParts);
            XElement connectionsXML  = SerializeConnections(selectedConnections);

            XElement root = new XElement("CircuitDiagram");

            root.Add(circuitPartsXML);
            root.Add(connectionsXML);

            root.Add(new XAttribute("OffsetX", 10));
            root.Add(new XAttribute("OffsetY", 10));

            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Xaml, root);
        }
Пример #8
0
        private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            XElement root = LoadSerializedDataFromFile();

            if (root == null)
            {
                return;
            }

            this.Children.Clear();
            this.SelectionService.ClearSelection();

            IEnumerable <XElement> itemsXML = root.Elements("CircuitParts").Elements("CircuitPart");

            foreach (XElement itemXML in itemsXML)
            {
                Guid        id   = new Guid(itemXML.Element("ID").Value);
                CircuitPart item = DeserializeCircuitPart(itemXML, id, 0, 0);
                this.Children.Add(item);
                SetConnectorDecoratorTemplate(item);
            }

            this.InvalidateVisual();

            IEnumerable <XElement> connectionsXML = root.Elements("Connections").Elements("Connection");

            foreach (XElement connectionXML in connectionsXML)
            {
                Guid sourceID = new Guid(connectionXML.Element("SourceID").Value);
                Guid sinkID   = new Guid(connectionXML.Element("SinkID").Value);

                String sourceConnectorName = connectionXML.Element("SourceConnectorName").Value;
                String sinkConnectorName   = connectionXML.Element("SinkConnectorName").Value;

                Connector sourceConnector = GetConnector(sourceID, sourceConnectorName);
                Connector sinkConnector   = GetConnector(sinkID, sinkConnectorName);

                Connection connection = new Connection(sourceConnector, sinkConnector);
                Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value));
                this.Children.Add(connection);
            }
        }
Пример #9
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);
            DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;

            if (dragObject != null && !String.IsNullOrEmpty(dragObject.Xaml))
            {
                CircuitPart newItem = null;
                Object      content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml)));

                Image img = content as Image;


                if (content != null)
                {
                    newItem         = new CircuitPart();
                    newItem.Content = content;

                    Point position = e.GetPosition(this);

                    if (dragObject.DesiredSize.HasValue)
                    {
                        Size desiredSize = dragObject.DesiredSize.Value;
                        newItem.Width  = desiredSize.Width;
                        newItem.Height = desiredSize.Height;

                        DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X - newItem.Width / 2));
                        DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y - newItem.Height / 2));
                    }
                    else
                    {
                        DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X));
                        DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y));
                    }

                    Canvas.SetZIndex(newItem, this.Children.Count);
                    this.Children.Add(newItem);
                    SetConnectorDecoratorTemplate(newItem);

                    //update selection
                    this.SelectionService.SelectItem(newItem);
                    newItem.Focus();
                }

                e.Handled = true;
                Console.WriteLine("number is {0:s}:", img.Name);



                if (img.Name == "hsc")
                {
                    hscc++;
                }
                if (img.Name == "hyc")
                {
                    hycc++;
                }
                if (img.Name == "stc")
                {
                    stcc++;
                }

                Console.WriteLine("count hsc is {0:d}:", hscc);
                Console.WriteLine("count hycc is {0:d}:", hycc);
                Console.WriteLine("count is {0:d}:", stcc);

                Console.WriteLine("amount is {0:d}:", (hscc * hscprice) + (hycc * hycprice) + (stcc * stcprice));
            }
        }
Пример #10
0
        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            XElement root = LoadSerializedDataFromClipBoard();

            if (root == null)
            {
                return;
            }

            // create CircuitItems
            Dictionary <Guid, Guid> mappingOldToNewIDs = new Dictionary <Guid, Guid>();
            List <ISelectable>      newItems           = new List <ISelectable>();
            IEnumerable <XElement>  itemsXML           = root.Elements("CircuitParts").Elements("CircuitPart");

            double offsetX = Double.Parse(root.Attribute("OffsetX").Value, CultureInfo.InvariantCulture);
            double offsetY = Double.Parse(root.Attribute("OffsetY").Value, CultureInfo.InvariantCulture);

            foreach (XElement itemXML in itemsXML)
            {
                Guid oldID = new Guid(itemXML.Element("ID").Value);
                Guid newID = Guid.NewGuid();
                mappingOldToNewIDs.Add(oldID, newID);
                CircuitPart item = DeserializeCircuitPart(itemXML, newID, offsetX, offsetY);
                this.Children.Add(item);
                SetConnectorDecoratorTemplate(item);
                newItems.Add(item);
            }

            // update group hierarchy
            SelectionService.ClearSelection();
            foreach (CircuitPart el in newItems)
            {
                if (el.ParentID != Guid.Empty)
                {
                    el.ParentID = mappingOldToNewIDs[el.ParentID];
                }
            }


            foreach (CircuitPart item in newItems)
            {
                if (item.ParentID == Guid.Empty)
                {
                    SelectionService.AddToSelection(item);
                }
            }

            // create Connections
            IEnumerable <XElement> connectionsXML = root.Elements("Connections").Elements("Connection");

            foreach (XElement connectionXML in connectionsXML)
            {
                Guid oldSourceID = new Guid(connectionXML.Element("SourceID").Value);
                Guid oldSinkID   = new Guid(connectionXML.Element("SinkID").Value);

                if (mappingOldToNewIDs.ContainsKey(oldSourceID) && mappingOldToNewIDs.ContainsKey(oldSinkID))
                {
                    Guid newSourceID = mappingOldToNewIDs[oldSourceID];
                    Guid newSinkID   = mappingOldToNewIDs[oldSinkID];

                    String sourceConnectorName = connectionXML.Element("SourceConnectorName").Value;
                    String sinkConnectorName   = connectionXML.Element("SinkConnectorName").Value;

                    Connector sourceConnector = GetConnector(newSourceID, sourceConnectorName);
                    Connector sinkConnector   = GetConnector(newSinkID, sinkConnectorName);

                    Connection connection = new Connection(sourceConnector, sinkConnector);
                    Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value));
                    this.Children.Add(connection);

                    SelectionService.AddToSelection(connection);
                }
            }

            DesignerCanvas.BringToFront.Execute(null, this);

            // update paste offset
            root.Attribute("OffsetX").Value = (offsetX + 10).ToString();
            root.Attribute("OffsetY").Value = (offsetY + 10).ToString();
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Xaml, root);
        }