/// <summary> /// Adds the connection. /// </summary> /// <param name="from">From parameter.</param> /// <param name="to">To parameter.</param> /// <param name="metric">The metric.</param> /// <returns>true if the connection was created; otherwise false.</returns> public bool AddConnection(string from, string to, int metric) { NetSimClient fromClient = this.Clients.FirstOrDefault(c => c.Id.Equals(from)); NetSimClient toClient = this.Clients.FirstOrDefault(c => c.Id.Equals(to)); if (fromClient == null || toClient == null) { return(false); } NetSimConnection connection = new NetSimConnection() { EndPointA = fromClient, EndPointB = toClient, Id = $"{from} - {to}", Metric = metric }; if (fromClient.Connections.ContainsKey(to) || toClient.Connections.ContainsKey(from)) { return(false); } // forward updates connection.StateUpdated += this.OnUpdated; this.Connections.Add(connection); fromClient.Connections.Add(to, connection); toClient.Connections.Add(from, connection); this.OnUpdated(); return(true); }
/// <summary> /// Creates the connection edge. /// </summary> /// <param name="edge">The edge.</param> /// <returns>The created element for the edge.</returns> private UIElement CreateConnectionEdge(NetSimConnection edge) { Line connectionLine = new Line { StrokeThickness = 4, StrokeDashOffset = 1, Stroke = Brushes.YellowGreen, Tag = edge, X1 = edge.EndPointA.Location.Left, Y1 = edge.EndPointA.Location.Top, X2 = edge.EndPointB.Location.Left, Y2 = edge.EndPointB.Location.Top, ToolTip = this.CreateConnectionTooltip(edge) }; if (edge.IsOffline) { connectionLine.StrokeDashArray = new DoubleCollection(new List <double>() { 2, 2 }); connectionLine.Stroke = Brushes.Red; } if (edge.IsTransmitting) { connectionLine.Stroke = Brushes.Orange; } return(connectionLine); }
/// <summary> /// Creates the message animation. /// </summary> /// <param name="edge">The edge.</param> /// <param name="message">The message.</param> /// <param name="uimessage">The message control.</param> /// <param name="step">The step.</param> /// <returns>The created storyboard for the message animation.</returns> private static Storyboard CreateMessageAnimation( NetSimConnection edge, ConnectionFrameMessage message, MessageControl uimessage, NetSimMessageTransmissionStep step) { INetSimVisualizableItem receiver; INetSimVisualizableItem sender; GetMessageReceiverSender(edge, message, out receiver, out sender); int middleTop = (edge.EndPointA.Location.Top + edge.EndPointB.Location.Top) / 2; int middleLeft = (edge.EndPointA.Location.Left + edge.EndPointB.Location.Left) / 2; if (receiver == null || sender == null) { // return empty storyboard return(new Storyboard()); } DoubleAnimation animationTop = new DoubleAnimation { Duration = TimeSpan.FromSeconds(0.5) }; DoubleAnimation animationLeft = new DoubleAnimation { Duration = TimeSpan.FromSeconds(0.5) }; if (step == NetSimMessageTransmissionStep.Sending) { animationTop.From = sender.Location.Top; animationTop.To = middleTop; animationLeft.From = sender.Location.Left; animationLeft.To = middleLeft; } else { animationTop.From = middleTop; animationTop.To = receiver.Location.Top; animationLeft.From = middleLeft; animationLeft.To = receiver.Location.Left; } Storyboard board = new Storyboard(); board.Children.Add(animationLeft); board.Children.Add(animationTop); Storyboard.SetTarget(animationTop, uimessage); Storyboard.SetTargetProperty(animationTop, new PropertyPath(Canvas.TopProperty)); Storyboard.SetTarget(animationLeft, uimessage); Storyboard.SetTargetProperty(animationLeft, new PropertyPath(Canvas.LeftProperty)); return(board); }
/// <summary> /// Gets the sender receiver. /// </summary> /// <param name="edge">The edge.</param> /// <param name="message">The message.</param> /// <param name="receiver">The receiver.</param> /// <param name="sender">The sender.</param> private static void GetMessageReceiverSender( NetSimConnection edge, ConnectionFrameMessage message, out INetSimVisualizableItem receiver, out INetSimVisualizableItem sender) { // receiver = (edge.EndPointA.Id.Equals(message.NextReceiver) ? edge.EndPointA : edge.EndPointB) as INetSimVisualizableItem; receiver = (edge.EndPointA.Id.Equals(message.Receiver) ? edge.EndPointA : edge.EndPointB) as INetSimVisualizableItem; sender = ((receiver == edge.EndPointA) ? edge.EndPointB : edge.EndPointA) as INetSimVisualizableItem; }
/// <summary> /// Creates the connection tooltip. /// </summary> /// <param name="edge">The edge.</param> /// <returns>The text for the connection tooltip.</returns> private string CreateConnectionTooltip(NetSimConnection edge) { if (edge == null) { return(string.Empty); } StringBuilder builder = new StringBuilder(); foreach (var message in edge.PendingMessages) { builder.AppendLine(message.ToString()); } return(builder.ToString()); }
/// <summary> /// Handles the mouse left button up. /// </summary> /// <param name="sender">The sender.</param> /// <param name="mouseButtonEventArgs">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param> public void HandleMouseLeftButtonUp(object sender, MouseButtonEventArgs mouseButtonEventArgs) { if (this.IsCreateNode && this.CurrentSelectedNode == null) { this.AddNode(mouseButtonEventArgs.GetPosition(this.DrawCanvas)); } if (this.IsCreateEdge && this.draftConnection?.EndPointA != null && this.draftConnection?.EndPointB != null) { this.AddEdge(this.draftConnection.EndPointA as NetSimItem, this.draftConnection.EndPointB as NetSimItem); this.draftConnection = null; } else { this.draftConnection = null; this.DrawCanvas.Children.Remove(this.draftConnectionLine); this.draftConnectionLine = null; } }
/// <summary> /// Handles the mouse left button down. /// </summary> /// <param name="sender">The sender.</param> /// <param name="mouseButtonEventArgs">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param> public void HandleMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs) { var item = this.GetCurrentItem(mouseButtonEventArgs.GetPosition(this.DrawCanvas)); if (item == null) { if (this.IsView) { this.CurrentViewedItem = null; } return; } // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull if (item is NetSimClient) { this.CurrentSelectedNode = item; switch (this.viewMode) { case ViewMode.CreateEdges: this.draftConnection = new NetSimConnection { EndPointA = (NetSimClient)item }; break; case ViewMode.View: this.CurrentViewedItem = item; break; } } if (item is NetSimConnection) { if (this.IsView) { this.CurrentViewedItem = item; } } }
/// <summary> /// Adds the edge. /// </summary> /// <param name="from">From item.</param> /// <param name="to">To item.</param> public void AddEdge(NetSimItem from, NetSimItem to) { try { lock (this.simulationLockObj) { if (!this.Simulator.AddConnection(from.Id, to.Id, 1)) { this.draftConnection = null; this.DrawCanvas.Children.Remove(this.draftConnectionLine); this.draftConnectionLine = null; } } this.CheckCanExecuteCommands(); } catch (Exception ex) { Trace.WriteLine(ex.Message); this.draftConnection = null; this.DrawCanvas.Children.Remove(this.draftConnectionLine); this.draftConnectionLine = null; } }
/// <summary> /// Initializes a new instance of the <see cref="ConnectionViewModel"/> class. /// </summary> /// <param name="connection">The connection.</param> public ConnectionViewModel(NetSimConnection connection) { this.Connection = connection; this.InitializeCommands(); }
/// <summary> /// Adds the message. /// </summary> /// <param name="edge">The edge.</param> /// <param name="message">The message.</param> private void AddMessage(NetSimConnection edge, ConnectionFrameMessage message) { INetSimVisualizableItem receiver; INetSimVisualizableItem sender; // get message receiver and sender GetMessageReceiverSender(edge, message, out receiver, out sender); var uimessage = new MessageControl(message.ShortName) { Width = 19, Height = 19, Tag = edge, MessagePath = { Tag = edge } }; // set message to sender location int top = sender.Location.Top; int left = sender.Location.Left; if (this.IsMessageSendingAnimationDone(message.Id)) { // calculate middle between A and B top = (edge.EndPointA.Location.Top + edge.EndPointB.Location.Top) / 2; left = (edge.EndPointA.Location.Left + edge.EndPointB.Location.Left) / 2; } // add meesage to this position Canvas.SetLeft(uimessage, left); Canvas.SetTop(uimessage, top); this.drawCanvas.Children.Add(uimessage); Storyboard storyBoard = null; if (message.TransmissionStep == NetSimMessageTransmissionStep.Sending) { if (!this.IsMessageSendingAnimationDone(message.Id)) { storyBoard = CreateMessageAnimation(edge, message, uimessage, NetSimMessageTransmissionStep.Sending); // messageStates[message.Id] = NetSimMessageTransmissionStep.Sending; } } else { if (!this.IsMessageReceivingAnimationDone(message.Id)) { storyBoard = CreateMessageAnimation( edge, message, uimessage, NetSimMessageTransmissionStep.Receiving); // messageStates[message.Id] = NetSimMessageTransmissionStep.Receiving; } } if (storyBoard == null) { return; } // when the animation has finished - mark animation as done // note: animation gets created multiple times - but only at last "redraw" // it has time to finish storyBoard.Completed += (s, e) => { this.messageStates[message.Id] = message.TransmissionStep; }; uimessage.BeginStoryboard(storyBoard); // start the animation storyBoard.Begin(); }