Exemple #1
0
        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);
            }
        }
Exemple #2
0
        void RefreshGridFromMode()
        {
            if (currentWorkingMode != null)
            {
                if (currentWorkingMode == WorkingMode.pipe)
                {

                        if (pipe != null)
                        {
                            flowly.RemovePipe(pipe);
                            pipe = null;
                            lastPoint = new Point(-1, -1);
                        }

                    ResetProperties();
                    flowly.UpdateGrid();
                }
            }
        }
Exemple #3
0
        private bool CheckInteresectsWithSelf(Point start, Point end, Pipe currentPipe)
        {
            int listOfComponentsCount = listOfComponents.Count;
            int currentPipeLines = currentPipe.PipePoints.Count;
            for (int i = 0; i < currentPipeLines - 1; i++)
            {
                Point other1 = currentPipe.PipePoints[i];
                Point other2 = currentPipe.PipePoints[i + 1];
                if (start != other1 && start != other2)
                {
                    if (CheckPipeIntersects(start, end, currentPipe.PipePoints[i], currentPipe.PipePoints[i + 1]))
                    {
                        MessageBox.Show("Intersects with itself");

                        return true;
                    }
                }
            }
            return false;
        }
Exemple #4
0
        private bool CheckIntersectAllOther(Point start, Point end, Pipe currentPipe)
        {
            if (CheckInteresectsWithSelf(start, end, currentPipe))
            {
                return false;
            }
            int listOfComponentsCount = listOfComponents.Count;
            for (int i = 0; i < listOfComponentsCount; i++)
            {
                ComponentDrawn d = listOfComponents[i];

                if (d is Pipe)
                {
                    if (CheckInteresectsWithPipe(start, end, currentPipe, d as Pipe))
                    {
                        return true;
                    }
                }
            }
            bool isColliding = false;
            for (int i = 0; i < listOfComponentsCount; i++)
            {
                ComponentDrawn d = listOfComponents[i];
                if (d != currentPipe.GiveMeYourConnectionPoints().Last().ComponentDrawnBelong)

                    if (CheckInteresectsWithComponentDrawn(start, end, currentPipe, d))
                    {
                        if (d.RectangleBig.Contains(start))
                        {
                            continue;
                        }
                        MessageBox.Show("Intersects component");
                        return true;

                    }
            }
            return false;
        }
Exemple #5
0
        private bool CheckInteresectsWithPipe(Point start, Point end, Pipe currentPipe, Pipe pipe)
        {
            int pipeLinesCount = pipe.PipePoints.Count;
            List<Point> pLines = pipe.PipePoints;

            for (int j = 0; j < pipeLinesCount - 1; j++)
            {
                if (CheckPipeIntersects(start, end, pLines[j], pLines[j + 1]))
                {
                    MessageBox.Show("Intersects another line");
                    return true;
                }
            }
            return false;
        }
Exemple #6
0
        private bool CheckInteresectsWithPipe(Rectangle r, Pipe pipe)
        {
            int pipeLinesCount = pipe.PipePoints.Count;
            List<Point> pLines = pipe.PipePoints;
            Point topLeft = r.Location;
            Point topRight = new Point(r.X + r.Width, r.Y);
            Point bottomRight = new Point(r.X + r.Width, r.Y + r.Height);
            Point bottomLeft = new Point(r.X, r.Y + r.Height);
            for (int j = 0; j < pipeLinesCount - 1; j++)
            {
                if (CheckPipeIntersects(topLeft, topRight, pLines[j], pLines[j + 1]))
                {
                    // MessageBox.Show("Intersects pipe");
                    return true;
                }
                if (CheckPipeIntersects(topRight, bottomRight, pLines[j], pLines[j + 1]))
                {
                    // MessageBox.Show("Intersects pipe");
                    return true;
                }
                if (CheckPipeIntersects(bottomRight, bottomLeft, pLines[j], pLines[j + 1]))
                {
                    // MessageBox.Show("Intersects pipe");
                    return true;
                }
                if (CheckPipeIntersects(topLeft, bottomLeft, pLines[j], pLines[j + 1]))
                {
                    //MessageBox.Show("Intersects pipe");
                    return true;
                }
            }

            return false;
        }
Exemple #7
0
        private bool CheckInteresectsWithComponentDrawn(Point start, Point end, Pipe currentPipe, ComponentDrawn d)
        {
            Rectangle dRect = d.RectangleBig;

            Point p = new Point(dRect.X, dRect.Y);
            Point p2 = new Point(dRect.X + dRect.Width, dRect.Y);
            if (CheckPipeIntersects(start, end, p, p2))
            {
                return true;
            }

            p = new Point(dRect.X + dRect.Width, dRect.Y);
            p2 = new Point(dRect.X + dRect.Width, dRect.Y + dRect.Height);
            if (CheckPipeIntersects(start, end, p, p2))
            {
                return true;
            }
            p = new Point(dRect.X, dRect.Y + dRect.Height);
            p2 = new Point(dRect.X + dRect.Width, dRect.Y + dRect.Height);
            if (CheckPipeIntersects(start, end, p, p2))
            {
                return true;
            }
            p = new Point(dRect.X, dRect.Y);
            p2 = new Point(dRect.X, dRect.Y + dRect.Height);
            if (CheckPipeIntersects(start, end, p, p2))
            {
                return true;
            }
            return false;
        }
