Exemplo n.º 1
0
        static void Main()
        {
            Crossing crossing = new Crossing();

            Console.WriteLine(crossing.State);
            // output: light A = red
            // output: light B = green

            crossing.PushButton(Trigger.Button1);
            // output: light A = red
            // output: light B = orange

            // output: light A = green
            // output: light B = red

            crossing.PushButton(Trigger.Button1);

            crossing.PushButton(Trigger.Button2);
            // output: light A = orange
            // output: light B = red

            // output: light A = red
            // output: light B = green

            crossing.PushButton(Trigger.Button2);
        }
        private void btnLook_Click(object sender, EventArgs e)
        {
            int id   = Convert.ToInt32(tbSearchId.Text) - 1;
            int cars = 0;

            try
            {
                Crossing cr = manager.SavedCrossingManager.Crossings[id];
                if ((cr is CrossingA) || (cr is CrossingB))
                {
                    foreach (Lane l in cr.Lanes)
                    {
                        cars += l.Flow;
                    }
                    MessageBox.Show("Total car flow is " + cars.ToString());
                }
                else
                {
                    foreach (Lane l in cr.Lanes)
                    {
                        cars += l.Flow;
                    }
                    MessageBox.Show("Total car flow is " + cars.ToString());
                }
            }
            catch
            {
                MessageBox.Show("Crossing ID " + (id + 1) + " does not exist in saved crossing manager.");
            }
        }
