private void ItemDropped(object sender, DragEventArgs e) { if (e.Data.GetData(typeof(MenuComputerElement)) is MenuComputerElement) { var point = e.GetPosition(GetMainPanel()); var computer = new Computer(point, ElementsController.GetNextId()); ElementsController.AddElement(computer); GetMainPanel().Children.Add(new ComputerElement(MENU_ELEMENT_WIDTH, MENU_ELEMENT_HEIGHT, ImageController.Open("computer.svg"), SchemeElementMouseDown, point.X, point.Y, 1, computer)); } else if (e.Data.GetData(typeof(MenuRouterElement)) is MenuRouterElement) { var point = e.GetPosition(GetMainPanel()); var router = new Router(point, ElementsController.GetNextId()); ElementsController.AddElement(router); GetMainPanel().Children.Add(new RouterElement(MENU_ELEMENT_WIDTH, MENU_ELEMENT_HEIGHT, ImageController.Open("router.svg"), SchemeElementMouseDown, point.X, point.Y, 8, router)); } else if (e.Data.GetData(typeof(ComputerElement)) is ComputerElement) { var elem = ((ComputerElement)(e.Data.GetData(typeof(ComputerElement)))); var point = e.GetPosition(GetMainPanel()); MoveElement(elem, point); } else if (e.Data.GetData(typeof(RouterElement)) is RouterElement) { var elem = ((RouterElement)(e.Data.GetData(typeof(RouterElement)))); var point = e.GetPosition(GetMainPanel()); MoveElement(elem, point); } }
private void buttonDeleteElement_Click(object sender, RoutedEventArgs e) { if (element is BaseUIElement) { var elem = element as BaseUIElement; ElementsController.RemoveElement(elem); UIController.RemoveElement(elem); } else { var elem = element as Link; ElementsController.RemoveLink(elem); UIController.RemoveLink(graphEdge); } Close(); }
private void MoveElement(BaseUIElement elem, Point point) { elem.SetLocation(point.X, point.Y); ElementsController.ChangeLocation(elem, point); GetMainPanel().Dispatcher.Invoke(delegate() { return; }, DispatcherPriority.Render); foreach (var child in GetMainPanel().Children) { if (child is GraphEdge) { var c = child as GraphEdge; if (c.SourceElement == elem) { var sourceOffset = c.SourceCB.TransformToAncestor(c.SourceCB.Parent as Canvas).Transform(new Point(0, 0)); var sourcePoint = c.SourceCB.TransformToAncestor(GetMainPanel()).Transform(new Point(0, 0)); sourcePoint.X += sourceOffset.X; sourcePoint.Y += sourceOffset.Y; c.Source = sourcePoint; } else if (c.DestinationElement == elem) { var sourceOffset = c.DestinationCB.TransformToAncestor(c.DestinationCB.Parent as Canvas).Transform(new Point(0, 0)); var sourcePoint = c.DestinationCB.TransformToAncestor(GetMainPanel()).Transform(new Point(0, 0)); sourcePoint.X += sourceOffset.X; sourcePoint.Y += sourceOffset.Y; c.Destination = sourcePoint; } } } }
private void OpenMenuClick(object sender, RoutedEventArgs e) { NewMenuClick(null, null); var text = File.ReadAllText(Directory.GetCurrentDirectory() + "save.txt"); var str = XElement.Parse(text); try { str.Elements().ToList().ForEach(x => { if (x.Name.ToString().Contains("Computer")) { var point = x.Attribute("Point"); var val = point.Value.Split(new char[] { ';' }); var id = x.Attribute("Id").Value; ElementsController.SetLastId(int.Parse(id)); var computer = new Computer(new Point(double.Parse(val[0]), double.Parse(val[1])), int.Parse(id)) { Name = x.Attribute("Name").Value }; ElementsController.AddElement(computer); GetMainPanel().Children.Add(new ComputerElement(MENU_ELEMENT_WIDTH, MENU_ELEMENT_HEIGHT, ImageController.Open("computer.svg"), SchemeElementMouseDown, computer.Point.X, computer.Point.Y, 1, computer, computer.Name)); } else if (x.Name.ToString().Contains("Router")) { var point = x.Attribute("Point"); var val = point.Value.Split(new char[] { ';' }); var id = x.Attribute("Id").Value; ElementsController.SetLastId(int.Parse(id)); var router = new Router(new Point(double.Parse(val[0]), double.Parse(val[1])), int.Parse(id)) { Name = x.Attribute("Name").Value }; ElementsController.AddElement(router); GetMainPanel().Children.Add(new RouterElement(MENU_ELEMENT_WIDTH, MENU_ELEMENT_HEIGHT, ImageController.Open("router.svg"), SchemeElementMouseDown, router.Point.X, router.Point.Y, 8, router, router.Name)); } else { var length = int.Parse(x.Attribute("Length").Value); var id1 = int.Parse(x.Attribute("Element1").Value); var id2 = int.Parse(x.Attribute("Element2").Value); var fp = int.Parse(x.Attribute("FirstPosition").Value); var sp = int.Parse(x.Attribute("SecondPosition").Value); var s = ElementsController.Elements.First(y => y.Id == id1); var d = ElementsController.Elements.First(y => y.Id == id2); ElementsController.AddLink(s, d, fp, sp, length); BaseUIElement firstBaseElement = null; BaseUIElement secondBaseElement = null; for (var i = 0; i < GetMainPanel().Children.Count; i++) { if (GetMainPanel().Children[i] is BaseUIElement) { var el = GetMainPanel().Children[i] as BaseUIElement; if (el.Element.Equals(s)) { firstBaseElement = el; } else if (el.Element.Equals(d)) { secondBaseElement = el; } } } var firstPin = firstBaseElement.GetPinByNumber(fp); var secondPin = secondBaseElement.GetPinByNumber(sp); var source = firstPin.GetCheckBox(); var destination = secondPin.GetCheckBox(); var sourceOffset = destination.TransformToAncestor(destination.Parent as Canvas).Transform(new Point(0, 0)); var destinationOffset = source.TransformToAncestor(source.Parent as Canvas).Transform(new Point(0, 0)); var sourcePoint = destination.TransformToAncestor(GetMainPanel()).Transform(new Point(0, 0)); sourcePoint.X += sourceOffset.X; sourcePoint.Y += sourceOffset.Y; var destinationPoint = source.TransformToAncestor(GetMainPanel()).Transform(new Point(0, 0)); destinationPoint.X += destinationOffset.X; destinationPoint.Y += destinationOffset.Y; DrawLine(destinationPoint, sourcePoint, firstPin.ParentElement, secondPin.ParentElement, source, destination); } GetMainPanel().Dispatcher.Invoke(delegate() { return; }, DispatcherPriority.Render); }); } catch { MessageBox.Show("Ошибка загрузки файла"); } }
private void NewMenuClick(object sender, RoutedEventArgs e) { GetMainPanel().Children.Clear(); ElementsController.Clear(); }