private void grid_Click(object sender, EventArgs e) { if (currentPB != null) { //if(theImageLocationIsValid) int x = xPos; int y = yPos; if (currentWorkingMode != WorkingMode.pipe) { if (pipe != null) { flowly.RemovePipe(pipe); pipe = null; lastPoint = new Point(-1, -1); } } Point newPoint; switch (currentWorkingMode) { case WorkingMode.pipe: ResetProperties(); if (currentPB.Image.Equals(toolPipe.Image)) { newPoint = new Point(x, y); // MessageBox.Show(newPoint.ToString() + "\nx:" + newPoint.X + " y:" + newPoint.Y); //start drawing pipeline. if (lastPoint.X == -1 || lastPoint.Y == -1) { ConnectionPoint cp = flowly.GetConnectionPointAt(newPoint); if (cp != null) { if (cp.IsOutput) { flowly.CreateChange("Pipeline");//////////////////////////////// newPoint = cp.PipeStartPoint; // cp.SetAvailable(false); pipe = new Pipe(); pipe.SetConnection(cp); cp.PipeConnection = pipe; lastPoint = newPoint; pipe.AddPointToList(lastPoint); flowly.HighlightAllAvailableInputs(pipe.GiveMeYourConnectionPoints().First().ComponentDrawnBelong); } } else { return; } } else { bool isAdded = flowly.DrawPipeToCursor(lastPoint, newPoint, ref pipe); if (isAdded) { //continues drawing line lastPoint = newPoint; //when connected to a component stops if (pipe.GiveMeYourConnectionPoints().Count == 2) { pipe.PipePoints.RemoveAt(pipe.PipePoints.Count - 1); pipe.PipePoints.Add(pipe.GiveMeYourConnectionPoints().Last().PipeStartPoint); flowly.DrawPipeline(pipe); flowly.AddComponentDrawn(pipe); flowly.UpdateGrid(); flowly.HighlightAllAvailableOutputs(); MessageBox.Show("Connected"); pipe = null; lastPoint = new Point(-1, -1); } } } } break; case WorkingMode.create: // ResetProperties(); x -= currentPB.Width / 2; y -= currentPB.Height / 2; if (flowly.Grid == null) { MessageBox.Show("No grid is open! Create a new grid or open an existing one!"); } else { int width = currentPB.Width; int height = currentPB.Height; Rectangle r = new Rectangle(x, y, width, height); if (flowly.CheckFreeSpot(r)) { ComponentName currentComponentName; ComponentDrawn createdComponent; if (currentPB.Image.Equals(toolPump.Image)) { currentComponentName = ComponentName.Pump; } else if (currentPB.Image.Equals(toolPipe.Image)) { currentComponentName = ComponentName.Pipe; } else if (currentPB.Image.Equals(toolSplitter.Image)) { currentComponentName = ComponentName.Splitter; } else if (currentPB.Image.Equals(toolSplitterAdj.Image)) { currentComponentName = ComponentName.SplitterAdj; } else if (currentPB.Image.Equals(toolMerger.Image)) { currentComponentName = ComponentName.Merger; } else //then it's a sink { currentComponentName = ComponentName.Sink; } if (flowly.CreateComponentDrawn(currentComponentName, r, nudFlow.Value, nudCapacity.Value, trackBarLeft.Value, trackBarRight.Value)) { if (currentComponentName == ComponentName.Pump) { } } } else { MessageBox.Show("The element is coliding with another element."); } } // KeyValuePair<Rectangle, Image> item = new KeyValuePair<Rectangle, Image>(r, currentPB.Image); // dictionary.Add(r, currentPB.Image); // Paint(item); // ResetProperties(); break; case WorkingMode.remove: ResetProperties(); newPoint = new Point(x, y); ComponentDrawn componentAtPoint = flowly.GetComponentPointAt(newPoint); if (componentAtPoint == null) { if (flowly.Grid == null) { MessageBox.Show("No grid is open! Create a new grid or open an existing one!"); } else { MessageBox.Show("No component selected"); } return; } else { DialogResult dg = MessageBox.Show(String.Format("Do you wish to delete {0}?", componentAtPoint.GetType().Name), "Deleting component", MessageBoxButtons.YesNo); if (dg == DialogResult.Yes) { flowly.DeleteComponent(componentAtPoint); } } break; case WorkingMode.edit: ResetProperties(); newPoint = new Point(x, y); currentSelectedComponent = flowly.GetComponentPointAt(newPoint); if (currentSelectedComponent == null) { if (flowly.Grid == null) { MessageBox.Show("No grid is open! Create a new grid or open an existing one!"); } else { MessageBox.Show("No component selected"); } return; } //If component is adjustable splitter -> if (currentSelectedComponent.DiffCurrFlowPossible == true) { SetTrackBarVisibility(true); List<ConnectionPoint> currentOutputConnectionPoints = new List<ConnectionPoint>(); foreach (ConnectionPoint item in currentSelectedComponent.GiveMeYourConnectionPoints()) { if (item.IsOutput) { currentOutputConnectionPoints.Add(item); } } //if (currentSelectedComponent.CurrentFlow == 0) //{ // trackBarLeft.Value = 50; // trackBarRight.Value = 50; //} //else //{ // int maxFlow = Convert.ToInt32(currentSelectedComponent.CurrentFlow); // trackBarLeft.Value = Convert.ToInt32((maxFlow - currentOutputConnectionPoints[1].CurrentFlow) * 100) / maxFlow; // trackBarRight.Value = Convert.ToInt32((maxFlow - currentOutputConnectionPoints[0].CurrentFlow) * 100) / maxFlow; //} Splitter testSplitter = (Splitter)currentSelectedComponent; trackBarLeft.Value = testSplitter.TopOutputPercentage; trackBarRight.Value = testSplitter.BottomOutputPercentage; SetTrackBarVisibility(true); } else { SetTrackBarVisibility(false); trackBarLeft.Enabled = false; trackBarLeft.Value = 50; trackBarRight.Enabled = false; trackBarRight.Value = 50; } nudCapacity.Value = (decimal)currentSelectedComponent.Capacity; nudFlow.Value = (decimal)currentSelectedComponent.CurrentFlow; btnUpdate.Enabled = true; if (currentSelectedComponent.EditableProperties.Contains(EditablePropertiesEnum.flow)) { nudFlow.Enabled = true; } else { nudFlow.Enabled = false; } if (currentSelectedComponent.EditableProperties.Contains(EditablePropertiesEnum.capacity)) { nudCapacity.Enabled = true; } else { nudCapacity.Enabled = false; } break; default: break; } // KeyValuePair<Rectangle, Image> item = new KeyValuePair<Rectangle, Image>(r, currentPB.Image); // dictionary.Add(r, currentPB.Image); // Paint(item); } }
/// <summary> /// Opposite of the other method. /// </summary> /// <param name="givenComponent"></param> /// <returns>True if successfull, false otherwise.</returns> public virtual bool RemoveComponentDrawnFromGridList(ComponentDrawn givenComponent) { //try // { foreach (ConnectionPoint cp in givenComponent.GiveMeYourConnectionPoints()) { if (cp.PipeConnection != null) { RemovePipe(cp.PipeConnection); } } this.ListOfComponents.Remove(givenComponent); PaintAllComponents(); return true; // } //catch (Exception e) //{ // MessageBox.Show("There are a problem removing the component to the grid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // return false; //} }