public override void MouseClick(PointF location, Keys controlKeys) { GraphicBaseElement element = m_Editor.GetElementAt(location); if (element == null) { if (m_FromElement != null) { //floating connection - create a new connection line FloatingConnection(location); m_LastMouseLocation = location; } return; } if (element is GraphicInputOutputElement == false && element is GraphicConnection == false) { return; } //user clicked a terminal of an element GraphicInputOutputElement ioelem = element as GraphicInputOutputElement; GraphicTerminal graphicTerminal = null; if (ioelem != null) { graphicTerminal = ioelem.GetTerminalAt(location); if (graphicTerminal == null || graphicTerminal.Equals(m_FromElement)) { return; } if (m_FromElement == null) { m_FromElement = graphicTerminal; m_LastMouseLocation = location; return; } TryConnectToTerminal(graphicTerminal, location); m_LastMouseLocation = location; } //user clicked a connection GraphicConnection connection = element as GraphicConnection; if (connection != null) { if (connection.Equals(m_FromElement)) { return; } if (m_FromElement == null) { m_FromElement = connection; m_LastMouseLocation = location; return; } TryConnectToConnection(connection, location); m_LastMouseLocation = location; } }
public override void MouseClick(PointF location, Keys controlKeys) { GraphicBaseElement element = m_Editor.GetElementAt(location); if (element == null) { return; } if (element is GraphicInputOutputElement == false && element is GraphicConnection == false) { return; } GraphicConnection graphicConnection = null; //user clicked a terminal of an element GraphicInputOutputElement ioelem = element as GraphicInputOutputElement; if (ioelem != null) { GraphicTerminal graphicTerminal = ioelem.GetTerminalAt(location); if (graphicTerminal != null) { Terminal terminal = (graphicTerminal.LinkedObject as Terminal); if (terminal.IsConnected) { Connection connection = terminal.Connection; graphicConnection = terminal.Connection.LinkedObject as GraphicConnection; graphicConnection.RemoveChild(graphicTerminal.ConnectionNode.Lines[0]); //terminal.Disconnect(); connection.DisconnectTerminal(terminal); if (connection.Terminals.Count == 0) { m_Editor.RemoveElement(connection.LinkedObject as GraphicConnection); } m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); m_Editor.Invalidate(); } } } //user clicked a connection graphicConnection = element as GraphicConnection; if (graphicConnection != null) { foreach (GraphicBaseElement child in graphicConnection.Children) { graphicConnection.RemoveChild(child); } Connection connection = graphicConnection.LinkedObject as Connection; foreach (Terminal terminal in connection.Terminals) { //terminal.Disconnect(); connection.DisconnectTerminal(terminal); } m_Editor.RemoveElement(graphicConnection); } }
public PortPart(PointF location, DirectionType direction, string name) { Terminal terminal = new Terminal(direction); m_GraphicTerminal = new GraphicTerminal(terminal); if (direction == DirectionType.Input) { m_GraphicTerminal.Angle = 180; } m_GraphicTerminal.Location = location; m_Location = location; m_Name = name; }
/// <summary> /// Constructor /// </summary> /// <param name="location">location of the port</param> /// <param name="direction">direction of the port</param> /// <param name="name">name of the port</param> public PortPart(PointF location, float angle, DirectionType direction, string name) { Terminal terminal = new Terminal(direction); m_GraphicTerminal = new GraphicTerminal(terminal); if (angle != 0) { m_GraphicTerminal.Angle = angle; } m_GraphicTerminal.Location = location; m_Location = location; m_Name = name; }
private void CreateConnectionLines(Circuit circuit, GraphicConnection graphicConnection, ConnectionData connectionData) { if (connectionData.Items == null || connectionData.Items.Length == 0) { //downward compatibility - no detailed information available: Connection connection = graphicConnection.LinkedObject as Connection; GraphicTerminal previous = null; //displays straight lines from one terminal to another foreach (Terminal terminal in connection.Terminals) { GraphicTerminal graphicTerminal = terminal.LinkedObject as GraphicTerminal; if (previous != null) { graphicConnection.AddChild(new ConnectionLine(previous.ConnectionNode, graphicTerminal.ConnectionNode)); } previous = graphicTerminal; } return; } //create nodes foreach (ConnectionItemData itemData in connectionData.Items) { if (itemData is NodeItemData) { NodeItemData nodeData = itemData as NodeItemData; ConnectionNode node = new ConnectionNode(nodeData.X, nodeData.Y); graphicConnection.AddChild(node); } } //create lines foreach (ConnectionItemData itemData in connectionData.Items) { if (itemData is LineItemData) { LineItemData lineData = itemData as LineItemData; ConnectionNode node1 = SearchNode(lineData.Node1, circuit, graphicConnection); ConnectionNode node2 = SearchNode(lineData.Node2, circuit, graphicConnection); if (node1 != null && node2 != null) { ConnectionLine line = new ConnectionLine(node1, node2); graphicConnection.AddChild(line); } } } }
public override void MouseMove(MouseButtons button, PointF location, Keys controlKeys) { if (m_Editor == null) { return; } GraphicBaseElement element = m_Editor.GetElementAt(location); if (m_HighLightElement != null) { if (element == null || (element.Equals(m_ParentElement) == false && m_HighLightElement is GraphicTerminal) || (element.Equals(m_HighLightElement) == false && m_HighLightElement is GraphicConnection)) { //Un-Hover the element UnHighlightElement(m_HighLightElement); return; } } if (element is GraphicInputOutputElement) { GraphicInputOutputElement ioelem = element as GraphicInputOutputElement; GraphicTerminal graphicTerminal = ioelem.GetTerminalAt(location); if (graphicTerminal != null) { if ((graphicTerminal.LinkedObject as Terminal).IsConnected == false) { //Hover an element m_ParentElement = element; HighlightElement(graphicTerminal); } } else if (m_HighLightElement != null) { //Un-Hover the element UnHighlightElement(m_HighLightElement); } } if (element is GraphicConnection) { if (m_HighLightElement == null) { //Hover an element HighlightElement(element); } } }
/// <summary> /// Connect to a Terminal (resp. its connection) /// </summary> /// <param name="graphicTerminal"></param> private void TryConnectToTerminal(GraphicTerminal graphicTerminal, PointF location) { Terminal toTerminal = graphicTerminal.LinkedObject as Terminal; if (toTerminal.Connection != null) { //merging connections not supported by clicking terminals return; } if (m_FromElement is GraphicTerminal) { Terminal fromTerminal = m_FromElement.LinkedObject as Terminal; //if (fromTerminal.Connection != null && toTerminal.Connection != null) //{ // MergeConnections(fromTerminal.Connection.LinkedObject as GraphicConnection, m_LastMouseLocation, // toTerminal.Connection.LinkedObject as GraphicConnection, location); //} //else if (fromTerminal.Connection != null) //{ // //IDEE: suche über allen linien die kürzeste entfernung vom ziel zur verbindung // GraphicConnection graphicConnection = fromTerminal.Connection.LinkedObject as GraphicConnection; // ConnectionLine line = new ConnectionLine((m_FromElement as GraphicTerminal).ConnectionNode, graphicTerminal.ConnectionNode); // graphicConnection.AddChild(line); // graphicConnection.ConnectTerminal(graphicTerminal); // m_Editor.UpdateDrawing(); // m_Editor.RaiseChangedEvent(); //} //else if (toTerminal.Connection != null) //{ // GraphicConnection graphicConnection = toTerminal.Connection.LinkedObject as GraphicConnection; // graphicConnection.ConnectTerminal(m_FromElement as GraphicTerminal); // m_Editor.UpdateDrawing(); // m_Editor.RaiseChangedEvent(); //} //else if (fromTerminal.Connection == null && toTerminal.Connection == null) { GraphicTerminal fromGraphicTerminal = m_FromElement as GraphicTerminal; if (IsOrthogonal(fromGraphicTerminal.ConnectionNode.Location, graphicTerminal.ConnectionNode.Location) == false) { return; } GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), new Connection()) as GraphicConnection; graphicConnection.Name = UniqueName.GetUniqueName(m_Editor.Circuit, typeof(Connection)); graphicConnection.ConnectTerminal(fromGraphicTerminal); graphicConnection.ConnectTerminal(graphicTerminal); ConnectionLine line = new ConnectionLine(fromGraphicTerminal.ConnectionNode, graphicTerminal.ConnectionNode); graphicConnection.AddChild(line); m_Editor.AddElement(graphicConnection); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } } if (m_FromElement is GraphicConnection) { GraphicConnection fromConnection = m_FromElement as GraphicConnection; IConnectionItem connectionItem = fromConnection.GetItemAt(m_LastMouseLocation); if (connectionItem is ConnectionNode) { ConnectionLine line = new ConnectionLine(connectionItem as ConnectionNode, graphicTerminal.ConnectionNode); fromConnection.AddChild(line); } else if (connectionItem is ConnectionLine) { ConnectionLine prevLine = connectionItem as ConnectionLine; ConnectionNode node = new ConnectionNode(prevLine.NearestPointOnLine(m_LastMouseLocation)); ConnectionLine line1 = new ConnectionLine(prevLine.Nodes[0], node); ConnectionLine line2 = new ConnectionLine(prevLine.Nodes[1], node); fromConnection.RemoveChild(prevLine); fromConnection.AddChild(node); fromConnection.AddChild(line1); fromConnection.AddChild(line2); ConnectionLine line = new ConnectionLine(node, graphicTerminal.ConnectionNode); fromConnection.AddChild(line); } fromConnection.ConnectTerminal(graphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } if (m_FromElement is ConnectionNode) { m_FromElement.Location = ForceOrthogonality(graphicTerminal.ConnectionNode.Location, m_FromElement.Location); ConnectionLine line = new ConnectionLine(m_FromElement as ConnectionNode, graphicTerminal.ConnectionNode); m_FromElement.Parent.AddChild(line); (m_FromElement.Parent as GraphicConnection).ConnectTerminal(graphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } m_FromElement = null; m_Editor.Invalidate(); }
/// <summary> /// Create a floating connnection, i.e. not completely connected everywhere /// </summary> /// <param name="location"></param> private void FloatingConnection(PointF location) { if (m_FromElement is GraphicConnection) { GraphicConnection graphicConnection = m_FromElement as GraphicConnection; IConnectionItem connectionItem = graphicConnection.GetItemAt(m_LastMouseLocation); if (connectionItem is ConnectionNode) { //user clicked near a node at fist, connect to this ConnectionNode prevNode = connectionItem as ConnectionNode; location = ForceOrthogonality(prevNode.Location, m_Editor.AlignToGrid(location)); bool skip = false; //check whether the new line would just lenghten the previous line if (prevNode.Lines.Length == 1) { ConnectionLine prevLine = prevNode.Lines[0]; if (prevLine.LineStyle == ConnectionLine.DetermineLineStyle(prevNode.Location, location)) { //place the node at the new location in this case prevNode.Location = location; skip = true; } } if (skip == false) { //place a new line ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(prevNode, node); m_FromElement.AddChild(node); m_FromElement.AddChild(line); m_FromElement = node; } } else if (connectionItem is ConnectionLine) { //user clicked near a line at first ConnectionLine prevLine = connectionItem as ConnectionLine; //place a connection node at location - split connection line ConnectionNode node = SplitConnectionLine(prevLine, m_LastMouseLocation); //the new node/line location = ForceOrthogonality(node.Location, m_Editor.AlignToGrid(location)); ConnectionNode fnode = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(node, fnode); m_FromElement.AddChild(fnode); m_FromElement.AddChild(line); m_FromElement = fnode; } } else if (m_FromElement is GraphicTerminal) { //floating connection from a terminal GraphicTerminal graphicTerminal = m_FromElement as GraphicTerminal; location = ForceOrthogonality(graphicTerminal.ConnectionNode.Location, m_Editor.AlignToGrid(location)); ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(node, graphicTerminal.ConnectionNode); GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), new Connection()) as GraphicConnection; graphicConnection.Name = UniqueName.GetUniqueName(m_Editor.Circuit, typeof(Connection)); graphicConnection.AddChild(node); graphicConnection.AddChild(line); graphicConnection.ConnectTerminal(graphicTerminal); m_Editor.AddElement(graphicConnection); //proceed connecting with this node m_FromElement = node; } else if (m_FromElement is ConnectionNode) { //special case -> could be covered by "is GraphicConnection" //sequential floating lines ConnectionNode prevNode = m_FromElement as ConnectionNode; location = ForceOrthogonality(prevNode.Location, m_Editor.AlignToGrid(location)); bool skip = false; //check whether the new line would just lenghten the previous line if (prevNode.Lines.Length == 1) { ConnectionLine prevLine = prevNode.Lines[0]; if (prevLine.LineStyle == ConnectionLine.DetermineLineStyle(prevNode.Location, location)) { //place the node at the new location in this case prevNode.Location = location; skip = true; } } if (skip == false) { //place a new line ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(prevNode, node); GraphicConnection graphicConnection = prevNode.Parent as GraphicConnection; graphicConnection.AddChild(node); graphicConnection.AddChild(line); m_FromElement = node; } } m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); m_Editor.Invalidate(); }