Exemplo n.º 3
0
 private void PicBoxTypeB_Click(object sender, EventArgs e)
 {
     state = SystemState.Place;
     crossingToBePlaced = new CrossingB(manager);
     ChangeBorder(PicBoxTypeB, true);
     ChangeBorder(PicBoxTypeA, false);
     ChangeBorder(PicBoxTypeC, false);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Removes the specified identifier.
 /// </summary>
 /// <param name="id">The crossing.</param>
 public void Remove(Crossing crossing)
 {
     for (int i = 0; i < this.crossings.Count; i++)
     {
         if (this.crossings[i] == crossing)
         {
             Remove(i);
             break;
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates the copy.
        /// </summary>
        /// <returns>Crossing.</returns>
        public Crossing CreateCopy()
        {
            var stream    = new System.IO.MemoryStream();
            var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            formatter.Serialize(stream, this);
            stream.Seek(0, System.IO.SeekOrigin.Begin);
            Crossing copy = formatter.Deserialize(stream) as Crossing;

            copy.Owner = this.Owner;
            return(copy);
        }
Exemplo n.º 6
0
 public UpdateMultipleIntervalAction(float interval, Crossing crossing)
 {
     previousIntervals   = new List <float>();
     this.grid           = crossing.Owner.Grid;
     this.Interval       = interval;
     this.CrossingColumn = crossing.Column;
     this.CrossingRow    = crossing.Row;
     foreach (Trafficlight light in Lights)
     {
         previousIntervals.Add(light.GreenSeconds);
     }
 }
        public UpdateMultipleFlowAction(int flow, Crossing crossing)
        {
            previousFlows       = new List <int>();
            this.Flow           = flow;
            this.grid           = crossing.Owner.Grid;
            this.CrossingColumn = crossing.Column;
            this.CrossingRow    = crossing.Row;

            foreach (Lane lane in Lanes)
            {
                previousFlows.Add(lane.Flow);
                //Lane is of the crossing before it was deleted
                //lane  has to be using the ID that is in the crossing, not the direct reference
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds at.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <param name="column">The column.</param>
        /// <param name="crossing">The crossing.</param>
        public void AddAt(int row, int column, Crossing crossing)
        {
            ValidateCanUse(row, column);

            if (this[row][column] != null)
            {
                RemoveAt(row, column);
            }

            crossing = crossing.CreateCopy();
            crossing.AssignGridLocation(row, column);
            this.Crossings[row][column] = crossing;

            OnCrossingAdded(crossing, row, column);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Removes at.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <param name="column">The column.</param>
        public void RemoveAt(int row, int column)
        {
            try
            {
                ValidateCanUse(row, column);

                Crossing crossing = this[row][column];
                this.Crossings[row][column] = null;
                crossing.RemoveFromGrid();
                OnCrossingRemoved(crossing, row, column);
            }
            catch
            {
                // Gets here after undo-ing a loaded grid.
            }
        }
        private void btnPlace_Click(object sender, EventArgs e)
        {
            int id     = Convert.ToInt32(tbPlaceId.Text) - 1;
            int row    = Convert.ToInt32(textBoxPlaceRow.Text) - 1;
            int column = Convert.ToInt32(tbPlaceColumn.Text) - 1;

            try
            {
                Crossing cr = manager.SavedCrossingManager.Crossings[id];

                manager.Grid.AddAt(row, column, cr);
            }
            catch
            {
                MessageBox.Show("Crossing ID " + (id + 1) + " does not exist in saved crossing manager.");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(textBox1.Text) - 1;

            try
            {
                Crossing cr = manager.RecycleCrossingManager.Crossings[id];

                manager.Grid.AddAt(cr.RowRecycleManager, cr.ColumnRecycleManager, cr);
                manager.RecycleCrossingManager.Remove(cr);

                UpdatePanel();
            }
            catch
            {
                MessageBox.Show("Crossing ID " + (id + 1) + " does not exist in recycle manager.");
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                int      column            = Convert.ToInt32(textBoxColumn.Text);
                int      row               = Convert.ToInt32(textBoxRow.Text);
                Crossing crossingToBeSaved = manager.Grid.Crossings[row - 1][column - 1];

                if (crossingToBeSaved.IsOnTheGrid)
                {
                    manager.SavedCrossingManager.Add(crossingToBeSaved);
                    UpdatePanel();
                }
            }
            catch
            {
                MessageBox.Show("There is no crossing there!");
            }
        }
Exemplo n.º 13
0
        private void SelectComponent(PictureBox sender, MouseEventArgs e)
        {
            if (manager.CurrentSimulation.IsActive)
            {
                return;
            }

            int      slot     = pBoxToSlotIDLookup[sender];
            Crossing crossing = manager.Grid.CrossingAt(slot);

            if (crossing != null)
            {
                foreach (Lane lane in crossing.Feeders)
                {
                    if (FindCollision(e, lane))
                    {
                        if (manager.CurrentActiveLane != null)
                        {
                            if (manager.CurrentActiveLane.Owner.Owner.IsOnTheGrid)
                            {
                                slotIDToPBoxLookup[manager.CurrentActiveLane.Owner.Owner.Column + manager.CurrentActiveLane.Owner.Owner.Row * 3].Invalidate();
                            }
                        }
                        manager.CurrentActiveComponent = lane;
                        sender.Invalidate();
                        return;
                    }
                }
                foreach (Trafficlight light in crossing.Lights)
                {
                    if (FindCollision(e, light))
                    {
                        if (manager.CurrentActiveTrafficLight != null)
                        {
                            slotIDToPBoxLookup[manager.CurrentActiveTrafficLight.Owner.Column + manager.CurrentActiveTrafficLight.Owner.Row * 3].Invalidate();
                        }
                        manager.CurrentActiveComponent = light;
                        sender.Invalidate();
                        return;
                    }
                }
            }
        }
Exemplo n.º 14
0
 public PlaceCrossingAction(int row, int column, Crossing crossing) : base(row, column, crossing)
 {
 }
 public RemoveCrossingAction(int row, int column, Crossing crossing)
 {
     this.row      = row;
     this.column   = column;
     this.crossing = crossing;
 }
Exemplo n.º 16
0
 public void SetOwner(Crossing owner)
 {
     this.Owner = owner;
 }
Exemplo n.º 17
0
 /// <summary>
 /// Places the crossing on a specific row and column
 /// </summary>
 public void PlaceCrossing(Crossing crossing, int row, int column)
 {
     this.Grid.AddAt(row, column, crossing);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Adds the specified crossing.
 /// </summary>
 /// <param name="crossing">The crossing.</param>
 public void Add(Crossing crossing)
 {
     this.crossings.Add(crossing);
 }
Exemplo n.º 19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            smform = new SavedManagerForm(manager);
            state  = SystemState.None;
            ActionStack.OnRedoAltered += (actionsToRedo, actionRedone) =>
            {
                if (actionsToRedo > 0)
                {
                    ToggleControl(redoToolStripMenuItem1, true);
                    ToggleControl(redoBtn, true);
                }
                else
                {
                    ToggleControl(redoToolStripMenuItem1, false);
                    ToggleControl(redoBtn, false);
                }
                PopulateActionStackListbox();

                if (actionRedone is UpdateFlowAction && manager.CurrentActiveLane == (actionRedone as UpdateFlowAction).Lane)
                {
                    propertiesEditNUD.Value = manager.CurrentActiveLane.Flow;
                }
                else if (actionRedone is UpdateLightIntervalAction && manager.CurrentActiveTrafficLight == (actionRedone as UpdateLightIntervalAction).Light)
                {
                    propertiesEditNUD.Value = (decimal)manager.CurrentActiveTrafficLight.GreenSeconds;
                }
                else if (actionRedone is UpdateMultipleIntervalAction)
                {
                    if (manager.CurrentActiveTrafficLight != null)
                    {
                        if (!isApplying)
                        {
                            propertiesEditNUD.Value = (decimal)manager.CurrentActiveTrafficLight.GreenSeconds;
                        }

                        slotIDToPBoxLookup[(actionRedone as UpdateMultipleIntervalAction).Crossing.Column + (actionRedone as UpdateMultipleIntervalAction).Crossing.Row * 3].Invalidate();
                    }
                }
                else if (actionRedone is UpdateMultipleFlowAction)
                {
                    if (manager.CurrentActiveLane != null)
                    {
                        if (!isApplying)
                        {
                            propertiesEditNUD.Value = manager.CurrentActiveLane.Flow;
                        }
                        //if (manager.CurrentActiveLane.Owner.Owner.IsOnTheGrid)
                    }
                    slotIDToPBoxLookup[(actionRedone as UpdateMultipleFlowAction).Crossing.Column + (actionRedone as UpdateMultipleFlowAction).Crossing.Row * 3].Invalidate();
                }
            };

            ActionStack.OnUndoAltered += (actionsToUndo, actionUndone) =>
            {
                if (actionsToUndo > 0)
                {
                    ToggleControl(undoToolStripMenuItem1, true);
                    ToggleControl(undoBtn, true);
                }
                else
                {
                    ToggleControl(undoToolStripMenuItem1, false);
                    ToggleControl(undoBtn, false);
                }
                PopulateActionStackListbox();
                if (actionUndone is UpdateFlowAction && manager.CurrentActiveLane == (actionUndone as UpdateFlowAction).Lane)
                {
                    propertiesEditNUD.Value = manager.CurrentActiveLane.Flow;
                }
                else if (actionUndone is UpdateLightIntervalAction && manager.CurrentActiveTrafficLight == (actionUndone as UpdateLightIntervalAction).Light)
                {
                    propertiesEditNUD.Value = (decimal)manager.CurrentActiveTrafficLight.GreenSeconds;
                }
                else if (actionUndone is UpdateMultipleIntervalAction)
                {
                    if (manager.CurrentActiveTrafficLight != null)
                    {
                        if (!isApplying)
                        {
                            propertiesEditNUD.Value = (decimal)manager.CurrentActiveTrafficLight.GreenSeconds;
                        }

                        slotIDToPBoxLookup[(actionUndone as UpdateMultipleIntervalAction).Crossing.Column + (actionUndone as UpdateMultipleIntervalAction).Crossing.Row * 3].Invalidate();
                    }
                }
                else if (actionUndone is UpdateMultipleFlowAction)
                {
                    if (manager.CurrentActiveLane != null)
                    {
                        if (!isApplying)
                        {
                            propertiesEditNUD.Value = manager.CurrentActiveLane.Flow;
                        }
                        //if (manager.CurrentActiveLane.Owner.Owner.IsOnTheGrid)
                    }
                    slotIDToPBoxLookup[(actionUndone as UpdateMultipleFlowAction).Crossing.Column + (actionUndone as UpdateMultipleFlowAction).Crossing.Row * 3].Invalidate();
                }
            };

            ToggleControl(redoToolStripMenuItem1, false);
            ToggleControl(redoBtn, false);
            ToggleControl(undoToolStripMenuItem1, false);
            ToggleControl(undoBtn, false);

            manager.OnCurrentActiveComponentChanged += (x) =>
            {
                if (x == null)
                {
                    propertiesEditGBox.Visible = false;
                    return;
                }
                propertiesEditGBox.Visible = true;
                if (x is Trafficlight)
                {
                    propertiesLbl.Text      = "Green interval";
                    propertiesEditNUD.Value = (decimal)manager.CurrentActiveTrafficLight.GreenSeconds;
                }

                else if (x is Lane)
                {
                    propertiesLbl.Text      = "Car flow";
                    propertiesEditNUD.Value = manager.CurrentActiveLane.Flow;
                }
            };

            manager.CurrentActiveComponent = null;

            foreach (Control item in this.Controls)
            {
                if (item.Name.StartsWith("gridSlot"))
                {
                    int slotID = int.Parse(item.Name.Substring("gridSlot".Length)) - 1;
                    pBoxToSlotIDLookup.Add(item as PictureBox, slotID);
                    slotIDToPBoxLookup.Add(slotID, item as PictureBox);


                    item.Click += (x, y) =>
                    {
                        if (state != SystemState.None)
                        {
                            PlaceCrossing(x as PictureBox);
                            RemoveCrossing(x as PictureBox);

                            Form fc = Application.OpenForms["SavedManagerForm"];

                            if (fc != null)
                            {
                                int row    = pBoxToSlotIDLookup[x as PictureBox] / 3;
                                int column = pBoxToSlotIDLookup[x as PictureBox] % 3;
                                manager.SavedCrossingManager.Add(manager.Grid[row][column]);
                                smform.UpdatePanel();
                            }
                        }
                        else
                        {
                            SelectComponent(x as PictureBox, y as MouseEventArgs);
                        }
                    };

                    item.Paint += (x, y) =>
                    {
                        int      slot     = pBoxToSlotIDLookup[x as PictureBox];
                        Crossing crossing = manager.Grid.CrossingAt(slot);
                        if (crossing != null)
                        {
                            crossing.Draw(y.Graphics);
                        }
                    };
                }
            }

            foreach (Control item in this.Controls)
            {
                item.KeyDown += MainForm_KeyDown;
            }

            this.Click += MainForm_Click;

            PicBoxTypeA.Click += PicBoxTypeA_Click;
            PicBoxTypeA.Click += ClearToggles;
            PicBoxTypeB.Click += PicBoxTypeB_Click;
            PicBoxTypeB.Click += ClearToggles;
            PicBoxTypeC.Click += PicBoxTypeC_Click;
            PicBoxTypeC.Click += ClearToggles;

            PicBoxTypeA.Cursor = Cursors.Hand;
            PicBoxTypeB.Cursor = Cursors.Hand;
            PicBoxTypeC.Cursor = Cursors.Hand;

            // Attach the grid.
            this.AttachGrid();

            this.manager.GridLoaded += () =>
            {
                gridSlot1.Image = null;
                gridSlot2.Image = null;
                gridSlot3.Image = null;
                gridSlot4.Image = null;
                gridSlot5.Image = null;
                gridSlot6.Image = null;
                gridSlot7.Image = null;
                gridSlot8.Image = null;
                gridSlot9.Image = null;

                foreach (Crossing[] c in this.manager.Grid.Crossings)
                {
                    foreach (Crossing cr in c)
                    {
                        if (cr != null)
                        {
                            int id = cr.Row * 3 + cr.Column;
                            this.slotIDToPBoxLookup[id].Image = cr.Image;
                        }
                    }
                }
            };
            manager.CurrentSimulation.OnPauseStateChanged +=
                (isPaused) =>
            {
                timer.Enabled = !isPaused;
                ToggleControl(undoToolStripMenuItem1, isPaused);
                ToggleControl(redoToolStripMenuItem1, isPaused);
                ToggleControl(undoBtn, isPaused);
                ToggleControl(redoBtn, isPaused);

                ToggleControl(newSimulationToolStripMenuItem, isPaused);
                ToggleControl(openToolStripMenuItem, isPaused);
                ToggleControl(saveToolStripMenuItem1, isPaused);

                ToggleControl(btnSaveCrossingManager, isPaused);
                ToggleControl(button2, isPaused);
                ToggleControl(button1, isPaused);
                ToggleControl(updatePropertiesBtn, isPaused);

                ToggleControl(PicBoxTypeA, isPaused);
                ToggleControl(PicBoxTypeB, isPaused);
                ToggleControl(PicBoxTypeC, isPaused);

                if (!isPaused)
                {
                    manager.CurrentActiveComponent = null;

                    ChangeBorder(PicBoxTypeA, false);
                    ChangeBorder(PicBoxTypeB, false);
                    ChangeBorder(PicBoxTypeC, false);
                    ChangeGridSlotsCursor(false);
                    button2.FlatStyle = FlatStyle.Standard;
                    rmToggled         = false;
                    state             = SystemState.None;
                }
            };
            manager.CurrentSimulation.OnSpeedChanged += (x) => lblSpeed.Text = x + "x";
            manager.CurrentSimulation.OnCompleted    += (x) => {
                timer.Stop();

                listBox1.Items.Clear();

                x.SaveResults(x.SimulationSetup.TimePassed, x.SimulationSetup.TotalCars, x.SimulationSetup.GetXTimesCrossingsCrossed());

                listBox1.Items.Add("Date performed: ");
                listBox1.Items.Add(x.DatePerformed.ToString());

                listBox1.Items.Add("");

                listBox1.Items.Add("Time passed: ");
                listBox1.Items.Add(x.TimePassed.ToString());

                listBox1.Items.Add("");

                listBox1.Items.Add("Total cars: ");
                listBox1.Items.Add(x.TotalCars.ToString());

                listBox1.Items.Add("");

                listBox1.Items.Add("Successfully crossings of cars: ");
                listBox1.Items.Add(x.CarsCrossed.ToString());
            };
        }