/// <summary>
        ///   Parses a string into a list of FloatString points
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List <FloatString4> StringToPoints(string data)
        {
            var newPoints = new List <FloatString4>();

            string[] lines = data.Split('\n');
            foreach (string line in lines)
            {
                string[] pcs = line.Trim().Split(new[] { '=', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (pcs.Length >= 3 && pcs[0] == "key")
                {
                    var nv = new FloatString4();
                    if (pcs.Length >= 5)
                    {
                        nv.strings = new[] { pcs[1], pcs[2], pcs[3], pcs[4] };
                    }
                    else
                    {
                        nv.strings = new[] { pcs[1], pcs[2], "0", "0" };
                    }

                    nv.UpdateFloats();
                    newPoints.Add(nv);
                }
            }

            return(newPoints);
        }
예제 #2
0
        protected void DrawCurveEditor()
        {
            GUILayout.BeginHorizontal(GUILayout.Height(texHeight));

            Vector2 sizeMax = GUI.skin.label.CalcSize(new GUIContent(maxY.ToString("F3")));
            Vector2 sizeMin = GUI.skin.label.CalcSize(new GUIContent(minY.ToString("F3")));

            GUILayout.BeginVertical(GUILayout.MinWidth(Mathf.Max(sizeMin.x, sizeMax.x)));
            const int   GraphLabels = 4;
            const float labelSpace  = 20f * (GraphLabels + 1) / GraphLabels;

            for (int i = 0; i <= GraphLabels; i++)
            {
                GUILayout.Label((maxY - (maxY - minY) * i / GraphLabels).ToString("F3"), new GUIStyle(GUI.skin.label)
                {
                    wordWrap = false
                });
                if (i != GraphLabels) //only do it if it's not the last one
                {
                    GUILayout.Space(texHeight / GraphLabels - labelSpace);
                }
            }
            GUILayout.EndVertical();

            GUILayout.Box(graphTexture);

            GUILayout.EndHorizontal();

            FloatString4 excludePoint = null;

            scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.ExpandWidth(true), GUILayout.Height(200));
            GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
            GUILayout.BeginVertical();
            GUILayout.Label("X");

            foreach (FloatString4 p in points)
            {
                string ns = GUILayout.TextField(p.strings[0]);
                if (ns != p.strings[0])
                {
                    p.strings[0] = ns;
                    p.UpdateFloats();
                    UpdateCurve(out curve);
                }
            }
            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            GUILayout.Label("Y");
            foreach (FloatString4 p in points)
            {
                string ns = GUILayout.TextField(p.strings[1]);
                if (ns != p.strings[1])
                {
                    p.strings[1] = ns;
                    p.UpdateFloats();
                    UpdateCurve(out curve);
                }
            }
            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            GUILayout.Label("In Tangent");
            foreach (FloatString4 p in points)
            {
                string ns = GUILayout.TextField(p.strings[2]);
                if (ns != p.strings[2])
                {
                    p.strings[2] = ns;
                    p.UpdateFloats();
                    UpdateCurve(out curve);
                }
            }
            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            GUILayout.Label("Out Tangent");
            foreach (FloatString4 p in points)
            {
                string ns = GUILayout.TextField(p.strings[3]);
                if (ns != p.strings[3])
                {
                    p.strings[3] = ns;
                    p.UpdateFloats();
                    UpdateCurve(out curve);
                }
            }
            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            GUILayout.Label("Remove");
            foreach (FloatString4 p in points)
            {
                if (GUILayout.Button("X"))
                {
                    excludePoint = p;
                }
            }
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            GUILayout.EndScrollView();

            if (excludePoint != null)
            {
                points.Remove(excludePoint);
                UpdateCurve(out curve);
            }

            GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
            // sort = GUILayout.Toggle(sort, "Sort");
            if (GUILayout.Button("New Curve"))
            {
                points.Clear();
                textVersion  = "";
                graphTexture = GraphUtils.GenerateCurveTexture(texWidth, texHeight, curve, Color.green);
            }
            if (GUILayout.Button("Smooth Tangents"))
            {
                //SmoothTangents();
            }
            if (GUILayout.Button("Copy out"))
            {
                GUIUtility.systemCopyBuffer = textVersion;
            }
            if (GUILayout.Button("Paste in"))
            {
                textVersion = GUIUtility.systemCopyBuffer;
                points      = GraphUtils.StringToPoints(textVersion);
                UpdateCurve(out curve);
            }
            if (GUILayout.Button("Add Node"))
            {
                if (points.Count > 0)
                {
                    points.Add(new FloatString4(points.Last().floats.x + 1, points.Last().floats.y, points.Last().floats.z, points.Last().floats.w));
                }
                else
                {
                    points.Add(new FloatString4(0, 0, 0, 0));
                }
                UpdateCurve(out curve);
            }
            GUILayout.EndHorizontal();
        }