using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; //***********************************************VERSION 5***************************************************// namespace TrackMaker4 { //**This form is the edit portion of the simulator. //**From this form, a user can create a track, edit it, save it, open it, and open a new form to run trains on it public partial class Form1 : Form { //**************Global Variables************// string Dir;//This will hold the current directory when I need it const int BlockSize = 30;//Size of Square(Button) int Current_Tool = 1;// Currently selected tool 1-5 7&8 int Xvals;//Number of x values on grid int Yvals;//Number of y values on grid private MyDocument myDoc;//Document for saving and opening Square[,] TakenSpots;//2-D Array to store track squares Square[] Turnouts;//Keep track of the turnouts for Run.cs int Turnout_Index = 0;//keep track of which turnout to look at in in/out GroupBox gbxGrid;//Holds Grid of clickable squares Image[] Piece_Images;//This holds the images a piece could be Image[,] Turnout_Images;//This holds the possible turnout images //**************End Global Variables************// //Form1()****************************** public Form1() { InitializeComponent(); myDoc = new MyDocument();//Start new document myDoc.Host = this; Dir = Environment.CurrentDirectory; //Put different piece images in the array from files Piece_Images = new Image[7] { Image.FromFile(Dir + "\\verticalP.jpg"), Image.FromFile(Dir+" \\horizontalP.jpg"), Image.FromFile(Dir + "\\TLP.jpg"), Image.FromFile(Dir + "\\TRP.jpg"), Image.FromFile(Dir + "\\BLP.jpg"), Image.FromFile(Dir + "\\BRP.jpg"), Image.FromFile(Dir+"\\Blank.jpg") }; //Put all possible different images into a 2-D Array from files Turnout_Images = new Image[6, 6]; for(int i=0;i<6;i++) { for(int j=0; j<6;j++) { string lookup = Dir + "\\T_" + i.ToString() + "_" + j.ToString()+".jpg"; Turnout_Images[i, j] = Image.FromFile(lookup); } } }//End Form1()************************** private void Form1_Load(object sender, EventArgs e) { TrackLayout Diments = new TrackLayout();//Brings up screen to get size of track Diments.ShowDialog();//** Get The dimentions of the track if (Diments.DialogResult == DialogResult.OK)//If user hit ok... { //Store the new width and height of the board Xvals = Diments.Xnum; Yvals = Diments.Ynum; TakenSpots = new Square[Xvals, Yvals];//Mak a new 2-D array of squares the size of Xvals and Yvals Turnouts = new Square[Xvals + Yvals - 4];//** That is the number of possible turnouts for (int i = 0; i < Xvals; i++)//Fill Array Up with default squares { for (int j = 0; j < Yvals; j++) { TakenSpots[i, j] = new Square(i, j); }//Default Square at location. Sets the X and Y equal to i and j } } Setboard(sender, e);//This creates a board of squares. See function for details } /// Setboard makes a board of Squares (buttons) from ints Xvals and Yvals /// *********************************************SetBoard******************************************** private void Setboard(object sender, EventArgs e) { int boarder = BlockSize / 2;//A distance used to set objects appart //Make a GroupBox gbxGrid = new GroupBox();//** Creates a Box for the grid to go in //Sets width, height and location gbxGrid.Width = Xvals * BlockSize + boarder; gbxGrid.Height = Yvals * BlockSize + boarder; gbxGrid.Location = new Point(boarder, boarder); Controls.Add(gbxGrid);//Add a new GroupBox to put the Squares for a track in //Make a grid of Squares for (int i = 0; i < Xvals; i++) { for (int j = 0; j < Yvals; j++) { gbxGrid.Controls.Add(new Square//** Add a new square to the board { // ** Initialize Properties Name = i.ToString() + ',' + j.ToString(), Text = i.ToString() + ',' + j.ToString(), Is_Active = false, BackColor = Color.LightGray, Location = new Point(i * (BlockSize - 3) + boarder, j * (BlockSize - 3) + boarder),//No science to the -3 just took gaps out Size = new Size(BlockSize, BlockSize), //Set x and y vals for location and array purposes SpotsTaken[,] X = i, Y = j, }); }//End for j }//End for i //** Add events to, and alter buttons foreach (Square S in gbxGrid.Controls) { S.Click += Grid_Click;//add handler to click event S.FlatAppearance.BorderSize = 0;//** Just changes the appearenace } //Find how big to make make the the GroupBox for the Tools //Find location for the GroupBox for Tools int x = gbxGrid.Width + 30; int y = gbxPieces.Location.Y; int width = gbxPieces.Width; int height = gbxPieces.Height; gbxPieces.SetBounds(x, y, width, height);//Sets location and size for Tools //Finds dimentions and location for the "Run" button width = bttnRunIt.Width; height = bttnRunIt.Height; y = bttnRunIt.Location.Y; bttnRunIt.SetBounds(x, y, width, height); //Open window up to fit controls this.Width = gbxPieces.Width + gbxGrid.Width + 60 + bttnRunIt.Width; this.Height = gbxPieces.Height + gbxGrid.Width ; }///// *********************************************End SetBoard******************************************* //**Event handler for the tools in gbxPieces private void Tool_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; Current_Tool = Convert.ToInt32(clickedButton.Tag); } //**Event handler for clicking on the grid buttons ******************************* private void Grid_Click(object sender, EventArgs e) { //** This makes a copy of the square that got clickded on Square clickedSquare = (Square)sender;//from msdn.Microsoft.com //**Array holding active Squares int x = clickedSquare.X; int y = clickedSquare.Y; if (Current_Tool == 8)//If it is a turnout (turnout has a tag of 8) { TurnoutFcn(sender, e);//What happens here? TakenSpots[x, y] = clickedSquare; } else if (Current_Tool == 7)//Eraser Id is 7 { if (clickedSquare.Is_Active == true) { clickedSquare.BackColor = Color.LightGray;//Grey out the Square clickedSquare.Text = x.ToString() + "," + y.ToString();//Change test to show index of the squares Square E = new Square("E", x, y, false);//Wipes the square from the spot in arrays...Not Tested int t = 0; if (clickedSquare.Is_Turnout == true)//If it was a turnout... { while (Turnouts[t] != clickedSquare && t < Turnouts.Count())//Find which turnout it was { t++; if (t > Turnouts.Length)//Check to see if the Turout wasn't in there { MessageBox.Show("GridClick fcn: Out of bounds on array Turnouts. Breaking out of loop."); return; } } Turnouts[t] = E;//Put a blank Square in Turnouts } //Look through the squares and "erase" the button foreach (Button B in gbxPieces.Controls) { if (Convert.ToInt32(B.Tag) == Current_Tool)//Set to Tool Selected { clickedSquare.Image = B.Image;//Wipe the Image out %%WHICH ONE? clickedSquare = E;//Set the square to a blank one TakenSpots[clickedSquare.X, clickedSquare.Y] = E;//Puts a blank spot in TakenSpots } } clickedSquare.Is_Active = false;//Deactivate the Square clickedSquare.Image = Piece_Images[6];//Bland out the Image %% WHICH ONE? } } else//It was a regular piece { foreach (Button B in gbxPieces.Controls) { if (Convert.ToInt32(B.Tag) == Current_Tool)//Set to Tool Selected { clickedSquare.Set_Piece(0, Current_Tool);//Function in square that sets the pieces array at 0 to P1 clickedSquare.Text = "";//Blank Text Out clickedSquare.BackColor = Color.LightGray;//Make the BackColor Grey clickedSquare.Is_Turnout = false;//IF its a regular piece, it isn't a turnout clickedSquare.Is_Active = true;//Activate the Square TakenSpots[x, y] = clickedSquare;//Store the clicked square in the array } }//end foreach }//end else //**Set any other properties you need to if(clickedSquare.Is_Active==true) clickedSquare.Set_Current_Piece(0); } //Brings up Turnout Screen, and sets the pieces in the clicked square to those... //... chosen in the turnout screen void TurnoutFcn(object sender, EventArgs e) { Square squareClicked = (Square)sender;//Holds which square was clicked Turnout Tout = new Turnout();//Create a new turnout form Tout.ShowDialog();//Show the form //Get the pieces chosen from the form int Piece1 = Tout.P1; int Piece2 = Tout.P2; if (Tout.DialogResult == DialogResult.OK) { squareClicked.Is_Turnout = true; //Change to be toggle //squareClicked.Set_Piece(0, Piece1);//Check setpiece out to see if it is the... //... best way of doing the image squareClicked.BackColor = Color.Black;//Blacks the color out to indicate it's a turnout squareClicked.Set_Turnout(Piece1, Piece2);//Set the pieces of the turnout squareClicked.Text = "T";//Change text to indicate that the square is a turnout squareClicked.CurrentPiece = squareClicked.Get_Piece(0);//Set the current piece to the first piece squareClicked.Is_Active = true;//The square is an active turnout Turnouts[Turnout_Index] = squareClicked;//Add the square to the turnout array Turnout_Index++;//** Increase Index } } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { //Doesn't check to see if the document was saved. //Just exits the program. this.Close(); } //Gets out of edit mode and enters run mode private void bttnRunIt_Click(object sender, EventArgs e) { //Set Xvals and Yvals for edit mode Xvals = TakenSpots.GetLength(0); Yvals = TakenSpots.GetLength(1); Run RunTrack = new Run();//New Run Form //Transfer necessary info over to the new Run Form RunTrack.Xvals = this.Xvals; RunTrack.Yvals = this.Yvals; RunTrack.TakenSpots = TakenSpots; RunTrack.Turnouts = Turnouts; RunTrack.ShowDialog();//Show the New Run Form } //Saves the track to a file private void saveToolStripMenuItem_Click(object sender, EventArgs e) { //Save to a file. Just save the object myDoc.DocBoard = TakenSpots;//Shallow Copy myDoc.DocXvals = Xvals; myDoc.DocYvals = Yvals; myDoc.Save(); } //THIS IS JUST FOR TESTING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 private void showArrayToolStripMenuItem_Click(object sender, EventArgs e)//Just for testing { string temp = ""; int cnt = 0; for (int i = 0; i < Xvals; i++) { for (int j = 0; j < Yvals; j++) { if (TakenSpots[i, j].Name == "") { cnt++; } else { Square z = TakenSpots[i, j]; temp += "ID:" + z.CurrentPiece.Id.ToString() + "X:" + z.X + "Y:" + z.Y; temp += "//"; } } } temp += "&" + Convert.ToInt32(cnt) + " Unused Spaces"; MessageBox.Show("Array" + temp); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { myDoc.DocBoard = TakenSpots; myDoc.DocXvals = Xvals; myDoc.DocYvals = Yvals; myDoc.SaveAs(); } //Opens a track from a file private void openToolStripMenuItem_Click(object sender, EventArgs e) {//From Program 3 Dr. McVey CS 350 if (myDoc.HasBeenModified == true) { //Addapted from Lab3 by Dr. McVey DialogResult result = MessageBox.Show("Do you want to save changes?", "Confirmation", MessageBoxButtons.YesNoCancel); if (result == DialogResult.Yes) { //Run SaveAs saveAsToolStripMenuItem.PerformClick(); } else { return; } } myDoc.Open();//Open a new document Xvals = myDoc.DocXvals; Yvals = myDoc.DocYvals; //Wipe the board off. Clear all values and images ClearBoard(); //Pull the track in from the file/document TakenSpots = myDoc.DocBoard; Setboard(this, e);//Set a blank board //Set the visible grid based on the file that was opened foreach (Square s in gbxGrid.Controls) { int i = s.X; int j = s.Y; if (TakenSpots[i, j].Is_Active == true) { s.Image = TakenSpots[i, j].CurrentPiece.Img; s.Text = ""; s.BackColor = Color.Gray; s.Is_Active = true; } else { s.Text = TakenSpots[i, j].X.ToString() + "," + TakenSpots[i, j].Y.ToString(); s.Image = null; ; s.BackColor = Color.Yellow;//%%Just For Testing Comment Later } } }//end of open click //**Clears controls from void ClearBoard() { while (gbxGrid.Controls.Count != 0)//very strange, but not all buttons are deleted. foreach (Square s in gbxGrid.Controls) gbxGrid.Controls.Remove(s); Controls.Remove(gbxGrid); } private void removeButtonsToolStripMenuItem_Click(object sender, EventArgs e) { ClearBoard(); } //**Creates a new document and wipes the old one out private void newToolStripMenuItem_Click(object sender, EventArgs e) { ClearBoard();//Wipes the visible track/board clear TrackLayout Diments = new TrackLayout();//Get new dimentions Diments.ShowDialog();//** Get The dimentions of the track if (Diments.DialogResult == DialogResult.OK) { //Set Xvals and Yvals to those just specified Xvals = Diments.Xnum; Yvals = Diments.Ynum; //Make a new array TakenSpots = new Square[Xvals, Yvals]; Turnouts = new Square[Xvals + Yvals - 4];//** That's the number of possible turnouts for (int i = 0; i < Xvals; i++)//Fill Array Up with default squares { for (int j = 0; j < Yvals; j++) { TakenSpots[i, j] = new Square(i, j); } } } myDoc.NewFile();//Create a new file for (int i = 0; i < Xvals; i++)//Fill Array (TakenSpots) Up with default squares { for (int j = 0; j < Yvals; j++) { TakenSpots[i, j] = new Square(i, j); } } Setboard(this,e); } } }