using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TrackMaker4 { //**This Class represents the options for the contents of a Square (see Square.Cs) //**A square can have 0 - 2. // 0 if it's inactive // 2 if it's a turnout // 1 if it's neither public partial class Piece { String Dir;//This will hold the current directory Image [] Piece_Pics;//An array of possible piece images const int ID_MAX = 5;//Maximum Id a piece can be const int ID_MIN = 0;//Minimum Id a piece can be //Sides of a Piece. Arbitrary numbers assigned to each side of a piece/square //To be used when deciding in/out const int LEFT = 0; const int TOP = 1; const int RIGHT = 2; const int BOTTOM = 3; //Different Piece Id's. Arbitrary numbers assigned to each piece type const int VERT = 0;//Vertical piece const int HORIZ = 1;//Horizontal piece const int TLP = 2;//Top Left Piece const int TRP = 3;//Top Right Piece const int BLP = 4;//Bottom Left Piece const int BRP = 5;//Borrom Rigt Piece private int id;//Id indicates what kind of piece it is private int sin;//Side of the Piece a Train Come In private int sout;//Side of the Piece a Train Goes Out private Image image;//String containing image location private Image [,] Turnout_Images;//An array of different turnout images it could be //constructors******************************** public Piece()//**Default Constructor { Dir = Environment.CurrentDirectory;//Holds Directory of Current Environment //Takes Pictures from current directory. All possible pieces Piece_Pics = 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") }; this.id = VERT;//Set Default to Vertical Piece this.sin = BOTTOM;//In through the bottom this.sout = TOP;//Out through the top this.image = Piece_Pics[0];//The default image is not vertical //this.bttn.DialogResult = DialogResult.OK;//Change this later } public Piece(Piece p)//copy constructor { //All values are set to that of p this.id = p.id; this.sin = p.sin; this.sout = p.sout; this.image = p.image; } public Piece(int pid)//explicit vlaue { Dir = Environment.CurrentDirectory;//Looks at current directory //Loads all possible images in from current directory Piece_Pics = 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") }; //Creats a 2-D array for all possible Turnout Images Turnout_Images = new Image[6, 6]; //Loads all possible images in for turnouts from current directory 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); } } id = pid;//Set specified Id if (this.id > ID_MAX)//if id is too high { MessageBox.Show("Error! Id set too high. Setting to " + ID_MAX.ToString());//error id = ID_MAX;//default to 5 } else if (this.id < ID_MIN)//else if id is too low { MessageBox.Show("Error! Id set too low. Setting to " + ID_MIN.ToString());//error id = ID_MIN;//default to 0 } //End of setting the Id //Setting the Image this.image = Piece_Pics[this.id]; //Setting the sin/sout //These get changed later so there's really no need, but its good to have values this.sin = Set_In(this.id); this.sout = Set_Out(this.id); //check to make sure that the ins and outs are good. play it safe if (this.sin == -1 || this.sout == -1) { MessageBox.Show ( "[Error Pieces.cs] Your ID is Invalid and causes your In/Out Values to be Invalid as well" ); MessageBox.Show("Showing ID" + this.id); } } //End Constructors********************************************* //Set/Gets**************************************************** public int Id//get or set values for the first piece { set { id = value; } get { return id; } } public int In//get or set values for the first piece { set { sin = value; }//Need to change if direction changes Maybe?? get { return sin; }//Train needs to see this } public int Out//get or set values for the first piece { set { sout = value; }//Need to set if direction changes Maybe?? get { return sout; }//Train needs to see this } public Image Img { set { image = value; } get { return image; } } //End Set/Gets************************************************ //*NOTE* all in/out values are based off clockwise movement //Function that sets the in "sin" value for the constructor //This really doesnt need to be here. The vlaues are set later.%% private int Set_In(int i)//All { if (i == BRP)//Bottom Right Piece { return TOP; } else if (i == BLP)//Bottom Right Piece { return RIGHT; } else if (i == TLP)//Top Left Piece { return BOTTOM; } else if (i==TRP)//Top Right Piece { return LEFT; } //These will be taken care of in run. Given Ambiguous value for now else if (i == VERT || i == HORIZ)//If horizontal or vertical return 10; else return -1;//error } //*NOTE* all in/out values are based off clockwise movement //Function that sets the out "sout" value for constructor //Again, this really doesnt matter, because run takes care of it.%% private int Set_Out(int i) { if (i == BRP)//Bottom Right Piece { return LEFT; } else if (i == BLP)//Bottom Left Piece { return TOP; } else if (i == TLP)//Top Left Piece { return RIGHT; } else if (i == TRP)//Top Right Piece { return BOTTOM; } //These will be taken care of in run. Given Ambiguous value for now else if (i == VERT || i == HORIZ)//Vertical or Horizontal return 10; else return -1;//error } //This is a function used to erase squares. IT blanks out the image public void Blank_Image() { this.Img = Piece_Pics[6]; } //This sets the image image to the correct turnout image //If first is true, it sets it with p1 in front(in black) //If not, it sets the image with p2 in front (in black) public void Set_Turnout_Piece(bool first, int p1,int p2) { if (first) { this.Img = Turnout_Images[p1, p2]; } else this.Img = Turnout_Images[p2, p1]; } }//End of public partial class Piece }//End of Namespace TrackMaker2