예제 #1
0
        public void Build(CodeWriter w)
        {
            w.WriteLine("[System.Serializable]");
            w.WriteLine("[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]");
            w.WriteLine("public partial struct {0} : System.IEquatable<{0}>", Name);
            w.WriteLine("{");
            w.PushIndent();

            // Fields
            w.WriteLine("public readonly {0} Position;", Vector.Name);
            w.WriteLine("public readonly {0} Size;", Vector.Name);

            w.WriteLine("");

            // Constructors
            w.Write("public {0}({1} position, {1} size)", Name, Vector.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("Position = position;");
            w.WriteLine("Size = size;");
            w.PopIndent();
            w.WriteLine("}");

            w.WriteLine("");

            if (Dimensions == 2)
            {
                w.Write("public {0}({1} x, {1} y, {1} sx, {1} sy)", Name, ElementType.Name);
                w.WriteLine("{");
                w.PushIndent();
                w.WriteLine("Position = new {0}(x, y);", Vector.Name);
                w.WriteLine("Size = new {0}(sx, sy);", Vector.Name);
                w.PopIndent();
                w.WriteLine("}");
            }
            else // 3
            {
                w.Write("public {0}({1} x, {1} y, {1} z, {1} sx, {1} sy, {1} sz)", Name, ElementType.Name);
                w.WriteLine("{");
                w.PushIndent();
                w.WriteLine("Position = new {0}(x, y, z);", Vector.Name);
                w.WriteLine("Size = new {0}(sx, sy, sz);", Vector.Name);
                w.PopIndent();
                w.WriteLine("}");
            }

            w.WriteLine("");

            // Casts
            foreach (var b in Boxes)
            {
                if (b != this && b.Dimensions == Dimensions)
                {
                    var isImplicit = ElementType.CanConvertImplicitlyTo(b.ElementType);
                    w.WriteLine("public static {0} operator {1}({2} v)",
                                isImplicit ? "implicit" : "explicit",
                                b.Name, Name
                                );
                    w.WriteLine("{");
                    w.PushIndent();
                    if (isImplicit)
                    {
                        w.WriteLine("return new {0}(v.Position, v.Size);", b.Name);
                    }
                    else
                    {
                        w.WriteLine("return new {0}(({1})v.Position, ({1})v.Size);", b.Name, b.Vector.Name);
                    }
                    w.PopIndent();
                    w.WriteLine("}");
                    w.WriteLine("");
                }
            }

            // ToString
            BuildToString(w);

            // Equals == != GetHashCode
            BuildEqualsRelatedMembers(w);

            // MinX, MaxX, etc
            BuildMinMax(w);

            // Center point
            BuildCenter(w);

            // Translate
            BuildTranslate(w);

            // End struct
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
예제 #2
0
        public void Build(CodeWriter w)
        {
            w.WriteLine("[System.Serializable]");
            w.WriteLine("[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]");
            w.WriteLine("public partial struct {0} : System.IEquatable<{0}>", Name);
            w.WriteLine("{");
            w.PushIndent();

            // Fields
            for (int i = 0; i < Dimensions; i++)
            {
                w.WriteLine("public readonly {0} {1};", ElementType.Name, s_fieldName[i]);
            }

            w.WriteLine("");

            // Constructors
            w.Write("public {0}(", Name);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0)
                {
                    w.Write(", ");
                }
                w.Write("{0} {1}", ElementType.Name, s_parameterName[i]);
            }
            w.WriteLine(")");
            w.WriteLine("{");
            w.PushIndent();
            for (int i = 0; i < Dimensions; i++)
            {
                w.WriteLine("{0} = {1};", s_fieldName[i], s_parameterName[i]);
            }
            w.PopIndent();
            w.WriteLine("}");

            w.WriteLine("");

            for (int d = 2; d < Dimensions; d++)
            {
                var vector = GetVector(ElementType, d);

                w.Write("public {0}({1} v", Name, vector.Name);
                for (int i = d; i < Dimensions; i++)
                {
                    w.Write(", {0} {1}", ElementType.Name, s_parameterName[i]);
                }
                w.WriteLine(")");
                w.WriteLine("{");
                w.PushIndent();
                for (int i = 0; i < d; i++)
                {
                    w.WriteLine("{0} = v.{1};", s_fieldName[i], s_fieldName[i]);
                }
                for (int i = d; i < Dimensions; i++)
                {
                    w.WriteLine("{0} = {1};", s_fieldName[i], s_parameterName[i]);
                }
                w.PopIndent();
                w.WriteLine("}");
                w.WriteLine("");
            }

            // Casts
            foreach (var c in Vectors)
            {
                if (c != this && c.Dimensions == Dimensions)
                {
                    var isImplicit = ElementType.CanConvertImplicitlyTo(c.ElementType);
                    w.WriteLine("public static {0} operator {1}({2} v)",
                                isImplicit ? "implicit" : "explicit",
                                c.Name, Name
                                );
                    w.WriteLine("{");
                    w.PushIndent();
                    w.Write("return new {0}(", c.Name);
                    for (int i = 0; i < Dimensions; i++)
                    {
                        if (i != 0)
                        {
                            w.Write(", ");
                        }
                        if (isImplicit)
                        {
                            w.Write("v.{0}", s_fieldName[i]);
                        }
                        else
                        {
                            w.Write("({0})v.{1}", c.ElementType.Name, s_fieldName[i]);
                        }
                    }
                    w.WriteLine(");");
                    w.PopIndent();
                    w.WriteLine("}");
                    w.WriteLine("");
                }
            }

            // ToString
            BuildToString(w);

            // Equals == != GetHashCode
            BuildEqualsRelatedMembers(w);

            // Arithmetic
            BuildBinaryOperator(w, "+");
            BuildBinaryOperator(w, "-");
            BuildBinaryOperator(w, "*");
            BuildBinaryOperator(w, "/");

            // -
            BuildNegate(w);

            // Dot Cross
            BuildDotAndCrossProducts(w);

            // SquaredLength, Length, Normalize
            BuildLengthRelatedMembers(w);

            // Zero, One, UnitX...W
            BuildStaticValues(w);

            // End struct
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }