diff --git a/Perspex.SceneGraph/LogicalTree/LogicalExtensions.cs b/Perspex.SceneGraph/LogicalTree/LogicalExtensions.cs new file mode 100644 index 0000000000..98c7ec6a70 --- /dev/null +++ b/Perspex.SceneGraph/LogicalTree/LogicalExtensions.cs @@ -0,0 +1,80 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.LogicalTree +{ + using System; + using System.Collections.Generic; + using System.Linq; + + public static class LogicalExtensions + { + public static IEnumerable GetLogicalAncestors(this ILogical logical) + { + Contract.Requires(logical != null); + + logical = logical.LogicalParent; + + while (logical != null) + { + yield return logical; + logical = logical.LogicalParent; + } + } + + public static IEnumerable GetSelfAndLogicalAncestors(this ILogical logical) + { + yield return logical; + + foreach (var ancestor in logical.GetLogicalAncestors()) + { + yield return ancestor; + } + } + + + public static IEnumerable GetLogicalChildren(this ILogical logical) + { + return logical.LogicalChildren; + } + + public static IEnumerable GetLogicalDescendents(this ILogical logical) + { + foreach (ILogical child in logical.LogicalChildren) + { + yield return child; + + foreach (ILogical descendent in child.GetLogicalDescendents()) + { + yield return descendent; + } + } + } + + public static ILogical GetLogicalParent(this ILogical logical) + { + return logical.LogicalParent; + } + + public static T GetLogicalParent(this ILogical logical) where T : class + { + return logical.LogicalParent as T; + } + + public static IEnumerable GetLogicalSiblings(this ILogical logical) + { + ILogical parent = logical.LogicalParent; + + if (parent != null) + { + foreach (ILogical sibling in parent.LogicalChildren) + { + yield return sibling; + } + } + } + } +} diff --git a/Perspex.SceneGraph/Perspex.SceneGraph.csproj b/Perspex.SceneGraph/Perspex.SceneGraph.csproj index 3760967f12..577272b72c 100644 --- a/Perspex.SceneGraph/Perspex.SceneGraph.csproj +++ b/Perspex.SceneGraph/Perspex.SceneGraph.csproj @@ -92,6 +92,7 @@ +