/// <summary> /// Update the resource source code /// </summary> private void UpdateSourceCode() { var path = selectedVisual; if (path == null) { SourceCode = string.Empty; return; } if (Normalize) { var normalizer = new NormalizeVisual(); path = normalizer.Normalize(selectedVisual, NormalizeAspect.Both, 100); } switch (SelectedGeometryTypeItem.GeometryGeneratorType) { case ResourceGeometryGeneratorType.Stream: { var streams = StreamSourceGenerator.GenerateStreamGeometries(path); SourceCode = string.Join("\n", streams); break; } case ResourceGeometryGeneratorType.Geometry: { SourceCode = GeometrySourceGenerator.GenerateGeometry(path); break; } case ResourceGeometryGeneratorType.PathGeometry: { SourceCode = PathGeometrySourceGenerator.GeneratePathGeometry(path); break; } } }
/// <summary> /// Create the beginning of the source code /// </summary> protected void InitCode(GraphicVisual visual) { Indent1 = SourceGeneratorHelper.GetSourceIndent(1); Indent2 = SourceGeneratorHelper.GetSourceIndent(2); var indent1 = Indent1; var indent2 = Indent2; this.finalized = false; if (NormalizeAspect != NormalizeGeometrySourceAspect.Individual) { string scaleVariableName = string.Empty; string opositeVariableName = string.Empty; string methodName = string.Empty; string opositeMethodName = string.Empty; switch (NormalizeAspect) { case NormalizeGeometrySourceAspect.Width: scaleVariableName = "width"; opositeVariableName = "height"; methodName = "CreateFromWidth"; opositeMethodName = "CreateFromHeight"; break; case NormalizeGeometrySourceAspect.Height: scaleVariableName = "height"; opositeVariableName = "width"; methodName = "CreateFromHeight"; opositeMethodName = "CreateFromWidth"; break; } // class header code.AppendLine("/// <summary>"); code.AppendLine("/// Create a geometry."); code.AppendLine("/// The origin is normalized to 0/0."); code.AppendLine(string.Format("/// The {0} is normalized to 1.0, the aspect ratio is kept", scaleVariableName)); code.AppendLine(string.Format("/// from the original stream, which means the {0} might be", opositeVariableName)); code.AppendLine("/// greater than 1 (depending on the aspect ratio)."); code.AppendLine("/// </summary>"); code.AppendLine("private static class xyzGeometry"); code.AppendLine("{"); // the aspect ratio constant code.AppendLine($"{indent1}/// <summary>"); code.AppendLine($"{indent1}/// The aspect ratio (height/width) of the geometry"); code.AppendLine($"{indent1}/// </summary>"); code.AppendLine(string.Format("{0}public const double AspectRatio = {1};", indent1, aspectRatio.ToString("G", CultureInfo.InvariantCulture))); code.AppendLine(""); // the creation method for the opposite string offsetMethodParameterString = string.Empty; string offsetParameterString = string.Empty; if (IncludeOffset) { offsetMethodParameterString = "double left, double top, "; offsetParameterString = "left, top, "; } code.AppendLine($"{indent1}/// <summary>"); code.AppendLine($"{indent1}/// Create the geometry from the given {opositeVariableName} and keep the original aspect ratio"); code.AppendLine($"{indent1}/// </summary>"); code.AppendLine(string.Format("{0}/// <param name=\"{1}\">the {1} in WPF units</param>", indent1, opositeVariableName)); code.AppendLine($"{indent1}/// <returns>Returns the geometry</returns>"); code.AppendLine($"{indent1}public static Geometry {opositeMethodName}({offsetMethodParameterString}double {opositeVariableName})"); code.AppendLine($"{indent1}{{"); if (NormalizeAspect == NormalizeGeometrySourceAspect.Width) { code.AppendLine($"{indent2}return {methodName}({offsetParameterString}{opositeVariableName} / AspectRatio);"); } else { code.AppendLine($"{indent2}return {methodName}({offsetParameterString}{opositeVariableName} * AspectRatio);"); } code.AppendLine($"{indent1}}}"); code.AppendLine(""); // primary creation method code.AppendLine($"{indent1}/// <summary>"); code.AppendLine($"{indent1}/// Create the geometry from the given {scaleVariableName} and keep the original aspect ratio"); if (!string.IsNullOrEmpty(Filename)) { code.AppendLine(string.Format($"{indent1}/// Shapes extracted from file \"{0}\"", Filename)); } code.AppendLine($"{indent1}/// Generated from the following stream geometry:"); var streams = StreamSourceGenerator.GenerateStreamGeometries(visual); foreach (var stream in streams) { code.Append($"{indent1}/// "); code.AppendLine(stream); } code.AppendLine($"{indent1}/// </summary>"); code.AppendLine(string.Format("{0}/// <param name=\"{1}\">the {1} in WPF units</param>", indent1, scaleVariableName)); code.AppendLine($"{indent1}/// <returns>Returns the geometry</returns>"); code.AppendLine($"{indent1}public static Geometry {methodName}({offsetMethodParameterString}double {scaleVariableName})"); code.AppendLine($"{indent1}{{"); code.AppendLine($"{indent2}double scale = {scaleVariableName};"); } else { // class header code.AppendLine("/// <summary>"); code.AppendLine("/// Create a geometry."); code.AppendLine("/// The origin is normalized to 0/0."); code.AppendLine("/// Width and height are normalized independently to 1.0"); code.AppendLine("/// </summary>"); code.AppendLine("private static class XYZGeometry"); code.AppendLine("{"); // the aspect ratio constant code.AppendLine($"{indent1}/// <summary>"); code.AppendLine($"{indent1}/// The aspect ratio (height/width) of the original geometry"); code.AppendLine($"{indent1}/// </summary>"); code.AppendLine(string.Format("{0}public const double AspectRatio = {1};", indent1, aspectRatio.ToString("G", CultureInfo.InvariantCulture))); code.AppendLine(""); string offsetMethodParameterString = string.Empty; string offsetParameterString = string.Empty; if (IncludeOffset) { offsetMethodParameterString = "double left, double top, "; offsetParameterString = "left, top, "; } // the creation method for CreateSecondaryCreationMethod(code, NormalizeGeometrySourceAspect.Height, "CreateFromHeight", "Create", "height", offsetMethodParameterString, offsetParameterString); CreateSecondaryCreationMethod(code, NormalizeGeometrySourceAspect.Width, "CreateFromWidth", "Create", "width", offsetMethodParameterString, offsetParameterString); // primary creation method code.AppendLine($"{indent1}/// <summary>"); code.AppendLine($"{indent1}/// Create the geometry from the given width and height with any aspect ratio"); if (!string.IsNullOrEmpty(Filename)) { code.AppendLine(string.Format("{0}/// Shapes extracted from file \"{1}\"", indent1, Filename)); } code.AppendLine($"{indent1}/// Generated from the following stream geometry:"); var streams = StreamSourceGenerator.GenerateStreamGeometries(visual); foreach (var stream in streams) { code.Append($"{indent1}/// "); code.AppendLine(stream); } code.AppendLine($"{indent1}/// </summary>"); code.AppendLine($"{indent1}/// <param name=\"width\">the width in WPF units</param>"); code.AppendLine($"{indent1}/// <param name=\"height\">the height in WPF units</param>"); code.AppendLine($"{indent1}/// <returns>Returns the geometry</returns>"); code.AppendLine($"{indent1}public static Geometry Create({offsetMethodParameterString}double width, double height)"); code.AppendLine($"{indent1}{{"); } }
/// <summary> /// Update the Stream /// </summary> private void UpdateStreamSourceCode() { if (TypeComboBox == null || StreamCode == null) { return; } GraphicVisual visual = selectedVisual; if (NormalizeCheckBox.IsChecked == true) { var normalizer = new NormalizeVisual(); visual = normalizer.Normalize(selectedVisual, NormalizeAspect.Both, 100); } string xamlStream; GraphicPath graphicPath = visual as GraphicPath; if (graphicPath != null) { enableAllItems = true; SetTypeItemStatus(); StreamCode.TextWrapping = TextWrapping.NoWrap; } else { enableAllItems = false; SetTypeItemStatus(); if (TypeComboBox.SelectedIndex >= 2) { TypeComboBox.SelectedIndex = 1; } StreamCode.TextWrapping = TextWrapping.NoWrap; } if (graphicPath != null) { if (TypeComboBox.SelectedIndex == 0) { var streams = StreamSourceGenerator.GenerateStreamGeometries(visual); xamlStream = string.Join("\n", streams); } else if (TypeComboBox.SelectedIndex == 2) { xamlStream = StreamSourceGenerator.GeneratePathGeometry(graphicPath); } else if (TypeComboBox.SelectedIndex == 3) { xamlStream = StreamSourceGenerator.GenerateGeometry(graphicPath); } else { xamlStream = StreamSourceGenerator.GeneratePath(visual); } } else if (TypeComboBox.SelectedIndex == 0) { var streams = StreamSourceGenerator.GenerateStreamGeometries(visual); xamlStream = string.Join("\n", streams); } else { xamlStream = StreamSourceGenerator.GeneratePath(visual); } StreamCode.Text = xamlStream; }