public void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // If the placement icon is null then we don't do anything if (null == m_placementIcon) { return; } // Remove the placement icon from the canvas m_canvas.RemoveChild(m_placementIcon); m_placementIcon = null; // Make sure we don't leave anything highlighted UnhighlightHover(); Point mousePt = e.GetPosition(m_canvas); // Get a reference to the workspace Workspace ws = m_canvas.GetWorkspace(); // There are two possibilities for a mouse-down. The first is that the mouse isn't over // any object that can have comments created for it, in which case we create a free- // floating comment. The second is that there is and we need to create an anchored comment. UIElement uie = m_canvas.GetChildAtIncludeStreams(mousePt); if (uie is ProcessUnitControl || uie is PFD.Streams.StreamControl) { // All we need to do is add a comment to the appropriate collection in the workspace, // but we need to compute a smart location for it. There's a static method in the // sticky note control that does this for is. MathCore.Vector pos = StickyNoteControl.ComputeNewCommentNoteLocation( m_canvas, uie, 100.0, 100.0); // Create the new sticky note and add it to the workspace. Event handlers will update // the UI appropriately. StickyNote sn = new StickyNote() { Width = 100.0, Height = 100.0, LocationX = pos.X, LocationY = pos.Y }; if (uie is ProcessUnitControl) { (uie as ProcessUnitControl).ProcessUnit.Comments.Add(sn); } else { (uie as PFD.Streams.StreamControl).Stream.Comments.Add(sn); } // Create an undo m_canvas.GetWorkspace().AddUndo(new UndoRedoCollection( "Undo creation of anchored comment", new Logic.Undos.RemoveStickyNote(sn))); // Tell the control palette to switch back to select mode. It will also set the drawing // canvas's state to null m_creator.SwitchToSelect(); } else { // This means we need to create a free-floating sticky note. StickyNote note = new StickyNote(); note.Height = note.Width = 100.0; note.LocationX = mousePt.X; note.LocationY = mousePt.Y; // Add it to the workspace. Event subscribers will update the UI automatically. m_canvas.GetWorkspace().StickyNotes.Add(note); // Add an undo that will remove the sticky note m_canvas.GetWorkspace().AddUndo(new UndoRedoCollection( "Undo creation of free-floating comment", new Logic.Undos.RemoveStickyNote(note))); // Tell the control palette to switch back to select mode. It will also set the drawing // canvas's state to null m_creator.SwitchToSelect(); } }
public void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // We don't process messages if the placement icon is null if (null == m_placementIcon) { return; } // Handle heat exchanger with utility as a special case if (m_type.Equals(typeof(HeatExchangerWithUtility))) { MLBD_HEWU(sender, e); return; } // Start by getting the mouse position Point pos = e.GetPosition(m_canvas); MathCore.Vector vPos = new MathCore.Vector(pos.X, pos.Y); // See if we have a DraggableStreamEndpoint where we clicked DraggableStreamEndpoint endpoint = m_canvas.GetChildAt(pos, m_placementIcon) as DraggableStreamEndpoint; // Remove the placement icon and then set it to null to indicate state completion m_canvas.RemoveChild(m_placementIcon); m_placementIcon = null; // Determine a unique identifier for the process unit int uid; do { uid = AbstractProcessUnit.GetNextUID(); }while (null != m_workspace.GetProcessUnit(uid)); // If there's not an endpoint, we create the process unit with no stream connections if (null == endpoint) { AbstractProcessUnit unit = (AbstractProcessUnit)Activator.CreateInstance( m_type, uid); // Set the location unit.Location = new MathCore.Vector(pos.X, pos.Y); // Add it to the workspace m_workspace.AddProcessUnit(unit); // Add an undo that will remove it m_workspace.AddUndo(new UndoRedoCollection("Undo process unit creation", new ChemProV.Logic.Undos.RemoveProcessUnit(unit))); m_canvas.SelectedElement = null; // Tell the control palette to switch back to select mode and then return m_palette.SwitchToSelect(); return; } // Otherwise, if we HAVE clicked on an endpoint... // Check to see if we can't connect this way AbstractProcessUnit temp = (AbstractProcessUnit) Activator.CreateInstance(m_type, -1); if (!endpoint.CanConnectTo(temp)) { // The usability here may be debatable. But for now we'll cancel placement and // give the user an error message m_palette.SwitchToSelect(); Core.App.MessageBox("The stream endpoint that you clicked cannot connect with " + "the process unit that you were placing."); return; } // Otherwise, if we CAN connect this way... // Create the process unit AbstractProcessUnit apu = (AbstractProcessUnit) Activator.CreateInstance(m_type, uid); // Make the undo and then the actual attachment if (DraggableStreamEndpoint.EndpointType.StreamDestination == endpoint.Type) { // Set the location apu.Location = endpoint.ParentStream.Stream.DestinationLocation; // Create an undo that sets the stream destination back to what it was and removes the // process unit m_workspace.AddUndo(new UndoRedoCollection("Undo process unit creation + attachment", new Logic.Undos.SetStreamDestination(endpoint.ParentStream.Stream, null, apu, vPos), new Logic.Undos.RemoveProcessUnit(apu))); apu.AttachIncomingStream(endpoint.ParentStream.Stream); endpoint.ParentStream.Stream.Destination = apu; } else { // Set the location apu.Location = endpoint.ParentStream.Stream.SourceLocation; // Create an undo that sets the stream source back to what it was and removes the // process unit from the canvas AbstractStream stream = endpoint.ParentStream.Stream; m_workspace.AddUndo(new UndoRedoCollection("Undo process unit creation + attachment", new Logic.Undos.SetStreamSource(stream, null, apu, stream.SourceLocation), new Logic.Undos.RemoveProcessUnit(apu))); apu.AttachOutgoingStream(endpoint.ParentStream.Stream); endpoint.ParentStream.Stream.Source = apu; } // Don't forget to add the process unit to the workspace. Event handlers will update the // UI appropriately. m_workspace.AddProcessUnit(apu); m_canvas.SelectedElement = null; // Go back to select mode m_palette.SwitchToSelect(); }