Beispiel #1
0
 public void Add(string id, string path, float delay, string into, params int[] frames)
 {
     animations[id] = new Animation()
     {
         Delay  = delay,
         Frames = GetFrames(path, frames),
         Goto   = Chooser <string> .FromString <string>(into)
     };
 }
Beispiel #2
0
        public void Add(XmlElement xml, string overridePath = null)
        {
            var source = new SpriteDataSource();

            source.XML          = xml;
            source.Path         = source.XML.Attr("path");
            source.OverridePath = overridePath;

            //Error Checking
            {
                var prefix = "Sprite '" + source.XML.Name + "': ";

                //Path
                if (!source.XML.HasAttr("path") && string.IsNullOrEmpty(overridePath))
                {
                    throw new Exception(prefix + "'path' is missing!");
                }

                //Anims
                var ids = new HashSet <string>();
                foreach (XmlElement anim in source.XML.GetElementsByTagName("Anim"))
                {
                    CheckAnimXML(anim, prefix, ids);
                }
                foreach (XmlElement loop in source.XML.GetElementsByTagName("Loop"))
                {
                    CheckAnimXML(loop, prefix, ids);
                }

                //Start
                if (source.XML.HasAttr("start") && !ids.Contains(source.XML.Attr("start")))
                {
                    throw new Exception(prefix + "starting animation '" + source.XML.Attr("start") + "' is missing!");
                }

                //Origin
                if (source.XML.HasChild("Justify") && source.XML.HasChild("Origin"))
                {
                    throw new Exception(prefix + "has both Origin and Justify tags!");
                }
            }

            //Create the Sprite
            {
                var normalPath  = source.XML.Attr("path", "");
                var masterDelay = source.XML.AttrFloat("delay", 0);

                //Build Animations
                foreach (XmlElement anim in source.XML.GetElementsByTagName("Anim"))
                {
                    Chooser <string> into;
                    if (anim.HasAttr("goto"))
                    {
                        into = Chooser <string> .FromString <string>(anim.Attr("goto"));
                    }
                    else
                    {
                        into = null;
                    }

                    var id     = anim.Attr("id");
                    var path   = anim.Attr("path", "");
                    var frames = Calc.ReadCSVIntWithTricks(anim.Attr("frames", ""));

                    if (!string.IsNullOrEmpty(overridePath) && HasFrames(Atlas, overridePath + path, frames))
                    {
                        path = overridePath + path;
                    }
                    else
                    {
                        path = normalPath + path;
                    }

                    Sprite.Add(id, path, anim.AttrFloat("delay", masterDelay), into, frames);
                }

                //Build Loops
                foreach (XmlElement loop in source.XML.GetElementsByTagName("Loop"))
                {
                    var id     = loop.Attr("id");
                    var path   = loop.Attr("path", "");
                    var frames = Calc.ReadCSVIntWithTricks(loop.Attr("frames", ""));

                    if (!string.IsNullOrEmpty(overridePath) && HasFrames(Atlas, overridePath + path, frames))
                    {
                        path = overridePath + path;
                    }
                    else
                    {
                        path = normalPath + path;
                    }

                    Sprite.AddLoop(id, path, loop.AttrFloat("delay", masterDelay), frames);
                }

                //Origin
                if (source.XML.HasChild("Center"))
                {
                    Sprite.CenterOrigin();
                    Sprite.Justify = new Vector2(.5f, .5f);
                }
                else if (source.XML.HasChild("Justify"))
                {
                    Sprite.JustifyOrigin(source.XML.ChildPosition("Justify"));
                    Sprite.Justify = source.XML.ChildPosition("Justify");
                }
                else if (source.XML.HasChild("Origin"))
                {
                    Sprite.Origin = source.XML.ChildPosition("Origin");
                }

                //Position
                if (source.XML.HasChild("Position"))
                {
                    Sprite.Position = source.XML.ChildPosition("Position");
                }

                //Start Animation
                if (source.XML.HasAttr("start"))
                {
                    Sprite.Play(source.XML.Attr("start"));
                }
            }

            Sources.Add(source);
        }