예제 #1
0
    private void OutputFrameAsJson(FrameInfo info)
    {
        string        data;
        FrameInfo     frameInfo     = info;
        FrameJsonInfo frameJsonInfo = new FrameJsonInfo();


        frameJsonInfo.x      = frameInfo.offsetX;
        frameJsonInfo.y      = frameInfo.offsetY;
        frameJsonInfo.width  = frameInfo.texture.width;
        frameJsonInfo.height = frameInfo.texture.height;

        List <object>          attachPoints = new List <object>();
        ArrayTypeUnkownAndSize iHonestlyDontKnowWhatToCallThisThing = new ArrayTypeUnkownAndSize(twoHandToggle.isOn ? 4 : 3);

        attachPoints.Add(iHonestlyDontKnowWhatToCallThisThing);

        var primaryHandX = (frameInfo.hand1PositionX + frameInfo.offsetX) / 16;
        var primaryHandY = (frameInfo.hand1PositionY + frameInfo.offsetY) / 16;

        attachPoints.Add(new AttachPoint("PrimaryHand", new PositionVector(primaryHandX, primaryHandY)));

        if (frameInfo.isTwoHanded)
        {
            var secondaryHandX = (frameInfo.hand2PositionX + frameInfo.offsetX) / 16;
            var secondaryHandY = (frameInfo.hand2PositionY + frameInfo.offsetY) / 16;
            attachPoints.Add(new AttachPoint("SecondaryHand", new PositionVector(secondaryHandX, secondaryHandY)));
        }

        attachPoints.Add(new AttachPoint("Clip", new PositionVector(0.5625f, 0.375f)));
        attachPoints.Add(new AttachPoint("Casing", new PositionVector(0.5625f, 0.375f)));

        frameJsonInfo.attachPoints = attachPoints.ToArray();


        data = JsonConvert.SerializeObject(frameJsonInfo, Formatting.Indented);


        if (!string.IsNullOrEmpty(frameInfo.path))
        {
            if (!frameInfo.path.EndsWith(".png"))
            {
                frameInfo.path += ".png";
            }
            File.WriteAllText(frameInfo.path.Replace(".png", ".json"), data);
            Debug.Log("nice, it (should have) worked");
            StartCoroutine(AppearAndDisappear(SuccessCheckmark));
        }
        else
        {
            Debug.LogError("Shit, path was empty!");
            StartCoroutine(AppearAndDisappear(FailureX));
        }
    }
    //this method attempts to load the image as well as a corrosponding json file, and create a matching FrameInfo object based off of those
    private FrameInfo LoadSingleFrame(string path, GaeAnimationInfo animationInfo)
    {
        path = System.IO.Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));

        string pngPath  = path + ".png";
        string jsonPath = path + ".json";

        if (!File.Exists(pngPath))
        {
            return(null);
        }
        byte[]    bytes = File.ReadAllBytes(pngPath);
        Texture2D tex   = new Texture2D(2, 2);

        tex.LoadImage(bytes);
        tex.filterMode = FilterMode.Point;
        Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f), 1);

        if (File.Exists(jsonPath))
        {
            try
            {
                string        jsonInfo      = File.ReadAllText(jsonPath);
                FrameJsonInfo frameJsonInfo = JsonConvert.DeserializeObject <FrameJsonInfo>(jsonInfo);

                for (int i = 1; i < frameJsonInfo.attachPoints.Length; i++)
                {
                    //this bit of code relies on conversion exceptions to make sure the data type is right. this only happens to class members are marked as [JsonRequiredAttribute]
                    //this is probably really bad practice. especially since it relies on loaded objects to be of " Newtonsoft.Json.Linq.JObject" type but i dont know any better as of writing this
                    try
                    {
                        frameJsonInfo.attachPoints[i] = (frameJsonInfo.attachPoints[i] as Newtonsoft.Json.Linq.JObject).ToObject <ArrayTypeUnkownAndSize>();
                    }
                    catch (Exception) {}
                    try
                    {
                        frameJsonInfo.attachPoints[i] = (frameJsonInfo.attachPoints[i] as Newtonsoft.Json.Linq.JObject).ToObject <AttachPoint>();
                    }
                    catch (Exception) {}
                }
                float convertedX1 = 0;
                float convertedX2 = 0;
                float convertedY1 = 0;
                float convertedY2 = 0;
                bool  isTwoHanded = false;
                for (int i = 0; i < frameJsonInfo.attachPoints.Length; i++)
                {
                    if (frameJsonInfo.attachPoints[i] is AttachPoint)
                    {
                        if ((frameJsonInfo.attachPoints[i] as AttachPoint).name == "PrimaryHand")
                        {
                            AttachPoint hand1 = frameJsonInfo.attachPoints[i] as AttachPoint;
                            convertedX1 = hand1.position.x * 16 - frameJsonInfo.x;
                            convertedY1 = hand1.position.y * 16 - frameJsonInfo.y;
                        }
                        else if ((frameJsonInfo.attachPoints[i] as AttachPoint).name == "SecondaryHand")
                        {
                            AttachPoint hand2 = frameJsonInfo.attachPoints[i] as AttachPoint;
                            convertedX2 = hand2.position.x * 16 - frameJsonInfo.x;
                            convertedY2 = hand2.position.y * 16 - frameJsonInfo.y;
                            isTwoHanded = true;
                        }
                    }
                }
                return(new FrameInfo(tex, sprite, convertedX1, convertedY1, convertedX2, convertedY2, frameJsonInfo.x, frameJsonInfo.y, isTwoHanded, path, animationInfo));
            }
            catch (Exception)
            {
                throw new Exception("json seems to be invalid! or i dont know how to read jsons (prob the second one ;) )!");
            }
        }
        return(new FrameInfo(tex, sprite, pngPath, animationInfo));
    }