Beispiel #1
0
        /// <summary> Finds the <see cref="Component"/> belonging to an <see cref="Element"/> furthest removed from the root-level that is moused over and implements the given <typeparamref name="T"/> interface. </summary>
        /// <typeparam name="T"> The type of interface to find. </typeparam>
        /// <param name="container"> The element container to check first. </param>
        /// <param name="bounds"> The bounds to check. </param>
        /// <param name="ignoreElement"> The element to ignore, or null if no elements are to be ignored. </param>
        /// <returns> The interfaced version of the moused over component. </returns>
        /// <remarks> Recursively calls upon this function in order to perform the search. The recursion happens before the component check, so elements with no children are checked first, then their parents, and so on. </remarks>
        internal T FindInBoundsWithInterfacedComponent <T>(ElementContainer container, Rectangle bounds, Element ignoreElement = null) where T : class
        {
            if (!typeof(T).IsInterface)
            {
                return(null);
            }

            Element element;

            for (int i = container.Count - 1; i >= 0; i--)
            {
                element = container.GetChildByIndex(i);

                // If this element is moused over, check its children. Ignore it if it's the ignore element.
                if (isMouseOverable(element, ignoreElement) && (bounds.Intersects(element.Bounds.AbsoluteTotalArea) || bounds.Contains(element.Bounds.AbsoluteTotalArea)))
                {
                    // If the element has children, recursively search them.
                    T component;
                    if (element.HasChildren)
                    {
                        component = FindInBoundsWithInterfacedComponent <T>(element, bounds, ignoreElement);
                        if (component != null)
                        {
                            return(component);
                        }
                    }

                    // If the element has a component of the given type, return it.
                    if (element.TryGetInterfacedComponent(out component))
                    {
                        return(component);
                    }
                }
            }

            // If the code ever gets to this point, it means no valid element was found, so return null.
            return(null);
        }