Ejemplo n.º 1
0
 private void btnSaveCrossingManager_Click(object sender, EventArgs e)
 {
     if (smform == null)
     {
         smform = new SavedManagerForm(manager);
     }
     if (smform.IsDisposed)
     {
         smform = new SavedManagerForm(manager);
     }
     smform.Show();
 }
Ejemplo n.º 2
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());
            };
        }