/// <summary> /// Fills the torque ratio Listview /// </summary> private void PopulateGears() { // Clear out any old items gearListView.Items.Clear(); // Reorder ReverseGears = ReverseGears.OrderBy(x => x.Ratio).ToList(); ForwardGears = ForwardGears.OrderByDescending(x => x.Ratio).ToList(); // Reverse Gears int i = 0; foreach (var gear in ReverseGears) { gear.GearIndex = i; AddGear(gear, i++); } // Forward Gears i = 0; foreach (var gear in ForwardGears) { gear.GearIndex = i; AddGear(gear, i++); } }
/// <summary> /// Method called whenever a gear is modified. This method will move the gear /// to the Forward or Reverse gear depending. /// </summary> /// <param name="oldGear">The gear before the changes</param> /// <param name="newGear">The new gear values</param> /// <param name="index">The index the oldGear is at in its corresponding list</param> /// <returns></returns> private bool OnGearEdit(TransmissionGear oldGear, TransmissionGear newGear, int index) { // Are we switching from forward to reverse (or vise-versa)? if (oldGear.IsReverse != newGear.IsReverse) { if (newGear.IsReverse) { ReverseGears.Add(newGear); ForwardGears.RemoveAt(index); } else { ForwardGears.Add(newGear); ReverseGears.RemoveAt(index); } } else { var list = (newGear.IsReverse) ? ReverseGears : ForwardGears; list[index] = newGear; } return(true); }
private void importButton_Click(object sender, EventArgs e) { // Request the user supply the steam library path OpenFileDialog Dialog = new OpenFileDialog(); Dialog.Title = "Transmission SII File Import"; Dialog.Filter = "SiiNunit|*.sii"; if (Dialog.ShowDialog() == DialogResult.OK) { try { // Create document var document = new SiiDocument(typeof(AccessoryTransmissionData), typeof(TransmissionNames)); using (FileStream stream = File.OpenRead(Dialog.FileName)) using (StreamReader reader = new StreamReader(stream)) { // Read the file contents string contents = reader.ReadToEnd().Trim(); document.Load(contents); // Grab the engine object List <string> objects = new List <string>(document.Definitions.Keys); if (objects.Count == 0) { MessageBox.Show("Unable to find any transmission data in this sii document!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning ); return; } // Grab the transmission var transmission = document.GetDefinition <AccessoryTransmissionData>(objects[0]); // === Set form values unitNameBox.Text = Path.GetFileNameWithoutExtension(Dialog.FileName); transNameBox.Text = transmission.Name; unlockBox.SetValueInRange(transmission.UnlockLevel); priceBox.SetValueInRange(transmission.Price); diffRatio.SetValueInRange(transmission.DifferentialRatio); if (transmission.StallTorqueRatio > 0m) { stallRatio.SetValueInRange(transmission.StallTorqueRatio); hasTorqueConverter.Checked = true; } if (transmission.Retarder > 0) { retardPositions.SetValueInRange(transmission.Retarder); hasRetarder.Checked = true; } fileDefaultsTextBox.Lines = transmission.Defaults; filenameTextBox.Text = Path.GetFileName(Dialog.FileName); conflictsTextBox.Lines = transmission.Conflicts; suitablesTextBox.Lines = transmission.Suitables; // Clear chart points and gears chart1.Series[0].Points.Clear(); gearListView.Items.Clear(); ForwardGears.Clear(); ReverseGears.Clear(); // Set gear ratios var nameList = transmission.GearNames?.Reverse?.ToList(); int i = 0; foreach (var item in transmission.ReverseRatios) { string name = (nameList != null && i < nameList.Count) ? nameList[i] : string.Empty; ReverseGears.Add(new TransmissionGear() { Name = name, Ratio = item, GearIndex = i++ }); } nameList = transmission.GearNames?.Forward?.ToList(); i = 0; foreach (var item in transmission.ForwardRatios) { string name = (nameList != null && i < nameList.Count) ? nameList[i] : string.Empty; ForwardGears.Add(new TransmissionGear() { Name = name, Ratio = item, GearIndex = i++ }); } // Fill ratio view PopulateGears(); // Flag GearsChanged = true; // Defaults (skip sounds) if (transmission.Defaults != null) { fileDefaultsTextBox.Lines = transmission.Defaults; } // Alert the user MessageBox.Show( $"Successfully imported the transmission \"{transmission.Name}\"! You must now select a series for this transmission.", "Import Successful", MessageBoxButtons.OK, MessageBoxIcon.Information ); } } catch (SiiSyntaxException ex) { StringBuilder builder = new StringBuilder("A Syntax error occured while parsing the sii file!"); builder.AppendLine(); builder.AppendLine(); builder.AppendLine($"Message: {ex.Message.Replace("\0", "\\0")}"); builder.AppendLine(); builder.AppendLine($"Line: {ex.Span.Start.Line}"); builder.AppendLine($"Column: {ex.Span.Start.Column}"); MessageBox.Show(builder.ToString(), "Sii Syntax Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SiiException ex) { StringBuilder builder = new StringBuilder("Failed to parse the sii file."); builder.AppendLine(); builder.AppendLine($"Message: {ex.Message}"); MessageBox.Show(builder.ToString(), "Sii Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "An Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void ConfirmButton_Click(object sender, EventArgs e) { // Check UnitName // Check for a valid identifier string if (!SiiFileBuilder.IsValidUnitName(unitNameBox.Text)) { // Tell the user this isnt allowed MessageBox.Show( "Invalid Transmission Sii Unit Name. Tokens must be 1 to 12 characters in length, seperated by a dot, " + "and contain alpha-numeric or underscores only", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning ); return; } // Check engine name if (transNameBox.Text.Length < 2 || transNameBox.Text.Contains('"')) { // Tell the user this isnt allowed MessageBox.Show( "Invalid Transmission Name string! The name must be at least 2 characters long and contain no quotes", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning ); return; } // Set new attribute values Transmission.SeriesId = ((TransmissionSeries)seriesModelBox.SelectedItem).Id; Transmission.UnitName = unitNameBox.Text.Trim(); Transmission.Name = transNameBox.Text.Trim(); Transmission.Price = (int)priceBox.Value; Transmission.Unlock = (int)unlockBox.Value; Transmission.DifferentialRatio = diffRatio.Value; Transmission.Defaults = fileDefaultsTextBox.Lines; Transmission.Comment = fileCommentTextBox.Lines; Transmission.Conflicts = conflictsTextBox.Lines; Transmission.SuitableFor = suitablesTextBox.Lines; Transmission.Retarder = (hasRetarder.Checked) ? (int)retardPositions.Value : 0; Transmission.StallTorqueRatio = (hasTorqueConverter.Checked) ? stallRatio.Value : 0.0m; // Figure out the filename if (!String.IsNullOrWhiteSpace(filenameTextBox.Text)) { Transmission.FileName = filenameTextBox.Text.Trim(); } // Validate and Save using (AppDatabase db = new AppDatabase()) using (SQLiteTransaction trans = db.BeginTransaction()) { // Verify that the series.Id and engine.UnitName are unique string query = "SELECT * FROM `Transmission` WHERE `SeriesId`=@P0 AND `UnitName`=@P1"; var eTrans = db.Query <Transmission>(query, Transmission.SeriesId, Transmission.UnitName).FirstOrDefault(); if (eTrans != null && (NewTransmission || eTrans.Id != Transmission.Id)) { // Tell the user this isnt allowed MessageBox.Show( $"The selected Transmission Series already contains a transmission with the Sii Unit Name of \"" + Transmission.UnitName + "\"! Please select a different Transmission Series or change the Sii " + "Unit Name to something unique.", "Unique Constraint Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning ); return; } // Wrap database changes in a try-catch block so we can Rollback on error try { // Update the current engine db.Transmissions.AddOrUpdate(Transmission); // If pre-existing transmission, delete all changed data if (!NewTransmission) { // Delete any and all TorueRatios from the database if (GearsChanged) { foreach (var gear in Transmission.Gears) { db.TransmissionGears.Remove(gear); } } // Set Conflicts if (ConflictsChanged) { foreach (var conflict in Transmission.EngineConflicts) { db.AccessoryConflicts.Remove(conflict); } } // Set Suitables if (SuitablesChanged) { foreach (var item in Transmission.SuitableEngines) { db.SuitableAccessories.Remove(item); } } // Set Conflicts if (TrucksChanged) { foreach (var item in Transmission.ItemOf) { db.TruckTransmissions.Remove(item); } } } // Add conflicts if any if ((NewTransmission || ConflictsChanged) && conflictListView.CheckedItems.Count > 0) { var ids = conflictListView.CheckedItems.Cast <ListViewItem>().Select(x => (int)x.Tag); foreach (var item in ids) { db.AccessoryConflicts.Add(new AccessoryConflict() { EngineId = item, TransmissionId = Transmission.Id }); } } // Add suitible fors if any if ((NewTransmission || SuitablesChanged) && suitsListView.CheckedItems.Count > 0) { var ids = suitsListView.CheckedItems.Cast <ListViewItem>().Select(x => (int)x.Tag); foreach (var item in ids) { db.SuitableAccessories.Add(new SuitableAccessory() { EngineId = item, TransmissionId = Transmission.Id }); } } // Add trucks if any if ((NewTransmission || TrucksChanged) && truckListView.CheckedItems.Count > 0) { var ids = truckListView.CheckedItems.Cast <ListViewItem>().Select(x => (int)x.Tag); foreach (var item in ids) { db.TruckTransmissions.Add(new TruckTransmission() { TruckId = item, TransmissionId = Transmission.Id }); } } // Add the new torque ratios if (NewTransmission || GearsChanged) { int i = 0; foreach (var gear in ReverseGears.OrderBy(x => x.Ratio)) { gear.TransmissionId = Transmission.Id; gear.GearIndex = i++; db.TransmissionGears.Add(gear); } foreach (var gear in ForwardGears.OrderByDescending(x => x.Ratio)) { gear.TransmissionId = Transmission.Id; gear.GearIndex = i++; db.TransmissionGears.Add(gear); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); // Tell the user about the failed validation error MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } this.DialogResult = DialogResult.OK; }
public TransmissionForm(Transmission transmission = null) { // Create form controls InitializeComponent(); headerPanel.BackColor = Color.FromArgb(51, 53, 53); chart1.ChartAreas[0].AxisX.Interval = 10; chart1.MouseWheel += Chart1_MouseWheel; NewTransmission = transmission == null; Transmission = transmission ?? new Transmission(); Sorter = new ListViewColumnSorter(truckListView) { Order = SortOrder.Ascending }; // Setup metrics if (Program.Config.UnitSystem == UnitSystem.Metric) { chart1.ChartAreas[0].AxisX.Title = "Speed (Kph)"; conflictListView.Columns[2].Text = "N·m"; suitsListView.Columns[2].Text = "N·m"; } // Add each sound to the lists using (AppDatabase db = new AppDatabase()) { foreach (var model in db.TransmissionSeries.OrderBy(x => x.ToString())) { seriesModelBox.Items.Add(model); if (!NewTransmission && model.Id == Transmission.SeriesId) { seriesModelBox.SelectedIndex = seriesModelBox.Items.Count - 1; } } if (!NewTransmission) { var gears = transmission.Gears.ToList(); ForwardGears.AddRange(gears.Where(x => !x.IsReverse)); ReverseGears.AddRange(gears.Where(x => x.IsReverse)); } else { ReverseGears.Add(new TransmissionGear() { Ratio = -5.55m }); ForwardGears.AddRange(new[] { new TransmissionGear() { Ratio = 4.7m }, new TransmissionGear() { Ratio = 2.21m }, new TransmissionGear() { Ratio = 1.53m }, new TransmissionGear() { Ratio = 1.0m }, new TransmissionGear() { Ratio = 0.76m }, new TransmissionGear() { Ratio = 0.67m } }); } // Grab a list of trucks that use this transmission List <int> trucks = new List <int>(); if (!NewTransmission) { trucks.AddRange(transmission.ItemOf.Select(x => x.TruckId)); } // Add trucks to the truck list foreach (var truck in db.Trucks) { ListViewItem item = new ListViewItem(truck.Name); item.Tag = truck.Id; item.Checked = trucks.Contains(truck.Id); truckListView.Items.Add(item); } } // Are we editing an engine? if (!NewTransmission) { // Set form values unitNameBox.Text = transmission.UnitName; transNameBox.Text = transmission.Name; unlockBox.Value = transmission.Unlock; priceBox.Value = transmission.Price; diffRatio.Value = transmission.DifferentialRatio; if (transmission.StallTorqueRatio > 0m) { stallRatio.Value = transmission.StallTorqueRatio; hasTorqueConverter.Checked = true; } if (transmission.Retarder > 0) { retardPositions.Value = transmission.Retarder; hasRetarder.Checked = true; } fileDefaultsTextBox.Lines = transmission.Defaults; fileCommentTextBox.Lines = transmission.Comment; filenameTextBox.Text = transmission.FileName; conflictsTextBox.Lines = transmission.Conflicts; suitablesTextBox.Lines = transmission.SuitableFor; } // Fill torque ratios PopulateGears(); PopulateEngines(); }