using System; using System.Globalization; using System.Text; using Azimuth; using Prism; namespace Konis.Testbed { /// /// Writes an emitter def's tunable properties as C# initializer lines, ready to paste into the def's source. /// public static class DefText { /// /// The indent of a line, which is how deep an initializer sits in a static class of defs. /// public const string Indent = " "; private const int MaxLineWidth = 120; private static string NumberText(Scalar number) { double value = number.Value; // The invariant culture keeps a machine set to a comma decimal from writing 0,6. return value == 0 ? "0" : value.ToString("R", CultureInfo.InvariantCulture); } private static string RangeText(Variation range) { if (range.Minimum.Value == range.Maximum.Value) { return NumberText(range.Minimum); } return "new Variation(" + NumberText(range.Minimum) + ", " + NumberText(range.Maximum) + ")"; } private static string ColorText(Color color) { string red = color.Red.ToString(CultureInfo.InvariantCulture); string green = color.Green.ToString(CultureInfo.InvariantCulture); string blue = color.Blue.ToString(CultureInfo.InvariantCulture); if (color.Alpha == byte.MaxValue) { return "Rgb(" + red + ", " + green + ", " + blue + ")"; } string alpha = color.Alpha.ToString(CultureInfo.InvariantCulture); return "Color.FromArgb(" + alpha + ", " + red + ", " + green + ", " + blue + ")"; } private static string NativeColorText(StopColor color) { string end = color.Alpha == 1 ? "))" : ", " + NumberText(color.Alpha) + "))"; if (color.Space == GradientSpace.LinearLight) { LinearSpaceColor linear = color.ToLinear(); return "StopColor.FromLinear(new LinearSpaceColor(" + NumberText(linear.Red) + ", " + NumberText(linear.Green) + ", " + NumberText(linear.Blue) + end; } if (color.Space == GradientSpace.Oklab) { Oklab oklab = color.ToOklab(); return "StopColor.FromOklab(new Oklab(" + NumberText(oklab.Lightness) + ", " + NumberText(oklab.A) + ", " + NumberText(oklab.B) + end; } if (color.Space == GradientSpace.Oklch) { Oklch oklch = color.ToOklch(); return "StopColor.FromOklch(new Oklch(" + NumberText(oklch.Lightness) + ", " + NumberText(oklch.Chroma) + ", " + NumberText(oklch.Hue) + end; } if (color.Space == GradientSpace.Hsv) { Hsv hsv = color.ToHsv(); return "StopColor.FromHsv(new Hsv(" + NumberText(hsv.Hue) + ", " + NumberText(hsv.Saturation) + ", " + NumberText(hsv.Value) + end; } Hsl hsl = color.ToHsl(); return "StopColor.FromHsl(new Hsl(" + NumberText(hsl.Hue) + ", " + NumberText(hsl.Saturation) + ", " + NumberText(hsl.Lightness) + end; } // The color a byte color stands for in the gradient's space, which is what a stop made from bytes holds. private static bool IsMadeOfBytes(Gradient gradient, int index) { Color bytes = gradient.GetStop(index).Color; return gradient.GetExactStop(index).Color == StopColor.FromColor(gradient.Space, bytes); } private static bool IsEveryStopMadeOfBytes(Gradient gradient) { for (int i = 0; i < gradient.StopCount; i++) { if (!IsMadeOfBytes(gradient, i)) { return false; } } return true; } // A stop that is a byte color says so in bytes, and the rest in the space's own numbers. The color goes on its // own line when the stop wouldn't fit on one. private static string ExactStopText(Gradient gradient, int index, string indent) { ExactStop stop = gradient.GetExactStop(index); string color; if (IsMadeOfBytes(gradient, index)) { color = "StopColor.FromColor(" + nameof(GradientSpace) + "." + gradient.Space + ", " + ColorText(gradient.GetStop(index).Color) + ")"; } else { color = NativeColorText(stop.Color); } string start = "new ExactStop(" + NumberText(stop.Offset) + ","; if (indent.Length + start.Length + " ".Length + color.Length + ")".Length + ",".Length <= MaxLineWidth) { return start + " " + color + ")"; } return start + Environment.NewLine + indent + " " + color + ")"; } // Laid out as Effects does, a line to the space and one to each stop, so a pasted gradient reads like its // neighbors. A gradient whose stops are all byte colors is written with those, and any other with exact stops. private static string GradientText(Gradient gradient) { const string stopIndent = Indent + " "; bool inBytes = IsEveryStopMadeOfBytes(gradient); var returnMe = new StringBuilder("Tint(new Gradient("); returnMe.Append(Environment.NewLine).Append(stopIndent); returnMe.Append(nameof(GradientSpace)).Append('.').Append(gradient.Space).Append(','); if (gradient.Direction != HueDirection.Shortest) { returnMe.Append(Environment.NewLine).Append(stopIndent); returnMe.Append(nameof(HueDirection)).Append('.').Append(gradient.Direction).Append(','); } for (int i = 0; i < gradient.StopCount; i++) { returnMe.Append(Environment.NewLine).Append(stopIndent); if (inBytes) { GradientStop stop = gradient.GetStop(i); returnMe.Append("new GradientStop(").Append(NumberText(stop.Offset)).Append(", "); returnMe.Append(ColorText(stop.Color)).Append(i == gradient.StopCount - 1 ? ")))" : "),"); } else { returnMe.Append(ExactStopText(gradient, i, stopIndent)); returnMe.Append(i == gradient.StopCount - 1 ? "))" : ","); } } return returnMe.ToString(); } // On one line when the whole property line fits, and otherwise a point to a line, as a gradient lays out its // stops. private static string FunctionText(TunableProperty property, object value) { var points = value as PointFunction; if (points == null) { throw new InvalidOperationException($"The {property.Name} of an emitter has no source to write."); } const string pointIndent = Indent + " "; var pointTexts = new string[points.PointCount]; for (int i = 0; i < pointTexts.Length; i++) { pointTexts[i] = "(" + NumberText(points.GetX(i)) + ", " + NumberText(points.GetY(i)) + ")"; } string start = "new " + points.GetType().Name + "("; string oneLine = start + string.Join(", ", pointTexts) + ")"; if (Indent.Length + property.Name.Length + " = ".Length + oneLine.Length + ",".Length <= MaxLineWidth) { return oneLine; } var returnMe = new StringBuilder(start); for (int i = 0; i < pointTexts.Length; i++) { returnMe.Append(Environment.NewLine).Append(pointIndent).Append(pointTexts[i]); returnMe.Append(i == pointTexts.Length - 1 ? ")" : ","); } return returnMe.ToString(); } private static string BoundText(Scalar bound) { if (Scalar.IsNegativeInfinity(bound)) { return "double.NegativeInfinity"; } if (Scalar.IsPositiveInfinity(bound)) { return "double.PositiveInfinity"; } return NumberText(bound); } private static string AxisTextOf(Vector3 normal) { if (normal.Y == 0 && normal.Z == 0 && normal.X == 1) { return nameof(Axis) + "." + Axis.X; } if (normal.X == 0 && normal.Z == 0 && normal.Y == 1) { return nameof(Axis) + "." + Axis.Y; } if (normal.X == 0 && normal.Y == 0 && normal.Z == 1) { return nameof(Axis) + "." + Axis.Z; } return null; } // A slab along an axis with one open end is written as the floor or ceiling it is. private static string SlabText(Slab slab) { string axis = AxisTextOf(slab.Normal); string direction = axis; if (axis == null) { direction = "new Vector3(" + NumberText(slab.Normal.X) + ", " + NumberText(slab.Normal.Y) + ", " + NumberText(slab.Normal.Z) + ")"; } bool hasMin = !Scalar.IsNegativeInfinity(slab.Min); bool hasMax = !Scalar.IsPositiveInfinity(slab.Max); if (axis != null && hasMin && !hasMax) { return "Slab.Above(" + axis + ", " + NumberText(slab.Min) + ")"; } if (axis != null && !hasMin && hasMax) { return "Slab.Below(" + axis + ", " + NumberText(slab.Max) + ")"; } return "Slab.Between(" + direction + ", " + BoundText(slab.Min) + ", " + BoundText(slab.Max) + ")"; } // An empty set is null, so a def that has had its slabs removed still writes a line. private static string SlabsText(TunableProperty property, Slab[] slabs) { if (slabs == null || slabs.Length == 0) { return "null"; } var slabTexts = new string[slabs.Length]; for (int i = 0; i < slabTexts.Length; i++) { slabTexts[i] = SlabText(slabs[i]); } string oneLine = "new[] { " + string.Join(", ", slabTexts) + " }"; if (Indent.Length + property.Name.Length + " = ".Length + oneLine.Length + ",".Length <= MaxLineWidth) { return oneLine; } var returnMe = new StringBuilder("new[]"); returnMe.Append(Environment.NewLine).Append(Indent).Append('{'); for (int i = 0; i < slabTexts.Length; i++) { returnMe.Append(Environment.NewLine).Append(Indent).Append(" ").Append(slabTexts[i]).Append(','); } returnMe.Append(Environment.NewLine).Append(Indent).Append('}'); return returnMe.ToString(); } /// /// Returns the property's value on as C# source, such as -30 or Axis.Z. /// /// /// The property is computed, or is a function that is absent or made of code, or is a sprite that no static /// field holds, so has no source to write. /// public static string ValueText(TunableProperty property, EmitterDef def) { object value = property.Get(def); switch (property.Kind) { case TunableKind.Whole: return ((int)value).ToString(CultureInfo.InvariantCulture); case TunableKind.Number: return NumberText((Scalar)value); case TunableKind.Range: return RangeText((Variation)value); case TunableKind.Choice: return property.ValueType.Name + "." + value; case TunableKind.Toggle: return (bool)value ? "true" : "false"; case TunableKind.Gradient: if (value == null) { throw new InvalidOperationException($"The {property.Name} of an emitter has no gradient."); } return GradientText((Gradient)value); case TunableKind.Function: return FunctionText(property, value); case TunableKind.Slabs: return SlabsText(property, (Slab[])value); case TunableKind.Sprite: string source = SpriteChoices.Find((SpriteDef)value)?.Source; if (source == null) { throw new InvalidOperationException( $"The {property.Name} of an emitter has no source to write."); } return source; default: throw new InvalidOperationException($"The {property.Name} of an emitter has no source to write."); } } /// /// Returns a line for each property of that can be changed, in declaration order. With /// a snapshot, only the properties whose value differs from it get a line. /// public static string Write(EmitterDef def, TunableSnapshot since) { var returnMe = new StringBuilder(); foreach (TunableProperty property in Tunables.Of(def)) { if (!property.HasSource(def)) { continue; } string text = ValueText(property, def); if (since != null && since.TextOf(property.Name) == text) { continue; } returnMe.Append(Indent).Append(property.Name).Append(" = ").Append(text).Append(','); returnMe.Append(Environment.NewLine); } return returnMe.ToString(); } } }