Ejemplo n.º 1
0
        public JSONNode ToJson(PrefabBuildFlags flags)
        {
            JSONObject json = new JSONObject();

            json["name"]  = Name;
            json["id"]    = ID;
            json["p"]     = ParentID;
            json["pt"]    = (PositionParenting ? "1" : "0") + (ScaleParenting ? "1" : "0") + (RotationParenting ? "1" : "0");
            json["po"][0] = PositionParentOffset.ToString();
            json["po"][1] = ScaleParentOffset.ToString();
            json["po"][2] = RotationParentOffset.ToString();
            json["d"]     = RenderDepth.ToString();
            json["ot"]    = ((int)ObjectType).ToString();
            json["shape"] = ((int)Shape).ToString();
            json["so"]    = ShapeOption.ToString();

            if (Shape == PrefabObjectShape.Text)
            {
                json["text"] = Text;
            }

            json["st"]           = StartTime.ToString();
            json["akt"]          = ((int)AutoKillType).ToString();
            json["ako"]          = AutoKillOffset.ToString();
            json["o"]["x"]       = Origin.X;
            json["o"]["y"]       = Origin.Y;
            json["ed"]["locked"] = EditorLocked.ToString();
            json["ed"]["shrink"] = EditorCollapse.ToString();
            json["ed"]["bin"]    = EditorBin.ToString();
            json["ed"]["layer"]  = EditorLayer.ToString();

            if ((flags & PrefabBuildFlags.SortKeyframes) != 0)
            {
                PositionKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
                ScaleKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
                ColorKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
            }

            if ((flags & (PrefabBuildFlags.AbsoluteRotation | PrefabBuildFlags.SortKeyframes)) != 0)
            {
                RotationKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
            }

            json["events"]["pos"] = new JSONArray();
            for (int i = 0; i < PositionKeyframes.Count; i++)
            {
                JSONNode         kfJson = json["events"]["pos"][i] = new JSONObject();
                PositionKeyframe kf     = PositionKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = kf.Value.X.ToString();
                kfJson["y"]  = kf.Value.Y.ToString();
                kfJson["ct"] = kf.Easing.ToString();

                if (kf.RandomMode != PrefabObjectRandomMode.None)
                {
                    kfJson["r"]  = ((int)kf.RandomMode).ToString();
                    kfJson["rx"] = kf.RandomValue.X.ToString();
                    kfJson["ry"] = kf.RandomValue.Y.ToString();
                    kfJson["rz"] = kf.RandomInterval.ToString();
                }
            }

            json["events"]["sca"] = new JSONArray();
            for (int i = 0; i < ScaleKeyframes.Count; i++)
            {
                JSONNode      kfJson = json["events"]["sca"][i] = new JSONObject();
                ScaleKeyframe kf     = ScaleKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = kf.Value.X.ToString();
                kfJson["y"]  = kf.Value.Y.ToString();
                kfJson["ct"] = kf.Easing.ToString();

                if (kf.RandomMode != PrefabObjectRandomMode.None)
                {
                    kfJson["r"]  = ((int)kf.RandomMode).ToString();
                    kfJson["rx"] = kf.RandomValue.X.ToString();
                    kfJson["ry"] = kf.RandomValue.Y.ToString();
                    kfJson["rz"] = kf.RandomInterval.ToString();
                }
            }

            json["events"]["rot"] = new JSONArray();
            float currentRot = 0.0f;

            for (int i = 0; i < RotationKeyframes.Count; i++)
            {
                JSONNode         kfJson = json["events"]["rot"][i] = new JSONObject();
                RotationKeyframe kf     = RotationKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = (kf.Value - currentRot).ToString();
                kfJson["ct"] = kf.Easing.ToString();

                if (kf.RandomMode != PrefabObjectRandomMode.None)
                {
                    kfJson["r"]  = ((int)kf.RandomMode).ToString();
                    kfJson["rx"] = (kf.RandomValue - currentRot).ToString();
                    kfJson["rz"] = kf.RandomInterval.ToString();
                }

                if ((flags & PrefabBuildFlags.AbsoluteRotation) != 0)
                {
                    currentRot += kf.Value;
                }
            }

            json["events"]["col"] = new JSONArray();
            for (int i = 0; i < ColorKeyframes.Count; i++)
            {
                JSONNode      kfJson = json["events"]["col"][i] = new JSONObject();
                ColorKeyframe kf     = ColorKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = kf.Value.ToString();
                kfJson["ct"] = kf.Easing.ToString();
            }

            return(json);
        }
