示例#1
0
        /// <summary>
        /// Tries to add the given graphic to any group attached to this graphic.  First, it
        /// will try to be attached to a group that has its preferred renderer type, and if
        /// there are multiple such groups it will choose the group with the smallest graphic
        /// count.
        ///
        /// If no group has the preferred renderer type, it will try to attach to a group
        /// that supports this type of graphic, again choosing the group with the smallest
        /// graphic count.
        ///
        /// If no such group is found, the attach will fail and this method will return false.
        /// </summary>
        public bool TryAddGraphic(LeapGraphic graphic)
        {
            LeapGraphicGroup targetGroup = null;

            //First try to attatch to a group that is preferred
            Type preferredType = graphic.preferredRendererType;

            if (preferredType != null)
            {
                foreach (var group in groups)
                {
                    Type rendererType = group.renderingMethod.GetType();
                    if (preferredType == rendererType || rendererType.IsSubclassOf(preferredType))
                    {
                        if (targetGroup == null || group.toBeAttachedCount < targetGroup.toBeAttachedCount)
                        {
                            targetGroup = group;
                        }
                    }
                }
            }

            if (targetGroup != null && targetGroup.TryAddGraphic(graphic))
            {
                return(true);
            }

            //If we failed, try to attach to a group that will take us
            foreach (var group in groups)
            {
                if (group.renderingMethod.IsValidGraphic(graphic))
                {
                    if (targetGroup == null || group.toBeAttachedCount < targetGroup.toBeAttachedCount)
                    {
                        targetGroup = group;
                    }
                }
            }

            if (targetGroup != null && targetGroup.TryAddGraphic(graphic))
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        /// <summary>
        /// Tries to add the given graphic to any group attached to this graphic.  First, it
        /// will try to be attached to a group that has its preferred renderer type, and if
        /// there are multiple such groups it will choose the group with the smallest graphic
        /// count.
        ///
        /// If no group has the preferred renderer type, it will try to attach to a group
        /// that supports this type of graphic, again choosing the group with the smallest
        /// graphic count.
        ///
        /// If no such group is found, the attach will fail and this method will return false.
        /// </summary>
        public bool TryAddGraphic(LeapGraphic graphic)
        {
            LeapGraphicGroup targetGroup = null;

            //First just try to attach to a group that is its favorite
            foreach (var group in groups)
            {
                if (group.name == graphic.favoriteGroupName)
                {
                    if (group.TryAddGraphic(graphic))
                    {
                        return(true);
                    }
                }
            }

            //Then try to attatch to a group that is of the preferred type
            //Choose the preferred group with the least graphics
            Type preferredType = graphic.preferredRendererType;

            if (preferredType != null)
            {
                foreach (var group in groups)
                {
                    Type rendererType = group.renderingMethod.GetType();
                    if (preferredType == rendererType ||
                        rendererType.IsSubclassOf(preferredType))
                    {
                        if (targetGroup == null || group.toBeAttachedCount < targetGroup.toBeAttachedCount)
                        {
                            targetGroup = group;
                        }
                    }
                }
            }

            if (targetGroup != null && targetGroup.TryAddGraphic(graphic))
            {
                return(true);
            }

            //If we failed, just try to attach to any group that will take us
            foreach (var group in groups)
            {
                if (group.renderingMethod.IsValidGraphic(graphic))
                {
                    if (targetGroup == null || group.toBeAttachedCount < targetGroup.toBeAttachedCount)
                    {
                        targetGroup = group;
                    }
                }
            }

            if (targetGroup != null && targetGroup.TryAddGraphic(graphic))
            {
                return(true);
            }

            //Unable to find any group that would accept the graphic :(
            return(false);
        }