/// <summary> /// Initializes the form /// </summary> public EnigmaMachineUI() { InitializeComponent(); this.KeyPreview = true; // makes the form the starting point for key presses // sets the sizes this.MaximumSize = this.Size; this.MinimumSize = this.Size; this.MaximizeBox = false; this.MinimizeBox = false; // sets the Rotor defaults Rotors.SetRotorDefaults = FileManagement.GetInformationFromFile(FileNames.Rotors); // initializes three rotors Rotors[] rotors = new Rotors[3]; // runs through each control in the rotor panel for (int i = 0; i < pnRotorNumbers.Controls.Count; i++) { // attempts to convert the control to a combobox ComboBox cb = pnRotorNumbers.Controls[i] as ComboBox; // if successful if (cb != null) { // sets the rotor information rotors[i] = new Rotors(i); RotorLoad(cb, rotors[i]); cb.SelectedIndex = i; } } // loads the reflector combo box ReflectorLoad(); // initializes a new machine machine = new Machine(rotors, Reflector.GetReflectorString(cbReflector.SelectedIndex), ""); // adds the labels to the three letter panels AddLabels(pnLamps); AddLabels(pnKeys); AddLabels(pnPlugboard); // counts the number of rotors on the form and sets the offset controls int num = pnRotorNumbers.Controls.Count; for (int i = 0; i < num; i++) { OffsetLoad(i); } // initializes the colours used array colourUsed = new bool[colours.Length]; // sets the plugboard header to the default foreground colour lbPlugboardHeader.ForeColor = DEFAULT_LETTER_FG_COLOUR; // centers the clear button btnClearPlugboard.Location = new Point(this.Width / 2 - btnClearPlugboard.Width / 2, 595); }
/// <summary> /// Rotates the cipher /// </summary> /// <param name="n">the rotor number</param> /// <param name="dir">the direction of rotation</param> private void RotateCypher(int n, int dir = 1) { // if the direction is positive, set it to 1 if (dir > 0) { dir = 1; } // if the direction is negative, set it to -1 else if (dir < 0) { dir = -1; } // gets the rotor with the desired number Rotors r = machine.GetRotor(n); // rotates the rotor machine.RotateRotor(r, dir); // rotate the rotor controls RotateControls(n, dir); }
/// <summary> /// Sets the initial rotor settings /// </summary> /// <param name="cb">the combobox to set</param> /// <param name="rotor">The rotor to store the information in</param> public void RotorLoad(ComboBox cb, Rotors rotor) { // if the combobox is initialized if (cb != null) { // store the rotor number and sets the cipher number to the index int rNum = pnRotorNumbers.Controls.IndexOf(cb as Control); rotor.CipherNumber = rNum; // if the combobox has values in it, clear it if (cb.Items.Count > 0) { cb.Items.Clear(); } // sets the combobox information based on the numbers from the rotors string[] rotorNums = Rotors.GetRotorNumbers().Split(' '); cb.Items.AddRange(rotorNums); // appends the keydown event cb.KeyDown += ComboBoxKeyDown; } }