/// <summary> /// Parses .osb file. /// </summary> /// <param name="lines">Array of text lines containing storyboard data.</param> /// <returns>A usable storyboard.</returns> public static Storyboard Decode(IEnumerable <string> lines) { storyboard = new Storyboard(); lastDrawable = null; commandGroup = null; foreach (var line in lines) { if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//") && !line.StartsWith("[")) { if (line.StartsWith("$")) { string[] split = line.Split('='); if (split.Length != 2) { continue; } storyboard.Variables.Add(split[0], split[1]); } else if (!line.StartsWith(" ") && !line.StartsWith("_")) { ParseSbObject(ParseVariables(line)); } else { ParseSbCommand(ParseVariables(line)); } } } return(storyboard); }
private static void ParseSbObject(string line) { string[] tokens = line.Split(','); EventType type; if (!Enum.TryParse(tokens[0], out type)) { return; } StoryboardLayer layer = (StoryboardLayer)Enum.Parse(typeof(StoryboardLayer), tokens[type == EventType.Sample ? 2 : 1]); switch (type) { case EventType.Sprite: { Origins origin = (Origins)Enum.Parse(typeof(Origins), tokens[2]); string fileName = tokens[3].Trim('"'); float x = ParseHelper.ToFloat(tokens[4]); float y = ParseHelper.ToFloat(tokens[5]); storyboard.GetLayer(layer).Add(new StoryboardSprite(origin, fileName, x, y)); lastDrawable = storyboard.GetLayer(layer).Last(); } break; case EventType.Animation: { Origins origin = (Origins)Enum.Parse(typeof(Origins), tokens[2]); string fileName = tokens[3].Trim('"'); float x = ParseHelper.ToFloat(tokens[4]); float y = ParseHelper.ToFloat(tokens[5]); int frameCount = Convert.ToInt32(tokens[6]); double frameDelay = ParseHelper.ToDouble(tokens[7]); LoopType loopType = tokens.Length > 8 ? (LoopType)Enum.Parse(typeof(LoopType), tokens[8]) : LoopType.LoopForever; storyboard.GetLayer(layer).Add(new StoryboardAnimation(origin, fileName, x, y, frameCount, frameDelay, loopType)); lastDrawable = storyboard.GetLayer(layer).Last(); } break; case EventType.Sample: { int time = Convert.ToInt32(tokens[1]); string filePath = tokens[3].Trim('"'); int volume = tokens.Length > 4 ? Convert.ToInt32(tokens[4]) : 100; storyboard.SamplesLayer.Add(new StoryboardSample(layer, time, filePath, volume)); } break; } }
private void ParseSbObject(string line) { string[] tokens = line.Split(','); EventType type = (EventType)Enum.Parse(typeof(EventType), tokens[0]); StoryboardLayer layer = (StoryboardLayer)Enum.Parse(typeof(StoryboardLayer), tokens[type == EventType.Sample ? 2 : 1]); switch (type) { case EventType.Sprite: { Origins origin = (Origins)Enum.Parse(typeof(Origins), tokens[2]); string fileName = tokens[3].Trim('"'); float x = float.Parse(tokens[4], NumberFormatInfo.InvariantInfo); float y = float.Parse(tokens[5], NumberFormatInfo.InvariantInfo); storyboard.GetLayer(layer).Add(new StoryboardSprite(origin, fileName, x, y)); lastDrawable = storyboard.GetLayer(layer).Last(); } break; case EventType.Animation: { Origins origin = (Origins)Enum.Parse(typeof(Origins), tokens[2]); string fileName = tokens[3].Trim('"'); float x = float.Parse(tokens[4], NumberFormatInfo.InvariantInfo); float y = float.Parse(tokens[5], NumberFormatInfo.InvariantInfo); int frameCount = Convert.ToInt32(tokens[6]); int frameDelay = Convert.ToInt32(tokens[7]); LoopType loopType = (LoopType)Enum.Parse(typeof(LoopType), tokens[8]); storyboard.GetLayer(layer).Add(new StoryboardAnimation(origin, fileName, x, y, frameCount, frameDelay, loopType)); lastDrawable = storyboard.GetLayer(layer).Last(); } break; case EventType.Sample: { int time = Convert.ToInt32(tokens[1]); string filePath = tokens[3].Trim('"'); int volume = tokens.Length > 4 ? Convert.ToInt32(tokens[4]) : 100; storyboard.SamplesLayer.Add(new StoryboardSample(layer, time, filePath, volume)); } break; } }
public Storyboard Decode(string[] lines) { storyboard = new Storyboard(); lastDrawable = null; commandGroup = null; foreach (var line in lines) { if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//") && !line.StartsWith("[Events]")) { if (!line.StartsWith(" ") && !line.StartsWith("_")) { ParseSbObject(line); } else { ParseSbCommand(line); } } } return(storyboard); }
public static List <string> StoryboardObject(IStoryboardObject storyboardObject, StoryboardLayer layer) { List <string> list = new List <string>(); if (storyboardObject is StoryboardSprite sprite) { list.Add($"Sprite,{layer},{sprite.Origin},\"{sprite.FilePath}\",{sprite.X.Format()},{sprite.Y.Format()}"); } else if (storyboardObject is StoryboardAnimation animation) { list.Add($"Animation,{layer},{animation.Origin},\"{animation.FilePath}\",{animation.X.Format()},{animation.Y.Format()},{animation.FrameCount},{animation.FrameDelay},{animation.LoopType}"); } else if (storyboardObject is StoryboardSample sample) { list.Add($"Sample,{sample.Time},{layer},\"{sample.FilePath}\",{sample.Volume}"); } if (storyboardObject is IHasCommands obj) { foreach (var loop in obj.Commands.Loops) { list.Add($" L,{loop.LoopStartTime},{loop.LoopCount}"); foreach (var command in loop.Commands.Commands) { if (command.StartTime == command.EndTime) { list.Add($" {command.GetAcronym()},{(int)command.Easing},{command.StartTime},,{GetCommandArguments(command)}"); } else { list.Add($" {command.GetAcronym()},{(int)command.Easing},{command.StartTime},{command.EndTime},{GetCommandArguments(command)}"); } } } foreach (var command in obj.Commands.Commands) { if (command.StartTime == command.EndTime) { list.Add($" {command.GetAcronym()},{(int)command.Easing},{command.StartTime},,{GetCommandArguments(command)}"); } else { list.Add($" {command.GetAcronym()},{(int)command.Easing},{command.StartTime},{command.EndTime},{GetCommandArguments(command)}"); } } foreach (var trigger in obj.Commands.Triggers) { if (trigger.TriggerEndTime == 0) { list.Add($" T,{trigger.TriggerName}{(trigger.GroupNumber != 0 ? $",{-trigger.GroupNumber}" : string.Empty)}"); } else { list.Add($" T,{trigger.TriggerName},{trigger.TriggerStartTime},{trigger.TriggerEndTime}{(trigger.GroupNumber != 0 ? $",{-trigger.GroupNumber}" : string.Empty)}"); } foreach (var command in trigger.Commands.Commands) { if (command.StartTime == command.EndTime) { list.Add($" {command.GetAcronym()},{(int)command.Easing},{command.StartTime},,{GetCommandArguments(command)}"); } else { list.Add($" {command.GetAcronym()},{(int)command.Easing},{command.StartTime},{command.EndTime},{GetCommandArguments(command)}"); } } } } return(list); }