Ejemplo n.º 2
0
        internal PrefabObject(JSONNode json, Prefab prefab)
        {
            Name     = json["name"];
            ID       = json["id"];
            ParentID = json["p"];

            if (json.HasKey("pt"))
            {
                string pt = json["pt"];
                PositionParenting = pt[0] == '1';
                ScaleParenting    = pt[1] == '1';
                RotationParenting = pt[2] == '1';
            }
            else
            {
                PositionParenting = true;
                ScaleParenting    = false;
                RotationParenting = true;
            }

            if (json.HasKey("po"))
            {
                JSONNode po = json["po"];
                PositionParentOffset = po[0].AsFloat;
                ScaleParentOffset    = po[1].AsFloat;
                RotationParentOffset = po[2].AsFloat;
            }

            RenderDepth = json["d"].AsInt;
            ObjectType  = (PrefabObjectType)json["ot"].AsInt;
            Shape       = (PrefabObjectShape)json["shape"].AsInt;
            ShapeOption = json["so"].AsInt;

            if (Shape == PrefabObjectShape.Text)
            {
                Text = json["text"];
            }

            StartTime      = json["st"].AsFloat;
            AutoKillType   = (PrefabObjectAutoKillType)json["akt"].AsInt;
            AutoKillOffset = json["ako"].AsFloat;

            if (json.HasKey("o"))
            {
                JSONNode o = json["o"];
                Origin.X = o["x"].AsFloat;
                Origin.Y = o["y"].AsFloat;
            }

            if (json.HasKey("ed"))
            {
                JSONNode ed = json["ed"];
                if (ed.HasKey("locked"))
                {
                    EditorLocked = json["locked"].AsBool;
                }

                if (ed.HasKey("shrink"))
                {
                    EditorCollapse = json["shrink"].AsBool;
                }

                EditorBin   = json["bin"].AsInt;
                EditorLayer = json["layer"].AsInt;
            }

            JSONNode events = json["events"];

            JSONNode pos = events["pos"];

            foreach (JSONNode kfJson in pos)
            {
                PositionKeyframe kf = new PositionKeyframe()
                {
                    Time   = kfJson["t"].AsFloat,
                    Value  = new Vector2(kfJson["x"].AsFloat, kfJson["y"].AsFloat),
                    Easing = kfJson.HasKey("ct") ? (PrefabObjectEasing)Enum.Parse(typeof(PrefabObjectEasing), kfJson["ct"]) : PrefabObjectEasing.Linear
                };

                if (kfJson.HasKey("r"))
                {
                    kf.RandomMode     = (PrefabObjectRandomMode)kfJson["r"].AsInt;
                    kf.RandomValue    = new Vector2(kfJson["rx"].AsFloat, kfJson["ry"].AsFloat);
                    kf.RandomInterval = kfJson["rz"].AsFloat;
                }

                PositionKeyframes.Add(kf);
            }

            JSONNode sca = events["sca"];

            foreach (JSONNode kfJson in sca)
            {
                ScaleKeyframe kf = new ScaleKeyframe()
                {
                    Time   = kfJson["t"].AsFloat,
                    Value  = new Vector2(kfJson["x"].AsFloat, kfJson["y"].AsFloat),
                    Easing = kfJson.HasKey("ct") ? (PrefabObjectEasing)Enum.Parse(typeof(PrefabObjectEasing), kfJson["ct"]) : PrefabObjectEasing.Linear
                };

                if (kfJson.HasKey("r"))
                {
                    kf.RandomMode     = (PrefabObjectRandomMode)kfJson["r"].AsInt;
                    kf.RandomValue    = new Vector2(kfJson["rx"].AsFloat, kfJson["ry"].AsFloat);
                    kf.RandomInterval = kfJson["rz"].AsFloat;
                }

                ScaleKeyframes.Add(kf);
            }

            JSONNode rot = events["rot"];

            foreach (JSONNode kfJson in rot)
            {
                RotationKeyframe kf = new RotationKeyframe()
                {
                    Time   = kfJson["t"].AsFloat,
                    Value  = kfJson["x"].AsFloat,
                    Easing = kfJson.HasKey("ct") ? (PrefabObjectEasing)Enum.Parse(typeof(PrefabObjectEasing), kfJson["ct"]) : PrefabObjectEasing.Linear
                };

                if (kfJson.HasKey("r"))
                {
                    kf.RandomMode     = (PrefabObjectRandomMode)kfJson["r"].AsInt;
                    kf.RandomValue    = kfJson["rx"].AsFloat;
                    kf.RandomInterval = kfJson["rz"].AsFloat;
                }

                RotationKeyframes.Add(kf);
            }

            JSONNode col = events["col"];

            foreach (JSONNode kfJson in col)
            {
                ColorKeyframe kf = new ColorKeyframe()
                {
                    Time   = kfJson["t"].AsFloat,
                    Value  = kfJson["x"].AsInt,
                    Easing = kfJson.HasKey("ct") ? (PrefabObjectEasing)Enum.Parse(typeof(PrefabObjectEasing), kfJson["ct"]) : PrefabObjectEasing.Linear
                };

                ColorKeyframes.Add(kf);
            }

            Prefab = prefab;
        }