public static SwayBone FindBoneByPath([NotNull] this SwayController controller, [NotNull] string path) { foreach (var bone in controller.SwayBones) { if (bone.Path == path) { return(bone); } } return(null); }
public static SwayManager FindManagerByPath([NotNull] this SwayController controller, [NotNull] string path) { var results = new List <SwayManager>(); foreach (var manager in controller.Managers) { if (path.StartsWith(manager.Path)) { results.Add(manager); } } if (results.Count == 0) { throw new ArgumentException("The given path is not in this sway controller."); } if (results.Count > 0) { results.Sort(CompareByPathLengthReversed); } return(results[0]); }
private static void BuildHierarchy([NotNull] SwayController controller) { var bones = controller.SwayBones; var boneChildrenMap = new Dictionary <SwayBone, List <SwayBone> >(); foreach (var bone in bones) { var myPath = bone.Path; var parentPath = GetParentBonePath(myPath); var parentBone = controller.FindBoneByPath(parentPath); bone.Parent = parentBone; var manager = controller.FindManagerByPath(myPath); bone.Manager = manager; if (parentBone == null) { continue; } List <SwayBone> children; if (boneChildrenMap.ContainsKey(parentBone)) { children = boneChildrenMap[parentBone]; } else { children = new List <SwayBone>(); boneChildrenMap.Add(parentBone, children); } children.Add(bone); } var rootBonesMap = new Dictionary <SwayManager, List <SwayBone> >(); foreach (var bone in bones) { if (bone.Parent == null) { List <SwayBone> rootBones; if (rootBonesMap.ContainsKey(bone.Manager)) { rootBones = rootBonesMap[bone.Manager]; } else { rootBones = new List <SwayBone>(); rootBonesMap.Add(bone.Manager, rootBones); } rootBones.Add(bone); } if (boneChildrenMap.ContainsKey(bone)) { var children = boneChildrenMap[bone]; bone.Children = children.ToArray(); } } foreach (var manager in controller.Managers) { if (rootBonesMap.TryGetValue(manager, out var rootBones)) { manager.RootBones = rootBones.ToArray(); } } }
public static SwayController Parse([NotNull] string text) { var lines = text.Split(LineSeps); for (var i = 0; i < lines.Length; ++i) { var l = lines[i]; if (l.Length == 0) { continue; } if (l[l.Length - 1] == '\r') { lines[i] = l.TrimEnd(TrimSeps); } } var controller = new SwayController(); var managers = new List <SwayManager>(); var colliders = new List <SwayCollider>(); var swayBones = new List <SwayBone>(); for (var i = 0; i < lines.Length; ++i) { var line = lines[i]; if (line.Length == 0) { continue; } var topKeyValue = ParseKeyValue(line); switch (topKeyValue.Key) { case "Top": controller.Top = topKeyValue.Value; break; case "SwayManager": { var sm = ReadSwayManger(lines, line, ref i); managers.Add(sm); break; } case "SwayCollider": { var sc = ReadSwayCollider(lines, line, ref i); colliders.Add(sc); break; } case "SwayBone": { var sb = ReadSwayBone(lines, line, ref i); swayBones.Add(sb); break; } default: throw new ArgumentOutOfRangeException(nameof(topKeyValue.Key), topKeyValue.Key, null); } } controller.Managers = managers.ToArray(); controller.Colliders = colliders.ToArray(); controller.SwayBones = swayBones.ToArray(); BuildHierarchy(controller); return(controller); }