private void List_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (ListBoxMain.SelectedIndex == 0 && ContentC != null) { ContentC.ContentTemplate = Resources["FieldTemplate"] as DataTemplate; TextBoxFileName.Text = "NewField"; FileEndingName.Content = ".fld"; field_templates = new ObservableCollection <string>(); // read field properties string[] s = Properties.Settings.Default.FieldTemplates.Split('\n'); for (int x = 0; x < s.Length; x++) { field_templates.Add(s[x].Trim('\r').Split(':')[0]); } } if (ListBoxMain.SelectedIndex == 1 && ContentC != null) { ContentC.ContentTemplate = Resources["StructureTemplate"] as DataTemplate; TextBoxFileName.Text = "NewStructure"; FileEndingName.Content = ".sct"; templates = new ObservableCollection <string>(); // read settings string template = Properties.Settings.Default.StructureTemplates; XElement xml = XElement.Parse(template); List <ClusterStructure> templist = new List <ClusterStructure>(); foreach (XElement definition in xml.Elements()) { ClusterStructure d = new ClusterStructure(definition); templates.Add(d.name); } dominoes = 1000; } }
public ClusterStructureProvider(int width, int height, int structIndex) { m_length = width; m_height = height; list = new List <string>(); description_imgs = new BitmapSource[9]; // initialize structure combo box ReadStructureTemplates(); selected_template = structure_templates[structIndex]; FillDescriptionImages(); // update structure logic }
private void ReadStructureTemplates() { list.Clear(); string templates = Properties.Settings.Default.StructureTemplates; XElement xml = XElement.Parse(templates); structure_templates = new List <ClusterStructure>(); foreach (XElement definition in xml.Elements()) { ClusterStructure d = new ClusterStructure(definition); list.Add(d.name); structure_templates.Add(d); } }
private void Button_Click(object sender, RoutedEventArgs e) { if (ListBoxMain.SelectedIndex == 0) // Field { if (image_path != null) { // read properties from settings string prop = Properties.Settings.Default.FieldTemplates.Split('\n')[field_index]; prop = prop.Trim('\r').Split(':')[1]; string[] distance_properties = prop.Split('*'); int a = int.Parse(distance_properties[0]); int b = int.Parse(distance_properties[1]); int c = int.Parse(distance_properties[2]); int d = int.Parse(distance_properties[3]); String filename = TextBoxFileName.Text; BitmapImage bit = new BitmapImage(new Uri(image_path)); String ct = GetVisualChild <TextBox>(ContentC).Text; int minimum = Int32.MaxValue; int length = 1; while (true) { length++; int height = (int)Math.Round(((double)length * (a + b) / (c + d) * bit.PixelHeight / bit.PixelWidth)); int difference = Math.Abs(length * height - int.Parse(ct)); if (difference <= minimum) { minimum = difference; } else { length--; break; } } // Copy Image String newpath = System.IO.Path.Combine(project_path, "Source Images", filename + System.IO.Path.GetExtension(image_path)); if (!(System.IO.File.Exists(System.IO.Path.Combine(project_path, filename + ".fld")) || File.Exists(newpath))) { File.Copy(image_path, newpath, false); File.SetAttributes(newpath, FileAttributes.Normal); doc = new FieldDocument(System.IO.Path.Combine(project_path, filename + ".fld"), newpath, colors, length, a, b, c, d); DialogResult = true; Close(); } else { MessageBox.Show("A field with this name already exists. Please choose another name."); } } else { MessageBox.Show("Please select an image."); } } if (ListBoxMain.SelectedIndex == 1) // Structure { if (image_path != null) { String filename = TextBoxFileName.Text; BitmapImage bit = new BitmapImage(new Uri(image_path)); string templates = Properties.Settings.Default.StructureTemplates; XElement xml = XElement.Parse(templates); ClusterStructure d = new ClusterStructure(xml.Elements("StructureDefinition").ToArray()[structure_index]); float center_width = d.cells[1, 1].width; float center_height = d.cells[1, 1].height; float add_width = d.cells[0, 0].width + d.cells[2, 2].width; float add_height = d.cells[0, 0].height + d.cells[2, 2].height; int s_width = 1; int s_height = 1; int minimum = int.MaxValue; while (true) { s_height = (int)((((center_width * s_width + add_width) * (float)bit.PixelHeight / (float)bit.PixelWidth) - add_height) / center_height); int difference = dominoes - d.GenerateStructure(s_width, s_height).dominoes.Length; if (Math.Abs(difference) <= minimum) { minimum = difference; } else { s_width--; s_height = (int)((((center_width * s_width + add_width) * (float)bit.PixelHeight / (float)bit.PixelWidth) - add_height) / center_height); break; } s_width++; } if (s_width == 0) { s_width++; } if (s_height == 0) { s_height++; } // Copy Image String newpath = System.IO.Path.Combine(project_path, "Source Images", filename + System.IO.Path.GetExtension(image_path)); if (!(System.IO.File.Exists(System.IO.Path.Combine(project_path, filename + ".sct")) || File.Exists(newpath))) { File.Copy(image_path, newpath, false); File.SetAttributes(newpath, FileAttributes.Normal); doc = new StructureDocument(System.IO.Path.Combine(project_path, filename + ".sct"), newpath, colors, new ClusterStructureProvider(s_width, s_height, structure_index)); DialogResult = true; Close(); } else { MessageBox.Show("A structure with this name already exists. Please choose another name."); } } else { MessageBox.Show("Please select an image."); } } }