using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace trackDesigner { public partial class simulatorStatus : Form { const int MAX_SIZE = 30; const int MAX_TRAINS = 5; private trackStructure[] myTrack; private int num_segments; private turnout[] myTurnouts; private int tCount; private sensor[] mySensors; private int sCount; private train[] myTrains; private int trCount; private System.Windows.Forms.Label[] sens; private System.Windows.Forms.RadioButton[] turns; private System.Windows.Forms.RadioButton[] trains; public trackStructure[] track { get { return myTrack; } set { myTrack = value; } } public int segs { get { return num_segments; } set { num_segments = value; } } public turnout[] turnout { get { return myTurnouts; } set { myTurnouts = value; } } public int turnCt { get { return tCount; } set { tCount = value; } } public sensor[] sensor { get { return mySensors; } set { mySensors = value; } } public int sensorCt { get { return sCount; } set { sCount = value; } } public train[] train { get { return myTrains; } set { myTrains = value; } } public int trainCt { get { return trCount; } set { trCount = value; } } /* EVENTS */ public event EventHandler UpdateTurnout; public simulatorStatus() { InitializeComponent(); } private void simulatorStatus_Load(object sender, EventArgs e) { sens = new Label[3 * MAX_SIZE]; turns = new RadioButton[MAX_SIZE]; trains = new RadioButton[MAX_TRAINS]; // dynamically populate the trains, turnouts & sensors group boxes gbTrains.AutoSize = true; gbTrains.Location = new System.Drawing.Point(13, 13); gbTrains.Height = 100; // if lots of trains, size will increase bc of autosize property for (int i = 0; i < trCount; i++) { trains[i] = new RadioButton(); if (i == 0) trains[i].Checked = true; // automatically check the first train trains[i].Width = 80; trains[i].ForeColor = Color.FromName(myTrains[i].color); trains[i].Text = "Train " + i.ToString(); trains[i].Name = "rbTrain " + i.ToString(); trains[i].Location = new System.Drawing.Point(5, 15 + 20 * i); this.gbTrains.Controls.Add(trains[i]); } // place buttons at appropriate locations btnSpeedUp.Location = new System.Drawing.Point(150, gbTrains.Height / 4); btnSpeedDown.Location = new System.Drawing.Point(150, gbTrains.Height / 2); btnReverse.Location = new System.Drawing.Point(150, 3 * gbTrains.Height / 4); gbSensors.Location = new System.Drawing.Point(13, gbTrains.Location.Y + gbTrains.Height + 10); gbSensors.AutoSize = true; gbSensors.Height = 5; for (int i = 0; i < sCount; i++) { sens[i] = new Label(); sens[i].AutoSize = true; if (i < 15) // two columns of sensors... 15 per col max sens[i].Location = new System.Drawing.Point(5, 15 + 15 * i); else sens[i].Location = new System.Drawing.Point(80, 15 + 15 * (i - 15)); sens[i].Name = "lblSensor" + i.ToString(); sens[i].Text = "Sensor " + i.ToString(); if (mySensors[i].covered) { // not really used yet because covered never gets changed sens[i].ForeColor = Color.Red; } else { sens[i].ForeColor = Color.Black; } this.gbSensors.Controls.Add(sens[i]); } gbTurnouts.Location = new System.Drawing.Point(13, gbSensors.Location.Y + gbSensors.Height + 10); gbTurnouts.AutoSize = true; gbTurnouts.Height = 20; // height will increase if necessary bc of autosize for (int i = 0; i < tCount; i++) { turns[i] = new RadioButton(); if (i == 0) turns[i].Checked = true; // check the first rb turns[i].Width = 80; turns[i].Text = "Turnout " + i.ToString(); turns[i].Name = "rbTurnout " + i.ToString(); if (i < 7) // two columns, 7 max per column turns[i].Location = new System.Drawing.Point(5, 15 + 20 * i); else turns[i].Location = new System.Drawing.Point(100, 15 + 20 * (i-7)); this.gbTurnouts.Controls.Add(turns[i]); } // put button at appropriate location btnChangeTurnout.Location = new System.Drawing.Point(gbTurnouts.Location.X + gbTurnouts.Width + 10, gbTurnouts.Location.Y + (gbTurnouts.Height / 3)); btnChangeTurnout.Text = "Change \n Turnout"; } private void btnSpeedUp_Click(object sender, EventArgs e) { if (trCount > 0) // only need to find a train if there IS one { RadioButton rbTemp = new RadioButton(); foreach (RadioButton rb in gbTrains.Controls) { if (rb.Checked) { rbTemp = rb; break; } } int i = Convert.ToInt32(rbTemp.Text.Substring(6)); // grab the index of the train if (myTrains[i].speed < 10) // increase only if possible myTrains[i].speed++; } } private void btnSpeedDown_Click(object sender, EventArgs e) { if (trCount > 0) // only need to find a train if there IS one { RadioButton rbTemp = new RadioButton(); foreach (RadioButton rb in gbTrains.Controls) { if (rb.Checked) { rbTemp = rb; break; } } int i = Convert.ToInt32(rbTemp.Text.Substring(6)); // grab the index of the train if (myTrains[i].speed > 0) // decrease only if possible myTrains[i].speed--; } } private void btnChangeTurnout_Click(object sender, EventArgs e) { if (tCount > 0) // only need to find a turnout if there IS one { RadioButton rbTemp = new RadioButton(); foreach (RadioButton rb in gbTurnouts.Controls) { if (rb.Checked) { rbTemp = rb; break; } } int i = Convert.ToInt32(rbTemp.Text.Substring(8)); // grab the index of the turnout Point p = new Point(myTurnouts[i].loc.X, myTurnouts[i].loc.Y); // match the trackPt by location int j, temp; for (j = 0; j < num_segments; j++) { if (myTrack[j].ptA.loc == p) { // swap val1 and val2 in the appropriate trackPt temp = myTrack[j].ptA.val1; myTrack[j].ptA.val1 = myTrack[j].ptA.val2; myTrack[j].ptA.val2 = temp; break; } else if (myTrack[j].ptB.loc == p) { // swap val1 and val2 in the appropriate trackPt temp = myTrack[j].ptB.val1; myTrack[j].ptB.val1 = myTrack[j].ptB.val2; myTrack[j].ptB.val2 = temp; break; } } if (this.UpdateTurnout != null) { // trigger event to redraw the screen... val1 and val2 have been switched so // the redraw will take care of visually changing the state this.UpdateTurnout(this, EventArgs.Empty); } } } private void btnReverse_Click(object sender, EventArgs e) { if (trCount > 0) // only find the train if there IS one { RadioButton rbTemp = new RadioButton(); foreach (RadioButton rb in gbTrains.Controls) { if (rb.Checked) { rbTemp = rb; break; } } int i = Convert.ToInt32(rbTemp.Text.Substring(6)); // grab the index of the train myTrains[i].direction = (myTrains[i].direction + 4) % 8; // change the direction by 4, // but never go above 7 } } } }