Exemple #8
0
        internal void RemovePipe(Pipe pipe)
        {
            foreach (ConnectionPoint CP in pipe.GiveMeYourConnectionPoints())
            {
                foreach (ComponentDrawn cd in this.ListOfComponents)
                {
                    cd.FlowWasUpdated = false;
                }
                if (!CP.IsOutput)
                {
                    Queue<ComponentDrawn> elements = new Queue<ComponentDrawn>();

                    ComponentDrawn drawnComponent = CP.ComponentDrawnBelong;
                    CP.PipeConnection.GetEndConnectionPoint().SetCurrentFlow(0);
                    elements.Enqueue(drawnComponent);

                    while (elements.Count > 0)
                    {
                        drawnComponent = elements.Dequeue();

                        drawnComponent.UpdateComponentFlow();
                        drawnComponent.FlowWasUpdated = true;
                        List<ConnectionPoint> lcp = drawnComponent.GiveMeYourOutputConnectionPoints();

                        foreach (ConnectionPoint output in lcp)
                        {

                            if (output.PipeConnection != null)
                            {
                                if (!output.PipeConnection.GetEndConnectionPoint().ComponentDrawnBelong.FlowWasUpdated)
                                {
                                    output.PipeConnection.GetEndConnectionPoint().SetCurrentFlow(drawnComponent.CurrentFlow - CP.CurrentFlow);

                                    elements.Enqueue(output.PipeConnection.GetEndConnectionPoint().ComponentDrawnBelong);
                                }
                            }
                        }
                    }

                }
                CP.PipeConnection = null;
            }
            ListOfComponents.Remove(pipe);
        }
Exemple #9
0
        internal bool DrawPipeToPoint(Point start, Point end, ref Pipe curPipe)
        {
            Pipe currentPipe = curPipe;
            int pointsCount = currentPipe.GiveMeYourConnectionPoints().Count;
            if (currentPipe.GiveMeYourConnectionPoints().First().ComponentDrawnBelong == GetComponentAt(end))
            {
                return false;
            }

            if (pointsCount != 2)
            {

                if (CheckInteresectsWithSelf(start, end, currentPipe))
                {

                    return false;
                }
                int listOfComponentsCount = listOfComponents.Count;
                for (int i = 0; i < listOfComponentsCount; i++)
                {
                    ComponentDrawn d = listOfComponents[i];
                    if (currentPipe.PipePoints.Count <= 1)
                    {
                        if (d.GiveMeYourConnectionPoints().Where(x => x == currentPipe.GiveMeYourConnectionPoints().Last()).Any())
                            continue;
                    }

                    if (d is Pipe)
                    {
                        if (CheckInteresectsWithPipe(start, end, currentPipe, d as Pipe))
                        {

                            return false;
                        }

                    }
                    else
                    {
                        if (CheckInteresectsWithComponentDrawn(start, end, currentPipe, d))
                        {

                            if (d.RectangleBig.Contains(end))
                            {
                                ConnectionPoint cp = IsInputOutput(end);
                                if (cp != null)
                                {
                                    if (!cp.IsOutput)
                                    {
                                        cp.PipeConnection = currentPipe;
                                        if (IsMakingCircularFlow(cp))
                                        {
                                            cp.PipeConnection = null;
                                            return false;

                                        }

                                        currentPipe.SetConnection(cp);

                                        if (CheckIntersectAllOther(start, end, currentPipe))
                                        {

                                            cp.PipeConnection = null;
                                            currentPipe.RemoveConnection(cp);
                                            return false;
                                        }
                                        // Pen myPen2 = new Pen(Color.Gray, PIPE_SIZE);
                                        // graphic.DrawLine(myPen2, start, end);

                                        currentPipe.AddPointToList(end);
                                        curPipe = currentPipe;

                                        return true;
                                    }
                                    MessageBox.Show("Intersects component");
                                    return false;

                                }
                            }
                            MessageBox.Show("Intersects component");
                            return false;
                        }
                    }

                }

            }
            Pen myPen = new Pen(Color.Gray, PIPE_SIZE);
            graphic.DrawLine(myPen, start, end);

            currentPipe.AddPointToList(end);
            curPipe = currentPipe;
            return true;
        }
Exemple #10
0
        internal void DrawPipelineAndUpdateFLow(Pipe pipe)
        {
            //Todo Check if it is a connection between elements that are not end or start
            ConnectionPoint startPoint = pipe.GetStartConnectionPoint();
            if (startPoint == null)
            {
                MessageBox.Show("What did you do? grid-drawpipeline");
                return;
            }
            ConnectionPoint endPoint = pipe.GetEndConnectionPoint();
            if (endPoint == null)
            {
                MessageBox.Show("You are drawing the pipe too soon, you need to have an endpoint!");
                return;
            }
            Pen colorOfPen = new Pen(Color.Gray, PIPE_SIZE);

            endPoint.SetCurrentFlow(startPoint.CurrentFlow);
            // endPoint.SetCurrentFlow(startPoint.CurrentFlow - (startPoint.CurrentFlow - endPoint.CurrentCapacity));
            endPoint.ComponentDrawnBelong.UpdateComponentFlow();

            if (((startPoint.CurrentFlow > endPoint.ComponentDrawnBelong.Capacity) && (startPoint.CurrentFlow - endPoint.ComponentDrawnBelong.Capacity >= 0.1F)) && endPoint.ComponentDrawnBelong is Sink)
            {
                colorOfPen.Color = Color.Red;
            }
            for (int i = 0; i < pipe.PipePoints.Count - 1; i++)
            {
                Point start = pipe.PipePoints[i];
                Point end = pipe.PipePoints[i + 1];
                graphic.DrawLine(colorOfPen, start, end);
            }

            //int index = listOfComponents.IndexOf(endPoint.ComponentDrawnBelong);
            //ListOfComponents.Insert(index, endPoint.ComponentDrawnBelong);
            //ListOfComponents.RemoveAt(index+1);

            //Paint(endPoint.ComponentDrawnBelong);
            // PaintAllComponents();
        }
Exemple #11
0
 internal void AddPipe(Pipe pipe)
 {
     this.ListOfComponents.Add(pipe);
 }